-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathtest_run.py
More file actions
290 lines (254 loc) · 10.8 KB
/
Copy pathtest_run.py
File metadata and controls
290 lines (254 loc) · 10.8 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
import argparse
from typing import List, Optional, Tuple
from unittest.mock import Mock
import pytest
from gpuhunt import AcceleratorVendor
from dstack._internal.cli.services.configurators import get_run_configurator_class
from dstack._internal.cli.services.configurators.run import BaseRunConfigurator
from dstack._internal.core.errors import ConfigurationError
from dstack._internal.core.models.common import RegistryAuth
from dstack._internal.core.models.configurations import (
BaseRunConfiguration,
PortMapping,
TaskConfiguration,
)
from dstack._internal.core.models.envs import Env
class TestApplyArgs:
def apply_args(
self, conf: BaseRunConfiguration, args: List[str]
) -> Tuple[BaseRunConfiguration, argparse.Namespace]:
parser = argparse.ArgumentParser()
configurator_class = get_run_configurator_class(conf.type)
configurator = configurator_class(Mock())
configurator.register_args(parser)
conf = conf.copy(deep=True) # to avoid modifying the original configuration
known, unknown = parser.parse_known_args(args)
configurator.apply_args(conf, known, unknown)
return conf, known
def test_env(self):
conf = TaskConfiguration(commands=["whoami"])
modified, args = self.apply_args(conf, ["-e", "A=1", "--env", "B=2"])
conf.env = Env.parse_obj({"A": "1", "B": "2"})
assert modified.dict() == conf.dict()
def test_ports(self):
conf = TaskConfiguration(commands=["whoami"])
modified, args = self.apply_args(conf, ["-p", "80", "--port", "8080"])
conf.ports = [
PortMapping(local_port=80, container_port=80),
PortMapping(local_port=8080, container_port=8080),
]
assert modified.dict() == conf.dict()
def test_container_ports_conflict(self):
conf = TaskConfiguration(commands=["whoami"])
with pytest.raises(ConfigurationError):
self.apply_args(conf, ["-p", "8000:80", "--port", "8001:80"])
def test_env_override(self):
conf = TaskConfiguration(commands=["whoami"], env=Env.parse_obj({"A": "0"}))
modified, args = self.apply_args(conf, ["-e", "A=1", "--env", "B=2"])
conf.env = Env.parse_obj({"A": "1", "B": "2"})
assert modified.dict() == conf.dict()
def test_ports_override(self):
conf = TaskConfiguration(commands=["whoami"], ports=["80"])
modified, args = self.apply_args(conf, ["-p", "8000:80", "--port", "8001:8000"])
conf.ports = [
PortMapping(local_port=8000, container_port=80),
PortMapping(local_port=8001, container_port=8000),
]
assert modified.dict() == conf.dict()
def test_local_ports_conflict(self):
conf = TaskConfiguration(commands=["whoami"], ports=["3000"])
with pytest.raises(ConfigurationError):
self.apply_args(conf, ["-p", "3000:4000"])
def test_any_port(self):
conf = TaskConfiguration(commands=["whoami"], ports=["8000"])
modified, args = self.apply_args(conf, ["-p", "*:8000"])
conf.ports = [PortMapping(local_port=None, container_port=8000)]
assert modified.dict() == conf.dict()
def test_interpolates_env(self):
conf = TaskConfiguration(
image="my_image",
registry_auth=RegistryAuth(
username="${{ env.REGISTRY_USERNAME }}",
password="${{ env.REGISTRY_PASSWORD }}",
),
env=Env.parse_obj(
{
"REGISTRY_USERNAME": "test_user",
"REGISTRY_PASSWORD": "test_password",
}
),
)
modified, args = self.apply_args(conf, [])
assert modified.registry_auth == RegistryAuth(
username="test_user",
password="test_password",
)
class TestValidateGPUVendorAndImage:
def prepare_conf(
self,
*,
image: Optional[str] = None,
gpu_spec: Optional[str] = None,
docker: Optional[bool] = None,
) -> BaseRunConfiguration:
conf_dict = {
"type": "none",
}
if image is not None:
conf_dict["image"] = image
if gpu_spec is not None:
conf_dict["resources"] = {
"gpu": gpu_spec,
}
if docker is not None:
conf_dict["docker"] = docker
return BaseRunConfiguration.parse_obj(conf_dict)
def validate(self, conf: BaseRunConfiguration) -> None:
BaseRunConfigurator(api_client=Mock()).validate_gpu_vendor_and_image(conf)
def test_no_gpu(self):
conf = self.prepare_conf()
self.validate(conf)
assert conf.resources.gpu is None
def test_zero_gpu(self):
conf = self.prepare_conf(gpu_spec="0")
self.validate(conf)
assert conf.resources.gpu.vendor is None
@pytest.mark.parametrize(
["gpu_spec", "expected_vendor"],
[
["nvidia", AcceleratorVendor.NVIDIA],
["tpu", AcceleratorVendor.GOOGLE],
["google", AcceleratorVendor.GOOGLE],
],
)
def test_non_amd_vendor_declared(self, gpu_spec, expected_vendor):
conf = self.prepare_conf(gpu_spec=gpu_spec)
self.validate(conf)
assert conf.resources.gpu.vendor == expected_vendor
def test_amd_vendor_declared_with_image(self):
conf = self.prepare_conf(image="tgi:rocm", gpu_spec="AMD")
self.validate(conf)
assert conf.resources.gpu.vendor == AcceleratorVendor.AMD
@pytest.mark.parametrize(
["gpu_spec", "expected_vendor"],
[
["a40,l40", AcceleratorVendor.NVIDIA], # lowercase
["V3-64", AcceleratorVendor.GOOGLE], # uppercase
],
)
def test_one_non_amd_vendor_inferred(self, gpu_spec, expected_vendor):
conf = self.prepare_conf(gpu_spec=gpu_spec)
self.validate(conf)
assert conf.resources.gpu.vendor == expected_vendor
@pytest.mark.parametrize("gpu_spec", ["MI300X", "MI300x", "mi300x"])
def test_amd_vendor_inferred_with_image(self, gpu_spec):
conf = self.prepare_conf(image="tgi:rocm", gpu_spec=gpu_spec)
self.validate(conf)
assert conf.resources.gpu.vendor == AcceleratorVendor.AMD
@pytest.mark.parametrize("gpu_spec", ["foo", "foo,bar"])
def test_one_unknown_vendor_inferred(self, gpu_spec):
conf = self.prepare_conf(gpu_spec=gpu_spec)
self.validate(conf)
assert conf.resources.gpu.vendor is None
@pytest.mark.parametrize(
"gpu_spec",
[
"A1000,v4", # Nvidia and Google
"v3-64,foo", # Google and unknown
],
)
def test_two_non_amd_vendors_inferred(self, gpu_spec):
conf = self.prepare_conf(gpu_spec=gpu_spec)
self.validate(conf)
assert conf.resources.gpu.vendor is None
@pytest.mark.parametrize(
"gpu_spec",
[
"A1000,mi300x", # Nvidia and AMD (lowercase)
"MI300x,v3-64", # AMD (mixedcase) and Google
"foo,MI300X", # unknown and AMD (uppercase)
],
)
def test_two_vendors_including_amd_inferred_with_image(self, gpu_spec):
conf = self.prepare_conf(image="tgi:rocm", gpu_spec=gpu_spec)
self.validate(conf)
assert conf.resources.gpu.vendor is None
def test_amd_vendor_declared_no_image(self):
conf = self.prepare_conf(gpu_spec="AMD")
with pytest.raises(
ConfigurationError, match=r"`image` is required if `resources.gpu.vendor` is `amd`"
):
self.validate(conf)
@pytest.mark.parametrize("gpu_spec", ["AMD", "MI300X"])
def test_amd_vendor_docker_true_no_image(self, gpu_spec):
conf = self.prepare_conf(gpu_spec=gpu_spec, docker=True)
self.validate(conf)
assert conf.resources.gpu.vendor == AcceleratorVendor.AMD
@pytest.mark.parametrize("gpu_spec", ["MI300X", "MI300x", "mi300x"])
def test_amd_vendor_inferred_no_image(self, gpu_spec):
conf = self.prepare_conf(gpu_spec=gpu_spec)
with pytest.raises(
ConfigurationError, match=r"`image` is required if `resources.gpu.vendor` is `amd`"
):
self.validate(conf)
@pytest.mark.parametrize(
"gpu_spec",
[
"A1000,mi300x", # Nvidia and AMD (lowercase)
"MI300x,v3-64", # AMD (mixedcase) and Google
"foo,MI300X", # unknown and AMD (uppercase)
],
)
def test_two_vendors_including_amd_inferred_no_image(self, gpu_spec):
conf = self.prepare_conf(gpu_spec=gpu_spec)
with pytest.raises(
ConfigurationError, match=r"`image` is required if `resources.gpu.vendor` is `amd`"
):
self.validate(conf)
@pytest.mark.parametrize("gpu_spec", ["n150", "n300"])
def test_tenstorrent_docker_true_no_image(self, gpu_spec):
conf = self.prepare_conf(gpu_spec=gpu_spec, docker=True)
self.validate(conf)
assert conf.resources.gpu.vendor == AcceleratorVendor.TENSTORRENT
class TestValidateCPUArchAndImage:
def prepare_conf(
self,
*,
cpu_spec: str,
gpu_spec: Optional[str] = None,
image: Optional[str] = None,
) -> BaseRunConfiguration:
conf_dict = {
"type": "none",
"resources": {
"cpu": cpu_spec,
},
}
if image is not None:
conf_dict["image"] = image
if gpu_spec is not None:
conf_dict["resources"]["gpu"] = gpu_spec
return BaseRunConfiguration.parse_obj(conf_dict)
def validate(self, conf: BaseRunConfiguration) -> None:
# validate_gpu_vendor_and_image sets GPU vendor if not set
BaseRunConfigurator(api_client=Mock()).validate_gpu_vendor_and_image(conf)
BaseRunConfigurator(api_client=Mock()).validate_cpu_arch_and_image(conf)
@pytest.mark.parametrize("gpu_spec", [None, "GH200", "H100"])
def test_explicit_arm_with_image(self, gpu_spec: Optional[str]):
conf = self.prepare_conf(cpu_spec="arm:1..", gpu_spec=gpu_spec, image="ubuntu")
self.validate(conf)
def test_inferred_arm_with_image(self):
conf = self.prepare_conf(cpu_spec="1..", gpu_spec="GH200", image="ubuntu")
self.validate(conf)
@pytest.mark.parametrize("cpu_spec", ["1..", "arm:1.."])
def test_arm_no_image(self, cpu_spec: str):
conf = self.prepare_conf(cpu_spec=cpu_spec, gpu_spec="GH200")
with pytest.raises(
ConfigurationError, match=r"`image` is required if `resources.cpu.arch` is `arm`"
):
self.validate(conf)
@pytest.mark.parametrize("cpu_spec", ["1..", "x86:1.."])
@pytest.mark.parametrize("image", [None, "ubuntu"])
def test_x86(self, cpu_spec: str, image: Optional[str]):
conf = self.prepare_conf(cpu_spec=cpu_spec, gpu_spec="H100", image=image)
self.validate(conf)