xamarin / GooglePlayServicesComponents

Other
313 stars 145 forks source link

Xamarin.TensorFlow.Lite.GPU missing bindings #793

Open taublast opened 11 months ago

taublast commented 11 months ago

The issue

Inside Xamarin.TensorFlow.Lite.Gpu" Version="2.12.0.1" we are missing

CompatibilityList.BestOptionsForThisDevice and constructor GpuDelegate(GpuDelegate.Options options)

to be able to to implement gpu acceleration as described here:

https://www.tensorflow.org/lite/android/delegates/gpu?hl=en#enable_gpu_acceleration_2

and in .net maui like that:

var compatList = new CompatibilityList();
if (compatList.IsDelegateSupportedOnThisDevice)
{
    var bestOptions = compatList.BestOptionsForThisDevice; //<- no BestOptionsForThisDevice
    var gpuDelegate = new GpuDelegate(bestOptions); //<- constructor not accepting parameters
    options.AddDelegate(gpuDelegate);
}

The c# code above started working for me after i implemented the solution described below.

The cause of this is that is either: a - tensorflow-lite-gpu-api was not included at all b - both gpu-related aar's where processed is same project: both tensorflow-lite-gpu-api-2.13.0.aar and tensorflow-lite-gpu-2.13.0.aar define classes absolutely for same package tensorflow/lite/gpu, that creates a conflict for bindings generator and it just skips one of them (tensorflow-lite-gpu-api in our case).

This makes GpuDelegateFactory.Options not to be available and bindings are not generated for CompatibilityList.BestOptionsForThisDevice and the second constructor GpuDelegate(GpuDelegate.Options options)

The solution

To bind tensorflow-lite-gpu-api-2.13.0.aar and tensorflow-lite-gpu-2.13.0.aar in different projects, the latter would reference the first one with "-api"

https://github.com/taublast/AppoMobi.Maui.TensorFlow.Lite

Context

This was all about a .net maui 7.0 for android, trying to use <PackageReference Include="Xamarin.TensorFlow.Lite.Gpu" Version="2.12.0.1" />

wollac11 commented 7 months ago

This issue has been ongoing since 2.5.0 (https://github.com/xamarin/XamarinComponents/issues/1319) and seems to affect every version I have tried since up to and including the very latest currently available (2.14.0). I have some older Xamarin projects that use monoandroid so I can't use the modified NuGet from @taublast sadly (but it could be useful after migrating to MAUI if this issue is still not fixed).

@taublast Could you perhaps open a PR to this repo with a fix?

wollac11 commented 7 months ago

The GPU bindings are also not the only missing bindings in the current Xamarin.Tensorflow.Lite packages as there is a similar issue with the processor builders (https://github.com/xamarin/GooglePlayServicesComponents/issues/754)

taublast commented 7 months ago

@wollac11 unfortunately won't be able to work on a PR here, but could help you with older droid if you provide more details on what sdk level you need bindings to support.