forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_onnx_tensorrt_export.py
More file actions
179 lines (132 loc) · 6.49 KB
/
Copy pathtest_onnx_tensorrt_export.py
File metadata and controls
179 lines (132 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Tests for ONNX and TensorRT-LLM export — config, validation, CLI."""
from unittest.mock import MagicMock
from unittest.mock import patch as mock_patch
from soup_cli.commands.export import SUPPORTED_FORMATS
# ─── Format Support Tests ────────────────────────────────────────────────
class TestExportFormats:
"""Test that new export formats are registered."""
def test_gguf_format_supported(self):
assert "gguf" in SUPPORTED_FORMATS
def test_onnx_format_supported(self):
assert "onnx" in SUPPORTED_FORMATS
def test_tensorrt_format_supported(self):
assert "tensorrt" in SUPPORTED_FORMATS
def test_format_count(self):
"""v0.53.1 — 7 prior formats + torchao + gguf-ud = 9."""
assert len(SUPPORTED_FORMATS) == 9
assert "bitnet" in SUPPORTED_FORMATS
assert "tq1_0" in SUPPORTED_FORMATS
assert "torchao" in SUPPORTED_FORMATS
assert "gguf-ud" in SUPPORTED_FORMATS
# ─── ONNX Export CLI Tests ──────────────────────────────────────────────
class TestOnnxExportCLI:
"""Test ONNX export via CLI."""
def test_onnx_export_missing_model_path(self, tmp_path):
"""soup export --format onnx should fail if model path doesn't exist."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(
app, ["export", "--model", str(tmp_path / "nonexistent"), "--format", "onnx"]
)
assert result.exit_code != 0
def test_onnx_export_format_in_help(self):
"""Export help should mention onnx format."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["export", "--help"])
assert "onnx" in result.output.lower()
def test_tensorrt_export_format_in_help(self):
"""Export help should mention tensorrt format."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["export", "--help"])
assert "tensorrt" in result.output.lower()
# ─── ONNX Export Function Tests ─────────────────────────────────────────
class TestOnnxExportFunction:
"""Test _export_onnx logic."""
def test_export_onnx_calls_main_export(self, tmp_path):
"""_export_onnx should call optimum's main_export."""
model_dir = tmp_path / "model"
model_dir.mkdir()
mock_main_export = MagicMock()
with mock_patch(
"soup_cli.commands.export.main_export",
mock_main_export,
create=True,
):
# Need to patch the import inside the function
import soup_cli.commands.export as export_mod
original = getattr(export_mod, "main_export", None)
try:
# Inject mock at module level for the lazy import
with mock_patch.object(
export_mod, "_export_onnx",
wraps=export_mod._export_onnx,
):
# Patch the actual import
with mock_patch.dict("sys.modules", {
"optimum": MagicMock(),
"optimum.exporters": MagicMock(),
"optimum.exporters.onnx": MagicMock(
main_export=mock_main_export
),
}):
export_mod._export_onnx(model_dir, str(tmp_path / "out"), None)
mock_main_export.assert_called_once()
finally:
if original is not None:
export_mod.main_export = original
def test_export_onnx_default_output_path(self, tmp_path):
"""Default output path should be model_name + _onnx suffix."""
model_dir = tmp_path / "my_model"
model_dir.mkdir()
mock_main_export = MagicMock()
with mock_patch.dict("sys.modules", {
"optimum": MagicMock(),
"optimum.exporters": MagicMock(),
"optimum.exporters.onnx": MagicMock(main_export=mock_main_export),
}):
import soup_cli.commands.export as export_mod
export_mod._export_onnx(model_dir, None, None)
call_kwargs = mock_main_export.call_args[1]
assert "my_model_onnx" in call_kwargs["output"]
# ─── TensorRT Export Function Tests ──────────────────────────────────────
class TestTensorrtExportFunction:
"""Test _export_tensorrt logic."""
def test_tensorrt_export_calls_subprocess(self, tmp_path):
"""TensorRT export should call subprocess for checkpoint conversion."""
model_dir = tmp_path / "model"
model_dir.mkdir()
mock_result = MagicMock()
mock_result.returncode = 0
with mock_patch.dict("sys.modules", {
"optimum": MagicMock(),
"optimum.exporters": MagicMock(),
"optimum.exporters.onnx": MagicMock(),
"tensorrt_llm": MagicMock(),
}), mock_patch("subprocess.run", return_value=mock_result) as mock_run:
import soup_cli.commands.export as export_mod
export_mod._export_tensorrt(
model_dir, str(tmp_path / "trt_out"), None
)
# Should call subprocess at least twice (checkpoint + build)
assert mock_run.call_count >= 2
# ─── Unsupported Format Test ─────────────────────────────────────────────
class TestExportUnsupportedFormat:
"""Test that unsupported formats are rejected."""
def test_invalid_format_rejected(self, tmp_path):
"""Export with unsupported format should exit with error."""
from typer.testing import CliRunner
from soup_cli.cli import app
model_dir = tmp_path / "model"
model_dir.mkdir()
runner = CliRunner()
result = runner.invoke(
app,
["export", "--model", str(model_dir), "--format", "safetensors"],
)
assert result.exit_code != 0
assert "Unsupported format" in result.output