BF16 dispatch chain (Phase 2/3): opt-in SafeTensors loader policy to keep BF16 native#612
Merged
Merged
Conversation
Phase 2 of the three-phase BF16 dispatch chain. Follow-up to #610 (Bf16TensorData merged). Today's loader unconditionally dequants BF16 → FP32 at load (`SafeTensorsParametersLoader.kt` line 87, `dequantBF16(bytes)`), which means even with the new Bf16TensorData type in place no SafeTensors-loaded weight ever reaches it. This PR adds an opt-in policy so consumers that want native BF16 (Gemma-3n is the obvious first one) get a Bf16DenseTensorData-backed tensor; everyone else stays on the dequant path with zero behavioural change. Surface: - new `Bf16LoadPolicy` enum (commonMain) with `DEQUANT_TO_FP32` (default) and `KEEP_NATIVE` cases. Documents the trade-off: memory halved vs. per-element decode cost on non-matmul ops. - new constructor parameter `bf16Policy: Bf16LoadPolicy = Bf16LoadPolicy.DEQUANT_TO_FP32`. Default preserves source + bytecode compat for every existing Kotlin caller. - new branch in the `DataType.BFLOAT16` case: when policy is `KEEP_NATIVE`, wrap the on-disk bytes in `Bf16DenseTensorData` and emit via `ctx.fromData(...)`. The consumer-visible dtype stays `FP32::class` (same pattern as Q4_K / Q8_0 tensors — quantised storage, FP32 dtype tag); only `tensor.data` differs. 4 new tests in `commonTest`: - DEQUANT_TO_FP32 path produces FloatArrayTensorData with values within BF16 precision. - KEEP_NATIVE path produces Bf16DenseTensorData whose packedData byte array matches the on-disk bytes verbatim. - Decoded values from both paths are bit-identical (both apply the same `bf16_bits << 16` math; only WHEN differs). - Mixed BF16+FP32 file under KEEP_NATIVE — BF16 becomes Bf16DenseTensorData, FP32 stays FloatArrayTensorData. Refs #611. Full `:skainet-io:skainet-io-safetensors:jvmTest` suite passes on linux-x86_64 / JDK 21. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #611. Phase 2 of the BF16 dispatch chain. Follows #610 (Bf16TensorData, merged).
What
SafeTensorsParametersLoadergains abf16Policy: Bf16LoadPolicy = DEQUANT_TO_FP32constructor parameter. Default preserves today's behavior verbatim; opt-inKEEP_NATIVEskips the dequant pass and emitsBf16DenseTensorData(added in #610) instead.DEQUANT_TO_FP32(default)FloatArrayTensorData<FP32>with dequanted valuesKEEP_NATIVE(new)Bf16DenseTensorDatawith packed bytesBf16MatmulKernelSPIWhy this is safe
FP32::classfor both policies — same convention asQ4_K/Q8_0tensors which have FP32 dtype but quantised storage. Pattern-matching ontensor.data is Bf16TensorDatais the recognition surface for the upcoming dispatch.float_bits = (bf16 & 0xFFFF) shl 16); the two policies apply that math at different points (load-time vs read-time), but the resulting FP32 values are bit-identical. Locked down by a dedicated test.Tests
4 new tests in
commonTest(SafeTensorsParametersLoaderBf16PolicyTest):bf16_default_policy_dequants_to_fp32_floatArray— confirms the default path still producesFloatArrayTensorDatawith correct values (within BF16 precision, 1e-2 abs).bf16_keep_native_policy_emits_bf16DenseTensorData— confirms KEEP_NATIVE producesBf16DenseTensorData,packedDatamatches the on-disk bytes byte-for-byte, andget()-decoded values still match the FP32 source within BF16 precision.bf16_keep_native_decoded_values_match_dequant_path_exactly— bit-identity check across the two policies. Samebf16_bits << 16math applied at different times; outputs must agree to the raw bits.mixed_bf16_fp32_file_keep_native_only_affects_bf16— loads a SafeTensors file with one BF16 and one FP32 tensor under KEEP_NATIVE; confirms only the BF16 path is affected (FP32 tensor still arrives asFloatArrayTensorDatawith exact values).Full
:skainet-io:skainet-io-safetensors:jvmTestsuite passes on linux-x86_64 / JDK 21.Phase 3 (next follow-up)
DefaultCpuOpsJvm.chooseQuantizedMatmuldispatch onis Bf16TensorData ->via the SPIBf16MatmulKernel. Mirrors #608's Q8_0 wiring. After Phase 3 lands, flippingbf16Policy = KEEP_NATIVEis the only consumer-side change needed to opt into the SIMD-vectorised BF16 matmul.🤖 Generated with Claude Code