forked from aws/sagemaker-hyperpod-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_inference.py
More file actions
639 lines (552 loc) · 23.4 KB
/
test_inference.py
File metadata and controls
639 lines (552 loc) · 23.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import pytest
from click.testing import CliRunner
from unittest.mock import Mock, patch
import sys
import importlib
import hyperpod_jumpstart_inference_template.registry as jreg
import hyperpod_custom_inference_template.registry as creg
# Import the non-create commands that don't need special handling
from sagemaker.hyperpod.cli.commands.inference import (
js_create,
custom_create,
custom_invoke,
js_list,
custom_list,
js_describe,
custom_describe,
js_delete,
custom_delete,
js_list_pods,
custom_list_pods,
js_get_logs,
custom_get_logs,
js_get_operator_logs,
custom_get_operator_logs,
)
# --------- JumpStart Commands ---------
@patch("sys.argv", ["pytest", "--version", "1.0"])
def test_js_create_with_required_args():
"""
Test js_create with all required options via CLI runner, mocking schema and endpoint.
"""
# Reload the inference module with mocked sys.argv
if "sagemaker.hyperpod.cli.commands.inference" in sys.modules:
importlib.reload(sys.modules["sagemaker.hyperpod.cli.commands.inference"])
from sagemaker.hyperpod.cli.commands.inference import js_create
with patch(
"sagemaker.hyperpod.cli.inference_utils.load_schema_for_version"
) as mock_load_schema, patch(
"sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint"
) as mock_endpoint_class, patch(
"sagemaker.hyperpod.common.cli_decorators._is_valid_jumpstart_model_id"
) as mock_model_validation, patch(
"sagemaker.hyperpod.common.cli_decorators._namespace_exists"
) as mock_namespace_exists, patch(
"sagemaker.hyperpod.inference.hp_jumpstart_endpoint.HPJumpStartEndpoint.validate_instance_type"
) as mock_validate_instance, patch(
"sagemaker.hyperpod.common.utils.get_jumpstart_model_instance_types"
) as mock_get_instance_types, patch(
"sagemaker.hyperpod.common.utils.get_cluster_instance_types"
) as mock_get_cluster_types, patch(
"sagemaker.hyperpod.inference.hp_jumpstart_endpoint.HPJumpStartEndpoint.create"
) as mock_create:
# Mock enhanced error handling
mock_model_validation.return_value = True # Allow test model-id
mock_namespace_exists.return_value = True # Allow test namespace
mock_validate_instance.return_value = None # Skip validation
mock_get_instance_types.return_value = [
"ml.p4d.24xlarge"
] # Mock supported types
mock_get_cluster_types.return_value = ["ml.p4d.24xlarge"] # Mock cluster types
mock_create.return_value = None # Mock successful creation
# Prepare mock model-to-domain mapping
mock_model_class = Mock()
mock_model_instance = Mock()
domain_obj = Mock()
domain_obj.create = mock_create
mock_model_instance.to_domain.return_value = domain_obj
mock_model_class.return_value = mock_model_instance
# Set up the registry for version 1.0
jreg.SCHEMA_REGISTRY["1.0"] = mock_model_class
runner = CliRunner()
result = runner.invoke(
js_create,
[
"--namespace",
"test-ns",
"--version",
"1.0",
"--model-id",
"test-model-id",
"--instance-type",
"ml.p4d.24xlarge", # Use a supported instance type
"--endpoint-name",
"test-endpoint",
],
)
assert result.exit_code == 0, result.output
mock_create.assert_called_once_with(debug=False)
def test_js_create_missing_required_args():
runner = CliRunner()
result = runner.invoke(js_create, [])
assert result.exit_code != 0
assert "Missing option" in result.output
def test_js_create_with_mig_profile():
"""
Test js_create with MIG profile (accelerator partition) options using v1.1 schema.
"""
with patch(
"sagemaker.hyperpod.cli.inference_utils.load_schema_for_version"
) as mock_load_schema, patch(
"sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint"
) as mock_endpoint_class, patch(
"sagemaker.hyperpod.common.cli_decorators._is_valid_jumpstart_model_id"
) as mock_model_validation, patch(
"sagemaker.hyperpod.common.cli_decorators._namespace_exists"
) as mock_namespace_exists:
# Mock enhanced error handling
mock_model_validation.return_value = True
mock_namespace_exists.return_value = True
# Mock schema loading
mock_load_schema.return_value = {
"properties": {
"model_id": {"type": "string"},
"instance_type": {"type": "string"},
},
"required": ["model_id", "instance_type"],
}
# Prepare mock model-to-domain mapping
mock_model_class = Mock()
mock_model_instance = Mock()
domain_obj = Mock()
domain_obj.create = Mock()
mock_model_instance.to_domain.return_value = domain_obj
mock_model_class.return_value = mock_model_instance
mock_endpoint_class.model_construct.return_value = domain_obj
jreg.SCHEMA_REGISTRY.clear()
jreg.SCHEMA_REGISTRY["1.1"] = mock_model_class
runner = CliRunner()
result = runner.invoke(
js_create,
[
"--namespace",
"test-ns",
"--version",
"1.1",
"--model-id",
"test-model-id",
"--instance-type",
"ml.p4d.24xlarge",
"--accelerator-partition-type",
"mig-1g.5gb",
"--accelerator-partition-validation",
"true",
"--endpoint-name",
"test-endpoint",
],
)
assert result.exit_code == 0, result.output
domain_obj.create.assert_called_once_with(debug=False)
# Verify the model instance was created with MIG profile parameters
mock_model_class.assert_called_once()
call_args = mock_model_class.call_args[1]
assert "accelerator_partition_type" in call_args
assert "accelerator_partition_validation" in call_args
def test_js_create_missing_required_args():
runner = CliRunner()
result = runner.invoke(js_create, [])
assert result.exit_code != 0
assert "Missing option" in result.output
def test_js_create_mig_validation_error_handling():
"""
Test js_create properly handles MIG profile validation errors using v1.1 schema.
"""
with patch(
"sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint"
) as mock_endpoint_class, patch(
"sagemaker.hyperpod.common.cli_decorators._is_valid_jumpstart_model_id"
) as mock_model_validation, patch(
"sagemaker.hyperpod.common.cli_decorators._namespace_exists"
) as mock_namespace_exists:
# Mock enhanced error handling
mock_model_validation.return_value = True
mock_namespace_exists.return_value = True
# Prepare mock model-to-domain mapping that raises validation error
mock_model_class = Mock()
mock_model_instance = Mock()
domain_obj = Mock()
# Simulate MIG validation error during create
domain_obj.create.side_effect = ValueError(
"MIG profile '1g.5gb' is not supported for instance type 'ml.c5.2xlarge'"
)
mock_model_instance.to_domain.return_value = domain_obj
mock_model_class.return_value = mock_model_instance
mock_endpoint_class.model_construct.return_value = domain_obj
# Set up the registry for version 1.1
jreg.SCHEMA_REGISTRY["1.1"] = mock_model_class
runner = CliRunner()
result = runner.invoke(
js_create,
[
"--namespace",
"test-ns",
"--version",
"1.1",
"--model-id",
"test-model-id",
"--instance-type",
"ml.c5.2xlarge", # Instance type that doesn't support MIG
"--accelerator-partition-type",
"1g.5gb", # Invalid MIG profile for this instance
"--accelerator-partition-validation",
"true",
"--endpoint-name",
"test-endpoint",
],
)
# Should fail due to MIG validation error
assert result.exit_code != 0
assert "MIG profile" in result.output or "not supported" in result.output
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_list(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock()
inst.list.return_value = [Mock(metadata=Mock(model_dump=lambda: {"name": "e"}))]
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(js_list, ["--namespace", "ns"])
assert result.exit_code == 0
inst.list.assert_called_once_with("ns")
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_describe(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock()
inst.get.return_value = Mock(model_dump=lambda: {"name": "e"})
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(js_describe, ["--name", "n", "--namespace", "ns"])
assert result.exit_code == 0
inst.get.assert_called_once_with("n", "ns")
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_delete(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock()
ep = Mock()
ep.delete = Mock()
inst.get.return_value = ep
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(js_delete, ["--name", "n", "--namespace", "ns"])
assert result.exit_code == 0
ep.delete.assert_called_once()
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_get_operator_logs(mock_hp):
inst = Mock(get_operator_logs=Mock(return_value="ol"))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(js_get_operator_logs, ["--since-hours", "2"])
assert result.exit_code == 0
assert "ol" in result.output
# --------- Custom Commands ---------
@patch("sys.argv", ["pytest", "--version", "1.0"])
def test_custom_create_with_required_args():
"""
Test custom_create with all required options via CLI runner, mocking schema and endpoint.
"""
# Reload the inference module with mocked sys.argv
if "sagemaker.hyperpod.cli.commands.inference" in sys.modules:
importlib.reload(sys.modules["sagemaker.hyperpod.cli.commands.inference"])
from sagemaker.hyperpod.cli.commands.inference import custom_create
with patch(
"sagemaker.hyperpod.cli.inference_utils.load_schema_for_version"
) as mock_load_schema, patch(
"sagemaker.hyperpod.cli.commands.inference.HPEndpoint"
) as mock_endpoint_class:
# Mock schema loading to include storage flags (v1.0 - no intelligent routing/KV cache)
mock_load_schema.return_value = {
"properties": {
"instance_type": {"type": "string"},
"model_name": {"type": "string"},
"model_source_type": {"type": "string", "enum": ["s3", "fsx"]},
"s3_bucket_name": {"type": "string"},
"s3_region": {"type": "string"},
"image_uri": {"type": "string"},
"container_port": {"type": "integer"},
"model_volume_mount_name": {"type": "string"},
},
"required": [
"instance_type",
"model_name",
"model_source_type",
"s3_bucket_name",
"s3_region",
"image_uri",
"container_port",
"model_volume_mount_name",
],
}
# Prepare mock model class
mock_model_class = Mock()
mock_model_instance = Mock()
domain_obj = Mock()
domain_obj.create = Mock()
mock_model_instance.to_domain.return_value = domain_obj
mock_model_class.return_value = mock_model_instance
mock_endpoint_class.model_construct.return_value = domain_obj
# Patch the registry mapping
creg.SCHEMA_REGISTRY.clear()
creg.SCHEMA_REGISTRY["1.0"] = mock_model_class
runner = CliRunner()
result = runner.invoke(
custom_create,
[
"--namespace",
"test-ns",
"--version",
"1.0",
"--instance-type",
"ml.t2.micro",
"--model-name",
"test-model",
"--model-source-type",
"s3",
"--s3-bucket-name",
"test-bucket",
"--s3-region",
"us-west-2",
"--image-uri",
"test-image:latest",
"--container-port",
"8080",
"--model-volume-mount-name",
"model-volume",
"--endpoint-name",
"test-endpoint",
],
)
assert result.exit_code == 0, result.output
domain_obj.create.assert_called_once_with(debug=False)
def test_custom_create_missing_required_args():
runner = CliRunner()
result = runner.invoke(custom_create, [])
assert result.exit_code != 0
assert "Missing option" in result.output
@patch("sagemaker.hyperpod.cli.commands.inference.Endpoint.get")
@patch("sagemaker.hyperpod.cli.commands.inference.create_boto3_client")
def test_custom_invoke_success(mock_create_client, mock_endpoint_get):
mock_endpoint = Mock()
mock_endpoint.endpoint_status = "InService"
mock_endpoint_get.return_value = mock_endpoint
mock_body = Mock()
mock_body.read.return_value.decode.return_value = '{"ok": true}'
mock_create_client.return_value.invoke_endpoint.return_value = {"Body": mock_body}
runner = CliRunner()
result = runner.invoke(
custom_invoke, ["--endpoint-name", "ep", "--body", '{"x": 1}']
)
assert result.exit_code == 0, result.output
assert '"ok": true' in result.output
@patch("sagemaker.hyperpod.cli.commands.inference.create_boto3_client")
def test_custom_invoke_invalid_json(mock_create_client):
runner = CliRunner()
result = runner.invoke(custom_invoke, ["--endpoint-name", "ep", "--body", "bad"])
assert result.exit_code != 0
assert "must be valid JSON" in result.output
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_list(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock()
inst.list.return_value = [Mock(metadata=Mock(model_dump=lambda: {"name": "e"}))]
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(custom_list, ["--namespace", "ns"])
assert result.exit_code == 0
inst.list.assert_called_once_with("ns")
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_describe(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock()
inst.get.return_value = Mock(model_dump=lambda: {"name": "e"})
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(custom_describe, ["--name", "n", "--namespace", "ns"])
assert result.exit_code == 0
inst.get.assert_called_once_with("n", "ns")
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_delete(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock()
ep = Mock()
ep.delete = Mock()
inst.get.return_value = ep
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(custom_delete, ["--name", "n", "--namespace", "ns"])
assert result.exit_code == 0
ep.delete.assert_called_once()
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_get_operator_logs(mock_hp):
inst = Mock(get_operator_logs=Mock(return_value="ol"))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(custom_get_operator_logs, ["--since-hours", "2"])
assert result.exit_code == 0
assert "ol" in result.output
# --------- Default Namespace Tests ---------
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_list_default_namespace(mock_hp):
inst = Mock(list=Mock(return_value=[]))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(js_list, [])
assert result.exit_code == 0
inst.list.assert_called_once_with("default")
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_list_default_namespace(mock_hp):
inst = Mock(list=Mock(return_value=[]))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(custom_list, [])
assert result.exit_code == 0
inst.list.assert_called_once_with("default")
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_list_pods(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock(list_pods=Mock(return_value="pods"))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(
js_list_pods, ["--namespace", "ns", "--endpoint-name", "js-endpoint"]
)
assert result.exit_code == 0
assert "pods" in result.output
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_list_pods(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock(list_pods=Mock(return_value="pods"))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(
custom_list_pods, ["--namespace", "ns", "--endpoint-name", "custom-endpoint"]
)
assert result.exit_code == 0
assert "pods" in result.output
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPJumpStartEndpoint")
def test_js_get_logs(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock(get_logs=Mock(return_value="logs"))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(js_get_logs, ["--pod-name", "p", "--namespace", "ns"])
assert result.exit_code == 0
assert "logs" in result.output
@patch("sagemaker.hyperpod.common.cli_decorators._namespace_exists")
@patch("sagemaker.hyperpod.cli.commands.inference.HPEndpoint")
def test_custom_get_logs(mock_hp, mock_namespace_exists):
mock_namespace_exists.return_value = True
inst = Mock(get_logs=Mock(return_value="l"))
mock_hp.model_construct.return_value = inst
runner = CliRunner()
result = runner.invoke(custom_get_logs, ["--pod-name", "p", "--namespace", "ns"])
assert result.exit_code == 0
assert "l" in result.output
@patch("sys.argv", ["pytest", "--version", "1.1"])
def test_custom_create_with_intelligent_routing_and_kv_cache():
"""Test custom_create with intelligent routing and KV cache options."""
# Patch BEFORE reloading the module
with patch(
"sagemaker.hyperpod.cli.inference_utils.load_schema_for_version"
) as mock_load_schema, patch(
"sagemaker.hyperpod.cli.commands.inference.HPEndpoint"
) as mock_endpoint_class:
# Set up the schema mock first
mock_load_schema.return_value = {
"properties": {
"instance_type": {"type": "string"},
"model_name": {"type": "string"},
"model_source_type": {"type": "string", "enum": ["s3", "fsx"]},
"s3_bucket_name": {"type": "string"},
"s3_region": {"type": "string"},
"image_uri": {"type": "string"},
"container_port": {"type": "integer"},
"model_volume_mount_name": {"type": "string"},
"intelligent_routing_enabled": {"type": "boolean"},
"routing_strategy": {"type": "string"},
"enable_l1_cache": {"type": "boolean"},
"enable_l2_cache": {"type": "boolean"},
"l2_cache_backend": {"type": "string"},
"l2_cache_local_url": {"type": "string"},
},
"required": [
"instance_type",
"model_name",
"model_source_type",
"s3_bucket_name",
"s3_region",
"image_uri",
"container_port",
"model_volume_mount_name",
],
}
# Set up the registry mock
mock_model_class = Mock()
mock_model_instance = Mock()
domain_obj = Mock()
domain_obj.create = Mock()
mock_model_instance.to_domain.return_value = domain_obj
mock_model_class.return_value = mock_model_instance
mock_endpoint_class.model_construct.return_value = domain_obj
with patch.object(creg, "SCHEMA_REGISTRY", new={"1.1": mock_model_class}):
# NOW reload the module with all patches in place
if "sagemaker.hyperpod.cli.commands.inference" in sys.modules:
importlib.reload(
sys.modules["sagemaker.hyperpod.cli.commands.inference"]
)
from sagemaker.hyperpod.cli.commands.inference import custom_create
runner = CliRunner()
result = runner.invoke(
custom_create,
[
"--version",
"1.1",
"--instance-type",
"ml.g5.xlarge",
"--model-name",
"test-model",
"--model-source-type",
"s3",
"--s3-bucket-name",
"test-bucket",
"--s3-region",
"us-west-2",
"--image-uri",
"test-image:latest",
"--container-port",
"8080",
"--model-volume-mount-name",
"model-volume",
"--intelligent-routing-enabled",
"true",
"--routing-strategy",
"prefixaware",
"--enable-l1-cache",
"true",
"--enable-l2-cache",
"true",
"--l2-cache-backend",
"redis/sagemaker",
"--l2-cache-local-url",
"redis://redis.redis-system.svc.cluster.local:6379",
],
)
assert result.exit_code == 0, result.output
domain_obj.create.assert_called_once_with(debug=False)