Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
tokenizer_test think_split_test gguf_meta_test mem_admit_test pm_routes_test \
laya_encoder_test http_guard_test moe_gguf_index_test laya_bounds_test \
npu_model_bounds_test npu_forward_loader_test npu_sampling_test \
npu_flm_head_import_test \
tokenizer_linear_test hrx_placement_test hrx_cwsr_test npu_device_lock_test -j
- name: Test
# One hung test must not hold the job for hours: each test gets 10 minutes.
Expand Down
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ option(ONEBIT_NPU_HOST "Build the NPU host library and its tests" ${_onebit_npu_
if(ONEBIT_NPU_HOST)
find_package(PkgConfig REQUIRED)
pkg_check_modules(PCRE2 REQUIRED IMPORTED_TARGET libpcre2-8)
add_library(onebit_npu STATIC npu/file_map.cpp npu/full_elf.cpp npu/model.cpp npu/pack.cpp npu/private_route.cpp npu/q4nx.cpp
add_library(onebit_npu STATIC npu/file_map.cpp npu/flm_head_import.cpp npu/full_elf.cpp npu/model.cpp npu/pack.cpp npu/private_route.cpp npu/q4nx.cpp
npu/tokenizer.cpp)
target_include_directories(onebit_npu PUBLIC npu)
target_link_libraries(onebit_npu PUBLIC nlohmann_json::nlohmann_json PRIVATE PkgConfig::PCRE2)
Expand Down Expand Up @@ -508,6 +508,11 @@ add_executable(npu_q4nx_test tests/npu_q4nx_test.cpp)
target_link_libraries(npu_q4nx_test PRIVATE onebit_npu)
add_test(NAME npu_q4nx COMMAND npu_q4nx_test ${CMAKE_SOURCE_DIR}/tests/golden/q4nx)
add_executable(npu_pack_test tests/npu_pack_test.cpp)
add_executable(flm_head_import tools/flm_head_import.cpp)
target_link_libraries(flm_head_import PRIVATE onebit_npu)
add_executable(npu_flm_head_import_test tests/npu_flm_head_import_test.cpp)
target_link_libraries(npu_flm_head_import_test PRIVATE onebit_npu)
add_test(NAME npu_flm_head_import COMMAND npu_flm_head_import_test ${CMAKE_SOURCE_DIR}/tests/golden/flm-i8)
target_link_libraries(npu_pack_test PRIVATE onebit_npu)

# The device weight layout contract (q4nx_weight_layout): pure index math, no device
Expand Down
5 changes: 5 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ FastFlowLM (https://github.com/ROCm/FastFlowLM)
(FastFlowLM/*-NPU2; the weights keep their own licenses, e.g. Qwen's Apache-2.0).
The engine runs them on its own kernels as full ELFs; it does not use
FastFlowLM's runtime or ship its kernel binaries.
tools/flm_head_import (the MTP-head converter) likewise reads FastFlowLM's published container and
4,736-byte weight-block format and rewrites it in our own chunk forms: no FastFlowLM code is linked,
copied or shipped, and the converter's self-check compares against floats recorded from their published
library rather than against their binary. The format facts it is written from are in
amd-oss/npu-re docs/FACTS-flm-q4nx-container-and-mtp-head.md.
Powered by FastFlowLM, as FastFlowLM asks.

PrismML Ternary Bonsai (https://huggingface.co/prism-ml, https://github.com/PrismML-Eng/llama.cpp)
Expand Down
31 changes: 31 additions & 0 deletions docs/npu.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,34 @@ The NPU needs the `amdxdna` kernel driver. `third_party/xdna-driver` pins upstre
against (the kernel's own copy also works); nothing from the pin is built into or shipped with
the release. `.github/workflows/bump-xdna.yml` moves the pin when run by hand, because a driver
move means rebuilding the module on Strix Halo and checking the NPU there first.

### The MTP head (FastFlowLM's container -> ours)

`tools/flm_head_import` (built as `flm-head-import`) turns a FastFlowLM MTP-head container
(`model.mtp.q4nx`) into a sidecar directory of ours. The head is one Qwen3.5 full-attention layer plus an input
fusion; the base model keeps its embedding, lm-head, tokenizer and RoPE, so this artifact is only the head.

The container is safetensors-shaped and its quantised tensors are rank-3 `[nrb, ncb, 4736]` with dtype `"I8"`.
That 4,736-byte cell **is our Q4_K chunk**: `u8 scale[g*32+n]` at 0, `u8 min[g*32+n]` at 256, nibbles at 512
(byte `k*16 + n/2`, even n in the low nibble), `bf16 S[n]` at 4608, `bf16 M[n]` at 4672, and
`w = q4 * (scale*S) + min*M`. Measured, not assumed: `q4nx::dequant_tile(Chunk::Q4K, …)` reproduces FastFlowLM's
own decoder **bit for bit** on six cells recorded from six of the head's matrices (`tests/golden/flm-i8`, floats
recorded from their published library; the layout facts are in amd-oss/npu-re
`docs/FACTS-flm-q4nx-container-and-mtp-head.md`). Two things follow, and they are the reason this tool is small:

* the default form copies every cell **unchanged** — no requantisation, nothing to lose, and the import
self-check re-decodes each matrix from the source and from the copy and compares;
* the alternative `--form q4_1` writes our mode-1 chunk (5,120 B: `bf16 S[256]`, `bf16 M[256]`, 4,096 code
bytes, `w = code*S + M`) through `q4nx::q4k_to_q4_1`, whose only loss is rounding `S*scale` and `M*min` to
bf16. The tool prints that factorisation error rather than hiding it (measured on the 27B head: 0.67 % of
values unchanged, max |delta| 1.2e-3, mean |delta| 6.8e-5).

flm-head-import model.mtp.q4nx -o head-q4k # cells copied unchanged
flm-head-import model.mtp.q4nx -o head-q4_1 --form q4_1 # our mode-1 chunks

`tools/flm_head_pb16i.py` writes the third form, the `pb16i` image (`npu/forward/include/q4nx_pb16i.h`), and it
is the one form this head cannot have exactly: `pb16i` carries one int16 magnitude per (32 K, column) with no
per-group bias, while the source blocks are asymmetric (`w = q4*S + M`) and their magnitudes span about 100x
inside a column, where an int16 with an 11-bit mantissa allows about 16x. Forcing it costs a measured 11-18 %
relative L1 residual, so the mode-1 or Q4_K forms are the ones to run; the script keeps the measurement next to
the images it writes.
Loading
Loading