Skip to content

Commit 66d2340

Browse files
michalharakalclaude
andcommitted
Add Bf16LoadPolicy.toDTypePolicy() adapter (W2 of #615)
Bridges the existing BF16-specific load-policy enum onto the generalised DTypePolicy sealed type from W1: DEQUANT_TO_FP32 -> Require(FP32) KEEP_NATIVE -> Require(BF16) Existing call sites keep using the enum verbatim (no signature changes), while new code paths — and the dtype-policy-aware SafeTensors entry point in W0b — can flow through DTypePolicy uniformly. The exhaustive-when test guards against future enum additions that forget to update the adapter: any new Bf16LoadPolicy arm forces a compile error in toDTypePolicy() until it's mapped. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 4a84a42 commit 66d2340

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

skainet-io/skainet-io-safetensors/src/commonMain/kotlin/sk/ainet/io/safetensors/Bf16LoadPolicy.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package sk.ainet.io.safetensors
22

3+
import sk.ainet.lang.types.BF16
4+
import sk.ainet.lang.types.DTypePolicy
5+
import sk.ainet.lang.types.FP32
6+
37
/**
48
* Controls how the SafeTensors loader handles `BFLOAT16` (BF16) tensors.
59
*
@@ -53,4 +57,23 @@ public enum class Bf16LoadPolicy {
5357
* transformer case).
5458
*/
5559
KEEP_NATIVE,
60+
;
61+
62+
/**
63+
* Maps this BF16-specific enum onto the generalised
64+
* [DTypePolicy] sealed type. [DEQUANT_TO_FP32] becomes
65+
* `Require(FP32)` (the loader must hand consumers an FP32
66+
* tensor); [KEEP_NATIVE] becomes `Require(BF16)` (consumers
67+
* dispatch on the native BF16 dtype).
68+
*
69+
* Bridge for the RFC's policy-driven loader work
70+
* (`rfc.md`, issue #615): existing call sites keep using this
71+
* enum verbatim while new code paths can flow through
72+
* [DTypePolicy] uniformly. The two are equivalent for BF16 —
73+
* this method is the explicit equivalence proof.
74+
*/
75+
public fun toDTypePolicy(): DTypePolicy = when (this) {
76+
DEQUANT_TO_FP32 -> DTypePolicy.Require(FP32)
77+
KEEP_NATIVE -> DTypePolicy.Require(BF16)
78+
}
5679
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sk.ainet.io.safetensors
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
import sk.ainet.lang.types.BF16
6+
import sk.ainet.lang.types.DTypePolicy
7+
import sk.ainet.lang.types.FP32
8+
9+
/**
10+
* Verifies the [Bf16LoadPolicy.toDTypePolicy] adapter — the bridge
11+
* between the BF16-specific enum (existing prior art) and the
12+
* generalised [DTypePolicy] sealed type (W1 of #615). Confirms
13+
* both arms of the enum land on equivalent `Require` policies so
14+
* downstream code paths can flow through `DTypePolicy` uniformly.
15+
*/
16+
class Bf16LoadPolicyToDTypePolicyTest {
17+
18+
@Test
19+
fun dequant_to_fp32_maps_to_require_fp32() {
20+
val policy = Bf16LoadPolicy.DEQUANT_TO_FP32.toDTypePolicy()
21+
assertEquals(DTypePolicy.Require(FP32), policy)
22+
}
23+
24+
@Test
25+
fun keep_native_maps_to_require_bf16() {
26+
val policy = Bf16LoadPolicy.KEEP_NATIVE.toDTypePolicy()
27+
assertEquals(DTypePolicy.Require(BF16), policy)
28+
}
29+
30+
@Test
31+
fun adapter_covers_every_enum_arm() {
32+
// Defensive: if a new arm is added to Bf16LoadPolicy without
33+
// also updating toDTypePolicy, this test surfaces it because
34+
// toDTypePolicy's `when` is exhaustive — Kotlin emits a
35+
// compile error rather than silently dropping the case.
36+
for (arm in Bf16LoadPolicy.entries) {
37+
val mapped = arm.toDTypePolicy()
38+
assertEquals(true, mapped is DTypePolicy.Require, "$arm must map to Require, got $mapped")
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)