|
| 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 | +"""Unit tests for Nova hosting config resolution in ModelBuilder. |
| 14 | +
|
| 15 | +Verifies that hosting configs published in the JumpStart hub document take |
| 16 | +priority over the hardcoded ``_NOVA_HOSTING_CONFIGS`` fallback. |
| 17 | +""" |
| 18 | + |
| 19 | +import unittest |
| 20 | +from unittest.mock import MagicMock, patch |
| 21 | + |
| 22 | +from sagemaker.serve.model_builder import ModelBuilder |
| 23 | + |
| 24 | + |
| 25 | +def _make_builder(region="us-east-1"): |
| 26 | + """Create a ModelBuilder without running __init__.""" |
| 27 | + mb = ModelBuilder.__new__(ModelBuilder) |
| 28 | + mb.image_uri = None |
| 29 | + mb.env_vars = None |
| 30 | + mb.instance_type = None |
| 31 | + session = MagicMock() |
| 32 | + session.boto_region_name = region |
| 33 | + mb.sagemaker_session = session |
| 34 | + return mb |
| 35 | + |
| 36 | + |
| 37 | +def _make_model_package(recipe_name="", hub_content_name="nova-textgeneration-lite"): |
| 38 | + pkg = MagicMock() |
| 39 | + base_model = MagicMock() |
| 40 | + base_model.recipe_name = recipe_name |
| 41 | + base_model.hub_content_name = hub_content_name |
| 42 | + pkg.inference_specification.containers = [MagicMock(base_model=base_model)] |
| 43 | + return pkg |
| 44 | + |
| 45 | + |
| 46 | +class TestNovaHostingConfigResolution(unittest.TestCase): |
| 47 | + """Tests for ModelBuilder._get_nova_hosting_config priority behavior.""" |
| 48 | + |
| 49 | + def test_hub_recipe_collection_config_takes_priority(self): |
| 50 | + """Hosting config from RecipeCollection in the hub doc is preferred.""" |
| 51 | + mb = _make_builder() |
| 52 | + hub_doc = { |
| 53 | + "RecipeCollection": [ |
| 54 | + { |
| 55 | + "Name": "my-nova-recipe", |
| 56 | + "HostingConfigs": [ |
| 57 | + { |
| 58 | + "Profile": "Default", |
| 59 | + "EcrAddress": "111.dkr.ecr.us-east-1.amazonaws.com/custom:tag", |
| 60 | + "InstanceType": "ml.p5.48xlarge", |
| 61 | + "Environment": { |
| 62 | + "CONTEXT_LENGTH": "999", |
| 63 | + "MAX_CONCURRENCY": "3", |
| 64 | + }, |
| 65 | + } |
| 66 | + ], |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + mp = _make_model_package( |
| 71 | + recipe_name="my-nova-recipe", hub_content_name="nova-textgeneration-lite" |
| 72 | + ) |
| 73 | + with patch.object( |
| 74 | + ModelBuilder, "_fetch_hub_document_for_custom_model", return_value=hub_doc |
| 75 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 76 | + cfg = mb._get_nova_hosting_config() |
| 77 | + |
| 78 | + self.assertEqual( |
| 79 | + cfg["image_uri"], "111.dkr.ecr.us-east-1.amazonaws.com/custom:tag" |
| 80 | + ) |
| 81 | + self.assertEqual( |
| 82 | + cfg["env_vars"], {"CONTEXT_LENGTH": "999", "MAX_CONCURRENCY": "3"} |
| 83 | + ) |
| 84 | + self.assertEqual(cfg["instance_type"], "ml.p5.48xlarge") |
| 85 | + |
| 86 | + def test_top_level_hosting_configs_used_when_no_recipe_match(self): |
| 87 | + """Top-level HostingConfigs is used when no RecipeCollection matches.""" |
| 88 | + mb = _make_builder() |
| 89 | + hub_doc = { |
| 90 | + "HostingConfigs": [ |
| 91 | + { |
| 92 | + "Profile": "Default", |
| 93 | + "EcrAddress": "222.dkr.ecr.us-east-1.amazonaws.com/top:tag", |
| 94 | + "InstanceType": "ml.g6.24xlarge", |
| 95 | + "Environment": {"CONTEXT_LENGTH": "100"}, |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + mp = _make_model_package( |
| 100 | + recipe_name="unmatched", hub_content_name="nova-textgeneration-micro" |
| 101 | + ) |
| 102 | + with patch.object( |
| 103 | + ModelBuilder, "_fetch_hub_document_for_custom_model", return_value=hub_doc |
| 104 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 105 | + cfg = mb._get_nova_hosting_config() |
| 106 | + |
| 107 | + self.assertEqual( |
| 108 | + cfg["image_uri"], "222.dkr.ecr.us-east-1.amazonaws.com/top:tag" |
| 109 | + ) |
| 110 | + |
| 111 | + def test_hardcoded_fallback_when_hub_has_no_hosting_config(self): |
| 112 | + """Hardcoded escrow config is used when the hub doc has no hosting config.""" |
| 113 | + mb = _make_builder() |
| 114 | + mp = _make_model_package(hub_content_name="nova-textgeneration-lite") |
| 115 | + with patch.object( |
| 116 | + ModelBuilder, "_fetch_hub_document_for_custom_model", return_value={} |
| 117 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 118 | + cfg = mb._get_nova_hosting_config() |
| 119 | + |
| 120 | + self.assertIn("nova-inference-repo:SM-Inference-latest", cfg["image_uri"]) |
| 121 | + self.assertEqual(cfg["instance_type"], "ml.g6.48xlarge") |
| 122 | + |
| 123 | + def test_hardcoded_fallback_when_hub_fetch_raises(self): |
| 124 | + """Hardcoded config is used defensively when hub fetch raises.""" |
| 125 | + mb = _make_builder() |
| 126 | + mp = _make_model_package(hub_content_name="nova-textgeneration-pro") |
| 127 | + with patch.object( |
| 128 | + ModelBuilder, |
| 129 | + "_fetch_hub_document_for_custom_model", |
| 130 | + side_effect=RuntimeError("hub unavailable"), |
| 131 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 132 | + cfg = mb._get_nova_hosting_config() |
| 133 | + |
| 134 | + self.assertEqual(cfg["instance_type"], "ml.p5.48xlarge") |
| 135 | + self.assertIn("nova-inference-repo:SM-Inference-latest", cfg["image_uri"]) |
| 136 | + |
| 137 | + def test_missing_ecr_address_falls_through_to_hardcoded(self): |
| 138 | + """A hub hosting config without EcrAddress falls back to the escrow image.""" |
| 139 | + mb = _make_builder() |
| 140 | + hub_doc = { |
| 141 | + "RecipeCollection": [ |
| 142 | + { |
| 143 | + "Name": "r", |
| 144 | + "HostingConfigs": [ |
| 145 | + {"Profile": "Default", "InstanceType": "ml.p5.48xlarge"} |
| 146 | + ], |
| 147 | + } |
| 148 | + ] |
| 149 | + } |
| 150 | + mp = _make_model_package( |
| 151 | + recipe_name="r", hub_content_name="nova-textgeneration-pro" |
| 152 | + ) |
| 153 | + with patch.object( |
| 154 | + ModelBuilder, "_fetch_hub_document_for_custom_model", return_value=hub_doc |
| 155 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 156 | + cfg = mb._get_nova_hosting_config() |
| 157 | + |
| 158 | + self.assertIn("nova-inference-repo:SM-Inference-latest", cfg["image_uri"]) |
| 159 | + |
| 160 | + def test_instance_type_match_in_hub_config(self): |
| 161 | + """A requested instance type selects the matching hub config entry.""" |
| 162 | + mb = _make_builder() |
| 163 | + hub_doc = { |
| 164 | + "RecipeCollection": [ |
| 165 | + { |
| 166 | + "Name": "r", |
| 167 | + "HostingConfigs": [ |
| 168 | + { |
| 169 | + "Profile": "Default", |
| 170 | + "EcrAddress": "333.dkr.ecr.us-east-1.amazonaws.com/a:tag", |
| 171 | + "InstanceType": "ml.p5.48xlarge", |
| 172 | + "Environment": {"CONTEXT_LENGTH": "1"}, |
| 173 | + }, |
| 174 | + { |
| 175 | + "EcrAddress": "333.dkr.ecr.us-east-1.amazonaws.com/b:tag", |
| 176 | + "InstanceType": "ml.g6.48xlarge", |
| 177 | + "Environment": {"CONTEXT_LENGTH": "2"}, |
| 178 | + }, |
| 179 | + ], |
| 180 | + } |
| 181 | + ] |
| 182 | + } |
| 183 | + mp = _make_model_package( |
| 184 | + recipe_name="r", hub_content_name="nova-textgeneration-lite" |
| 185 | + ) |
| 186 | + with patch.object( |
| 187 | + ModelBuilder, "_fetch_hub_document_for_custom_model", return_value=hub_doc |
| 188 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 189 | + cfg = mb._get_nova_hosting_config(instance_type="ml.g6.48xlarge") |
| 190 | + |
| 191 | + self.assertEqual( |
| 192 | + cfg["image_uri"], "333.dkr.ecr.us-east-1.amazonaws.com/b:tag" |
| 193 | + ) |
| 194 | + self.assertEqual(cfg["instance_type"], "ml.g6.48xlarge") |
| 195 | + |
| 196 | + def test_unsupported_instance_type_raises(self): |
| 197 | + """Requesting an unsupported instance type raises ValueError (fallback path).""" |
| 198 | + mb = _make_builder() |
| 199 | + mp = _make_model_package(hub_content_name="nova-textgeneration-pro") |
| 200 | + with patch.object( |
| 201 | + ModelBuilder, "_fetch_hub_document_for_custom_model", return_value={} |
| 202 | + ), patch.object(ModelBuilder, "_fetch_model_package", return_value=mp): |
| 203 | + with self.assertRaises(ValueError): |
| 204 | + mb._get_nova_hosting_config(instance_type="ml.invalid.type") |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == "__main__": |
| 208 | + unittest.main() |
0 commit comments