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
19 changes: 12 additions & 7 deletions tests/models/test_hy_v4_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
EPS = 1e-6


# f32 GEMM runs at TF32 precision on M5 (~1e-3 relative), so the fp64
# reference comparison sits well above that noise; the CPU path is exact.
_ATOL = 5e-3


def _args(**over):
base = dict(
model_type="hy_v4", vocab_size=32, hidden_size=DIM, intermediate_size=32,
Expand Down Expand Up @@ -172,7 +177,7 @@ def test_dense_attention_matches_fp64_reference(L):
got = attn(x, mask=mask)[0]
mx.eval(got)
ref = _ref_attention(attn, x, mask)
assert np.allclose(np.array(got), ref, atol=2e-4), np.abs(
assert np.allclose(np.array(got), ref, atol=_ATOL), np.abs(
np.array(got) - ref).max()


Expand All @@ -192,7 +197,7 @@ def test_sparse_selection_matches_fp64_reference(keys, topk):
got = attn(x, mask=mask, top_k=mx.array(sel))[0]
mx.eval(got)
ref = _ref_attention(attn, x, mask, top_k=sel)
assert np.allclose(np.array(got), ref, atol=2e-4), np.abs(
assert np.allclose(np.array(got), ref, atol=_ATOL), np.abs(
np.array(got) - ref).max()


Expand Down Expand Up @@ -220,7 +225,7 @@ def test_decode_step_gather_matches_fp64_reference():
full = mx.concatenate([xs, step], axis=1)
ref = _ref_attention(attn, full, mask=None, top_k=np.broadcast_to(
sel, (1, 1, S, topk)))[:, -1:, :]
assert np.allclose(np.array(got), ref, atol=2e-4), np.abs(
assert np.allclose(np.array(got), ref, atol=_ATOL), np.abs(
np.array(got) - ref).max()


Expand Down Expand Up @@ -253,7 +258,7 @@ def test_tiled_prefill_matches_the_direct_path(monkeypatch):
monkeypatch.setattr(hy_v4_model, "_STREAM_MIN_KEYS", 4)
tiled = np.array(attn(x, mask=mask)[0])

assert np.allclose(direct, tiled, atol=2e-4), np.abs(direct - tiled).max()
assert np.allclose(direct, tiled, atol=_ATOL), np.abs(direct - tiled).max()


def test_tiled_prefill_matches_the_fp64_reference_under_selection(monkeypatch):
Expand All @@ -271,7 +276,7 @@ def test_tiled_prefill_matches_the_fp64_reference_under_selection(monkeypatch):
monkeypatch.setattr(hy_v4_model, "_STREAM_MIN_KEYS", 4)
got = np.array(attn(x, mask=mask, top_k=mx.array(sel))[0])
ref = _ref_attention(attn, x, mask, top_k=sel)
assert np.allclose(got, ref, atol=2e-4), np.abs(got - ref).max()
assert np.allclose(got, ref, atol=_ATOL), np.abs(got - ref).max()


# --- the rope-convention fork ------------------------------------------------
Expand All @@ -289,7 +294,7 @@ def test_reference_holds_under_either_rope_convention(traditional):
mask = mx.array(_causal(6, 6))
got = attn(x, mask=mask)[0]
mx.eval(got)
assert np.allclose(np.array(got), _ref_attention(attn, x, mask), atol=2e-4)
assert np.allclose(np.array(got), _ref_attention(attn, x, mask), atol=_ATOL)


def test_rope_convention_default_is_interleaved():
Expand Down Expand Up @@ -317,5 +322,5 @@ def test_sparse_disable_env_forces_dense(monkeypatch):
sparse = np.array(attn(x, mask=mask, top_k=mx.array(sel))[0])
monkeypatch.setenv("GMLX_HY4_SPARSE_DISABLE", "1")
forced = np.array(attn(x, mask=mask, top_k=mx.array(sel))[0])
assert np.allclose(forced, _ref_attention(attn, x, mask), atol=2e-4)
assert np.allclose(forced, _ref_attention(attn, x, mask), atol=_ATOL)
assert not np.allclose(sparse, forced) # the selection did bite
11 changes: 8 additions & 3 deletions tests/models/test_hy_v4_hyper_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
MAGNITUDE = 2.0


# f32 GEMM runs at TF32 precision on M5 (~1e-3 relative), so the fp64
# reference comparison sits well above that noise; the CPU path is exact.
_ATOL = 5e-3


def _args(**over):
base = dict(
model_type="hy_v4", vocab_size=16, hidden_size=DIM, intermediate_size=16,
Expand Down Expand Up @@ -74,8 +79,8 @@ def test_pre_collapse_matches_reference():
pre_ref, post_ref = _ref_gates(x, hc.fn, hc.base, hc.scale)
collapsed = (pre_ref[..., None] * x.astype(np.float64)).sum(axis=-2)

assert np.allclose(np.array(got), collapsed, atol=2e-5)
assert np.allclose(np.array(post), post_ref, atol=2e-5)
assert np.allclose(np.array(got), collapsed, atol=_ATOL)
assert np.allclose(np.array(post), post_ref, atol=_ATOL)
assert got.shape == (2, 3, DIM)
assert post.shape == (2, 3, HC)

Expand All @@ -92,7 +97,7 @@ def test_expand_matches_reference():

ref = (residual.astype(np.float64)
+ post.astype(np.float64)[..., None] * y.astype(np.float64)[..., None, :])
assert np.allclose(np.array(got), ref, atol=2e-5)
assert np.allclose(np.array(got), ref, atol=_ATOL)
assert got.shape == residual.shape


Expand Down