forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_awq_gptq_export.py
More file actions
907 lines (683 loc) · 33.3 KB
/
Copy pathtest_awq_gptq_export.py
File metadata and controls
907 lines (683 loc) · 33.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
"""Tests for AWQ and GPTQ export — config, validation, CLI."""
import builtins
import tempfile
from pathlib import Path
from unittest.mock import MagicMock
from unittest.mock import patch as mock_patch
import pytest
from click.exceptions import Exit as ClickExit
from soup_cli.commands.export import SUPPORTED_FORMATS
def _mock_import(awq_mock=None, gptq_mock=None):
"""Create a side_effect for builtins.__import__ that intercepts awq/auto_gptq."""
real_import = builtins.__import__
def custom_import(name, *args, **kwargs):
if name == "awq" and awq_mock is not None:
return awq_mock
if name == "auto_gptq" and gptq_mock is not None:
return gptq_mock
return real_import(name, *args, **kwargs)
return custom_import
# ─── Format Support Tests ────────────────────────────────────────────────
class TestExportFormatsExtended:
"""Test that AWQ and GPTQ formats are registered."""
def test_awq_format_supported(self):
assert "awq" in SUPPORTED_FORMATS
def test_gptq_format_supported(self):
assert "gptq" in SUPPORTED_FORMATS
def test_format_count(self):
"""v0.53.1 — 7 prior formats + torchao + gguf-ud = 9."""
assert len(SUPPORTED_FORMATS) == 9
def test_all_formats_present(self):
"""v0.53.1 — adds torchao + gguf-ud to the v0.52.0 baseline."""
expected = {
"gguf", "onnx", "tensorrt", "awq", "gptq",
"bitnet", "tq1_0",
"torchao", "gguf-ud",
}
assert set(SUPPORTED_FORMATS) == expected
# ─── AWQ Export CLI Tests ─────────────────────────────────────────────────
class TestAwqExportCLI:
"""Test AWQ export via CLI."""
def test_awq_export_missing_model_path(self, tmp_path):
"""soup export --format awq 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", "awq"]
)
assert result.exit_code != 0
def test_awq_format_in_help(self):
"""Export help should mention awq format."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["export", "--help"])
assert "awq" in result.output.lower()
def test_gptq_format_in_help(self):
"""Export help should mention gptq format."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["export", "--help"])
assert "gptq" in result.output.lower()
# ─── GPTQ Export CLI Tests ────────────────────────────────────────────────
class TestGptqExportCLI:
"""Test GPTQ export via CLI."""
def test_gptq_export_missing_model_path(self, tmp_path):
"""soup export --format gptq 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", "gptq"]
)
assert result.exit_code != 0
# ─── AWQ Export Function Tests ────────────────────────────────────────────
class TestAwqExportFunction:
"""Test _export_awq logic."""
def test_export_awq_import_error(self, tmp_path):
"""AWQ export should print friendly error when autoawq not installed."""
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", "awq"]
)
assert result.exit_code != 0
assert "autoawq" in result.output.lower() or "not installed" in result.output.lower()
def test_export_awq_calls_quantize(self, tmp_path):
"""AWQ export should call AutoAWQForCausalLM methods."""
model_dir = tmp_path / "model"
model_dir.mkdir()
mock_model = MagicMock()
mock_awq_class = MagicMock()
mock_awq_class.from_pretrained = MagicMock(return_value=mock_model)
mock_tokenizer = MagicMock()
awq_mod = MagicMock()
awq_mod.AutoAWQForCausalLM = mock_awq_class
import soup_cli.commands.export as export_mod
out_path = tmp_path / "out"
with mock_patch.object(builtins, "__import__", side_effect=_mock_import(awq_mock=awq_mod)):
with mock_patch(
"transformers.AutoTokenizer.from_pretrained",
return_value=mock_tokenizer,
):
with mock_patch(
"soup_cli.commands.export._validate_output_path",
return_value=out_path,
):
export_mod._export_awq(
model_dir, str(out_path), None,
bits=4, group_size=128, calibration_data=None,
)
mock_awq_class.from_pretrained.assert_called_once()
mock_model.quantize.assert_called_once()
mock_model.save_quantized.assert_called_once()
def test_export_awq_default_output_path(self, tmp_path):
"""Default AWQ output path should be model_name + _awq suffix."""
model_dir = tmp_path / "my_model"
model_dir.mkdir()
mock_model = MagicMock()
mock_awq_class = MagicMock()
mock_awq_class.from_pretrained = MagicMock(return_value=mock_model)
mock_tokenizer = MagicMock()
awq_mod = MagicMock()
awq_mod.AutoAWQForCausalLM = mock_awq_class
import soup_cli.commands.export as export_mod
with mock_patch.object(builtins, "__import__", side_effect=_mock_import(awq_mock=awq_mod)):
with mock_patch(
"transformers.AutoTokenizer.from_pretrained",
return_value=mock_tokenizer,
):
export_mod._export_awq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=None,
)
save_call = mock_model.save_quantized.call_args
output_path = save_call[0][0]
assert "my_model_awq" in output_path
def test_export_awq_with_calibration_data(self, tmp_path):
"""AWQ export with calibration data should load and pass it."""
import json
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
lines = [json.dumps({"text": f"sample {i}"}) for i in range(10)]
cal_file.write_text("\n".join(lines), encoding="utf-8")
mock_model = MagicMock()
mock_awq_class = MagicMock()
mock_awq_class.from_pretrained = MagicMock(return_value=mock_model)
mock_tokenizer = MagicMock()
awq_mod = MagicMock()
awq_mod.AutoAWQForCausalLM = mock_awq_class
import soup_cli.commands.export as export_mod
out_path = tmp_path / "out"
with mock_patch.object(builtins, "__import__", side_effect=_mock_import(awq_mock=awq_mod)):
with mock_patch(
"transformers.AutoTokenizer.from_pretrained",
return_value=mock_tokenizer,
):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
with mock_patch(
"soup_cli.commands.export._validate_output_path",
return_value=out_path,
):
export_mod._export_awq(
model_dir, str(out_path), None,
bits=4, group_size=128,
calibration_data=str(cal_file),
)
quant_call = mock_model.quantize.call_args
assert quant_call is not None
assert "calib_data" in quant_call.kwargs
def test_export_awq_invalid_bits(self, tmp_path):
"""Invalid bits value should raise ClickExit."""
model_dir = tmp_path / "model"
model_dir.mkdir()
import soup_cli.commands.export as export_mod
with pytest.raises(ClickExit):
export_mod._export_awq(
model_dir, None, None, bits=3, group_size=128,
calibration_data=None,
)
def test_export_awq_calibration_path_traversal(self, tmp_path):
"""Calibration data path should be confined to cwd."""
model_dir = tmp_path / "model"
model_dir.mkdir()
import soup_cli.commands.export as export_mod
with pytest.raises(ClickExit):
export_mod._export_awq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=str(Path("C:/Windows/System32/drivers/etc/hosts")),
)
# ─── GPTQ Export Function Tests ───────────────────────────────────────────
class TestGptqExportFunction:
"""Test _export_gptq logic."""
def test_export_gptq_import_error(self, tmp_path, monkeypatch):
"""GPTQ export should print friendly error when auto-gptq not installed."""
from typer.testing import CliRunner
from soup_cli.cli import app
monkeypatch.chdir(tmp_path)
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text('{"text": "sample"}\n', encoding="utf-8")
real_import = builtins.__import__
def custom_import(name, *args, **kwargs):
if name == "auto_gptq":
raise ModuleNotFoundError("No module named 'auto_gptq'", name="auto_gptq")
return real_import(name, *args, **kwargs)
monkeypatch.setattr(builtins, "__import__", custom_import)
runner = CliRunner()
result = runner.invoke(
app,
[
"export", "--model", str(model_dir), "--format", "gptq",
"--calibration-data", str(cal_file),
],
)
assert result.exit_code != 0
assert "auto-gptq" in result.output.lower() or "not installed" in result.output.lower()
def test_export_gptq_no_calibration_data_raises(self, tmp_path, capsys):
"""GPTQ has no built-in fallback dataset: no --calibration-data must raise,
never fall through to model.quantize(tokenizer) (auto-gptq expects examples,
not a tokenizer, and rejects one with 'object is not iterable')."""
model_dir = tmp_path / "model"
model_dir.mkdir()
mock_gptq_class = MagicMock()
gptq_mod = MagicMock()
gptq_mod.AutoGPTQForCausalLM = mock_gptq_class
gptq_mod.BaseQuantizeConfig = MagicMock
import soup_cli.commands.export as export_mod
with mock_patch.object(
builtins, "__import__", side_effect=_mock_import(gptq_mock=gptq_mod)
):
with pytest.raises(ClickExit):
export_mod._export_gptq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=None,
)
mock_gptq_class.from_pretrained.assert_not_called()
assert "--calibration-data" in capsys.readouterr().out
def test_export_gptq_empty_calibration_file_raises(self, tmp_path):
"""A --calibration-data file that yields zero usable samples (empty,
or only malformed JSON lines) must raise the same way as no file at
all, before the model is ever loaded."""
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text("\n\nnot json\n", encoding="utf-8")
mock_gptq_class = MagicMock()
gptq_mod = MagicMock()
gptq_mod.AutoGPTQForCausalLM = mock_gptq_class
gptq_mod.BaseQuantizeConfig = MagicMock
import soup_cli.commands.export as export_mod
with mock_patch.object(
builtins, "__import__", side_effect=_mock_import(gptq_mock=gptq_mod)
):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
with pytest.raises(ClickExit):
export_mod._export_gptq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=str(cal_file),
)
mock_gptq_class.from_pretrained.assert_not_called()
def test_export_gptq_calls_quantize(self, tmp_path):
"""GPTQ export should call AutoGPTQForCausalLM methods."""
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text('{"text": "sample"}\n', encoding="utf-8")
mock_model = MagicMock()
mock_gptq_class = MagicMock()
mock_gptq_class.from_pretrained = MagicMock(return_value=mock_model)
mock_tokenizer = MagicMock()
gptq_mod = MagicMock()
gptq_mod.AutoGPTQForCausalLM = mock_gptq_class
gptq_mod.BaseQuantizeConfig = MagicMock
import soup_cli.commands.export as export_mod
out_path = tmp_path / "out"
out_path.mkdir()
with mock_patch.object(
builtins, "__import__", side_effect=_mock_import(gptq_mock=gptq_mod)
):
with mock_patch(
"transformers.AutoTokenizer.from_pretrained",
return_value=mock_tokenizer,
):
with mock_patch(
"soup_cli.commands.export._validate_output_path",
return_value=out_path,
):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
export_mod._export_gptq(
model_dir, str(out_path), None,
bits=4, group_size=128, calibration_data=str(cal_file),
)
mock_gptq_class.from_pretrained.assert_called_once()
mock_model.quantize.assert_called_once()
mock_model.save_quantized.assert_called_once()
def test_export_gptq_default_output_path(self, tmp_path):
"""Default GPTQ output path should be model_name + _gptq suffix."""
model_dir = tmp_path / "my_model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text('{"text": "sample"}\n', encoding="utf-8")
mock_model = MagicMock()
mock_gptq_class = MagicMock()
mock_gptq_class.from_pretrained = MagicMock(return_value=mock_model)
mock_tokenizer = MagicMock()
gptq_mod = MagicMock()
gptq_mod.AutoGPTQForCausalLM = mock_gptq_class
gptq_mod.BaseQuantizeConfig = MagicMock
import soup_cli.commands.export as export_mod
with mock_patch.object(
builtins, "__import__", side_effect=_mock_import(gptq_mock=gptq_mod)
):
with mock_patch(
"transformers.AutoTokenizer.from_pretrained",
return_value=mock_tokenizer,
):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
export_mod._export_gptq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=str(cal_file),
)
save_call = mock_model.save_quantized.call_args
output_path = save_call[0][0]
assert "my_model_gptq" in output_path
def test_export_gptq_invalid_bits(self, tmp_path):
"""Invalid bits value should raise ClickExit."""
model_dir = tmp_path / "model"
model_dir.mkdir()
import soup_cli.commands.export as export_mod
with pytest.raises(ClickExit):
export_mod._export_gptq(
model_dir, None, None, bits=3, group_size=128,
calibration_data=None,
)
def test_export_gptq_calibration_path_traversal(self, tmp_path):
"""Calibration data path should be confined to cwd."""
model_dir = tmp_path / "model"
model_dir.mkdir()
import soup_cli.commands.export as export_mod
with pytest.raises(ClickExit):
export_mod._export_gptq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=str(Path("C:/Windows/System32/drivers/etc/hosts")),
)
# ─── GPTQ Shard Name Regression (issue #338, P2) ─────────────────────────
class TestGptqStandardShardName:
"""auto_gptq's save_quantized names its shard gptq_model-<bits>bit-<group>g.safetensors,
which AutoModelForCausalLM.from_pretrained does not look for."""
def test_standardize_renames_shard_to_model_safetensors(self, tmp_path):
from soup_cli.commands.export import _standardize_gptq_shard_name
out = tmp_path / "out"
out.mkdir()
(out / "gptq_model-4bit-128g.safetensors").write_bytes(b"weights")
_standardize_gptq_shard_name(out, bits=4, group_size=128)
assert (out / "model.safetensors").exists()
assert not (out / "gptq_model-4bit-128g.safetensors").exists()
def test_standardize_does_not_overwrite_existing_standard_name(self, tmp_path):
from soup_cli.commands.export import _standardize_gptq_shard_name
out = tmp_path / "out"
out.mkdir()
(out / "model.safetensors").write_bytes(b"already-standard")
(out / "gptq_model-4bit-128g.safetensors").write_bytes(b"weights")
_standardize_gptq_shard_name(out, bits=4, group_size=128)
assert (out / "model.safetensors").read_bytes() == b"already-standard"
def test_standardize_no_op_when_shard_absent(self, tmp_path):
from soup_cli.commands.export import _standardize_gptq_shard_name
out = tmp_path / "out"
out.mkdir()
_standardize_gptq_shard_name(out, bits=4, group_size=128)
assert not (out / "model.safetensors").exists()
def test_export_gptq_end_to_end_leaves_standard_name(self, tmp_path):
"""Full _export_gptq run: save_quantized's mock writes the real auto_gptq
shard name, and the standard model.safetensors must exist afterward."""
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text('{"text": "sample"}\n', encoding="utf-8")
out_path = tmp_path / "out"
out_path.mkdir()
def fake_save_quantized(path):
Path(path, "gptq_model-4bit-128g.safetensors").write_bytes(b"weights")
mock_model = MagicMock()
mock_model.save_quantized.side_effect = fake_save_quantized
mock_gptq_class = MagicMock()
mock_gptq_class.from_pretrained = MagicMock(return_value=mock_model)
mock_tokenizer = MagicMock()
gptq_mod = MagicMock()
gptq_mod.AutoGPTQForCausalLM = mock_gptq_class
gptq_mod.BaseQuantizeConfig = MagicMock
import soup_cli.commands.export as export_mod
with mock_patch.object(
builtins, "__import__", side_effect=_mock_import(gptq_mock=gptq_mod)
):
with mock_patch(
"transformers.AutoTokenizer.from_pretrained",
return_value=mock_tokenizer,
):
with mock_patch(
"soup_cli.commands.export._validate_output_path",
return_value=out_path,
):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
export_mod._export_gptq(
model_dir, str(out_path), None,
bits=4, group_size=128, calibration_data=str(cal_file),
)
assert (out_path / "model.safetensors").exists()
assert not (out_path / "gptq_model-4bit-128g.safetensors").exists()
# ─── Import-Error Masking Regression (issue #338, ImportError swallowing) ──
class TestAwqGptqImportErrorSurfacesRealCause:
"""except ImportError must not print a fixed 'not installed' string when the
package itself imports but a transitive import inside it fails for an
unrelated reason, which used to discard the real exception."""
def test_gptq_transitive_import_failure_message(self, tmp_path, capsys):
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text('{"text": "sample"}\n', encoding="utf-8")
real_import = builtins.__import__
def custom_import(name, *args, **kwargs):
if name == "auto_gptq":
raise ImportError("libfoo.so: cannot open shared object file", name="libfoo")
return real_import(name, *args, **kwargs)
import soup_cli.commands.export as export_mod
with mock_patch.object(builtins, "__import__", side_effect=custom_import):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
with pytest.raises(ClickExit):
export_mod._export_gptq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=str(cal_file),
)
output = capsys.readouterr().out
assert "libfoo.so" in output
assert "not installed" not in output.lower()
def test_awq_transitive_import_failure_message(self, tmp_path, capsys):
model_dir = tmp_path / "model"
model_dir.mkdir()
real_import = builtins.__import__
def custom_import(name, *args, **kwargs):
if name == "awq":
raise ImportError("libbar.so: cannot open shared object file", name="libbar")
return real_import(name, *args, **kwargs)
import soup_cli.commands.export as export_mod
with mock_patch.object(builtins, "__import__", side_effect=custom_import):
with pytest.raises(ClickExit):
export_mod._export_awq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=None,
)
output = capsys.readouterr().out
assert "libbar.so" in output
assert "not installed" not in output.lower()
def test_gptq_real_missing_package_still_shows_install_hint(self, tmp_path, capsys):
"""When auto-gptq is genuinely absent (exc.name == 'auto_gptq'), the
friendly install hint must still show; this must not regress."""
model_dir = tmp_path / "model"
model_dir.mkdir()
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text('{"text": "sample"}\n', encoding="utf-8")
real_import = builtins.__import__
def custom_import(name, *args, **kwargs):
if name == "auto_gptq":
raise ModuleNotFoundError("No module named 'auto_gptq'", name="auto_gptq")
return real_import(name, *args, **kwargs)
import soup_cli.commands.export as export_mod
with mock_patch.object(builtins, "__import__", side_effect=custom_import):
with mock_patch(
"soup_cli.commands.export._validate_calibration_path",
return_value=cal_file,
):
with pytest.raises(ClickExit):
export_mod._export_gptq(
model_dir, None, None, bits=4, group_size=128,
calibration_data=str(cal_file),
)
output = capsys.readouterr().out
assert "not installed" in output.lower()
# ─── CLI Integration Tests ───────────────────────────────────────────────
class TestAwqGptqCLIArgs:
"""Test new CLI arguments are declared in export function signature."""
def test_bits_flag_exists(self):
"""Export function should have a --bits parameter."""
import inspect
from soup_cli.commands.export import export
sig = inspect.signature(export)
assert "bits" in sig.parameters
def test_group_size_flag_exists(self):
"""Export function should have a --group-size parameter."""
import inspect
from soup_cli.commands.export import export
sig = inspect.signature(export)
assert "group_size" in sig.parameters
def test_calibration_data_flag_exists(self):
"""Export function should have a --calibration-data parameter."""
import inspect
from soup_cli.commands.export import export
sig = inspect.signature(export)
assert "calibration_data" in sig.parameters
def test_calibration_samples_flag_exists(self):
"""Export function should have a --calibration-samples parameter."""
import inspect
from soup_cli.commands.export import export
sig = inspect.signature(export)
assert "calibration_samples" in sig.parameters
# ─── Security Tests ──────────────────────────────────────────────────────
class TestAwqGptqSecurity:
"""Security validation for AWQ/GPTQ export."""
def test_calibration_data_path_traversal_awq(self, tmp_path):
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", "awq",
"--calibration-data", "C:/Windows/System32/drivers/etc/hosts",
],
)
assert result.exit_code != 0
def test_calibration_data_path_traversal_gptq(self, tmp_path):
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", "gptq",
"--calibration-data", "C:/Windows/System32/drivers/etc/hosts",
],
)
assert result.exit_code != 0
# ─── Calibration Data Loader Tests ────────────────────────────────────────
class TestCalibrationLoader:
"""Test _load_calibration_texts function."""
def test_load_text_field(self, tmp_path):
import json
from soup_cli.commands.export import _load_calibration_texts
cal_file = tmp_path / "cal.jsonl"
lines = [json.dumps({"text": f"line {i}"}) for i in range(5)]
cal_file.write_text("\n".join(lines), encoding="utf-8")
texts = _load_calibration_texts(cal_file, max_samples=10)
assert len(texts) == 5
assert texts[0] == "line 0"
def test_load_max_samples(self, tmp_path):
import json
from soup_cli.commands.export import _load_calibration_texts
cal_file = tmp_path / "cal.jsonl"
lines = [json.dumps({"text": f"line {i}"}) for i in range(100)]
cal_file.write_text("\n".join(lines), encoding="utf-8")
texts = _load_calibration_texts(cal_file, max_samples=10)
assert len(texts) == 10
def test_load_none_returns_empty(self):
from soup_cli.commands.export import _load_calibration_texts
assert _load_calibration_texts(None) == []
def test_load_skips_empty_lines(self, tmp_path):
import json
from soup_cli.commands.export import _load_calibration_texts
cal_file = tmp_path / "cal.jsonl"
content = json.dumps({"text": "hello"}) + "\n\n" + json.dumps({"text": "world"})
cal_file.write_text(content, encoding="utf-8")
texts = _load_calibration_texts(cal_file, max_samples=10)
assert len(texts) == 2
def test_load_concatenates_non_text_fields(self, tmp_path):
import json
from soup_cli.commands.export import _load_calibration_texts
cal_file = tmp_path / "cal.jsonl"
cal_file.write_text(
json.dumps({"prompt": "hello", "response": "world"}),
encoding="utf-8",
)
texts = _load_calibration_texts(cal_file, max_samples=10)
assert len(texts) == 1
assert "hello" in texts[0]
assert "world" in texts[0]
# ─── Path Validation Tests ───────────────────────────────────────────────
class TestCalibrationPathValidation:
"""Test _validate_calibration_path function."""
def test_none_returns_none(self):
from soup_cli.commands.export import _validate_calibration_path
assert _validate_calibration_path(None) is None
def test_nonexistent_file_raises(self):
"""Nonexistent calibration file should raise ClickExit."""
from soup_cli.commands.export import _validate_calibration_path
nope = Path.cwd() / "nonexistent_cal_data_test_xyzzy.jsonl"
with pytest.raises(ClickExit):
_validate_calibration_path(str(nope))
def test_valid_path_returns_path(self):
"""Valid path under cwd should return resolved Path."""
from soup_cli.commands.export import _validate_calibration_path
cal_file = Path.cwd() / "test_cal_temp_xyzzy.jsonl"
try:
cal_file.write_text("{}", encoding="utf-8")
result = _validate_calibration_path(str(cal_file))
assert result is not None
assert result.name == "test_cal_temp_xyzzy.jsonl"
finally:
if cal_file.exists():
cal_file.unlink()
def test_path_outside_cwd_raises(self):
from soup_cli.commands.export import _validate_calibration_path
with pytest.raises(ClickExit):
_validate_calibration_path("C:/Windows/System32/drivers/etc/hosts")
# ─── Output Path Validation Tests ────────────────────────────────────────
class TestOutputPathValidation:
"""Test _validate_output_path function."""
def test_none_returns_none(self):
from soup_cli.commands.export import _validate_output_path
assert _validate_output_path(None) is None
def test_valid_path_returns_path(self):
"""Valid path under cwd should return resolved Path."""
from soup_cli.commands.export import _validate_output_path
out = Path.cwd() / "test_output_xyzzy"
result = _validate_output_path(str(out))
assert result is not None
assert result.name == "test_output_xyzzy"
def test_path_outside_cwd_raises(self):
"""Path outside cwd should raise ClickExit."""
from soup_cli.commands.export import _validate_output_path
# Use a path guaranteed to be outside cwd on all platforms
outside = Path(tempfile.gettempdir()).resolve()
cwd = Path.cwd().resolve()
# Only test if temp dir is actually outside cwd
try:
outside.relative_to(cwd)
pytest.skip("tempdir is under cwd, cannot test outside path")
except ValueError:
pass
with pytest.raises(ClickExit):
_validate_output_path(str(outside / "evil_output"))
def test_path_traversal_rejected(self, tmp_path, monkeypatch):
"""Path outside cwd via relative traversal should be rejected."""
from soup_cli.commands.export import _validate_output_path
# Set cwd to a subdirectory so ../evil is outside
sub = tmp_path / "deep" / "nested"
sub.mkdir(parents=True)
monkeypatch.chdir(sub)
with pytest.raises(ClickExit):
_validate_output_path(str(tmp_path / "evil"))
# ─── Optional Dependency Tests ────────────────────────────────────────────
class TestOptionalDeps:
"""Test that optional deps are declared in pyproject.toml."""
def _read_pyproject(self):
pyproject = Path(__file__).parent.parent / "pyproject.toml"
return pyproject.read_text(encoding="utf-8")
def test_awq_optional_dep(self):
content = self._read_pyproject()
assert "awq" in content
assert "autoawq" in content
def test_gptq_optional_dep(self):
content = self._read_pyproject()
assert "gptq" in content
assert "auto-gptq" in content