tf-lite-unity-sample icon indicating copy to clipboard operation
tf-lite-unity-sample copied to clipboard

Support Android 16 KB page sizes

Open asus4 opened this issue 9 months ago • 9 comments

A warning below is shown when building Android since Unity 6 (6000.0.43f1):

Plugin 'Packages/com.github.asus4.tflite/Plugins/Android/arm64-v8a/libtensorflowlite_gpu_gl.so' is not 16KB-aligned. This may cause issues on ARM64 devices running Android 15+.

Image

16KB page sizes in TensorFlow Lite is already supported on the Arm64 platform at this PR:

  • https://github.com/tensorflow/tensorflow/issues/69459

Other platforms and the GPU delegate library are not supported yet:

  • https://github.com/google-ai-edge/LiteRT/issues/43

I will upgrade this library to the 16kb page sizes version when the TensorFlow Lite library is ready.

asus4 avatar Mar 26 '25 10:03 asus4

Hi @asus4 I think tensorflow already addressed this issue: https://github.com/tensorflow/tensorflow/commit/2d72742d40f1d3121c895f8584ec8882d1e97fc8#diff-b1347ea198bdaf8b30f267f4391025bd5d7eb86dbeb1c4201f00f4489e3ecbe8

nhannguyentrong2gl avatar May 15 '25 03:05 nhannguyentrong2gl

Thanks @nhannguyentrong2gl, I'll try to build it when I get some time

asus4 avatar May 15 '25 06:05 asus4

Hello, I hope you are doing well. I was wondering if there are any updates regarding this issue. I am looking forward to hearing from you.

Additionally, I would like to ask if there is any possibility of receiving a version with updates applied only to the arm64-v8a side to support the 16KB page size adjustment.

sgju-pincat avatar Jun 09 '25 05:06 sgju-pincat

@sgju-pincat Sorry for the late reply, I was on a short holiday.

@DeNA @stakemura has done a great job on their tflite native library, which includes support for the 16kb page size.

I'll prepare the v2.19.0 release based on it next week (hopefully). If you're in a hurry, replacing their native libs in the tf-lite-unity-sample should work.

asus4 avatar Jun 12 '25 15:06 asus4

Although the library is updated to v2.19.0 at #389, libtensorflowlite_gpu_gl.so still outputs a warning about not 16KB-aligned. I have dropped this library from the latest release for now, as most users seem to have migrated to tensorflow-lite-gpu.aar (GpuDelegateV2.cs). I will try to look deeply into the code to support a 16kb page size in libtensorflowlite_gpu_gl.so.

16kb page size aligned

  • Packages/com.github.asus4.tflite/Plugins/Android/tensorflow-lite.aar
  • Packages/com.github.asus4.tflite/Plugins/Android/tensorflow-lite-gpu.aar

Not aligned yet

  • Packages/com.github.asus4.tflite/Plugins/Android/arm64-v8a/libtensorflowlite_gpu_gl.so

asus4 avatar Jun 13 '25 18:06 asus4

hi @asus4, may I know how did you see the warning for libtensorflowlite_gpu_gl.so? Is it from unity Inspector or via uploading the apk/aab onto google play? My team faced the issue with other libraries which say they already support 16kb but Unity inspector still says they are not. It turns out it's due to Unity bug for not updating the meta file, all we need to do is deleting the current meta file of the .so file and let Unity check again. Hope this help!

Edited: deleting meta files does not work in this case

nhannguyentrong2gl avatar Jun 15 '25 12:06 nhannguyentrong2gl

Thanks @nhannguyentrong2gl.

I've regenerated the metadata and run the check_elf_alignment.sh script. The output below shows that libtensorflowlite_gpu_gl.so is still unaligned.

=== ELF alignment ===
/.../tf-lite-unity-sample_out/lib/arm64-v8a/libmain.so: ALIGNED (2**14)
/.../tf-lite-unity-sample_out/lib/arm64-v8a/libtensorflowlite_gpu_jni.so: ALIGNED (2**14)
/.../tf-lite-unity-sample_out/lib/arm64-v8a/libil2cpp.so: ALIGNED (2**14)
/.../tf-lite-unity-sample_out/lib/arm64-v8a/libunity.so: ALIGNED (2**14)
/.../tf-lite-unity-sample_out/lib/arm64-v8a/libtensorflowlite_jni.so: ALIGNED (2**14)
/.../tf-lite-unity-sample_out/lib/arm64-v8a/lib_burst_generated.so: ALIGNED (2**14)
/.../tf-lite-unity-sample_out/lib/arm64-v8a/libtensorflowlite_gpu_gl.so: UNALIGNED (2**12)
Found 1 unaligned libs (only arm64-v8a/x86_64 libs need to be aligned).
=====================

As a side note, I've seen a similar issue with Unity Burst versions earlier than 1.8.21😅

asus4 avatar Jun 16 '25 13:06 asus4

Thank you. I’ve successfully confirmed that the alignment is properly applied in v2.19.0. Also, as you mentioned, I confirmed that libtensorflowlite_gpu_gl.so is still not aligned.

Just to be thorough, I also checked @DeNA ’s .so file, and it seems that alignment is not supported there either.

I’ll be looking forward to alignment support for libtensorflowlite_gpu_gl.so.

Additionally, I noticed that the Unity version in the master branch has been updated to 2022.3.62f1. May I ask if there was a specific reason for this update?

Also, I truly appreciate your prompt response.

sgju-pincat avatar Jun 18 '25 18:06 sgju-pincat

@sgju-pincat Upgrading Unity 2022.3.62f1 was required due to the internal Burst version. The Burst library before 1.8.21 was not 16 KB page size aligned.

For more details: Unity forum Burst change log

asus4 avatar Jun 19 '25 00:06 asus4

another information regarding Android 15, NNAPI will be deprecated

nhannguyentrong2gl avatar Jun 27 '25 18:06 nhannguyentrong2gl

DllNotFoundException: libtensorflowlite_c assembly: type: member:(null)

chengxiaoai avatar Jun 28 '25 08:06 chengxiaoai

I've created a PR for the libtensorflowlite_gpu_gl.so 16kb alignment issue. I hope it will be officially merged in a future release of TFLite or LiteRT.

https://github.com/tensorflow/tensorflow/pull/96702 https://github.com/google-ai-edge/LiteRT/pull/3003

In this repository, these changes are included in the release v2.19.0-p2

asus4 avatar Jul 28 '25 10:07 asus4