|
14 | 14 | from sagemaker.serve.spec.inference_spec import InferenceSpec |
15 | 15 | from sagemaker.train.model_trainer import ModelTrainer |
16 | 16 | from sagemaker.core.resources import TrainingJob, Model |
| 17 | +from sagemaker.core.session_settings import SessionSettings |
17 | 18 | from sagemaker.core.training.configs import Compute, Networking, SourceCode |
18 | 19 |
|
19 | 20 |
|
@@ -530,5 +531,58 @@ def test_prepare_for_mode_unsupported_mode(self): |
530 | 531 | self.assertIn("Unsupported deployment mode", str(context.exception)) |
531 | 532 |
|
532 | 533 |
|
| 534 | +class TestModelBuilderLocalDownloadDir(unittest.TestCase): |
| 535 | + """Test that build wires model_path into settings.local_download_dir correctly.""" |
| 536 | + |
| 537 | + def setUp(self): |
| 538 | + """Set up test fixtures.""" |
| 539 | + self.mock_session = Mock() |
| 540 | + self.mock_session.boto_region_name = "us-west-2" |
| 541 | + self.mock_session.settings = SessionSettings() |
| 542 | + |
| 543 | + def _make_builder(self, model_path): |
| 544 | + builder = ModelBuilder( |
| 545 | + model=Mock(), |
| 546 | + image_uri="123.dkr.ecr.us-west-2.amazonaws.com/custom:latest", |
| 547 | + role_arn="arn:aws:iam::123456789012:role/TestRole", |
| 548 | + sagemaker_session=self.mock_session, |
| 549 | + ) |
| 550 | + builder.model_path = model_path |
| 551 | + builder._passthrough = True |
| 552 | + return builder |
| 553 | + |
| 554 | + def _run(self, builder): |
| 555 | + with patch.object(builder, "_get_serve_setting"), patch.object( |
| 556 | + builder, "_is_model_customization", return_value=False |
| 557 | + ), patch.object( |
| 558 | + builder, "_get_client_translators", return_value=(Mock(), Mock()) |
| 559 | + ), patch.object( |
| 560 | + builder, "_handle_mlflow_input" |
| 561 | + ), patch.object( |
| 562 | + builder, "_build_validations" |
| 563 | + ), patch.object( |
| 564 | + builder, "_build_for_passthrough", return_value=Mock() |
| 565 | + ): |
| 566 | + builder._build_single_modelbuilder() |
| 567 | + |
| 568 | + def test_local_model_path_is_created_and_used(self): |
| 569 | + """A local model_path is created on disk and set as local_download_dir.""" |
| 570 | + model_path = os.path.join(tempfile.mkdtemp(), "model-builder", "abc123") |
| 571 | + self.assertFalse(os.path.exists(model_path)) |
| 572 | + |
| 573 | + builder = self._make_builder(model_path) |
| 574 | + self._run(builder) |
| 575 | + |
| 576 | + self.assertTrue(os.path.isdir(model_path)) |
| 577 | + self.assertEqual(self.mock_session.settings.local_download_dir, model_path) |
| 578 | + |
| 579 | + def test_s3_model_path_leaves_local_download_dir_unset(self): |
| 580 | + """An s3:// model_path is not treated as a local download dir.""" |
| 581 | + builder = self._make_builder("s3://my-bucket/my-model/") |
| 582 | + self._run(builder) |
| 583 | + |
| 584 | + self.assertIsNone(self.mock_session.settings.local_download_dir) |
| 585 | + |
| 586 | + |
533 | 587 | if __name__ == "__main__": |
534 | 588 | unittest.main() |
0 commit comments