-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtest_model.py
More file actions
198 lines (156 loc) · 7.04 KB
/
test_model.py
File metadata and controls
198 lines (156 loc) · 7.04 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
# BSD 2-Clause License
#
# Copyright (c) 2021-2025, Hewlett Packard Enterprise
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from uuid import uuid4
import numpy as np
import pytest
from smartsim import Experiment
from smartsim._core.launcher.step import SbatchStep, SrunStep
from smartsim.entity import Ensemble, Model
from smartsim.entity.model import _parse_model_parameters
from smartsim.error import EntityExistsError, SSUnsupportedError
from smartsim.settings import RunSettings, SbatchSettings, SrunSettings
from smartsim.settings.mpiSettings import _BaseMPISettings
# The tests in this file belong to the slow_tests group
pytestmark = pytest.mark.slow_tests
def test_register_incoming_entity_preexists(test_dir):
exp = Experiment("experiment", test_dir, launcher="local")
rs = RunSettings("python", exe_args="sleep.py")
ensemble = exp.create_ensemble(name="ensemble", replicas=1, run_settings=rs)
m = exp.create_model("model", run_settings=rs)
m.register_incoming_entity(ensemble["ensemble_0"])
assert len(m.incoming_entities) == 1
with pytest.raises(EntityExistsError):
m.register_incoming_entity(ensemble["ensemble_0"])
def test_disable_key_prefixing(test_dir):
exp = Experiment("experiment", test_dir, launcher="local")
rs = RunSettings("python", exe_args="sleep.py")
m = exp.create_model("model", run_settings=rs)
m.disable_key_prefixing()
assert m.query_key_prefixing() == False
def test_catch_colo_mpmd_model(test_dir):
exp = Experiment("experiment", test_dir, launcher="local")
rs = _BaseMPISettings("python", exe_args="sleep.py", fail_if_missing_exec=False)
# make it an mpmd model
rs_2 = _BaseMPISettings("python", exe_args="sleep.py", fail_if_missing_exec=False)
rs.make_mpmd(rs_2)
model = exp.create_model("bad_colo_model", rs)
# make it colocated which should raise and error
with pytest.raises(SSUnsupportedError):
model.colocate_db()
def test_attach_batch_settings_to_model(test_dir):
exp = Experiment("experiment", test_dir, launcher="slurm")
bs = SbatchSettings()
rs = SrunSettings("python", exe_args="sleep.py")
model_wo_bs = exp.create_model("test_model", run_settings=rs)
assert model_wo_bs.batch_settings is None
model_w_bs = exp.create_model("test_model_2", run_settings=rs, batch_settings=bs)
assert isinstance(model_w_bs.batch_settings, SbatchSettings)
@pytest.fixture
def monkeypatch_exp_controller(monkeypatch):
def _monkeypatch_exp_controller(exp):
entity_steps = []
def start_wo_job_manager(
self,
exp_name,
exp_path,
manifest,
block=True,
kill_on_interrupt=True,
monitor=True,
):
self._launch(exp_name, exp_path, manifest)
return None
def launch_step_nop(self, step, entity, monitor):
entity_steps.append((step, entity))
monkeypatch.setattr(
exp._control,
"start",
start_wo_job_manager.__get__(exp._control, type(exp._control)),
)
monkeypatch.setattr(
exp._control,
"_launch_step",
launch_step_nop.__get__(exp._control, type(exp._control)),
)
return entity_steps
return _monkeypatch_exp_controller
def test_model_with_batch_settings_makes_batch_step(
monkeypatch_exp_controller, test_dir
):
exp = Experiment("experiment", launcher="slurm", exp_path=test_dir)
bs = SbatchSettings()
rs = SrunSettings("python", exe_args="sleep.py")
model = exp.create_model("test_model", run_settings=rs, batch_settings=bs)
entity_steps = monkeypatch_exp_controller(exp)
exp.start(model)
assert len(entity_steps) == 1
step, entity = entity_steps[0]
assert isinstance(entity, Model)
assert isinstance(step, SbatchStep)
def test_model_without_batch_settings_makes_run_step(
monkeypatch, monkeypatch_exp_controller, test_dir
):
exp = Experiment("experiment", launcher="slurm", exp_path=test_dir)
rs = SrunSettings("python", exe_args="sleep.py")
model = exp.create_model("test_model", run_settings=rs)
# pretend we are in an allocation to not raise alloc err
monkeypatch.setenv("SLURM_JOB_ID", "12345")
entity_steps = monkeypatch_exp_controller(exp)
exp.start(model)
assert len(entity_steps) == 1
step, entity = entity_steps[0]
assert isinstance(entity, Model)
assert isinstance(step, SrunStep)
def test_models_batch_settings_are_ignored_in_ensemble(
monkeypatch_exp_controller, test_dir
):
exp = Experiment("experiment", launcher="slurm", exp_path=test_dir)
bs_1 = SbatchSettings(nodes=5)
rs = SrunSettings("python", exe_args="sleep.py")
model = exp.create_model("test_model", run_settings=rs, batch_settings=bs_1)
bs_2 = SbatchSettings(nodes=10)
ens = exp.create_ensemble("test_ensemble", batch_settings=bs_2)
ens.add_model(model)
entity_steps = monkeypatch_exp_controller(exp)
exp.start(ens)
assert len(entity_steps) == 1
step, entity = entity_steps[0]
assert isinstance(entity, Ensemble)
assert isinstance(step, SbatchStep)
assert step.batch_settings.batch_args["nodes"] == "10"
assert len(step.step_cmds) == 1
step_cmd = step.step_cmds[0]
assert any("srun" in tok for tok in step_cmd) # call the model using run settings
assert not any("sbatch" in tok for tok in step_cmd) # no sbatch in sbatch
@pytest.mark.parametrize("dtype", [int, np.float32, str])
def test_good_model_params(dtype):
print(dtype(0.6))
params = {"foo": dtype(0.6)}
assert all(isinstance(val, str) for val in _parse_model_parameters(params).values())
@pytest.mark.parametrize("bad_val", [["eggs"], {"n": 5}, object])
def test_bad_model_params(bad_val):
with pytest.raises(TypeError):
_parse_model_parameters({"foo": bad_val})