Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
4aae74e
Add the ggml block formats of GGUF files
woodsonl Oct 16, 2026
a80b464
Add a reader for basalt's .basalt model file
woodsonl Oct 16, 2026
aa4d285
Test the formats and the reader on the official Q2_0 pack
woodsonl Oct 16, 2026
31514d8
Register F32 and F16 as GGUF formats
woodsonl Oct 16, 2026
f7d1c06
Record basalt's and Qwen's licences for the reader and the fixture
woodsonl Oct 16, 2026
c9820f3
Check every basalt index against its section and pin each refusal
woodsonl Oct 16, 2026
ad2b25f
Refuse negative and non-integer numbers in basalt's indexes and meta
woodsonl Oct 16, 2026
53a403f
Set the Flash-Next numerics, acceptance setup and memory budget
woodsonl Oct 16, 2026
c4d4207
Record nvcc's arithmetic for basalt's q8_1 quantizers and one-column …
woodsonl Oct 16, 2026
4f2d6ec
Add basalt's fp helpers, measure the platform, and amend invariant 5
woodsonl Oct 17, 2026
361b42d
Port basalt's q8_1 activation quantizers to Metal
woodsonl Oct 17, 2026
45543ff
WIP: native_mmvq Metal kernel, builders and tests (untested)
woodsonl Oct 17, 2026
c16fef6
Port basalt's native_mmvq to Metal for the pack's ten weight types
woodsonl Oct 17, 2026
ee67f54
Record nvcc's arithmetic for basalt's multi-column mmvq kernel
woodsonl Oct 17, 2026
0fc6b40
Port basalt's iq_parity: dequantizers and native_mmvq for Q2_0, Q3_K,…
woodsonl Oct 17, 2026
6e952be
Quote check_q8_1_finite's criteria in the test and point invariant 5 …
woodsonl Oct 17, 2026
6e58940
Correct the fmax subnormal claim and test each FTZ helper on its own
woodsonl Oct 17, 2026
0363d5e
Pre-fill mmvq test outputs apart and give column invariance a 2-row tail
woodsonl Oct 17, 2026
32a1762
Credit basalt and ggml in the test files that carry their code
woodsonl Oct 17, 2026
7c6ba41
Rewrap matvec.md section 3's paragraph after the fmax correction
woodsonl Oct 17, 2026
14eb4b0
Record nvcc's arithmetic for basalt's routed-expert kernels
woodsonl Oct 17, 2026
5a81cb3
Port basalt's Q2_0 routed-expert kernels to Metal
woodsonl Oct 17, 2026
17f0ec8
Port basalt's grouped-expert parity tests and emulate the experts
woodsonl Oct 17, 2026
5c0b0d0
Test the SwiGLU product's IEEE subnormals on planted values
woodsonl Oct 17, 2026
bc4f122
Check the routed experts against float64 references
woodsonl Oct 17, 2026
f414a39
Point invariant 5 at experts.md and record the down kernel's DEP check
woodsonl Oct 17, 2026
46aabb4
Compare gu2 with the one-row gate/up kernel in every --gu2 call
woodsonl Oct 17, 2026
366b8be
Give ieee_mul IEEE's result for infinities and NaN against zeros
woodsonl Oct 17, 2026
f11c57b
Build the whole-expert reference on the host codec and tighten per_entry
woodsonl Oct 17, 2026
11d14fa
Correct experts.md's .FTZ sentence and down table; complete NOTICE
woodsonl Oct 17, 2026
5415ca8
Describe the division slow path's .FTZ compare precisely
woodsonl Oct 17, 2026
cca8e18
Hand the routed-expert kernels basalt's grp_ptr table
woodsonl Oct 17, 2026
00c01b9
Rewrap the grp_ptr comments to the line width
woodsonl Oct 17, 2026
fb70ad2
Record the arithmetic nvcc compiles for basalt's BF16 GEMVs
woodsonl Oct 17, 2026
1b20225
Port basalt's BF16 GEMVs on fp32 activations to Metal
woodsonl Oct 17, 2026
44f1098
Port basalt's MMVF parity tests and check the kernel bit for bit
woodsonl Oct 17, 2026
dd171e3
State bf16.md's FFMA counts as the static counts they are
woodsonl Oct 17, 2026
a44ca22
Round fma_ftz and mul_ftz up to 2^-126 where FFMA.FTZ does
woodsonl Oct 17, 2026
7014be8
Pin the rounding before the flush in the helper and MMVF tests
woodsonl Oct 17, 2026
6a25f70
Correct the docs' claim that the GPU's flushing matches .FTZ
woodsonl Oct 17, 2026
53638e5
Port basalt's dense_tc window projections to Metal
woodsonl Oct 17, 2026
89319f6
Port basalt's Q5_K head to Metal
woodsonl Oct 18, 2026
da86cbe
Port basalt's drafter experts to Metal
woodsonl Oct 18, 2026
5fa0249
Store sm_120's NaN encoding at every basalt port store
woodsonl Oct 18, 2026
54eb320
Bind .basalt tensors through read-only windows of the file
woodsonl Oct 18, 2026
73051ab
Emit basalt's single-dispatch GEMVs and q8_1 quantizers as ops
woodsonl Oct 18, 2026
7141a06
Port basalt's Q2_0 CPU expert kernels to ARM NEON
woodsonl Oct 18, 2026
bf9eec3
Port basalt's CPU SwiGLU and glibc expf, held against its recorded x8…
woodsonl Oct 19, 2026
957b125
Build basalt's int8 hyper-connection copies on the host
woodsonl Oct 20, 2026
48c6a2d
Port basalt's hyper-connection write as basalt_gr_post
woodsonl Oct 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ These documents describe the current architecture, implementation contracts, and
| [Serving](design/serving.md) | Startup, request execution, protocol adapters, prefix caching, and streaming |
| [Model adapters](design/models.md) | Checkpoint interpretation, supported model structures, and numerical conventions |
| [Extension contracts](design/extensions.md) | Interfaces for models, formats, operations, drafters, and chip backends |
| [Flash-Next port contract](design/flash-next.md) | Numerics, acceptance setup, and 48 GiB memory budget for the Metal 4 port of basalt's Qwen3.8-Flash-Next Q2_0 pack |

