forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_artifact_path_resolution.py
More file actions
485 lines (412 loc) · 20.4 KB
/
test_artifact_path_resolution.py
File metadata and controls
485 lines (412 loc) · 20.4 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
"""
Unit tests for ModelBuilder artifact path resolution.
Tests the _resolve_model_artifact_uri method with various scenarios.
Requirements: 7.3
"""
import unittest
from unittest.mock import Mock, patch, MagicMock
import pytest
from sagemaker.serve.model_builder import ModelBuilder
from sagemaker.serve.mode.function_pointers import Mode
class TestArtifactPathResolution(unittest.TestCase):
"""Test artifact path resolution - Requirements 7.3"""
def setUp(self):
"""Set up test fixtures."""
self.mock_session = Mock()
self.mock_session.boto_region_name = "us-west-2"
self.mock_session.default_bucket.return_value = "test-bucket"
self.mock_session.default_bucket_prefix = "test-prefix"
self.mock_session.config = {}
self.mock_session.sagemaker_config = {}
self.mock_session.settings = Mock()
self.mock_session.settings.include_jumpstart_tags = False
mock_credentials = Mock()
mock_credentials.access_key = "test-key"
mock_credentials.secret_key = "test-secret"
mock_credentials.token = None
self.mock_session.boto_session = Mock()
self.mock_session.boto_session.get_credentials.return_value = mock_credentials
self.mock_session.boto_session.region_name = "us-west-2"
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_hub_document_for_custom_model")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_base_model_artifact_uri_retrieval(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package, mock_fetch_hub
):
"""Test base model artifact URI retrieval from JumpStart metadata."""
# Setup: Base model (not LORA, not full fine-tuned)
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL" # Not LORA
# Setup: Model package with base_model but no model_data_source
mock_package = Mock()
mock_container = Mock()
mock_container.base_model = Mock()
mock_container.base_model.recipe_name = "test-recipe"
# No model_data_source attribute (base model)
mock_container.model_data_source = None
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
# Setup: Hub document with HostingArtifactUri
mock_fetch_hub.return_value = {
"HostingArtifactUri": "s3://jumpstart-bucket/base-model/artifacts.tar.gz"
}
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return HostingArtifactUri from JumpStart
assert artifact_uri == "s3://jumpstart-bucket/base-model/artifacts.tar.gz"
assert mock_fetch_hub.called
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_fine_tuned_adapter_artifact_location(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package
):
"""Test fine-tuned model returns None (model data handled by recipe/container)."""
# Setup: Full fine-tuned model (not LORA)
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
# Setup: Model package with model_data_source (fine-tuned model)
mock_package = Mock()
mock_container = Mock()
mock_s3_data_source = Mock()
mock_s3_data_source.s3_uri = "s3://my-bucket/fine-tuned-model/model.tar.gz"
mock_model_data_source = Mock()
mock_model_data_source.s3_data_source = mock_s3_data_source
mock_container.model_data_source = mock_model_data_source
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Fine-tuned models return None - model data is handled by recipe/container
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_lora_adapter_returns_none(self, mock_is_model_customization, mock_fetch_peft):
"""Test that LORA adapters return None (no artifact URI needed)."""
# Setup: LORA adapter
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "LORA"
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None for LORA adapters
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_hub_document_for_custom_model")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_missing_hosting_artifact_uri_returns_none(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package, mock_fetch_hub
):
"""Test error handling when HostingArtifactUri is missing from metadata."""
# Setup: Base model without HostingArtifactUri
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
# Setup: Model package with base_model but no model_data_source
mock_package = Mock()
mock_container = Mock()
mock_container.base_model = Mock()
mock_container.base_model.recipe_name = "test-recipe"
mock_container.model_data_source = None
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
# Setup: Hub document WITHOUT HostingArtifactUri
mock_fetch_hub.return_value = {}
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None and log warning
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_hub_document_for_custom_model")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_hub_document_fetch_exception_handling(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package, mock_fetch_hub
):
"""Test error handling when hub document fetch fails."""
# Setup: Base model
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
# Setup: Model package with base_model
mock_package = Mock()
mock_container = Mock()
mock_container.base_model = Mock()
mock_container.base_model.recipe_name = "test-recipe"
mock_container.model_data_source = None
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
# Setup: Hub document fetch raises exception
mock_fetch_hub.side_effect = Exception("Hub service unavailable")
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute - should not raise exception
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None and log warning
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_non_model_customization_returns_none(self, mock_is_model_customization):
"""Test that non-model-customization deployments return None."""
# Setup: Not a model customization deployment
mock_is_model_customization.return_value = False
builder = ModelBuilder(
model="my-model",
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge", # Provide instance_type to avoid auto-detection
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None for non-model-customization
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_missing_model_package_returns_none(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package
):
"""Test error handling when model package is not available."""
# Setup: Model customization but no model package
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
mock_fetch_package.return_value = None
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None when model package is unavailable
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_missing_inference_specification_returns_none(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package
):
"""Test error handling when model package has no inference specification."""
# Setup: Model package without inference_specification
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
mock_package = Mock()
mock_package.inference_specification = None
mock_fetch_package.return_value = mock_package
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None when inference_specification is missing
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_empty_containers_list_returns_none(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package
):
"""Test error handling when containers list is empty."""
# Setup: Model package with empty containers list
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
mock_package = Mock()
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = []
mock_fetch_package.return_value = mock_package
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None when containers list is empty
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_hub_document_for_custom_model")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_base_model_without_base_model_attribute_returns_none(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package, mock_fetch_hub
):
"""Test error handling when container has no base_model attribute."""
# Setup: Container without base_model attribute
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
mock_package = Mock()
mock_container = Mock()
mock_container.model_data_source = None
mock_container.base_model = None # No base_model
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return None when base_model is not present
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_fine_tuned_model_with_nested_s3_data_source(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package
):
"""Test fine-tuned model with nested s3_data_source returns None."""
# Setup: Full fine-tuned model with nested structure
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
# Setup: Properly nested model_data_source structure
mock_package = Mock()
mock_container = Mock()
mock_s3_data_source = Mock()
mock_s3_data_source.s3_uri = "s3://custom-bucket/my-fine-tuned-model/artifacts.tar.gz"
mock_model_data_source = Mock()
mock_model_data_source.s3_data_source = mock_s3_data_source
mock_container.model_data_source = mock_model_data_source
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Fine-tuned models return None - model data handled by recipe/container
assert artifact_uri is None
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_hub_document_for_custom_model")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_model_package")
@patch("sagemaker.serve.model_builder.ModelBuilder._fetch_peft")
@patch("sagemaker.serve.model_builder.ModelBuilder._is_model_customization")
def test_base_model_with_multiple_hosting_artifact_uris(
self, mock_is_model_customization, mock_fetch_peft, mock_fetch_package, mock_fetch_hub
):
"""Test base model retrieval when hub document has HostingArtifactUri."""
# Setup: Base model
mock_is_model_customization.return_value = True
mock_fetch_peft.return_value = "FULL"
# Setup: Model package with base_model
mock_package = Mock()
mock_container = Mock()
mock_container.base_model = Mock()
mock_container.base_model.recipe_name = "test-recipe"
mock_container.model_data_source = None
mock_package.inference_specification = Mock()
mock_package.inference_specification.containers = [mock_container]
mock_fetch_package.return_value = mock_package
# Setup: Hub document with HostingArtifactUri
mock_fetch_hub.return_value = {
"HostingArtifactUri": "s3://jumpstart-cache/base-model-v2/model.tar.gz",
"HostingEcrUri": "123456789012.dkr.ecr.us-west-2.amazonaws.com/jumpstart:latest",
}
builder = ModelBuilder(
model="huggingface-llm-mistral-7b",
model_metadata={
"CUSTOM_MODEL_ID": "huggingface-llm-mistral-7b",
"CUSTOM_MODEL_VERSION": "1.0.0",
},
mode=Mode.SAGEMAKER_ENDPOINT,
role_arn="arn:aws:iam::123456789012:role/TestRole",
sagemaker_session=self.mock_session,
instance_type="ml.m5.xlarge",
)
# Execute
artifact_uri = builder._resolve_model_artifact_uri()
# Verify: Should return HostingArtifactUri
assert artifact_uri == "s3://jumpstart-cache/base-model-v2/model.tar.gz"
if __name__ == "__main__":
unittest.main()