forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gluon_model_export.py
315 lines (260 loc) · 11.5 KB
/
test_gluon_model_export.py
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
from packaging.version import Version
import os
import warnings
import yaml
import mxnet as mx
import numpy as np
import pandas as pd
import pytest
from mxnet import context as ctx
from mxnet.gluon import Trainer
from mxnet.gluon.data import DataLoader
from mxnet.gluon.nn import HybridSequential, Dense
import mlflow
import mlflow.gluon
import mlflow.pyfunc.scoring_server as pyfunc_scoring_server
from mlflow import pyfunc
from mlflow.models import infer_signature, Model
from mlflow.models.utils import _read_example
from mlflow.tracking.artifact_utils import _download_artifact_from_uri
from mlflow.utils.environment import _mlflow_conda_env
from mlflow.utils.file_utils import TempDir
from mlflow.utils.model_utils import _get_flavor_configuration
from tests.gluon.utils import get_estimator
from tests.helper_functions import (
pyfunc_serve_and_score_model,
_compare_conda_env_requirements,
_assert_pip_requirements,
_is_available_on_pypi,
)
if Version(mx.__version__) >= Version("2.0.0"):
array_module = mx.np
else:
array_module = mx.nd
EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("mxnet") else ["--no-conda"]
@pytest.fixture
def model_path(tmpdir):
return os.path.join(tmpdir.strpath, "model")
@pytest.fixture
def gluon_custom_env(tmpdir):
conda_env = os.path.join(str(tmpdir), "conda_env.yml")
_mlflow_conda_env(conda_env, additional_pip_deps=["mxnet", "pytest"])
return conda_env
@pytest.fixture(scope="module")
def model_data():
mnist = mx.test_utils.get_mnist()
train_data = array_module.array(mnist["train_data"].reshape(-1, 784))
train_label = array_module.array(mnist["train_label"])
test_data = array_module.array(mnist["test_data"].reshape(-1, 784))
return train_data, train_label, test_data
@pytest.fixture(scope="module")
def gluon_model(model_data):
train_data, train_label, _ = model_data
dataset = mx.gluon.data.ArrayDataset(train_data, train_label)
train_data_loader = DataLoader(dataset, batch_size=128, last_batch="discard")
model = HybridSequential()
model.add(Dense(128, activation="relu"))
model.add(Dense(64, activation="relu"))
model.add(Dense(10))
model.initialize()
model.hybridize()
trainer = Trainer(
model.collect_params(), "adam", optimizer_params={"learning_rate": 0.001, "epsilon": 1e-07}
)
est = get_estimator(model, trainer)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
est.fit(train_data_loader, epochs=3)
return model
@pytest.mark.large
def test_model_save_load(gluon_model, model_data, model_path):
_, _, test_data = model_data
expected = array_module.argmax(gluon_model(test_data), axis=1)
mlflow.gluon.save_model(gluon_model, model_path)
# Loading Gluon model
model_loaded = mlflow.gluon.load_model(model_path, ctx.cpu())
actual = array_module.argmax(model_loaded(test_data), axis=1)
assert all(expected == actual)
# Loading pyfunc model
pyfunc_loaded = mlflow.pyfunc.load_model(model_path)
test_pyfunc_data = pd.DataFrame(test_data.asnumpy())
pyfunc_preds = pyfunc_loaded.predict(test_pyfunc_data)
assert all(np.argmax(pyfunc_preds.values, axis=1) == expected.asnumpy())
# test with numpy array input
pyfunc_preds = pyfunc_loaded.predict(test_pyfunc_data.values)
assert all(np.argmax(pyfunc_preds, axis=1) == expected.asnumpy())
@pytest.mark.large
def test_signature_and_examples_are_saved_correctly(gluon_model, model_data):
model = gluon_model
signature_ = infer_signature(model_data[0].asnumpy())
example_ = model_data[0].asnumpy()[
:3,
]
for signature in (None, signature_):
for example in (None, example_):
with TempDir() as tmp:
path = tmp.path("model")
mlflow.gluon.save_model(
model, path=path, signature=signature, input_example=example
)
mlflow_model = Model.load(path)
assert signature == mlflow_model.signature
if example is None:
assert mlflow_model.saved_input_example_info is None
else:
assert np.array_equal(_read_example(mlflow_model, path), example)
@pytest.mark.large
def test_model_log_load(gluon_model, model_data, model_path):
# pylint: disable=unused-argument
_, _, test_data = model_data
expected = array_module.argmax(gluon_model(test_data), axis=1)
artifact_path = "model"
with mlflow.start_run():
model_info = mlflow.gluon.log_model(gluon_model, artifact_path=artifact_path)
model_uri = "runs:/{run_id}/{artifact_path}".format(
run_id=mlflow.active_run().info.run_id, artifact_path=artifact_path
)
assert model_info.model_uri == model_uri
# Loading Gluon model
model_loaded = mlflow.gluon.load_model(model_uri, ctx.cpu())
actual = array_module.argmax(model_loaded(test_data), axis=1)
assert all(expected == actual)
# Loading pyfunc model
pyfunc_loaded = mlflow.pyfunc.load_model(model_uri)
test_pyfunc_data = pd.DataFrame(test_data.asnumpy())
pyfunc_preds = pyfunc_loaded.predict(test_pyfunc_data)
assert all(np.argmax(pyfunc_preds.values, axis=1) == expected.asnumpy())
@pytest.mark.large
def test_model_save_persists_specified_conda_env_in_mlflow_model_directory(
gluon_model, model_path, gluon_custom_env
):
mlflow.gluon.save_model(gluon_model=gluon_model, path=model_path, conda_env=gluon_custom_env)
pyfunc_conf = _get_flavor_configuration(model_path=model_path, flavor_name=pyfunc.FLAVOR_NAME)
saved_conda_env_path = os.path.join(model_path, pyfunc_conf[pyfunc.ENV])
assert os.path.exists(saved_conda_env_path)
assert saved_conda_env_path != gluon_custom_env
with open(gluon_custom_env, "r") as f:
gluon_custom_env_parsed = yaml.safe_load(f)
with open(saved_conda_env_path, "r") as f:
saved_conda_env_parsed = yaml.safe_load(f)
assert saved_conda_env_parsed == gluon_custom_env_parsed
@pytest.mark.large
def test_model_save_persists_requirements_in_mlflow_model_directory(
gluon_model, model_path, gluon_custom_env
):
mlflow.gluon.save_model(gluon_model=gluon_model, path=model_path, conda_env=gluon_custom_env)
saved_pip_req_path = os.path.join(model_path, "requirements.txt")
_compare_conda_env_requirements(gluon_custom_env, saved_pip_req_path)
@pytest.mark.large
def test_save_model_with_pip_requirements(gluon_model, tmpdir):
# Path to a requirements file
tmpdir1 = tmpdir.join("1")
req_file = tmpdir.join("requirements.txt")
req_file.write("a")
mlflow.gluon.save_model(gluon_model, tmpdir1.strpath, pip_requirements=req_file.strpath)
_assert_pip_requirements(tmpdir1.strpath, ["mlflow", "a"], strict=True)
# List of requirements
tmpdir2 = tmpdir.join("2")
mlflow.gluon.save_model(
gluon_model, tmpdir2.strpath, pip_requirements=[f"-r {req_file.strpath}", "b"]
)
_assert_pip_requirements(tmpdir2.strpath, ["mlflow", "a", "b"], strict=True)
# Constraints file
tmpdir3 = tmpdir.join("3")
mlflow.gluon.save_model(
gluon_model, tmpdir3.strpath, pip_requirements=[f"-c {req_file.strpath}", "b"]
)
_assert_pip_requirements(
tmpdir3.strpath, ["mlflow", "b", "-c constraints.txt"], ["a"], strict=True
)
@pytest.mark.large
def test_save_model_with_extra_pip_requirements(gluon_model, tmpdir):
default_reqs = mlflow.gluon.get_default_pip_requirements()
# Path to a requirements file
tmpdir1 = tmpdir.join("1")
req_file = tmpdir.join("requirements.txt")
req_file.write("a")
mlflow.gluon.save_model(gluon_model, tmpdir1.strpath, extra_pip_requirements=req_file.strpath)
_assert_pip_requirements(tmpdir1.strpath, ["mlflow", *default_reqs, "a"])
# List of requirements
tmpdir2 = tmpdir.join("2")
mlflow.gluon.save_model(
gluon_model, tmpdir2.strpath, extra_pip_requirements=[f"-r {req_file.strpath}", "b"]
)
_assert_pip_requirements(tmpdir2.strpath, ["mlflow", *default_reqs, "a", "b"])
# Constraints file
tmpdir3 = tmpdir.join("3")
mlflow.gluon.save_model(
gluon_model, tmpdir3.strpath, extra_pip_requirements=[f"-c {req_file.strpath}", "b"]
)
_assert_pip_requirements(
tmpdir3.strpath, ["mlflow", *default_reqs, "b", "-c constraints.txt"], ["a"]
)
@pytest.mark.large
def test_model_save_accepts_conda_env_as_dict(gluon_model, model_path):
conda_env = dict(mlflow.gluon.get_default_conda_env())
conda_env["dependencies"].append("pytest")
mlflow.gluon.save_model(gluon_model=gluon_model, path=model_path, conda_env=conda_env)
pyfunc_conf = _get_flavor_configuration(model_path=model_path, flavor_name=pyfunc.FLAVOR_NAME)
saved_conda_env_path = os.path.join(model_path, pyfunc_conf[pyfunc.ENV])
assert os.path.exists(saved_conda_env_path)
with open(saved_conda_env_path, "r") as f:
saved_conda_env_parsed = yaml.safe_load(f)
assert saved_conda_env_parsed == conda_env
@pytest.mark.large
def test_log_model_persists_specified_conda_env_in_mlflow_model_directory(
gluon_model, gluon_custom_env
):
artifact_path = "model"
with mlflow.start_run():
mlflow.gluon.log_model(
gluon_model=gluon_model, artifact_path=artifact_path, conda_env=gluon_custom_env
)
model_path = _download_artifact_from_uri(
"runs:/{run_id}/{artifact_path}".format(
run_id=mlflow.active_run().info.run_id, artifact_path=artifact_path
)
)
pyfunc_conf = _get_flavor_configuration(model_path=model_path, flavor_name=pyfunc.FLAVOR_NAME)
saved_conda_env_path = os.path.join(model_path, pyfunc_conf[pyfunc.ENV])
assert os.path.exists(saved_conda_env_path)
assert saved_conda_env_path != gluon_custom_env
with open(gluon_custom_env, "r") as f:
gluon_custom_env_parsed = yaml.safe_load(f)
with open(saved_conda_env_path, "r") as f:
saved_conda_env_parsed = yaml.safe_load(f)
assert saved_conda_env_parsed == gluon_custom_env_parsed
@pytest.mark.large
def test_model_log_persists_requirements_in_mlflow_model_directory(gluon_model, gluon_custom_env):
artifact_path = "model"
with mlflow.start_run():
mlflow.gluon.log_model(
gluon_model=gluon_model, artifact_path=artifact_path, conda_env=gluon_custom_env
)
model_path = _download_artifact_from_uri(
"runs:/{run_id}/{artifact_path}".format(
run_id=mlflow.active_run().info.run_id, artifact_path=artifact_path
)
)
saved_pip_req_path = os.path.join(model_path, "requirements.txt")
_compare_conda_env_requirements(gluon_custom_env, saved_pip_req_path)
@pytest.mark.large
def test_gluon_model_serving_and_scoring_as_pyfunc(gluon_model, model_data):
_, _, test_data = model_data
expected = array_module.argmax(gluon_model(test_data), axis=1)
artifact_path = "model"
with mlflow.start_run():
mlflow.gluon.log_model(gluon_model, artifact_path=artifact_path)
model_uri = "runs:/{run_id}/{artifact_path}".format(
run_id=mlflow.active_run().info.run_id, artifact_path=artifact_path
)
scoring_response = pyfunc_serve_and_score_model(
model_uri=model_uri,
data=pd.DataFrame(test_data.asnumpy()),
content_type=pyfunc_scoring_server.CONTENT_TYPE_JSON_SPLIT_ORIENTED,
extra_args=EXTRA_PYFUNC_SERVING_TEST_ARGS,
)
response_values = pd.read_json(scoring_response.content, orient="records").values.astype(
np.float32
)
assert all(np.argmax(response_values, axis=1) == expected.asnumpy())