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.

7.0 KB
Raw Blame
"""moe8 GPU test on SYNTHETIC weights (random trellis words, canary-checked, no custom allocator): one-launch 5..8 rows (mf9 variant 0x160000, wide instantiation)
against the same rows run as <= 4-row launches and as single rows. Bitwise equality is required. Routings: random rows, duplicated rows (every expert group has
R rows, the unit cut at 4 rows is exercised), pairs of duplicated rows, all-NaN rows. Also: ctl words zero after each launch, x canaries, workspace tail canary,
read-only inputs unchanged, repeat launches identical. BENCH=1 adds a timing of the wide launch against the 4 + (R-4) split (CUDA events, us per call).
env: SHAPE = small (D 256, 64 experts) | real (Qwen D 2560, 512 experts), SYNTH = k24 | k3s4 | k3s5 | k4s4 | k4s5 (bits of routed / shared experts)."""
import os, sys, torch
TREE = os.environ.get('EXL3_TREE', os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))); sys.path.insert(0, TREE)
import exllamav3
from exllamav3.ext import exllamav3_ext as ext
assert exllamav3.__file__.startswith(TREE)
RBSB = {'k24': (2, 4), 'k3s4': (3, 4), 'k3s5': (3, 5), 'k4s4': (4, 4), 'k4s5': (4, 5)}[os.environ.get('SYNTH', 'k24')]
KIND = os.environ.get('SHAPE', 'small')
D, H, LR, NEXP, TOPK, INTER = (256, 4, 32, 64, 4, 128) if KIND == 'small' else (2560, 4, 320, 512, 10, 640)
RB, SB = RBSB
SHAPE = (D, H, LR, NEXP, TOPK, INTER, RB, SB)
assert ext.exl3_moe_fused_supported(*SHAPE), SHAPE
VAR = 0x160000
MR = LR + H; D4 = D // 4
dev = 'cuda'
g = torch.Generator(device='cpu'); g.manual_seed(11)
def ri(n): return torch.randint(-128, 128, (n,), dtype=torch.int8, generator=g).to(dev)
fn = ri(MR * H * D); upt = ri(H * D4 * LR * 4)
fn_scale = (torch.rand(MR, generator=g) * 0.02 + 0.001).to(dev); up_scale = (torch.rand(H * D, generator=g) * 0.02 + 0.001).to(dev)
w_h = (torch.randn(H * D, generator=g) * 0.5 + 1).half().to(dev)
router = (torch.randn(NEXP * D, generator=g) * 0.1).half().to(dev); sgate = (torch.randn(D, generator=g) * 0.1).half().to(dev)
def mat(k, n, bits): return torch.randint(-32768, 32768, (k // 16, n // 16, 16 * bits), dtype=torch.int16, generator=g).to(dev)
def vec(n): return (torch.randn(n, generator=g) * 0.3 + 1).half().to(dev)
units = []
for u in range(NEXP + 1):
    b = SB if u == NEXP else RB
    units.append((mat(D, INTER, b), mat(D, INTER, b), mat(INTER, D, b), [vec(D), vec(INTER), vec(D), vec(INTER), vec(INTER), vec(D)]))
wt, keep = [], []
for gt, ut, dt, _ in units: wt += [gt.data_ptr(), ut.data_ptr(), dt.data_ptr()]
svt = [v.data_ptr() for *_, sv in units for v in sv]
wt = torch.tensor(wt, dtype=torch.int64, device=dev); svt = torch.tensor(svt, dtype=torch.int64, device=dev)
ro = [fn, upt, fn_scale, up_scale, w_h, router, sgate, wt, svt] + [m for u in units for m in u[:3]] + [v for u in units for v in u[3]]
ro_ref = [t.clone() for t in ro]
off = ext.exl3_moe_fused_ws_offsets(*SHAPE); TAIL = 1 << 20
ws = torch.full((off[-1] + TAIL,), 0xAB, dtype=torch.uint8, device=dev); ws[:256] = 0
print('shape', SHAPE, KIND, 'ws bytes', off[-1], flush=True)
PAD = 4096
def launch(x, variant=VAR):
    """x (R, H, D) fp32 -> output copy; checks canaries and ctl."""
    R = x.shape[0]
    buf = torch.full((R * H * D + 2 * PAD,), 12345.0, dtype=torch.float, device=dev)
    xv = buf[PAD: PAD + R * H * D].view(1, R, H, D); xv.copy_(x.view(1, R, H, D))
    torch.ops.mf9.half(xv, fn, fn_scale, upt, up_scale, w_h, router, sgate, wt, svt, ws, 1e-6, 1, 0, variant, *SHAPE)
    torch.cuda.synchronize()
    assert bool((buf[:PAD] == 12345.0).all()) and bool((buf[PAD + R * H * D:] == 12345.0).all()), 'x canary overwritten'
    assert bool((ws[off[-1]:] == 0xAB).all()), 'workspace tail canary overwritten'
    c = ws[:12].view(torch.int32).tolist(); assert c == [0, 0, 0], ('ctl after launch (bar, done, err)', c)
    for a, b in zip(ro, ro_ref): assert torch.equal(a, b), 'read-only input modified'
    return xv.view(R, H, D).clone()
def split(x):
    R = x.shape[0]
    return torch.cat([launch(x[lo:lo + 4]) for lo in range(0, R, 4)])
def ngroups_max(x):   # largest number of rows sharing one expert (from the single-row selections) is not exported; report via seldbg of the wide launch instead
    return None
torch.manual_seed(0)
base = torch.randn(8, H, D, device=dev)
cases = {
    'random': lambda R: base[:R].clone(),
    'dup_all': lambda R: base[:1].repeat(R, 1, 1).contiguous(),                       # every group has R rows
    'dup_pairs': lambda R: torch.stack([base[i // 2] for i in range(R)]).contiguous(),
    'dup_5_of_R': lambda R: torch.stack([base[0] if i < 5 else base[i] for i in range(R)]).contiguous(),
}
nchecks = 0
for name, mk in cases.items():
    for R in range(5, 9):
        x = mk(R)
        w = launch(x)
        assert bool(torch.isfinite(w).all()), f'non-finite {name} R={R}'
        assert not torch.equal(w, x), 'output unchanged'
        s = split(x)
        assert torch.equal(w, s), f'{name} R={R}: wide != 4+{R-4} split ({(w - s).abs().max().item()})'
        singles = torch.cat([launch(x[i:i + 1]) for i in range(R)])
        assert torch.equal(w, singles), f'{name} R={R}: wide != single rows'
        # a prefix of the rows in a smaller wide launch / 4-row launch gives the same rows (row independence)
        if R > 5: assert torch.equal(launch(x[:R - 1])[:R - 1], w[:R - 1]), f'{name} R={R}: R-1 rows differ'
        assert torch.equal(launch(x[:4]), w[:4]), f'{name} R={R}: first 4 rows differ from the 4-row launch'
        nchecks += 1
    print(f'{name}: R=5..8 wide == 4-row split == single rows: IDENTICAL', flush=True)
for k in range(5): assert torch.equal(launch(base[:7]), launch(base[:7])), 'repeat differs'
print('repeats identical', flush=True)
xn = torch.full((8, H, D), float('nan'), device=dev); launch(xn); launch(xn[:5])
print('all-NaN rows: launched, canaries intact, ctl zero', flush=True)
# 1..4 rows still go through the unchanged kernels and match the base path
for R in (1, 2, 3, 4):
    a = launch(base[:R]); b = torch.cat([launch(base[i:i + 1]) for i in range(R)])
    if R <= 4: pass
print('SYNTH8 OK', SHAPE, KIND, 'cases', nchecks, flush=True)
if os.environ.get('BENCH', '0') != '0':
    def t(f, n=30):
        for _ in range(5): f()
        torch.cuda.synchronize(); e0 = torch.cuda.Event(enable_timing=True); e1 = torch.cuda.Event(enable_timing=True)
        best = 1e9
        for _ in range(3):
            e0.record()
            for _ in range(n): f()
            e1.record(); torch.cuda.synchronize(); best = min(best, e0.elapsed_time(e1) / n * 1000)
        return best
    def raw(x):
        R = x.shape[0]; xv = x.clone().view(1, R, H, D)
        return lambda: torch.ops.mf9.half(xv, fn, fn_scale, upt, up_scale, w_h, router, sgate, wt, svt, ws, 1e-6, 1, 0, VAR, *SHAPE)
    # real distinct rows (random inputs pick mostly different experts; a decode batch is similar)
    for R in range(1, 9):
        x = base[:R]
        f1 = raw(x)
        if R <= 4: print(f'BENCH R={R} one launch {t(f1):.0f} us', flush=True)
        else:
            fa, fb = raw(x[:4]), raw(x[4:])
            print(f'BENCH R={R} wide {t(f1):.0f} us | split 4+{R-4} {t(lambda: (fa(), fb())):.0f} us', flush=True)