-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[typed_data] Add Float16List
+ [vm/ffi] Add Float16
#56319
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
Comments
Summary: This issue proposes adding |
It's also technically possible to support a |
What about just make float16List as an alias of Uint16List, which means a float16List is actually stored as Uint16List, but convert to dart double when getting values and convert to Uint16 when setting values? |
I did find this:
I'm not sure if using machine instructions from float16->float32->float64 is slower or faster than going from float16->float64 manually. If users need this and are going to manually use slow conversions to doubles anyway, then we might as well make their lives easier and add it in @rainyl do you want to use the float16's as
Any XXXList is stored as bytes and only converted when reading/writing values! 😄 If you just want to shuffle bytes around, you don't want to do it via something that requires conversions when reading writing. So you'd want to use If we add
So it might be tricky to fully add |
Float16
/Half
Both, the ideal use cases are very similar to other native types, but the most important for my project now is creating a view of Float16List and providing a proper way to get/set values. Now I can regard Uint16List as Float16List, but it's not elegant if users want to get/set values. // Currently I interact with native float16 using:
final ffi.Pointer<ffi.Uint16> ptr = ...;
final Uint16List view = ptr.asTypedList(length);
// Without Float16List, users have to set/get values via:
final double val = fp16_int_to_double(view[0]);
view[0] = fp16_double_to_int(val);
// `fp16_int_to_double` and `fp16_double_to_int` are implemented referring to https://github.com/opencv/opencv/blob/71d3237a093b60a27601c20e9ee6c3e52154e8b1/modules/core/include/opencv2/core/cvdef.h#L828-L917
// It will be user-friendly if users can set/get using dart double directly, maybe some thing like:
final ffi.Pointer<ffi.Float16> ptr = ...;
final Float16List view = ptr.asTypedList(length);
// With Float16List, users can set/get values via:
final double val = view[0];
view[0] = val;
Sounds like easy to implement the above operations for Float16List, good news.
Yes, but maybe the implementation of opencv can be a reference? It defined a |
Happy to receive a PR for this! Should it be A PR for only this should add errors on
hehe so it's an uint16 if it's not available. Well, Dart is not compiled at the same time as your library that uses open-cv, so we risk compiling with different flags which will lead to segfaults. On the other hand, we also assume SoftFP on Android arm32 and hard fp on arm32 Linux. Technically there can be Androids out there with hardfp and linuxes with softfp, but we've not run into them. I'd be fine simply assuming the type is defined (except for risc-v). I'm also open for getting a PR for adding this. This PR will be much more involved, as it includes getting the calling conventions right. If you want to work on these PRs I can provide pointers for where to start. |
I would like to suggest |
Same as @Wdestroier , I like Float16 too.
Sure, I am willing to work on this when having some free time, so could you please provide some instructions? So that other developers can work on this too. 😄 |
@sigmundch @mkustermann can For adding
For adding support for
If the rest of the Dart team is in favor of adding this, my suggestion would be to split this work up in multiple PRs:
|
Float16
/Half
Float16List
+ [vm/ffi] Add Float16
Float16List
+ [vm/ffi] Add Float16
Float16List
+ [vm/ffi] Add Float16
I don't think a I'm not sure bad support is better than no support. (We'll probably also want a |
➕ I was thinking about that too. |
AI/ML models can use different 16-bit floating point number formats, most commonly IEEE float16 and bfloat16 (which has more exponent bits). I So if the reason is AI/ML it would make sense to extend the discussion to be Those two are somewhat separate and can be discussed separately (e.g. we support For For But if the only use is via import 'dart:ffi';
extension type BFloat16P(Pointer<Uint16> pointer) {
double operator [](int index) {
final int value = pointer.value;
// ... code to bfloat16->double ... (XXX)
return convertedValue;
}
void operator []=(int index, double value) {
// ... code to double->bfloat16 ... (XXX)
return convertedValue;
}
BFloat16List asTypedList(int length) => BFloat16List(this, length);
}
class BFloat16List implements List<double> {
final BFloat16P pointer;
final int length;
BFloat16List(this.pointer, this.length);
double operator [](int index) => pointer[index];
void setRange(...) {
// Would e.g. delegate to already optimized `pointer.asTypedList().setRange()`
}
} And then users can use it via @Native<Pointer<Uint16> Function()>()
external Pointer<Uint16> getTensor();
main() {
final BFloat16List tensor = BFloat16P(getTensor()).asTypedList(64);
for (int i = 0; i < tensor.length; ++i) {
print(tensor[i]);
}
} or if we allow convenience usage of extension types in FFI: @Native<BFloat16P Function()>()
external BFloat16P getTensor();
main() {
final BFloat16List tensor = getTensor().asTypedList(64);
for (int i = 0; i < tensor.length; ++i) {
print(tensor[i]);
}
} We could ensure the conversion code in @rainyl Would your use case be solved by this? |
👍 Tracked in:
It would be even more efficient with I'd be cautious adding |
Yes, I am working on opencv bindings for dart, so I have to get a |
Chrome intends to add Float16Array |
Currently, only
Float (Float32)
andDouble (Float64)
are introduced indart:ffi
.However, the application of
Float16
is becoming more and more widespread, especially for AI-releated computations, and it is very inconvenient when interacting to native libraries that supports Float16, developers have to access the fp16 pointers or values usingUint16
and write the convension menthods by themselves, even so, some methods likeUint8List.view()
are not possible forfp16
if developers want to return a float16 view instead of a copy.I have read #52250 and #51994, but both of them are talking about more specific primitive types for dart lang, however this issue just for dart:ffi.
The text was updated successfully, but these errors were encountered: