forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_merge.py
More file actions
104 lines (71 loc) · 2.97 KB
/
Copy pathtest_merge.py
File metadata and controls
104 lines (71 loc) · 2.97 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
"""Tests for soup merge command."""
import json
from pathlib import Path
from typer.testing import CliRunner
from soup_cli.cli import app
from soup_cli.commands.merge import _detect_base_model, _format_size
runner = CliRunner()
# --- _format_size ---
def test_format_size_bytes():
assert _format_size(512) == "512.0 B"
def test_format_size_kb():
assert _format_size(2048) == "2.0 KB"
def test_format_size_mb():
assert _format_size(5 * 1024 * 1024) == "5.0 MB"
def test_format_size_gb():
assert _format_size(3 * 1024**3) == "3.0 GB"
# --- _detect_base_model ---
def test_detect_base_model(tmp_path: Path):
config = tmp_path / "adapter_config.json"
config.write_text(json.dumps({
"base_model_name_or_path": "meta-llama/Llama-3.1-8B",
}))
assert _detect_base_model(config) == "meta-llama/Llama-3.1-8B"
def test_detect_base_model_missing_key(tmp_path: Path):
config = tmp_path / "adapter_config.json"
config.write_text(json.dumps({"r": 64}))
assert _detect_base_model(config) is None
def test_detect_base_model_bad_json(tmp_path: Path):
config = tmp_path / "adapter_config.json"
config.write_text("not valid json")
assert _detect_base_model(config) is None
def test_detect_base_model_missing_file(tmp_path: Path):
config = tmp_path / "nonexistent.json"
assert _detect_base_model(config) is None
# --- CLI validation ---
def test_merge_missing_adapter():
result = runner.invoke(app, ["merge", "--adapter", "/nonexistent"])
assert result.exit_code == 1
assert "not found" in result.output.lower()
def test_merge_not_a_lora_adapter(tmp_path: Path):
"""Directory without adapter_config.json should fail."""
model_dir = tmp_path / "model"
model_dir.mkdir()
result = runner.invoke(app, ["merge", "--adapter", str(model_dir)])
assert result.exit_code == 1
assert "not a lora adapter" in result.output.lower()
def test_merge_no_base_model_detected(tmp_path: Path):
"""Adapter with empty config (no base_model_name_or_path) and no --base flag."""
adapter_dir = tmp_path / "adapter"
adapter_dir.mkdir()
(adapter_dir / "adapter_config.json").write_text(json.dumps({"r": 64}))
result = runner.invoke(app, ["merge", "--adapter", str(adapter_dir)])
assert result.exit_code == 1
assert "base" in result.output.lower()
def test_merge_invalid_dtype(tmp_path: Path):
"""Invalid dtype should fail."""
adapter_dir = tmp_path / "adapter"
adapter_dir.mkdir()
(adapter_dir / "adapter_config.json").write_text(json.dumps({
"base_model_name_or_path": "meta-llama/Llama-3.1-8B",
}))
result = runner.invoke(
app, ["merge", "--adapter", str(adapter_dir), "--dtype", "int8"]
)
assert result.exit_code == 1
assert "invalid dtype" in result.output.lower()
def test_merge_help():
result = runner.invoke(app, ["merge", "--help"])
assert result.exit_code == 0
assert "adapter" in result.output.lower()
assert "base" in result.output.lower()