|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | +import pytest |
| 15 | +from unittest.mock import patch |
| 16 | +from sagemaker.jumpstart.constants import JUMPSTART_MODEL_HUB_NAME |
| 17 | +from sagemaker.jumpstart.factory.estimator import ( |
| 18 | + _add_model_uri_to_kwargs, |
| 19 | + get_model_info_default_kwargs, |
| 20 | +) |
| 21 | +from sagemaker.jumpstart.types import JumpStartEstimatorInitKwargs |
| 22 | +from sagemaker.jumpstart.enums import JumpStartScriptScope |
| 23 | + |
| 24 | + |
| 25 | +class TestAddModelUriToKwargs: |
| 26 | + @pytest.fixture |
| 27 | + def mock_kwargs(self): |
| 28 | + return JumpStartEstimatorInitKwargs( |
| 29 | + model_id="test-model", |
| 30 | + model_version="1.0.0", |
| 31 | + instance_type="ml.m5.large", |
| 32 | + model_uri=None, |
| 33 | + ) |
| 34 | + |
| 35 | + @patch( |
| 36 | + "sagemaker.jumpstart.factory.estimator._model_supports_training_model_uri", |
| 37 | + return_value=True, |
| 38 | + ) |
| 39 | + @patch("sagemaker.jumpstart.factory.estimator.model_uris.retrieve") |
| 40 | + def test_add_model_uri_to_kwargs_default_uri( |
| 41 | + self, mock_retrieve, mock_supports_training, mock_kwargs |
| 42 | + ): |
| 43 | + """Test adding default model URI when none is provided.""" |
| 44 | + default_uri = "s3://jumpstart-models/training/test-model/1.0.0" |
| 45 | + mock_retrieve.return_value = default_uri |
| 46 | + |
| 47 | + result = _add_model_uri_to_kwargs(mock_kwargs) |
| 48 | + |
| 49 | + mock_supports_training.assert_called_once() |
| 50 | + mock_retrieve.assert_called_once_with( |
| 51 | + model_scope=JumpStartScriptScope.TRAINING, |
| 52 | + instance_type=mock_kwargs.instance_type, |
| 53 | + **get_model_info_default_kwargs(mock_kwargs), |
| 54 | + ) |
| 55 | + assert result.model_uri == default_uri |
| 56 | + |
| 57 | + @patch( |
| 58 | + "sagemaker.jumpstart.factory.estimator._model_supports_training_model_uri", |
| 59 | + return_value=True, |
| 60 | + ) |
| 61 | + @patch( |
| 62 | + "sagemaker.jumpstart.factory.estimator._model_supports_incremental_training", |
| 63 | + return_value=True, |
| 64 | + ) |
| 65 | + @patch("sagemaker.jumpstart.factory.estimator.model_uris.retrieve") |
| 66 | + def test_add_model_uri_to_kwargs_custom_uri_with_incremental( |
| 67 | + self, mock_retrieve, mock_supports_incremental, mock_supports_training, mock_kwargs |
| 68 | + ): |
| 69 | + """Test using custom model URI with incremental training support.""" |
| 70 | + default_uri = "s3://jumpstart-models/training/test-model/1.0.0" |
| 71 | + custom_uri = "s3://custom-bucket/my-model" |
| 72 | + mock_retrieve.return_value = default_uri |
| 73 | + mock_kwargs.model_uri = custom_uri |
| 74 | + |
| 75 | + result = _add_model_uri_to_kwargs(mock_kwargs) |
| 76 | + |
| 77 | + mock_supports_training.assert_called_once() |
| 78 | + mock_supports_incremental.assert_called_once() |
| 79 | + assert result.model_uri == custom_uri |
| 80 | + |
| 81 | + @patch( |
| 82 | + "sagemaker.jumpstart.factory.estimator._model_supports_training_model_uri", |
| 83 | + return_value=True, |
| 84 | + ) |
| 85 | + @patch( |
| 86 | + "sagemaker.jumpstart.factory.estimator._model_supports_incremental_training", |
| 87 | + return_value=False, |
| 88 | + ) |
| 89 | + @patch("sagemaker.jumpstart.factory.estimator.model_uris.retrieve") |
| 90 | + @patch("sagemaker.jumpstart.factory.estimator.JUMPSTART_LOGGER.warning") |
| 91 | + def test_add_model_uri_to_kwargs_custom_uri_without_incremental( |
| 92 | + self, |
| 93 | + mock_warning, |
| 94 | + mock_retrieve, |
| 95 | + mock_supports_incremental, |
| 96 | + mock_supports_training, |
| 97 | + mock_kwargs, |
| 98 | + ): |
| 99 | + """Test using custom model URI without incremental training support logs warning.""" |
| 100 | + default_uri = "s3://jumpstart-models/training/test-model/1.0.0" |
| 101 | + custom_uri = "s3://custom-bucket/my-model" |
| 102 | + mock_retrieve.return_value = default_uri |
| 103 | + mock_kwargs.model_uri = custom_uri |
| 104 | + |
| 105 | + result = _add_model_uri_to_kwargs(mock_kwargs) |
| 106 | + |
| 107 | + mock_supports_training.assert_called_once() |
| 108 | + mock_supports_incremental.assert_called_once() |
| 109 | + mock_warning.assert_called_once() |
| 110 | + assert "does not support incremental training" in mock_warning.call_args[0][0] |
| 111 | + assert result.model_uri == custom_uri |
| 112 | + |
| 113 | + @patch( |
| 114 | + "sagemaker.jumpstart.factory.estimator._model_supports_training_model_uri", |
| 115 | + return_value=False, |
| 116 | + ) |
| 117 | + def test_add_model_uri_to_kwargs_no_training_support(self, mock_supports_training, mock_kwargs): |
| 118 | + """Test when model doesn't support training model URI.""" |
| 119 | + result = _add_model_uri_to_kwargs(mock_kwargs) |
| 120 | + |
| 121 | + mock_supports_training.assert_called_once() |
| 122 | + assert result.model_uri is None |
| 123 | + |
| 124 | + @patch( |
| 125 | + "sagemaker.jumpstart.factory.estimator._model_supports_training_model_uri", |
| 126 | + return_value=False, |
| 127 | + ) |
| 128 | + @patch("sagemaker.jumpstart.factory.estimator.model_uris.retrieve") |
| 129 | + def test_add_model_uri_to_kwargs_private_hub( |
| 130 | + self, mock_retrieve, mock_supports_training, mock_kwargs |
| 131 | + ): |
| 132 | + """Test when model is from a private hub.""" |
| 133 | + default_uri = "s3://jumpstart-models/training/test-model/1.0.0" |
| 134 | + mock_retrieve.return_value = default_uri |
| 135 | + mock_kwargs.hub_arn = "arn:aws:sagemaker:us-west-2:123456789012:hub/private-hub" |
| 136 | + |
| 137 | + result = _add_model_uri_to_kwargs(mock_kwargs) |
| 138 | + |
| 139 | + # Should not check if model supports training model URI for private hub |
| 140 | + mock_supports_training.assert_not_called() |
| 141 | + mock_retrieve.assert_called_once() |
| 142 | + assert result.model_uri == default_uri |
| 143 | + |
| 144 | + @patch( |
| 145 | + "sagemaker.jumpstart.factory.estimator._model_supports_training_model_uri", |
| 146 | + return_value=False, |
| 147 | + ) |
| 148 | + @patch("sagemaker.jumpstart.factory.estimator.model_uris.retrieve") |
| 149 | + def test_add_model_uri_to_kwargs_public_hub( |
| 150 | + self, mock_retrieve, mock_supports_training, mock_kwargs |
| 151 | + ): |
| 152 | + """Test when model is from the public hub.""" |
| 153 | + mock_kwargs.hub_arn = ( |
| 154 | + f"arn:aws:sagemaker:us-west-2:123456789012:hub/{JUMPSTART_MODEL_HUB_NAME}" |
| 155 | + ) |
| 156 | + |
| 157 | + result = _add_model_uri_to_kwargs(mock_kwargs) |
| 158 | + |
| 159 | + # Should check if model supports training model URI for public hub |
| 160 | + mock_supports_training.assert_called_once() |
| 161 | + mock_retrieve.assert_not_called() |
| 162 | + assert result.model_uri is None |
0 commit comments