dtype.bend checks
raw source on the hub · import bendtorch@1.1.0.0/dtype.bend as Dtype
BendTorch's dtypes (plan M11): BF16 and F16 stored two per U32, int8 with a scale per row stored four per U32, a two-F32 ("double-word") accumulator, and the layers that give a model BF16 storage around F32 arithmetic.
Every number is computed in F32. A 16-bit value lives in the low 16 bits of a U32 while it is handled alone, and two of them share a word in storage (element 2k in the low half of word k, element 2k + 1 in the high half: the bytes of a little-endian torch.bfloat16 / torch.float16 tensor). The conversions from F32 round to nearest, ties to even, and treat NaN as PyTorch 2.14.1's CPU kernels do on this machine (tested bit for bit by reference/test_dtype.py): - F32 -> BF16: every NaN becomes 0xFFFF (the vectorized kernel's answer; c10's scalar code would give 0x7FC0). - F32 -> F16: a NaN keeps its sign and the top 9 bits of its payload, with the quiet bit set (F16C's vcvtps2ph). - F16 -> F32: a NaN gets the quiet bit set; subnormals become normal F32 numbers, exactly. No @unsafe and no foreign code: plain Bend over U32 and F32 bit operations.
3 imports
import Base import ./main.bend as T import bend-ml-tensor-array@0.1.14.0/main.bend as TA
Types
type HW source · line 138 · raw
Data
a pair of 16-bit values, the unit of storage
HW@lo:U32 -> @hi:U32 -> HW
type HMat source · line 167 · raw
@-r:Nat -> @-c:Nat -> Type
HMat@-r:Nat -> @-c:Nat -> @d:Array<U32> -> HMat<r, c>
type UL source · line 170 · raw
Type
UL@a:Array<U32> -> @l:List<&2, U32> -> UL
type Q8 source · line 297 · raw
@-r:Nat -> @-c:Nat -> Type
Q8@-r:Nat -> @-c:Nat -> @q:Array<U32> -> @s:Array<F32> -> Q8<r, c>
type QR source · line 339 · raw
Type
QR@q:List<&2, U32> -> @s:List<&2, F32> -> QR
type FL source · line 386 · raw
Type
FL@a:Array<F32> -> @l:List<&2, F32> -> FL
type QL source · line 406 · raw
Type
the words and scales, for printing and tests
QL@q:List<&2, U32> -> @s:List<&2, F32> -> QL
type UF source · line 422 · raw
Type
UF@q:Array<U32> -> @l:List<&2, F32> -> UF
type QD source · line 449 · raw
Type
QD@x:Array<F32> -> @q:Array<U32> -> @acc:F32 -> QD
type QS source · line 460 · raw
Type
QS@x:Array<F32> -> @q:Array<U32> -> @s:Array<F32> -> @y:Array<F32> -> QS
type Q8Mul source · line 488 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> Type
Q8Mul@-n:Nat -> @-k:Nat -> @-m:Nat -> @x:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, k> -> @w:Q8<m, k> -> @y:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, m> -> Q8Mul<n, k, m>
type DW source · line 509 · raw
Data
DW@hi:F32 -> @lo:F32 -> DW
type AF source · line 558 · raw
Type
AF@a:Array<F32> -> @v:F32 -> AF
type AF2 source · line 575 · raw
Type
AF2@a:Array<F32> -> @b:Array<F32> -> @v:F32 -> AF2
Definitions
def u2f source · line 22 · raw
@u:U32 -> F32
U32 and F32 hold the same Word(32n)
def f2u source · line 27 · raw
@x:F32 -> U32
def pick_u source · line 32 · raw
@b:Bool -> @+x:U32 -> @+y:U32 -> U32
def bf16_rne source · line 45 · raw
@+u:U32 -> U32
F32 -> BF16, round to nearest even: add 0x7FFF plus the lowest kept bit, keep the top half. The carry rounds up into the exponent when the mantissa overflows, and past the largest finite value to infinity, as IEEE asks.
def bf16_of_bits source · line 48 · raw
@+u:U32 -> U32
def BF16.of_f32 source · line 51 · raw
@x:F32 -> U32
def BF16.to_f32 source · line 55 · raw
@+h:U32 -> F32
BF16 -> F32 is exact: the 16 bits become the top half
def BF16.round source · line 59 · raw
@+x:F32 -> F32
x rounded to the nearest BF16 value, as an F32 (x.to(torch.bfloat16).to(torch.float32))
def f16_normal source · line 68 · raw
@+a:U32 -> U32
a normal F16 (|x| >= 2^-14): rebias the exponent (127 - 15 = 112), round the 23-bit mantissa to 10 bits to nearest even; past 65504 the carry reaches the infinity pattern 0x7C00 and anything above it is infinity too
def f16_sub2 source · line 75 · raw
@+q:U32 -> @+rem:U32 -> @+half:U32 -> U32
a subnormal F16 (2^-25 < |x| < 2^-14): the value in units of 2^-24 is mant / 2^sh, sh = 126 - e in 14..24, rounded to nearest even; a result of 0x400 is the smallest normal, which is the right answer
def f16_sub source · line 79 · raw
@+a:U32 -> U32
def f16_nan source · line 85 · raw
@+a:U32 -> U32
a NaN keeps the top 9 bits of its payload and gets the quiet bit
def f16_mag source · line 88 · raw
@nan:Bool -> @normal:Bool -> @tiny:Bool -> @+a:U32 -> U32
def f16_of_bits source · line 100 · raw
@+u:U32 -> U32
F32 -> F16, round to nearest even, with the sign kept (so -0.0 and the negative tiny numbers give 0x8000)
def F16.of_f32 source · line 104 · raw
@x:F32 -> U32
def f16_up source · line 108 · raw
@zero:Bool -> @top:Bool -> @+m:U32 -> @+s:U32 -> @+eu:U32 -> F32
F16 -> F32, exact. A subnormal is m · 2^-24: the product of two F32 numbers that is exact and normal.
def F16.to_f32 source · line 117 · raw
@+h:U32 -> F32
def F16.round source · line 121 · raw
@+x:F32 -> F32
def half_join source · line 128 · raw
@+lo:U32 -> @+hi:U32 -> U32
def half_lo source · line 131 · raw
@+w:U32 -> U32
def half_hi source · line 134 · raw
@+w:U32 -> U32
def pairs source · line 142 · raw
@xs:List<&2, U32> -> List<&2, HW>
the values two by two; an odd last one is paired with 0
def unpairs source · line 152 · raw
@ws:List<&2, HW> -> List<&2, U32>
every value of a list of pairs, in order
def unpairs_n source · line 160 · raw
@+n:Nat -> @ws:List<&2, HW> -> List<&2, U32>
the first n of them: the padding of an odd count is left out
def fill_u source · line 173 · raw
@xs:List<&2, U32> -> @+i:U32 -> @a:Array<U32> -> Array<U32>
def read_u source · line 181 · raw
@k:Nat -> @+i:U32 -> @acc:List<&2, U32> -> @ra:Pair(Array<U32>, U32) -> UL
the k + 1 values from index i, in order (ra holds the array and the value at i, already read)
def ul_list source · line 188 · raw
@r:UL -> List<&2, U32>
def join_l source · line 207 · raw
@ws:List<&2, HW> -> List<&2, U32>
def split_l source · line 214 · raw
@ws:List<&2, U32> -> List<&2, HW>
def hwords source · line 222 · raw
@+n:Nat -> Nat
the number of words for n halves
def ml_l source · line 225 · raw
@-r:Nat -> @-c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.ML<r, c> -> List<&2, F32>
def Half.words source · line 231 · raw
@+n:Nat -> @hs:List<&2, U32> -> Array<U32>
the values of a 16-bit list packed two per word
def Half.values source · line 235 · raw
@+n:Nat -> @a:Array<U32> -> List<&2, U32>
the first n 16-bit values of a packed array
def BF16.pack source · line 248 · raw
@+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> HMat<r, c>
def BF16.unpack source · line 251 · raw
@+r:Nat -> @+c:Nat -> @h:HMat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def F16.pack source · line 254 · raw
@+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> HMat<r, c>
def F16.unpack source · line 257 · raw
@+r:Nat -> @+c:Nat -> @h:HMat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def HMat.words source · line 261 · raw
@+r:Nat -> @+c:Nat -> @h:HMat<r, c> -> List<&2, U32>
the words of a packed matrix, for printing and tests
def unpair_pair source · line 273 · raw
@xs:List<&2, U32> -> {List.take(&2, U32, unpairs(pairs(xs)), List.length(&2, U32, xs)) == xs : List<&2, U32>}LAW (unpair_pair): packing a list two by two and reading back as many values as it had gives the list back, in order, for any length (an odd last value is paired with a 0 that the read leaves out). Proof: by induction two values at a time; the pair HW{a, b} unpacks to a then b, and the rest is the induction hypothesis.
def pick_f source · line 300 · raw
@b:Bool -> @+x:F32 -> @+y:F32 -> F32
def rne_f source · line 308 · raw
@+v:F32 -> F32
v rounded to the nearest integer, ties to even (torch.round), for |v| < 2^23 (here |v| <= 127 and a bit)
def q8_of source · line 315 · raw
@+w:F32 -> @+s:F32 -> F32
def q8_byte source · line 319 · raw
@+q:F32 -> U32
an integer -127..127 (as an F32) to its two's complement byte, and back
def q8_val source · line 322 · raw
@+b:U32 -> F32
def amax_l source · line 325 · raw
@xs:List<&2, F32> -> @+acc:F32 -> F32
def q8_row source · line 332 · raw
@xs:List<&2, F32> -> @+s:F32 -> List<&2, U32>
def qr_add source · line 342 · raw
@+row:List<&2, F32> -> @r:QR -> QR
def q8_rows source · line 349 · raw
@k:Nat -> @+c:Nat -> @+xs:List<&2, F32> -> QR
the rows of a flat list of k rows of c, from the last one: each row's bytes and scale
def quads source · line 357 · raw
@xs:List<&2, U32> -> List<&2, U32>
four bytes to a word, little-endian; a short last group is padded with zeros
def fill_f source · line 370 · raw
@xs:List<&2, F32> -> @+i:U32 -> @a:Array<F32> -> Array<F32>
def q8_fin source · line 377 · raw
@-r:Nat -> @-c:Nat -> @+n:Nat -> @+rr:Nat -> @qr:QR -> Q8<r, c>
def Q8.quantize source · line 383 · raw
@+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> Q8<r, c>
W quantized by rows (the matrix is consumed)
def read_f source · line 389 · raw
@k:Nat -> @+i:U32 -> @acc:List<&2, F32> -> @ra:Pair(Array<F32>, F32) -> FL
def fl_list source · line 396 · raw
@r:FL -> List<&2, F32>
def q8_at source · line 402 · raw
@+e:U32 -> @+w:U32 -> F32
byte e of the packed words, as its integer
def Q8.lists source · line 409 · raw
@+r:Nat -> @+c:Nat -> @w:Q8<r, c> -> QL
def dq_row source · line 415 · raw
@k:Nat -> @+e:U32 -> @+s:F32 -> @acc:List<&2, F32> -> @rq:Pair(Array<U32>, U32) -> UF
W back in F32: q[i, j] · s_i, one F32 product each
def dq_rows source · line 425 · raw
@k:Nat -> @+c:Nat -> @+cu:U32 -> @+i:U32 -> @ss:List<&2, F32> -> @r:UF -> List<&2, F32>
def dq_fin source · line 434 · raw
@+r:Nat -> @+c:Nat -> @q:Array<U32> -> @ss:List<&2, F32> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def dq_go source · line 437 · raw
@+r:Nat -> @+c:Nat -> @q:Array<U32> -> @sl:FL -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def Q8.dequantize source · line 442 · raw
@+r:Nat -> @+c:Nat -> @w:Q8<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def q8_dot source · line 453 · raw
@k:Nat -> @+xi:U32 -> @+qe:U32 -> @+acc:F32 -> @rx:Pair(Array<F32>, F32) -> @rq:Pair(Array<U32>, U32) -> QD
Σ_l x[xi + l] · q[qe + l] for l < k, in order, from acc
def q8_put2 source · line 463 · raw
@+idx:U32 -> @+acc:F32 -> @x:Array<F32> -> @q:Array<U32> -> @rs:Pair(Array<F32>, F32) -> @y:Array<F32> -> QS
def q8_put source · line 468 · raw
@+idx:U32 -> @+j:U32 -> @d:QD -> @s:Array<F32> -> @y:Array<F32> -> QS
def q8_cols source · line 474 · raw
@ml:Nat -> @+i:U32 -> @+j:U32 -> @+k:Nat -> @+ku:U32 -> @+mu:U32 -> @st:QS -> QS
the m outputs of row i
def q8_rowsm source · line 481 · raw
@nl:Nat -> @+i:U32 -> @+m:Nat -> @+k:Nat -> @+ku:U32 -> @+mu:U32 -> @st:QS -> QS
def q8_mm_fin source · line 491 · raw
@-n:Nat -> @-k:Nat -> @-m:Nat -> @st:QS -> Q8Mul<n, k, m>
def Q8.matmul_nt source · line 497 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @x:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, k> -> @w:Q8<m, k> -> Q8Mul<n, k, m>
y = x · Wᵀ; x and W come back
def DW.zero source · line 512 · raw
DW
def DW.value source · line 515 · raw
@d:DW -> F32
def dw_norm source · line 521 · raw
@+s:F32 -> @+e:F32 -> DW
hi + lo when |hi| >= |lo| (Fast2Sum), as a renormalized pair
def dw_fin source · line 526 · raw
@+s:F32 -> @+bb:F32 -> @+a:F32 -> @+x:F32 -> @+lo:F32 -> @+c:F32 -> DW
hi + x: TwoSum's s and its exact error, plus the old lo and an extra error term c
def dw_add3 source · line 530 · raw
@d:DW -> @+x:F32 -> @+c:F32 -> DW
def DW.add source · line 536 · raw
@d:DW -> @+x:F32 -> DW
def split_hi source · line 539 · raw
@+a:F32 -> F32
def two_prod_e source · line 544 · raw
@+a:F32 -> @+b:F32 -> @+p:F32 -> F32
a · b exactly as p + e
def DW.fma source · line 552 · raw
@d:DW -> @+a:F32 -> @+b:F32 -> DW
acc + a · b, with the product's rounding error kept
def sum_f source · line 561 · raw
@k:Nat -> @+i:U32 -> @+acc:F32 -> @ra:Pair(Array<F32>, F32) -> AF
def sum_dw source · line 568 · raw
@k:Nat -> @+i:U32 -> @acc:DW -> @ra:Pair(Array<F32>, F32) -> AF
def dot_f source · line 578 · raw
@k:Nat -> @+i:U32 -> @+acc:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> AF2
def dot_dw source · line 585 · raw
@k:Nat -> @+i:U32 -> @acc:DW -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> AF2
def Sum.f32 source · line 593 · raw
@+n:Nat -> @a:Array<F32> -> AF
the sums of the first n numbers of a (the array comes back)
def Sum.dw source · line 596 · raw
@+n:Nat -> @a:Array<F32> -> AF
def Dot.f32 source · line 599 · raw
@+n:Nat -> @a:Array<F32> -> @b:Array<F32> -> AF2
def Dot.dw source · line 602 · raw
@+n:Nat -> @a:Array<F32> -> @b:Array<F32> -> AF2
def bf16_mat source · line 619 · raw
@+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def cast_f source · line 622 · raw
@-P:Type -> @-S:Type -> @+r:Nat -> @+c:Nat -> @f:0x88d35bdf07184f57aa5174c6f06ab791/main.Fwd<P, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S> -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Fwd<P, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S>
def castp_f2 source · line 636 · raw
@-X:Type -> @-Y:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @w:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @f:0x88d35bdf07184f57aa5174c6f06ab791/main.Fwd<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, Y, S> -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Fwd<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, Y, S>
def castp_b2 source · line 646 · raw
@-X:Type -> @+r:Nat -> @+c:Nat -> @w:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @b:0x88d35bdf07184f57aa5174c6f06ab791/main.Bwd<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X> -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Bwd<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X>
Templates
template enc_l source · line 193 · raw
@-f:(@_:F32 -> U32) -> @xs:List<&2, F32> -> List<&2, U32>
template dec_l source · line 200 · raw
@-f:(@+h:U32 -> F32) -> @xs:List<&2, U32> -> List<&2, F32>
template Half.pack source · line 239 · raw
@-f:(@_:F32 -> U32) -> @+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> HMat<r, c>
a matrix encoded by f (BF16.of_f32 or F16.of_f32) and packed
template Half.unpack source · line 243 · raw
@-g:(@+h:U32 -> F32) -> @+r:Nat -> @+c:Nat -> @h:HMat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
a packed matrix decoded by g (BF16.to_f32 or F16.to_f32) into F32
template cast_fwd source · line 627 · raw
@-P:Type -> @-X:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @-l:0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<P, X, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S> -> @p:P -> @x:X -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Fwd<P, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S>
template cast_bwd source · line 630 · raw
@-P:Type -> @-X:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @-l:0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<P, X, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S> -> @p:P -> @s:S -> @dy:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @g:P -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Bwd<P, X>
template CastOut source · line 633 · raw
@-P:Type -> @-X:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @-l:0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<P, X, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S> -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<P, X, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, S>
template castp_f source · line 641 · raw
@-X:Type -> @-Y:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @-l:0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X, Y, S> -> @d:0x88d35bdf07184f57aa5174c6f06ab791/main.Two<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>> -> @x:X -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Fwd<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, Y, S>
template castp_b source · line 651 · raw
@-X:Type -> @-Y:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @-l:0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X, Y, S> -> @d:0x88d35bdf07184f57aa5174c6f06ab791/main.Two<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>> -> @s:S -> @dy:Y -> @g:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Bwd<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X>
template CastP source · line 656 · raw
@-X:Type -> @-Y:Type -> @-S:Type -> @-r:Nat -> @-c:Nat -> @-l:0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X, Y, S> -> 0x88d35bdf07184f57aa5174c6f06ab791/main.Layer<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>, X, Y, S>