mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
D1 and D2 together: every multiply of the native runner's DiT call on our own GEMM (src/image/dit/dit_gemm.cu: weights decoded to f16 by te_dequant, activations to f16, cuBLAS / hipBLAS GemmEx with float32 sums). Groups: blocks + img_in (D1); timestep MLP, modulation, norm_out; txt_in; the head (D2). Wired into the pinned stable-diffusion.cpp by tools/image/sdcpp_dit/apply.py (build_variant.py --dit) behind --model-args strata_dit_gemm=1. No build or product path uses it; sd.cpp stays the default and the fallback. Measured together, not D1 alone: D1 alone was 0.00331 from the f32 reference against the emulation's 0.00065 (5x); only all four groups together reach the emulation's level, because the multiplies left on ggml's MMQ (8-bit activations) then dominate. vel_0 rel L2 to f32, 512 prompts 0-3 (mean), per group switched on: none (native runner) 0.00609 | all (D1+D2) 0.00065 | D1 0.00331 | D1 + timestep/modulation/norm_out 0.00271 | D1 + txt_in 0.00342 | D1 + head 0.00200 | f16 emulation 0.00065 | stock 0.00678 Stage rule (coordinator's restated rule: mean within 1.25x the emulation, no case above 1.5x, every case closer than stock and native). Nine cases (prompts 0-7 at 512, prompt 0 at 1024), velocity rel L2 to f32: against the emulation with float32 sums (dit_ref mode f16acc32): mean ratio 1.039, largest 1.177 (512 p6); against the emulation with exact sums (mode f16op): mean ratio 1.116, 512 p5 at 2.078. Every case 0.00048-0.00114 from f32; stock 0.0049-0.0090, native 0.0052-0.0083. The comparison was changed to the float32-sum emulation after the measurement (coordinator, 11-11): on prompt 5 the exact-sum emulation moves from 0.00055 to 0.00086 when started from inputs that differ by 3e-6; a per-block trace found no step that differs from the emulation's arithmetic (block 20 replayed from our input agrees to 0.0002). Both modes stay in dit_ref.py; tools/image/gate/dit_acc32_cases.py reproduces the table. Gate (a): with the argument off, 16 pictures (prompts 0-7, seeds 42 and 1234, 1024) byte-identical to the build before D1: 16 of 16 (dgxspark-20261111-0513-8147). Pictures, 16 against stock sd.cpp, PSNR mean / min, SSIM mean / min (GB10): sd.cpp cuBLAS-forced 33.35 / 23.06 dB, 0.979 / 0.937 sd.cpp native runner 34.32 / 21.32 dB, 0.982 / 0.923 ours (D1+D2) 34.45 / 23.74 dB, 0.982 / 0.948 ours vs native 35.90 / 20.71 dB, 0.982 / 0.912 Sampling median at 1024: stock 19.57 s, native 12.32 s, ours 12.54 s. Also: te_type_supported / te_row_bytes moved into te_dequant.cu (needed without the host decoder); run_set.py --model-arg; test-only trace hooks in apply.py (STRATA_DIT_TRACE_DIR / STRATA_DIT_TRACE_BLOCK, read after a backend sync, change no value); dit_replay.py / dit_replay2.py (the prompt-5 trace tools). Tests: tools/test_sdcpp_dit.py, tools/image/gate/test_dit_ref.py (modes).
82 lines
3.9 KiB
Python
82 lines
3.9 KiB
Python
"""tools/image/gate/test_dit_ref.py - TEST-ONLY: dit_ref.py's pieces against their definitions (no model file needed).
|
|
|
|
python -m pytest tools/image/gate/test_dit_ref.py"""
|
|
import math
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import dit_ref as R # noqa: E402
|
|
|
|
|
|
class Layout(unittest.TestCase):
|
|
def test_positions(self):
|
|
p = R.layout_positions(3, 2, 3)
|
|
self.assertEqual(p.shape, (3 + 6, 3))
|
|
self.assertEqual(p[:3].tolist(), [[0, 0, 0], [1, 1, 1], [2, 2, 2]])
|
|
# image tokens: (text length, h - (H - H/2), w - (W - W/2)), token h*W + w
|
|
self.assertEqual(p[3].tolist(), [3, -1, -2])
|
|
self.assertEqual(p[3 + 5].tolist(), [3, 0, 0])
|
|
|
|
def test_rope_table_and_rotation(self):
|
|
pos = R.layout_positions(4, 4, 4)
|
|
c, s = R.rope_table(pos)
|
|
self.assertEqual(c.shape, (4 + 16, 64))
|
|
self.assertTrue(np.all(c[0] == 1) and np.all(s[0] == 0)) # position 0: no rotation
|
|
x = np.random.default_rng(2).standard_normal((20, 3, 128)).astype(np.float32)
|
|
y = R.apply_rope(x, c, s)
|
|
self.assertTrue(np.allclose(np.linalg.norm(y, axis=-1), np.linalg.norm(x, axis=-1), rtol=1e-5))
|
|
self.assertTrue(np.array_equal(y[0], x[0]))
|
|
|
|
|
|
class Modes(unittest.TestCase):
|
|
def test_three_modes(self):
|
|
rng = np.random.default_rng(5)
|
|
x = rng.standard_normal((6, 256)).astype(np.float32)
|
|
Wm = rng.standard_normal((32, 256)).astype(np.float32)
|
|
f32, ex, acc = (R.make_mode(n) for n in ("f32", "f16op", "f16acc32"))
|
|
self.assertFalse(f32.f16)
|
|
self.assertTrue(ex.f16 and acc.f16) # both emulations round Q / K / V and P
|
|
y32, yex, yacc = f32.gemm(x, f32.w(Wm)), ex.gemm(x, ex.w(Wm)), acc.gemm(x, acc.w(Wm))
|
|
x16, w16 = R.f16r(x).astype(np.float64), R.f16r(Wm).astype(np.float64)
|
|
self.assertTrue(np.array_equal(yex, (x16 @ w16.T).astype(np.float32))) # exact sums, rounded once
|
|
self.assertTrue(np.allclose(yacc, yex, rtol=1e-5, atol=1e-5)) # float32 sums: same up to rounding
|
|
self.assertFalse(np.allclose(y32, yex, rtol=1e-5, atol=1e-5)) # f16 operands change the result
|
|
with self.assertRaises(SystemExit):
|
|
R.make_mode("f16")
|
|
|
|
|
|
class Pieces(unittest.TestCase):
|
|
def test_timestep_embedding(self):
|
|
e = R.timestep_embedding(np.array([0.0, 500.0], dtype=np.float32))
|
|
self.assertTrue(np.all(e[0, :128] == 1) and np.all(e[0, 128:] == 0))
|
|
j = 7
|
|
f = math.exp(-math.log(10000) * j / 128)
|
|
self.assertAlmostEqual(float(e[1, j]), math.cos(500 * f), places=5)
|
|
self.assertAlmostEqual(float(e[1, 128 + j]), math.sin(500 * f), places=5)
|
|
|
|
def test_norms_and_activations(self):
|
|
x = np.random.default_rng(1).standard_normal((5, 64)).astype(np.float32) * 3 + 1
|
|
y = R.layer_norm(x)
|
|
self.assertTrue(np.allclose(y.mean(-1), 0, atol=1e-5) and np.allclose(y.std(-1), 1, atol=1e-3))
|
|
self.assertAlmostEqual(float(R.gelu_tanh(np.float32([1.0]))[0]), 0.8411920, places=5)
|
|
self.assertAlmostEqual(float(R.silu(np.float32([1.0]))[0]), 1 / (1 + math.exp(-1)), places=6)
|
|
|
|
def test_attention_mask(self):
|
|
rng = np.random.default_rng(4)
|
|
q, k, v = (rng.standard_normal((7, 2, 128)).astype(np.float32) for _ in range(3))
|
|
o = R.attention(q, k, v, 3, False)
|
|
self.assertTrue(np.allclose(o[0], v[0], atol=1e-6)) # first text token sees only itself
|
|
o2 = R.attention(q[:3], k[:3], v[:3], 3, False) # text rows ignore the image tokens
|
|
self.assertTrue(np.allclose(o[:3], o2, atol=1e-6))
|
|
k2 = k.copy()
|
|
k2[5] += 1 # image rows see every token
|
|
self.assertFalse(np.allclose(R.attention(q, k2, v, 3, False)[6], o[6]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|