-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathtest_cache.py
More file actions
571 lines (477 loc) · 22.3 KB
/
test_cache.py
File metadata and controls
571 lines (477 loc) · 22.3 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
import json
import shutil
from pathlib import Path
from unittest.mock import ANY, mock_open, patch
import pytest
from olive.cache import CacheConfig, OliveCache, SharedCache
from olive.common.constants import DEFAULT_WORKFLOW_ID
# pylint: disable=W0201
class TestCache:
@pytest.mark.parametrize("clean_cache", [True, False])
def test_cache_init(self, clean_cache, tmp_path):
# setup
cache_dir = tmp_path / "cache_dir"
runs_dir = cache_dir / DEFAULT_WORKFLOW_ID / "runs"
runs_dir.mkdir(parents=True, exist_ok=True)
dummy_file = runs_dir / "0_p(・◡・)p.json"
with open(dummy_file, "w") as _:
pass
# execute
CacheConfig(cache_dir=cache_dir, clean_cache=clean_cache).create_cache()
# assert
assert cache_dir.exists()
assert runs_dir.exists()
assert clean_cache == (not dummy_file.exists())
@pytest.mark.parametrize(
("pass_name", "pass_config", "input_model_id", "accelerator_spec", "expected"),
[
(
"pass_name",
{"param": "value"},
"model",
"GPU",
{
"input_model_id": "model",
"pass_name": "pass_name",
"pass_config": {"param": "value"},
"accelerator_spec": "GPU",
},
),
(
"pass_name",
{},
"model",
None,
{"input_model_id": "model", "pass_name": "pass_name", "pass_config": {}, "accelerator_spec": None},
),
],
)
def test_get_run_json(self, pass_name, pass_config, input_model_id, accelerator_spec, expected):
# execute
result = OliveCache.get_run_json(pass_name, pass_config, input_model_id, accelerator_spec)
# assert
assert result == expected
def test_cache_model(self, tmp_path):
# setup
model_id = "model"
model_json = {"config": {"model_path": "path/to/model"}}
cache = CacheConfig(cache_dir=tmp_path).create_cache()
# execute
cache.cache_model(model_id, model_json)
# assert
model_json_path = cache.get_model_json_path(model_id)
assert model_json_path.exists()
with model_json_path.open() as f:
cached_model_json = json.load(f)
assert cached_model_json == model_json
@patch("olive.cache.SharedCache.cache_model")
def test_cache_model_with_shared_cache(self, mock_shared_cache_model, tmp_path):
# setup
model_id = "model_1"
model_json = {"config": {"model_path": "path/to/model_1"}}
cache_config = CacheConfig(cache_dir=tmp_path, enable_shared_cache=True)
cache = cache_config.create_cache()
# execute
cache.cache_model(model_id, model_json)
# assert
model_json_path = cache.get_model_json_path(model_id)
assert model_json_path.exists()
with model_json_path.open() as f:
cached_model_json = json.load(f)
assert cached_model_json == model_json
mock_shared_cache_model.assert_called_once_with(model_id, model_json)
def test_load_model(self, tmp_path):
# setup
cache_config = CacheConfig(cache_dir=tmp_path)
cache = cache_config.create_cache()
model_id = "model"
model_json = {"config": {"model_path": "path/to/model"}}
model_json_path = cache.get_model_json_path(model_id)
model_json_path.parent.mkdir(parents=True, exist_ok=True)
with model_json_path.open("w") as f:
json.dump(model_json, f)
# execute
loaded_model = cache.load_model(model_id)
# assert
assert loaded_model == model_json
@patch("olive.cache.SharedCache.load_model")
def test_load_model_with_shared_cache(self, mock_shared_cache_load_model, tmp_path):
# setup
model_id = "model_1"
model_json = {"config": {"model_path": "path/to/model_1"}}
cache_config = CacheConfig(cache_dir=tmp_path, enable_shared_cache=True)
cache = cache_config.create_cache()
mock_shared_cache_load_model.return_value = model_json
# execute
loaded_model = cache.load_model(model_id)
# assert
assert loaded_model == model_json
mock_shared_cache_load_model.assert_called_once_with(model_id, cache.get_model_json_path(model_id))
@pytest.mark.parametrize(
("pass_name", "pass_config", "input_model_id", "output_model_id", "accelerator_spec"),
[
("pass_name", {"param": "value"}, "input_model", "output_model", "GPU"),
("pass_name", {}, "input_model", "output_model", None),
],
)
def test_cache_run(self, pass_name, pass_config, input_model_id, output_model_id, accelerator_spec, tmp_path):
# setup
cache = CacheConfig(cache_dir=tmp_path).create_cache()
run_json_path = cache.get_run_json_path(output_model_id)
# execute
cache.cache_run(pass_name, pass_config, input_model_id, output_model_id, accelerator_spec)
# assert
assert run_json_path.exists()
with run_json_path.open() as f:
cached_run_json = json.load(f)
expected_run_json = OliveCache.get_run_json(pass_name, pass_config, input_model_id, accelerator_spec)
expected_run_json["output_model_id"] = output_model_id
assert cached_run_json == expected_run_json
@patch("olive.cache.SharedCache.cache_run")
@pytest.mark.parametrize(
("pass_name", "pass_config", "input_model_id", "output_model_id", "accelerator_spec"),
[
("pass_name", {"param": "value"}, "input_model", "output_model", "GPU"),
("pass_name", {}, "input_model", "output_model", None),
],
)
def test_cache_run_with_shared_cache(
self, mock_shared_cache_run, pass_name, pass_config, input_model_id, output_model_id, accelerator_spec, tmp_path
):
# setup
cache_config = CacheConfig(cache_dir=tmp_path, enable_shared_cache=True)
cache = cache_config.create_cache()
run_json_path = cache.get_run_json_path(output_model_id)
# execute
cache.cache_run(pass_name, pass_config, input_model_id, output_model_id, accelerator_spec)
# assert
assert run_json_path.exists()
with run_json_path.open() as f:
cached_run_json = json.load(f)
expected_run_json = OliveCache.get_run_json(pass_name, pass_config, input_model_id, accelerator_spec)
expected_run_json["output_model_id"] = output_model_id
assert cached_run_json == expected_run_json
mock_shared_cache_run.assert_called_once_with(output_model_id, run_json_path)
def test_load_run_from_model_id(self, tmp_path):
# setup
cache_config = CacheConfig(cache_dir=tmp_path)
cache = cache_config.create_cache()
model_id = "model"
run_json = {"key": "value"}
run_json_path = cache.get_run_json_path(model_id)
run_json_path.parent.mkdir(parents=True, exist_ok=True)
with run_json_path.open("w") as f:
json.dump(run_json, f)
# execute
loaded_run = cache.load_run_from_model_id(model_id)
# assert
assert loaded_run == run_json
@patch("olive.cache.SharedCache.load_run")
def test_load_run_from_model_id_with_shared_cache(self, mock_shared_cache_load_run, tmp_path):
# setup
model_id = "model_1"
run_json = {"key": "value"}
cache_config = CacheConfig(cache_dir=tmp_path, enable_shared_cache=True)
cache = cache_config.create_cache()
mock_shared_cache_load_run.return_value = run_json
# execute
loaded_run = cache.load_run_from_model_id(model_id)
# assert
assert loaded_run == run_json
mock_shared_cache_load_run.assert_called_once_with(model_id, cache.get_run_json_path(model_id))
def test_load_run_from_model_id_not_found(self, tmp_path):
# setup
cache_config = CacheConfig(cache_dir=tmp_path)
cache = cache_config.create_cache()
model_id = "non_existent_model"
# execute
loaded_run = cache.load_run_from_model_id(model_id)
# assert
assert loaded_run == {}
def test_load_run_from_model_id_invalid_json(self, tmp_path):
# setup
cache_config = CacheConfig(cache_dir=tmp_path)
cache = cache_config.create_cache()
model_id = "model"
run_json_path = cache.get_run_json_path(model_id)
run_json_path.parent.mkdir(parents=True, exist_ok=True)
with run_json_path.open("w") as f:
f.write("invalid json")
# execute
loaded_run = cache.load_run_from_model_id(model_id)
# assert
assert loaded_run == {}
def test_cache_evaluation(self, tmp_path):
# setup
model_id = "model"
evaluation_json = {"accuracy": 0.95}
cache = CacheConfig(cache_dir=tmp_path).create_cache()
# execute
cache.cache_evaluation(model_id, evaluation_json)
# assert
evaluation_json_path = cache.get_evaluation_json_path(model_id)
assert evaluation_json_path.exists()
with evaluation_json_path.open() as f:
cached_evaluation_json = json.load(f)
assert cached_evaluation_json == evaluation_json
@pytest.mark.parametrize(
"model_path",
["model_folder", "model.onnx"],
)
def test_save_model(self, model_path, tmp_path):
# setup
model_id = "model"
cache = CacheConfig(cache_dir=tmp_path / "cache").create_cache()
model_parent = tmp_path / "model_parent"
model_parent.mkdir(parents=True, exist_ok=True)
if model_path == "model_folder":
model_folder = model_parent / model_path
model_folder.mkdir(parents=True, exist_ok=True)
# create a model .onnx file and external data file
onnx_file = model_folder / "model.onnx"
onnx_file.touch()
external_data_file = model_folder / "model.onnx.data"
external_data_file.touch()
model_p = str(model_folder)
model_json = {"type": "onnxmodel", "config": {"model_path": model_p, "onnx_file_name": "model.onnx"}}
else:
model_p = str(model_parent / model_path)
Path(model_p).touch()
model_json = {"type": "onnxmodel", "config": {"model_path": model_p}}
# cache model to cache_dir
model_cache_file_path = cache.get_model_json_path(model_id)
with open(model_cache_file_path, "w") as f:
json.dump(model_json, f)
# output model to output_dir
output_dir = tmp_path / "output"
shutil.rmtree(output_dir, ignore_errors=True)
output_json = cache.save_model(model_id, output_dir, True)
# Expected output depends on whether model has external data
if model_path == "model_folder":
# Model has external data → folder + onnx_file_name
expected_output_path = str(output_dir.resolve())
assert output_json["config"]["model_path"] == expected_output_path
assert output_json["config"]["onnx_file_name"] == "model.onnx"
assert (output_dir / "model.onnx").exists()
assert (output_dir / "model.onnx.data").exists()
else:
# Model has no external data → single file path
expected_output_path = output_dir / "model.onnx"
expected_output_path = str(expected_output_path.resolve())
assert output_json["config"]["model_path"] == expected_output_path
assert "onnx_file_name" not in output_json["config"]
assert Path(expected_output_path).exists()
output_json_path = output_dir / "model_config.json"
assert output_json_path.exists()
def test_save_model_with_custom_onnx_filename(self, tmp_path):
# setup
model_id = "model"
cache = CacheConfig(cache_dir=tmp_path / "cache").create_cache()
model_parent = tmp_path / "model_parent"
model_parent.mkdir(parents=True, exist_ok=True)
model_p = str(model_parent / "source.onnx")
Path(model_p).touch()
model_cache_file_path = cache.get_model_json_path(model_id)
model_json = {"type": "onnxmodel", "config": {"model_path": model_p}}
with open(model_cache_file_path, "w") as f:
json.dump(model_json, f)
output_models_dir = tmp_path / "output"
output_models_dir.mkdir(parents=True, exist_ok=True)
output_file_path = output_models_dir / "custom.onnx"
output_json = cache.save_model(model_id, output_file_path, True)
expected_output_path = str(output_file_path.resolve())
# assert
assert output_json["config"]["model_path"] == expected_output_path
assert output_file_path.exists()
output_json_path = output_models_dir / "model_config.json"
assert output_json_path.exists()
with open(output_json_path) as f:
assert expected_output_path == json.load(f)["config"]["model_path"]
def test_save_model_no_flatten_rebases_component_and_additional_file_paths(self, tmp_path):
# setup
model_id = "composite_model"
cache = CacheConfig(cache_dir=tmp_path / "cache").create_cache()
source_dir = tmp_path / "source_model"
decoder_dir = source_dir / "decoder"
embedding_dir = source_dir / "embedding"
decoder_dir.mkdir(parents=True, exist_ok=True)
embedding_dir.mkdir(parents=True, exist_ok=True)
(decoder_dir / "model.onnx").write_text("decoder")
(embedding_dir / "model.onnx").write_text("embedding")
(decoder_dir / "decoder_local.txt").write_text("decoder local")
(source_dir / "genai_config.json").write_text("{}")
(source_dir / "tokenizer.json").write_text("{}")
model_json = {
"type": "compositemodel",
"config": {
"model_path": str(source_dir),
"model_component_names": ["decoder", "embedding"],
"model_components": [
{
"type": "onnxmodel",
"config": {
"model_path": str(decoder_dir),
"onnx_file_name": "model.onnx",
"model_attributes": {"additional_files": [str(decoder_dir / "decoder_local.txt")]},
},
},
{
"type": "onnxmodel",
"config": {"model_path": str(embedding_dir), "onnx_file_name": "model.onnx"},
},
],
"model_attributes": {
"no_flatten": True,
"additional_files": [str(source_dir / "genai_config.json"), str(source_dir / "tokenizer.json")],
},
},
}
with cache.get_model_json_path(model_id).open("w") as f:
json.dump(model_json, f)
output_dir = tmp_path / "output"
output_json = cache.save_model(model_id, output_dir, True)
# assert copied layout
assert (output_dir / "decoder" / "model.onnx").exists()
assert (output_dir / "embedding" / "model.onnx").exists()
assert (output_dir / "genai_config.json").exists()
assert (output_dir / "tokenizer.json").exists()
# assert rewritten config paths
assert output_json["config"]["model_path"] == str(output_dir)
decoder_component = output_json["config"]["model_components"][0]["config"]
assert decoder_component["model_path"] == str(output_dir / "decoder")
assert decoder_component["onnx_file_name"] == "model.onnx"
assert decoder_component["model_attributes"]["additional_files"] == [
str(output_dir / "decoder" / "decoder_local.txt")
]
assert output_json["config"]["model_attributes"]["additional_files"] == [
str(output_dir / "genai_config.json"),
str(output_dir / "tokenizer.json"),
]
class TestSharedCache:
@pytest.fixture(autouse=True)
@patch("olive.common.container_client_factory.AzureContainerClientFactory")
def setup(self, mock_AzureContainerClientFactory):
self.mock_factory_instance = mock_AzureContainerClientFactory.return_value
self.shared_cache = SharedCache("dummy_account", "dummy_container")
@patch("olive.cache.AzureContainerClientFactory.upload_blob")
def test_cache_run(self, mock_upload_blob, tmp_path):
# setup
model_id = "model"
run_json_path = tmp_path / "run.json"
run_json_path.write_text(json.dumps({"key": "value"}))
# execute
self.shared_cache.cache_run(model_id, run_json_path)
# assert
self.shared_cache.container_client_factory.upload_blob.assert_called_once_with(f"{model_id}/run.json", ANY)
@patch("olive.cache.AzureContainerClientFactory.upload_blob")
@patch("olive.cache.AzureContainerClientFactory.delete_blob")
def test_cache_run_exception(self, mock_delete_blob, mock_upload_blob, tmp_path):
# setup
model_id = "model"
run_json_path = tmp_path / "run.json"
run_json_path.write_text(json.dumps({"key": "value"}))
# execute
with patch.object(self.shared_cache.container_client_factory, "upload_blob", side_effect=Exception):
self.shared_cache.cache_run(model_id, run_json_path)
# assert
self.shared_cache.container_client_factory.delete_blob.assert_called_once_with(model_id)
@patch("olive.cache.AzureContainerClientFactory.exists")
def test_load_run_not_found(self, mock_exists, tmp_path):
# setup
model_id = "model"
run_json_path = tmp_path / "run.json"
mock_exists.return_value = False
# execute
loaded_run = self.shared_cache.load_run(model_id, run_json_path)
# assert
assert loaded_run == {}
@patch("olive.cache.AzureContainerClientFactory.exists")
@patch("olive.cache.AzureContainerClientFactory.download_blob")
def test_load_run(self, mock_download_blob, mock_exists, tmp_path):
# setup
model_id = "model"
run_json_path = tmp_path / "run.json"
run_json = {"key": "value"}
mock_exists.return_value = True
run_json_path.write_text(json.dumps(run_json))
# execute
loaded_run = self.shared_cache.load_run(model_id, run_json_path)
# assert
assert loaded_run == run_json
mock_download_blob.assert_called_once_with(f"{model_id}/run.json", run_json_path)
@patch("olive.cache.AzureContainerClientFactory.exists")
@patch("olive.cache.AzureContainerClientFactory.upload_blob")
def test_cache_model(self, mock_upload_blob, mock_exists):
# setup
model_id = "model_id"
model_json = {"type": "onnxmodel", "config": {"model_path": "model.onnx"}}
model_binary_data = b"Test binary data"
m = mock_open(read_data=model_binary_data)
mock_exists.return_value = False
with patch("builtins.open", m), patch.object(Path, "exists", return_value=True):
with open("model.onnx", "rb") as model_data:
model_data.read()
# execute
self.shared_cache.cache_model(model_id, model_json)
# assert
# 1. model_config.json 2. model
assert mock_upload_blob.call_count == 2
@patch("olive.cache.AzureContainerClientFactory.exists")
@patch("olive.cache.AzureContainerClientFactory.upload_blob")
@patch("olive.cache.AzureContainerClientFactory.delete_blob")
def test_cache_model_exception(self, mock_delete_blob, mock_upload_blob, mock_exists):
# setup
model_id = "model_id"
model_json = {"type": "onnxmodel", "config": {"model_path": "model.onnx"}}
model_binary_data = b"Test binary data"
m = mock_open(read_data=model_binary_data)
mock_exists.return_value = False
with patch("builtins.open", m), patch.object(Path, "exists", return_value=True):
with open("model.onnx", "rb") as model_data:
model_data.read()
# execute
with patch.object(self.shared_cache.container_client_factory, "upload_blob", side_effect=Exception):
self.shared_cache.cache_model(model_id, model_json)
# assert
self.shared_cache.container_client_factory.delete_blob.assert_called_once_with(model_id)
@patch("olive.cache.AzureContainerClientFactory.exists")
@patch("olive.cache.AzureContainerClientFactory.download_blob")
def test_load_model(self, mock_download_blob, mock_exists, tmp_path):
# setup
model_id = "model"
model_json_path = tmp_path / "model.json"
model_json = {"config": {"model_path": "path/to/model"}}
model_json_path.write_text(json.dumps(model_json))
mock_exists.return_value = True
# execute
loaded_model = self.shared_cache.load_model(model_id, model_json_path)
# assert
assert loaded_model == model_json
mock_download_blob.assert_called_once()
@pytest.mark.parametrize("expected_exists", [True, False])
@patch("olive.cache.AzureContainerClientFactory.exists")
def test_exist_in_shared_cache(self, mock_exists, expected_exists):
# setup
blob_name = "model_id/model.json"
mock_exists.return_value = expected_exists
# execute
actual_exists = self.shared_cache.exist_in_shared_cache(blob_name)
# assert
assert actual_exists == expected_exists
mock_exists.assert_called_once_with(blob_name)
@patch("olive.cache.AzureContainerClientFactory.upload_blob")
def test_upload_model_files(self, mock_upload_blob, tmp_path):
# setup
model_path = tmp_path / "model"
model_path.mkdir()
file_path = model_path / "file.txt"
file_path.write_text("dummy content")
model_blob = "model_blob"
# execute
self.shared_cache.upload_model_files(str(model_path), model_blob)
# assert
mock_upload_blob.assert_called_once_with(f"{model_blob}/file.txt", ANY)