For installation and examples, see the [project README](../README.md).
For development setup and test tiers, see [CONTRIBUTING.md](../CONTRIBUTING.md).
Expand Down
627 changes: 627 additions & 0 deletions docs/design/flash-next.md

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions docs/research/basalt-cpu-expf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# basalt's CPU SwiGLU: which `expf`, which order, which NaN

`runtime/src/basalt_cpu_expf.cpp` is the one piece of arithmetic basalt's CPU expert pool performs itself:
`src/kernels/cpu/pool.cpp:434`, `res = (g / (1.f + std::exp(-g))) * u`. Every other value the pool produces comes
out of the Q2_0 kernels. The port has to return the same bits basalt does, because a token's output has to be the
same number whichever tier produced it, so three things have to be settled rather than assumed: which
implementation of `expf` runs, in which order the three arithmetic instructions execute, and which NaN each one
writes. This records the answers and where they came from.

Evidence tags: [M] measured, [S] vendor specification, [R] third-party report.

## 1. Which `expf`

basalt runs on Linux, so `std::exp` of a float at pool.cpp:434 is glibc's `expf`. Apple's is a different
implementation; neither is correctly rounded, and the two differ at 1,930,129 of the 2,218,786,818 float32
arguments in [-40, 40] [M, this repository against this machine's own libm, Apple clang 21.0.0 on an M3 Max, so
the count is a property of Apple's `expf` and moves with macOS], so calling the platform's cannot be bit-exact and
the algorithm is transcribed instead.

glibc's generic `expf` has been Arm's `optimized-routines` `math/expf.c` since 2017
(`sysdeps/ieee754/flt-32/e_expf.c`) [R]. That source is MIT, where glibc's copy is LGPL-2.1-or-later, so the
transcription is taken from Arm's at `f67e7daa5457646fad9f75580263dba654c988aa` and keeps this tree's licence
position (`third_party/NOTICE`, `third_party/ARM-OPTIMIZED-ROUTINES-LICENSE`).

### The variant

glibc's x86-64 multiarch holds a second build of that same source, `__expf_fma`, compiled with `-mfma -mavx2`, and
an ifunc picks it on any host with FMA and AVX2 [M, `expf@GLIBC_2.27` through the PLT resolving to `libm+0x7dc40`,
glibc 2.39-0ubuntu8.9, Xeon Platinum 8375C and 8488C]. Every host that can run basalt's AVX-512 expert kernels has
both features, so `__expf_fma` is the variant basalt runs and the one the port reproduces.

The two variants are the same function except at the rounding step. Arm writes every multiply-add with operators;
the contraction sites are the three of the polynomial and, once `z = InvLn2N * xd` is substituted, `z + SHIFT` and
`z - kd`. Contracting the polynomial changes nothing at any of the 2^32 arguments [M], because the double carries
29 bits more than the float it rounds back to. Contracting the rounding step changes two: `0x4202422f` and
`0xc27c65d9` [M], because the fused form subtracts `kd` from the unrounded product where the written form
subtracts it from the rounded `z` and is itself exact. `__expf_fma` fuses them; `__expf_sse2` does not and returns
the correctly rounded value there [M, the same hosts with `GLIBC_TUNABLES=glibc.cpu.hwcaps=-AVX2,-FMA`]. The port
writes those two steps as explicit `std::fma` so that `-ffp-contract` cannot decide which function it is.

Arm's other rounding branch, the `round`/`lround` one, is not glibc's anywhere: glibc 2.39 carries a single
`sysdeps/ieee754/flt-32/math_config.h`, which defaults `TOINT_INTRINSICS` to 0 and defines it nowhere else, so
every architecture's glibc compiles the shift branch the port transcribes [M, lines 40-44 of that file]. It would
be the same function here in any case: `InvLn2N * (double) x` is never exactly a tie at any float32 argument
[M, swept over all 2^32], so no argument reaches the one place the two branches round differently.

## 2. The Codegen For Line 434

x86-64 gcc 14.3, `-std=c++20 -O3 -DNDEBUG`, no `-m` flags, which is how basalt builds `pool.cpp` [M, Compiler
Explorer, with pool.cpp:425-434 kept structurally intact]:

```
movss xmm1, [g] ; g
movaps xmm0, xmm1
xorps xmm0, [.LC27] ; -g, a sign flip (0x80000000), exact
movss [rsp+8], xmm1 ; spill g across the call
call expf ; e = expf(-g), one scalar call
addss xmm0, [.LC28] ; d = e + 1.0f
movss xmm1, [rsp+8]
divss xmm1, xmm0 ; q = g / d
mulss xmm1, [u] ; res = q * u
movss [res], xmm1
```

Three single-precision instructions, no fused multiply-add (there is no multiply feeding an add in the
expression), one scalar `expf` call. With `-march=x86-64-v3` the same sequence appears as the VEX encodings, so
the bits do not depend on which of basalt's build configurations compiles the pool [M].

The operand order is the compiler's, not the source's, and it decides the NaN. In Intel syntax the destination is
SRC1: the add takes `e` as SRC1 and the constant as SRC2, which is the source's operands swapped and is exact
since addition commutes and `1.f` is never a NaN; the divide takes `g`, which does not commute; the multiply takes
the quotient, which is the one that could have gone either way.

## 3. Which NaN

x86 writes the floating-point indefinite `0xffc00000`, sign bit set, where AArch64 writes the default NaN
`0x7fc00000` [S, Intel SDM Vol. 1 4.8.3.7; S, Arm ARM A1.5.3]. An SSE scalar arithmetic instruction handed a NaN
returns the first of its sources that is one, quieted, and otherwise the indefinite when the operation itself
creates one. So:

* `g` = -inf gives `e` = +inf, `d` = +inf and `-inf / inf` at the divide: `0xffc00000`.
* The add never creates one. Its only invalid form is `inf + -inf`, and `e` is `expf`'s own return, which is
never negative at any of the 2^32 arguments [M]. It is written as the instruction for fidelity to what basalt
executes, and the recorded vectors cannot tell it from the C operator.
* `0 * inf` and `inf * 0` at the multiply: `0xffc00000`. Several recorded rows reach it, including
`g` = `0xc2b17218` (just past the overflow threshold, where `g / inf` is a signed zero) with `u` = +inf.
* A NaN argument comes back quieted with its payload and sign: `expf` returns `x + x`, which quiets on both
architectures, and then the divide returns its first source. `0x7f812345` gives `0x7fc12345`.
* With a NaN `g` and a NaN `u` the multiply keeps the quotient's payload, not `u`'s.

`expf` itself needs no help, since `x + x` is the same on both. The add, the divide and the multiply are written
as the x86 instructions rather than as C operators (`x86_add`, `x86_div`, `x86_mul` in the kernel).

One corner of the SSE rule is outside the recorded corpus. SSE returns the first source operand that is a NaN,
where an AArch64 instruction prefers a signalling NaN wherever it sits; the two part only when SRC1 is a quiet NaN
and SRC2 a signalling one, and no recorded row holds that pair. Case D3 asserts it [S] against the two manuals
rather than against a measurement, and says so.

## 4. The Fixtures

Two recorded files under `tests/kernels/fixtures/`, both measured on Compiler Explorer's x86-64 executors (Xeon
Platinum 8375C and 8488C, AVX-512) under glibc 2.39 = Ubuntu 24.04 libc6 2.39-0ubuntu8.9, libm build-id
`d53fcbff9f855463606b25cfbe2a2e10856ea3a7`, MXCSR control `0x1f80` [M, 2026-10-18]. Each carries its own
provenance in its header comment.

