Skip to content

feat(dart): add bfloat16 and bfloat16_array support#3527

Open
miantalha45 wants to merge 3 commits intoapache:mainfrom
miantalha45:feat/bfloat16-dart
Open

feat(dart): add bfloat16 and bfloat16_array support#3527
miantalha45 wants to merge 3 commits intoapache:mainfrom
miantalha45:feat/bfloat16-dart

Conversation

@miantalha45
Copy link
Copy Markdown
Contributor

Description

This PR introduces the bfloat16 and bfloat16_array primitives 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 existing float16 work.

Fixes #3287

Changes Made

  1. Dart Runtime Type Definitions:

    • Introduced BFloat16 as a strongly-typed subclass of the FixedNum base class, safely wrapping the 16-bit integer representing the representation bits without exposing raw uint16 in public math APIs.
    • Implemented BFloat16Array as a packed contiguous array collection optimally backed by Uint16List.
  2. IEEE 754 Conversions:

    • Implemented BFloat16.fromFloat32 and toFloat32() 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.
    • Exposed explicit scalar getters (value, toBits(), fromBits()).
  3. Reflection & Serialization Integration:

    • Registered _BFloat16SerializerCache inside primitive_type_serializer.dart directly wiring to BFloat16Serializer.
    • Populated the BFloat16 serialization route inside the typeToTypeInfo registry in serializer_pool.dart.
    • Exported the new datatypes in the main fory.dart barrel file.
  4. Testing:

    • Added rigorous tests targeting scalar numeric conversions (validating mappings like 0x3f80 == 1.0).
    • Covered object equality, hashcode matching, and BFloat16Array dynamic generic injections handling List<dynamic> scaling natively.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Performance optimization
  • Documentation update

Checklist:

  • I have performed a self-review of my own code
  • I have added standard ASF license headers and Dartdoc comments (///)
  • I have added necessary unit tests confirming the bitwise boundaries
  • Existing and new tests pass locally (dart test)

AI Assistance Checklist

  • Substantial AI assistance was used in this PR (yes)
  • I included the standardized AI Usage Disclosure block below
  • I can explain and defend all important changes without AI help
  • I reviewed AI-assisted code changes line by line before submission
  • I ran adequate human verification and recorded evidence (local/CI checks, pass/fail summary, and review confirmation)
  • I added/updated tests and specs where required
  • I validated protocol/performance impacts with evidence when applicable
  • I verified licensing and provenance compliance

AI Usage Disclosure

  • substantial_ai_assistance: yes
  • scope: <design drafting | code drafting>

@miantalha45 miantalha45 changed the title Feat/bfloat16 dart feat(dart): add bfloat16 and bfloat16_array support Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Dart] add bfloat16 and bfloat16_array

1 participant