Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dart ffi: Structs pass by value #41062

Closed
am15h opened this issue Mar 16, 2020 · 3 comments
Closed

dart ffi: Structs pass by value #41062

am15h opened this issue Mar 16, 2020 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi type-question A question about expected behavior or functionality

Comments

@am15h
Copy link

am15h commented Mar 16, 2020

I want to write dart binding for the following C code:

c api:

TFL_CAPI_EXPORT extern TfLiteQuantizationParams TfLiteTensorQuantizationParams(  
const TfLiteTensor* tensor);  


typedef struct TfLiteQuantizationParams {  
float scale;  
int32_t zero_point;  
} TfLiteQuantizationParams;

I tried doing this:

dart binding:

TfLiteQuantizationParams Function(Pointer<TfLiteTensor> tensor)  
TfLiteTensorQuantizationParams = tflitelib  
.lookup<NativeFunction<_TfLiteTensorQuantizationParams_native_t>>(  
'TfLiteTensorQuantizationParams')  
.asFunction();

typedef _TfLiteTensorQuantizationParams_native_t  
= TfLiteQuantizationParams Function(Pointer<TfLiteTensor> tensor);  

But I am facing the following error;

Compiler message:                                                       
../lib/src/bindings/tensor.dart:126:6: Error: Expected type 'NativeFunction<TfLiteQuantizationParams Function(Pointer<TfLiteTensor>)>' to be a valid and instantiated subtype of 'NativeType'.
 - 'NativeFunction' is from 'dart:ffi'.                                 
 - 'TfLiteQuantizationParams' is from 'package:tflite_flutter_plugin/src/bindings/types.dart' ('../lib/src/bindings/types.dart').
 - 'Pointer' is from 'dart:ffi'.                                        
 - 'TfLiteTensor' is from 'package:tflite_flutter_plugin/src/bindings/types.dart' ('../lib/src/bindings/types.dart').
    .asFunction();

dart struct

class TfLiteQuantizationParams extends Struct {  
@Float()  
double scale;

@Int32()  
int zero_point;

@override  
String toString() {  
return 'TfLiteQuantizationParams{scale: $scale, zero_point: $zero_point}';  
}  
}

I need some help to get this working.

Thanks in advance

@am15h
Copy link
Author

am15h commented Mar 18, 2020

Related issue #36730. @dcharkes Can you suggest a workaround for my problem?

@dcharkes dcharkes added library-ffi type-question A question about expected behavior or functionality labels Mar 18, 2020
@dcharkes
Copy link
Contributor

We do not support passing structs by value yet indeed (I'm working on it).

The workaround is to write some wrapper C code that turns it into a pointer.

Dart code:

final tfLiteTensorQuantizationParamsWrapped = tflitelib.lookupFunction<
    Pointer<TfLiteQuantizationParams> Function(Pointer<TfLiteTensor> tensor),
    Pointer<TfLiteQuantizationParams> Function(
        Pointer<TfLiteTensor> tensor)>('TfLiteTensorQuantizationParams');

@keertip keertip added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Mar 18, 2020
@am15h am15h changed the title Error when binding to function returning c struct: Expected type 'NativeFunction<...>' to be a valid and instantiated subtype of 'NativeType', dart ffi: Structs pass by value Mar 18, 2020
@dcharkes
Copy link
Contributor

dcharkes commented Sep 7, 2020

Closing issue as #36730, please reopen if you have any issues with the workaround in the mean time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants