-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathtest_tinker_service.py
More file actions
178 lines (144 loc) · 6.27 KB
/
test_tinker_service.py
File metadata and controls
178 lines (144 loc) · 6.27 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
import asyncio
import importlib.util
import json
from pathlib import Path
import sys
import types
import pytest
def _install_stub(monkeypatch: pytest.MonkeyPatch, name: str, module: types.ModuleType):
monkeypatch.setitem(sys.modules, name, module)
def _load_tinker_service_module(monkeypatch: pytest.MonkeyPatch):
repo_root = Path(__file__).resolve().parents[2]
service_path = repo_root / "src" / "art" / "tinker" / "service.py"
art_pkg = types.ModuleType("art")
art_pkg.__path__ = [str(repo_root / "src" / "art")] # type: ignore[attr-defined]
_install_stub(monkeypatch, "art", art_pkg)
tinker_pkg = types.ModuleType("art.tinker")
tinker_pkg.__path__ = [str(repo_root / "src" / "art" / "tinker")] # type: ignore[attr-defined]
_install_stub(monkeypatch, "art.tinker", tinker_pkg)
preprocessing_pkg = types.ModuleType("art.preprocessing")
preprocessing_pkg.__path__ = [str(repo_root / "src" / "art" / "preprocessing")] # type: ignore[attr-defined]
_install_stub(monkeypatch, "art.preprocessing", preprocessing_pkg)
dev_mod = types.ModuleType("art.dev")
dev_mod.InternalModelConfig = dict
dev_mod.OpenAIServerConfig = dict
dev_mod.TrainConfig = dict
_install_stub(monkeypatch, "art.dev", dev_mod)
types_mod = types.ModuleType("art.types")
types_mod.TrainConfig = dict
_install_stub(monkeypatch, "art.types", types_mod)
loss_mod = types.ModuleType("art.loss")
loss_mod.loss_fn = lambda *args, **kwargs: None
loss_mod.shift_tensor = lambda tensor, _: tensor
_install_stub(monkeypatch, "art.loss", loss_mod)
inputs_mod = types.ModuleType("art.preprocessing.inputs")
inputs_mod.TrainInputs = dict
inputs_mod.create_train_inputs = lambda *args, **kwargs: {}
_install_stub(monkeypatch, "art.preprocessing.inputs", inputs_mod)
pack_mod = types.ModuleType("art.preprocessing.pack")
pack_mod.DiskPackedTensors = dict
pack_mod.packed_tensors_from_dir = lambda **kwargs: kwargs
_install_stub(monkeypatch, "art.preprocessing.pack", pack_mod)
server_mod = types.ModuleType("art.tinker.server")
server_mod.OpenAICompatibleTinkerServer = type(
"OpenAICompatibleTinkerServer", (), {}
)
_install_stub(monkeypatch, "art.tinker.server", server_mod)
yaml_mod = types.ModuleType("yaml")
def safe_load(stream_or_text):
if hasattr(stream_or_text, "read"):
return json.loads(stream_or_text.read())
return json.loads(stream_or_text)
def safe_dump(data, stream=None):
text = json.dumps(data)
if stream is None:
return text
stream.write(text)
return None
yaml_mod.safe_load = safe_load
yaml_mod.safe_dump = safe_dump
_install_stub(monkeypatch, "yaml", yaml_mod)
torch_mod = types.ModuleType("torch")
torch_mod.Tensor = type("Tensor", (), {})
torch_mod.float32 = "float32"
_install_stub(monkeypatch, "torch", torch_mod)
tinker_mod = types.ModuleType("tinker")
tinker_mod.ServiceClient = type("ServiceClient", (), {})
tinker_mod.TrainingClient = type("TrainingClient", (), {})
tinker_mod.Datum = type("Datum", (), {})
tinker_mod.TensorData = type(
"TensorData", (), {"from_torch": staticmethod(lambda value: value)}
)
tinker_mod.ModelInput = type(
"ModelInput", (), {"from_ints": staticmethod(lambda ints: ints)}
)
tinker_mod.AdamParams = type("AdamParams", (), {})
_install_stub(monkeypatch, "tinker", tinker_mod)
tinker_lib_pkg = types.ModuleType("tinker.lib")
tinker_lib_pkg.__path__ = [] # type: ignore[attr-defined]
_install_stub(monkeypatch, "tinker.lib", tinker_lib_pkg)
public_interfaces_pkg = types.ModuleType("tinker.lib.public_interfaces")
public_interfaces_pkg.__path__ = [] # type: ignore[attr-defined]
_install_stub(monkeypatch, "tinker.lib.public_interfaces", public_interfaces_pkg)
rest_client_mod = types.ModuleType("tinker.lib.public_interfaces.rest_client")
rest_client_mod.RestClient = type("RestClient", (), {})
_install_stub(monkeypatch, "tinker.lib.public_interfaces.rest_client", rest_client_mod)
spec = importlib.util.spec_from_file_location("art.tinker.service", service_path)
assert spec is not None and spec.loader is not None
service_module = importlib.util.module_from_spec(spec)
monkeypatch.setitem(sys.modules, "art.tinker.service", service_module)
spec.loader.exec_module(service_module)
return service_module
def test_get_state_reuses_nested_user_metadata_from_training_client_args(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
service_module = _load_tinker_service_module(monkeypatch)
checkpoint_dir = tmp_path / "checkpoints" / "0001"
checkpoint_dir.mkdir(parents=True)
info_path = checkpoint_dir / "info.yaml"
info_path.write_text(
json.dumps(
{
"state_with_optimizer_path": "tinker://state/0001",
"sampler_weights_path": "tinker://sampler/0001",
}
)
)
observed: dict[str, object] = {}
fake_training_client = object()
class FakeServiceClient:
def create_rest_client(self) -> object:
return object()
async def create_training_client_from_state_with_optimizer_async(
self,
*,
path: str,
user_metadata: dict[str, str] | None = None,
) -> object:
observed["path"] = path
observed["user_metadata"] = user_metadata
return fake_training_client
monkeypatch.setattr(
service_module.tinker,
"ServiceClient",
FakeServiceClient,
)
service = service_module.TinkerService(
model_name="test-model",
base_model="Qwen/Qwen3-30B-A3B-Instruct-2507",
config={
"tinker_args": {
"renderer_name": "qwen3_5",
"training_client_args": {
"user_metadata": {"tenant": "test-tenant"},
},
}
},
output_dir=str(tmp_path),
)
state = asyncio.run(service._get_state())
assert observed["path"] == "tinker://state/0001"
assert observed["user_metadata"] == {"tenant": "test-tenant"}
assert state.training_client is fake_training_client
assert state.models["test-model"] == "tinker://sampler/0001"