`swiglu_vectors.txt`, 4,226 rows of `g u res` as binary32 bit patterns: basalt's whole line, built as basalt
builds it. 57 special `g` against two `u` (the thresholds, the two arguments of section 1, zeros, infinities,
quiet and signalling NaNs, subnormals), 16 special-`u` pairs, and 4,096 random pairs with `|g| < 40` and most of
their mass near zero. Six runs across the two CPUs agree, and every run's own digest of the `res` column is
`dfdc90ecf37deba2`. Test case A2 holds every row.

`expf_digests.txt`, 4,096 digests: `expf` at every one of the 4,294,967,296 float32 arguments. Block k covers bit
patterns `k << 20 .. (k << 20) | 0xfffff` in increasing order, and its digest is the FNV-1a of the result bits,
`h = 0xcbf29ce484222325` then `h = (h ^ bits) * 0x100000001b3 mod 2^64` per argument. The trailer carries the two
blocks `__expf_sse2` digests differently, `0x420` and `0xc27`, which are the blocks holding the two arguments of
section 1. Test case B4 holds every block, through `_native.basalt_expf_digest`.

## 5. What The Port Reproduces

[M, this repository on a 48 GB 40-core M3 Max, Apple clang 21.0.0 at `-O2 -ffp-contract=off`]:

* All 4,096 blocks of `expf_digests.txt`, so `expf_glibc` is `__expf_fma` at every float32 argument.
* All 4,226 rows of `swiglu_vectors.txt`, including every NaN row, and the digest of its own `res` column is
`dfdc90ecf37deba2`. Blocks 0x7f8-0x7ff and 0xff8-0xfff of the digest sweep are `expf`'s own output at all
16,777,214 NaN arguments, so glibc's NaN bits are pinned too.
* Built with the rounding step unfused instead, it fails exactly blocks `0x420` and `0xc27`, and its digests at
those two blocks are the trailer's `__expf_sse2` digests exactly. The port is therefore either variant, and the
only thing that separates them is the two `std::fma` calls.
Loading