Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

2.6 KB
Raw Blame
#!/usr/bin/env python3
"""Q8_0 multi-row decode GEMV (x-rows 2..16, BOLT_FQ_Q8_MROWS): output row m must equal the one-row kernel's output for x-row m BIT FOR BIT,
for R = 1..16 (row groups of 8: R = 9..16 runs two groups), half and float output, several (N, K) shapes. Also checks that
BOLT_FQ_Q8_MROWS=0 (the old per-(row, column) launch) gives the same bytes, so batch composition never changes an output.
usage: python tests/test_q8_gemv_mrows.py            (needs a CUDA / HIP GPU and the built extension)"""
import os, subprocess, sys

CASES = [(1, 32), (37, 1024), (512, 2048), (1000, 2048), (2048, 4096), (4096, 512), (5, 12288)]   # (N, K)


def child(tag):
    import torch
    from exllamav3.ext import exllamav3_ext as ext
    torch.manual_seed(0)
    bad = n = 0
    outs = []
    for N, K in CASES:
        nb = K // 32
        d = (torch.rand(N, nb, device="cuda") * 0.02 + 1e-3).half()
        q = torch.randint(-127, 128, (N, nb, 32), device="cuda", dtype=torch.int8)
        w = torch.cat([d.view(torch.uint8).reshape(N, nb, 2), q.view(torch.uint8)], dim=2).contiguous()
        for R in range(1, 17):
            x = torch.randn(R, K, device="cuda", dtype=torch.half)
            for dt in (torch.half, torch.float):
                out = torch.empty(R, N, device="cuda", dtype=dt)
                ext.fq_gemv_q8_0(x, w, out)
                for m in range(R):
                    o1 = torch.empty(1, N, device="cuda", dtype=dt)
                    ext.fq_gemv_q8_0(x[m:m + 1].contiguous(), w, o1)
                    n += 1
                    if not torch.equal(out[m], o1[0]):
                        bad += 1
                        print(f"{tag}: MISMATCH N {N} K {K} R {R} row {m} {dt}", flush=True)
                outs.append(out.view(torch.uint8).flatten().cpu())
    torch.save(torch.cat(outs), sys.argv[3])
    print(f"{tag}: {n} row checks, {bad} mismatches", flush=True)
    sys.exit(1 if bad else 0)


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "--child":
        child(sys.argv[2])
    import tempfile
    import torch
    with tempfile.TemporaryDirectory() as td:
        rc = 0
        for tag, val in (("mrows", "1"), ("per_row", "0")):
            env = dict(os.environ, BOLT_FQ_Q8_MROWS=val)
            rc |= subprocess.run([sys.executable, __file__, "--child", tag, f"{td}/{tag}.pt"], env=env).returncode
        same = torch.equal(torch.load(f"{td}/mrows.pt"), torch.load(f"{td}/per_row.pt"))
        print("BOLT_FQ_Q8_MROWS=1 vs 0: " + ("identical bytes" if same else "DIFFERENT"))
        print("PASS" if rc == 0 and same else "FAIL")
        sys.exit(0 if rc == 0 and same else 1)