1818 quantize_matrix ,
1919 reset_dq_gemm_telemetry ,
2020)
21+ from fastvideo .mlx_runtime .minimax_h3 import linear as h3_linear # noqa: E402
2122
2223AFFINE_BITS = (2 , 3 , 4 , 5 , 6 , 8 )
2324GROUP_SIZES = (32 , 64 , 128 )
@@ -65,7 +66,7 @@ def test_dq_gemm_matches_qmm_for_supported_bit_widths(bits: int, group_size: int
6566 x = mx .random .normal ((16 , in_features )).astype (mx .bfloat16 )
6667 mx .eval (x )
6768 before = dq_gemm_engaged ()
68- got = linear (x , quantized )
69+ got = h3_linear (x , quantized )
6970 ref = _qmm (x , quantized )
7071 mx .eval (got , ref )
7172 assert dq_gemm_engaged () == before + 1
@@ -84,7 +85,7 @@ def test_dq_gemm_with_bias_and_batched_rows(monkeypatch: pytest.MonkeyPatch) ->
8485 x = mx .random .normal ((2 , 8 , 128 )).astype (mx .bfloat16 )
8586 bias = mx .random .normal ((64 , )).astype (mx .bfloat16 )
8687 mx .eval (x , bias )
87- got = linear (x , quantized , bias )
88+ got = h3_linear (x , quantized , bias )
8889 ref = _qmm (x , quantized ) + bias
8990 mx .eval (got , ref )
9091 assert dq_gemm_engaged () == 1
@@ -99,7 +100,7 @@ def test_dq_gemm_stays_on_qmm_below_threshold(monkeypatch: pytest.MonkeyPatch) -
99100 quantized = _try_quantize (64 , 128 , bits = 6 , group_size = 64 )
100101 x = mx .random .normal ((32 , 128 )).astype (mx .bfloat16 )
101102 mx .eval (x )
102- got = linear (x , quantized )
103+ got = h3_linear (x , quantized )
103104 ref = _qmm (x , quantized )
104105 mx .eval (got , ref )
105106 assert dq_gemm_engaged () == 0
@@ -113,9 +114,23 @@ def test_dq_gemm_env_zero_disables_dispatch(monkeypatch: pytest.MonkeyPatch) ->
113114 quantized = _try_quantize (64 , 128 , bits = 6 , group_size = 64 )
114115 x = mx .random .normal ((1024 , 128 )).astype (mx .bfloat16 )
115116 mx .eval (x )
116- linear (x , quantized )
117- mx .eval (x )
117+ got = h3_linear (x , quantized )
118+ ref = _qmm (x , quantized )
119+ mx .eval (got , ref )
120+ assert dq_gemm_engaged () == 0
121+ np .testing .assert_array_equal (np .asarray (got .astype (mx .float32 )), np .asarray (ref .astype (mx .float32 )))
122+
123+
124+ def test_shared_linear_stays_on_qmm_at_wide_m (monkeypatch : pytest .MonkeyPatch ) -> None :
125+ monkeypatch .setenv ("FASTVIDEO_MLX_DQ_GEMM" , "1" )
126+ reset_dq_gemm_telemetry ()
127+ quantized = _try_quantize (64 , 128 , bits = 6 , group_size = 64 )
128+ x = mx .random .normal ((1024 , 128 )).astype (mx .bfloat16 )
129+ got = linear (x , quantized )
130+ ref = _qmm (x , quantized )
131+ mx .eval (got , ref )
118132 assert dq_gemm_engaged () == 0
133+ np .testing .assert_array_equal (np .asarray (got .astype (mx .float32 )), np .asarray (ref .astype (mx .float32 )))
119134
120135
121136def test_non_affine_weights_stay_on_quantized_matmul (monkeypatch : pytest .MonkeyPatch ) -> None :
@@ -130,7 +145,7 @@ def test_non_affine_weights_stay_on_quantized_matmul(monkeypatch: pytest.MonkeyP
130145 pytest .skip (f"mxfp8 unsupported: { exc } " )
131146 x = mx .random .normal ((1024 , 64 )).astype (mx .bfloat16 )
132147 mx .eval (x )
133- got = linear (x , quantized )
148+ got = h3_linear (x , quantized )
134149 mx .eval (got )
135150 assert dq_gemm_engaged () == 0
136151 assert got .shape == (1024 , 64 )
0 commit comments