feat(dart): add bfloat16 and bfloat16_array support#3527
feat(dart): add bfloat16 and bfloat16_array support#3527miantalha45 wants to merge 4 commits intoapache:mainfrom
Conversation
| } | ||
|
|
||
| /// A fixed-length list of [BFloat16] values, backed by a [Uint16List]. | ||
| class BFloat16Array { |
There was a problem hiding this comment.
BFloat16Array is public here, but the runtime wiring still only handles the scalar BFloat16. DartTypeResolver.getForyType() only special-cases List and TypedDataList, and neither SerializerPool nor DartTypeEnum register BFloat16Array / ObjType.BFLOAT16_ARRAY, so Fory().serialize(BFloat16Array.fromList([1.0])) currently fails with Unregistered type: BFloat16Array. This needs the same built-in array registration path as the other default array types before bfloat16_array support is actually complete.
| expect(arr.raw.length, 5); | ||
| }); | ||
|
|
||
| test('creates from list and sets values', () { |
There was a problem hiding this comment.
These tests only cover the wrapper API. They never round-trip BFloat16Array through Fory.serialize / deserialize, which is why they still pass even though BFloat16Array is currently unregistered and cannot be serialized. Please add a serializer round-trip case for the new array type so this feature is actually verified.
Description
This PR introduces the
bfloat16andbfloat16_arrayprimitives to the Fory Dart runtime, fulfilling the requirement for a memory-efficient brain floating-point formulation commonly used in ML/AI workflows. It strictly mirrors the structural implementations of the existingfloat16work.Fixes #3287
Changes Made
Dart Runtime Type Definitions:
BFloat16as a strongly-typed subclass of theFixedNumbase class, safely wrapping the 16-bit integer representing the representation bits without exposing rawuint16in public math APIs.BFloat16Arrayas a packed contiguous array collection optimally backed byUint16List.IEEE 754 Conversions:
BFloat16.fromFloat32andtoFloat32()doing bitwise round-to-nearest and ties-to-even operations over proxy memory layers (Float32List/Uint32List.view) mapped exactly to cross-language canonical IEEE 754 bfloat16 bit representations.value,toBits(),fromBits()).Reflection & Serialization Integration:
_BFloat16SerializerCacheinsideprimitive_type_serializer.dartdirectly wiring toBFloat16Serializer.BFloat16serialization route inside thetypeToTypeInforegistry inserializer_pool.dart.fory.dartbarrel file.Testing:
0x3f80 == 1.0).BFloat16Arraydynamic generic injections handlingList<dynamic>scaling natively.Type of change
Checklist:
///)dart test)AI Assistance Checklist
AI Usage Disclosure