|
9 | 9 |
|
10 | 10 | import pytest |
11 | 11 |
|
| 12 | +from mobius._build_context import build_context |
| 13 | +from mobius._execution_providers import ep_registry |
12 | 14 | from mobius._testing import ( |
13 | 15 | count_op_type, |
14 | 16 | create_test_builder, |
@@ -125,7 +127,60 @@ def test_matmulnbits_attributes(self): |
125 | 127 | assert attrs["N"] == OUT_FEATURES |
126 | 128 | assert attrs["bits"] == 4 |
127 | 129 | assert attrs["block_size"] == 32 |
| 130 | + # No active build context -> default EP (accuracy_level 0), |
| 131 | + # so the attribute is omitted and ORT keeps its default path. |
| 132 | + assert "accuracy_level" not in attrs |
128 | 133 | break |
| 134 | + else: |
| 135 | + pytest.fail("MatMulNBits node not found") |
| 136 | + |
| 137 | + def test_no_accuracy_level_without_context(self): |
| 138 | + """Default EP omits accuracy_level (ORT default / highest precision).""" |
| 139 | + ql = QuantizedLinear(IN_FEATURES, OUT_FEATURES, bits=4, block_size=32) |
| 140 | + b, op, graph = create_test_builder() |
| 141 | + x = create_test_input(b, "x", [1, 4, IN_FEATURES]) |
| 142 | + result = ql(op, x) |
| 143 | + b._adapt_outputs([result], "") |
| 144 | + for node in graph: |
| 145 | + if node.op_type == "MatMulNBits": |
| 146 | + attrs = {a.name: a.value for a in node.attributes.values()} |
| 147 | + assert "accuracy_level" not in attrs |
| 148 | + break |
| 149 | + else: |
| 150 | + pytest.fail("MatMulNBits node not found") |
| 151 | + |
| 152 | + def test_cpu_ep_emits_accuracy_level_4(self): |
| 153 | + """CPU EP context stamps accuracy_level=4 (int8 MLAS path).""" |
| 154 | + with build_context(ep_registry.require("cpu")): |
| 155 | + ql = QuantizedLinear(IN_FEATURES, OUT_FEATURES, bits=4, block_size=32) |
| 156 | + b, op, graph = create_test_builder() |
| 157 | + x = create_test_input(b, "x", [1, 4, IN_FEATURES]) |
| 158 | + result = ql(op, x) |
| 159 | + b._adapt_outputs([result], "") |
| 160 | + for node in graph: |
| 161 | + if node.op_type == "MatMulNBits": |
| 162 | + attrs = {a.name: a.value for a in node.attributes.values()} |
| 163 | + assert attrs["accuracy_level"] == 4 |
| 164 | + break |
| 165 | + else: |
| 166 | + pytest.fail("MatMulNBits node not found") |
| 167 | + |
| 168 | + def test_cpu_ep_omits_accuracy_level_for_non_int4(self): |
| 169 | + """accuracy_level is INT4-specific: 8-bit weights keep ORT's default.""" |
| 170 | + with build_context(ep_registry.require("cpu")): |
| 171 | + ql = QuantizedLinear(IN_FEATURES, OUT_FEATURES, bits=8, block_size=32) |
| 172 | + b, op, graph = create_test_builder() |
| 173 | + x = create_test_input(b, "x", [1, 4, IN_FEATURES]) |
| 174 | + result = ql(op, x) |
| 175 | + b._adapt_outputs([result], "") |
| 176 | + for node in graph: |
| 177 | + if node.op_type == "MatMulNBits": |
| 178 | + attrs = {a.name: a.value for a in node.attributes.values()} |
| 179 | + assert attrs["bits"] == 8 |
| 180 | + assert "accuracy_level" not in attrs |
| 181 | + break |
| 182 | + else: |
| 183 | + pytest.fail("MatMulNBits node not found") |
129 | 184 |
|
130 | 185 | def test_3_inputs_without_zero_points(self): |
131 | 186 | ql = QuantizedLinear(IN_FEATURES, OUT_FEATURES) |
@@ -299,6 +354,8 @@ def test_graph_has_gather_block_quantized_node(self): |
299 | 354 | result = qe(op, ids) |
300 | 355 | b._adapt_outputs([result], "") |
301 | 356 | assert count_op_type(graph, "GatherBlockQuantized") == 1 |
| 357 | + assert result.dtype == ir.DataType.FLOAT |
| 358 | + assert result.shape == ir.Shape([1, 4, self.DIM]) |
302 | 359 |
|
303 | 360 | def test_node_domain_and_attributes(self): |
304 | 361 | import onnx_ir as ir |
|
0 commit comments