|
21 | 21 | """ |
22 | 22 |
|
23 | 23 | import os |
24 | | -import sys |
25 | 24 | import tempfile |
26 | 25 |
|
27 | | -# Add parent and current directory to path |
28 | | -_test_dir = os.path.dirname(os.path.abspath(__file__)) |
29 | | -sys.path.insert(0, os.path.dirname(_test_dir)) |
30 | | -sys.path.insert(0, _test_dir) |
31 | | - |
32 | 26 | import models as _test_models |
33 | 27 | import onnx |
34 | 28 | import onnx_graphsurgeon as gs |
@@ -205,20 +199,47 @@ def test_set_profile_region(self, simple_conv_model): |
205 | 199 | pytest.skip("No regions discovered") |
206 | 200 |
|
207 | 201 | def test_generate_scheme(self, simple_conv_model): |
208 | | - """Test generating an insertion scheme.""" |
| 202 | + """Test generating multiple schemes and that Q/DQ nodes appear in exported model.""" |
209 | 203 | autotuner = QDQAutotuner(simple_conv_model) |
210 | 204 | config = _create_test_config() |
211 | 205 | autotuner.initialize(config) |
212 | 206 |
|
213 | | - if len(autotuner.regions) > 0: |
214 | | - region = autotuner.regions[0] |
215 | | - autotuner.set_profile_region(region) |
216 | | - # Generate a scheme |
| 207 | + if len(autotuner.regions) == 0: |
| 208 | + pytest.skip("No regions discovered") |
| 209 | + |
| 210 | + autotuner.submit(10.0) # baseline |
| 211 | + region = autotuner.regions[0] |
| 212 | + autotuner.set_profile_region(region) |
| 213 | + |
| 214 | + # Generate multiple schemes and submit a latency for each |
| 215 | + num_generated = 0 |
| 216 | + while True: |
217 | 217 | scheme_idx = autotuner.generate() |
218 | | - # Should return a valid index (>= 0) or -1 if no more unique schemes |
| 218 | + if scheme_idx < 0: |
| 219 | + break |
219 | 220 | assert isinstance(scheme_idx, int) |
220 | | - else: |
221 | | - pytest.skip("No regions discovered") |
| 221 | + autotuner.submit(10.0 + num_generated * 0.1) # dummy latency |
| 222 | + num_generated += 1 |
| 223 | + if num_generated >= 5: # cap iterations |
| 224 | + break |
| 225 | + |
| 226 | + assert num_generated > 0, "Expected at least one scheme to be generated" |
| 227 | + autotuner.set_profile_region(None, commit=True) |
| 228 | + |
| 229 | + # Export with Q/DQ and verify Q/DQ nodes are in the model |
| 230 | + with tempfile.NamedTemporaryFile(suffix=".onnx", delete=False) as f: |
| 231 | + output_path = f.name |
| 232 | + try: |
| 233 | + autotuner.export_onnx(output_path, insert_qdq=True) |
| 234 | + exported = onnx.load(output_path) |
| 235 | + node_ops = [n.op_type for n in exported.graph.node] |
| 236 | + assert "QuantizeLinear" in node_ops, "Expected QuantizeLinear nodes in exported model" |
| 237 | + assert "DequantizeLinear" in node_ops, ( |
| 238 | + "Expected DequantizeLinear nodes in exported model" |
| 239 | + ) |
| 240 | + finally: |
| 241 | + if os.path.exists(output_path): |
| 242 | + os.unlink(output_path) |
222 | 243 |
|
223 | 244 | def test_submit_latency(self, simple_conv_model): |
224 | 245 | """Test submitting performance measurement.""" |
|
0 commit comments