Skip to content

lean ROCm: Hadamard-rotated Q4_0 with W4A4 prompt processing (Qwen3.8-27B pp512 509) - #189

Merged
bong-water-water-bong merged 4 commits into
mainfrom
lean-h32
Sep 28, 2026
Merged

bong-water-water-bong merged 4 commits into
mainfrom
lean-h32

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Makes the Hadamard W4A4 prompt-processing path a supported route (owner's call: not experimental).

What changes

  • Pin third_party/llama.cpp-rocmfpx → d572668 (ggml-cuda (gfx1151): Hadamard-rotated W4A4 for Q4_0, chunked delta-net prefill, tiled transposed concat ROCmFPX#1): per-tensor Q4_0 W4A4 on the IU4 kernel, Hadamard-rotating activation quantizers (MMQ, MMVQ, W4A4), a chunked delta-net prefill kernel and a tiled transposed concat. test-backend-ops -b ROCm0: GATED_DELTA_NET 42/42, CONCAT 129/129, every Q4_0 MUL_MAT case (the 14 q4_0_rocmi4 failures are that type's lossy W4A4 path, unchanged).
  • tools/hadamard_q4_0.py: rotates the attention/FFN/delta-net alpha-beta projections of a Q8_0/BF16 source (and the imatrix), quantizes them to Q4_0 and every other matmul weight to another type, stamps onebit.hadamard_q4_0 = 32, and checks that every Q4_0 tensor is rotated (deletes the file otherwise).
  • 1bit serve reads the stamp (gguf_int in app/gguf_meta.h): --device auto → lean ROCm route with GGML_Q4_0_HADAMARD=1 and GGML_W4A4_TENSORS=all (a value set in the environment wins); any other device, or --laya, is refused. Unstamped files are unaffected.
  • Test tests/hadamard_route.sh (ctest hadamard_route, no GPU): refusal on vulkan, auto → ROCm0 with both variables, unstamped → Vulkan0 with neither. 6/6; smoke_serve PASS.
  • Docs docs/lean.md (the route, how to make a file, numbers); an update note on the 2026-09-28 blog post.

Measured (Strix Halo, Qwen3.8-27B, file made by the tool from Unsloth's Q8_0 + imatrix)

pp512 PPL KLD vs BF16 top-1
Q4_0 exact int8 ~400 6.021 0.029 91.9%
Q4_0 W4A4, no rotation 461-488 6.241 0.084 87.4%
Q4_0-H32 W4A4 509 (engine lean ROCm build) 6.096 0.055 89.3%

1bit serve -m Qwen3.8-27B-Q4_0-H32.gguf (no device flag): picks the ROCm route, answers "Paris", 1,838-token prompt at 440-470 tok/s; decode 11.4 tok/s (decode-heavy work stays on Vulkan + --dflash).

The file is published as 1bit-MONSTER/Qwen3.8-27B-Q4_0-H32-GGUF (Apache-2.0, as the base model).

🤖 Generated with Claude Code

bong-water-water-bong and others added 4 commits September 28, 2026 07:06
…mFPX d572668)

WIP: tool + serve detection + pin; docs and measurements follow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@context7

context7 Bot commented Sep 28, 2026

Copy link
Copy Markdown

Docs7 for 1bit-monster/engine

Result Status Action
Deployment ➖ Not used —
Content review ➖ Did not run. This site has no agent runs available this month. Wait for the monthly reset or check your Docs7 plan. —

Commit a273d35

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect device validation for Hadamard Q4_0 files

The validation logic in launch_for function incorrectly allows --device rocm to proceed with Hadamard Q4_0 files, but then throws an error if device != "rocm" (line 645). This is contradictory because the condition on line 634 already ensures that device != "vulkan" && device != "rocm" throws an error, so the subsequent check on line 645 will never be reached for a valid device. This could lead to confusion or incorrect behavior if the device validation logic is modified in the future.

if (device != "vulkan" && device != "rocm")
    throw std::runtime_error("--lean runs on --device vulkan or rocm");
if (!o.prefill_device.empty()) throw std::runtime_error("--prefill-device works with --device vulkan only");
argv = {o.llama_server.empty() ? default_lean_server(device) : o.llama_server,
        "-m", o.model, "--host", "127.0.0.1", "--port", std::to_string(child_port),
        "--device", device == "rocm" ? "ROCm0" : "Vulkan0", "-ngl", "99", "--jinja"};
if (o.ctx_size > 0) { argv.push_back("-c"); argv.push_back(std::to_string(o.ctx_size)); }
if (hadamard_q4_0(o.model)) {
    // tools/hadamard_q4_0.py stamped it: its Q4_0 weights are rotated, so the activation
    // quantizers must rotate too, and every Q4_0 matmul takes the W4A4 kernel (docs/lean.md).
    // A value set in the environment wins (GGML_W4A4_TENSORS= keeps exact int8).
    if (device != "rocm") throw std::runtime_error(o.model + " is Hadamard-rotated: it runs on --device rocm only");
    if (!std::getenv("GGML_Q4_0_HADAMARD")) env.push_back("GGML_Q4_0_HADAMARD=1");
    if (!std::getenv("GGML_W4A4_TENSORS")) env.push_back("GGML_W4A4_TENSORS=all");
}
Potential runtime error in serve_child function

In the serve_child function, if a Hadamard Q4_0 file is detected, the code sets o.device = "rocm" when o.device == "auto" (line 840). However, if o.device is already set to "rocm", it will not be changed. This could lead to unexpected behavior if the user explicitly sets --device rocm and the model is not Hadamard-rotated, as the code does not validate that the device matches the model requirements.

int serve_child(const Options& given) {
    Options o = given;
    if (hadamard_q4_0(o.model)) {
        // a rotated file has one route: the lean ROCm build with the Hadamard activation quantizer
        if (o.device == "auto") o.device = "rocm";
        if (o.device != "rocm")
            throw std::runtime_error(o.model + " is Hadamard-rotated (tools/hadamard_q4_0.py): it runs on --device rocm only, docs/lean.md");
        if (o.laya_auto || !o.laya_model.empty())
            throw std::runtime_error("--laya routes among devices; a Hadamard-rotated file runs on rocm only");
        std::fprintf(stderr, "1bit serve: %s is Hadamard-rotated: lean ROCm route, W4A4 prompt processing\n", o.model.c_str());
    }
Insecure temporary file handling

The script uses os.path.join(work, os.path.basename(a.out) + ".rotated-source.gguf") to create temporary files. If work is not properly validated or controlled, this could lead to insecure file handling, potentially allowing an attacker to manipulate the temporary file creation process or cause unexpected behavior.

rot_src = os.path.join(work, os.path.basename(a.out) + ".rotated-source.gguf")

@bong-water-water-bong
bong-water-water-bong merged commit 85578be into main Sep 28, 2026
11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the lean-h32 branch September 28, 2026 10:54
bong-water-water-bong added a commit that referenced this pull request Sep 28, 2026
* Blog: 509 tokens a second of prompt - the Hadamard route ships (#189)

Why prompts are a matrix-unit problem, the rotation (x.w = (Hx).(Hw), exact on the 8-bit
path: KLD 0.031 vs 0.029), the numbers (pp512 509, KLD 0.055 vs 0.084 unrotated), the tool
and serve's stamp routing, the delta-net prefill kernels, and what the route is for (decode
11 tok/s; Vulkan + DFlash2 for long answers). Numbers from docs/lean.md and docs/serve.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Blog: slug without underscore (site rule)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant