|
11 | 11 | # ANY KIND, either express or implied. See the License for the specific |
12 | 12 | # language governing permissions and limitations under the License. |
13 | 13 | """Test for JumpStart Document.""" |
| 14 | + |
14 | 15 | from __future__ import absolute_import |
15 | 16 |
|
16 | 17 | import json |
@@ -81,3 +82,145 @@ def test_get_hub_content_document_failure(jumpstart_session): |
81 | 82 | get_hub_content_and_document( |
82 | 83 | jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session |
83 | 84 | ) |
| 85 | + |
| 86 | + |
| 87 | +# --------------------------------------------------------------------------- |
| 88 | +# Tests for private-hub content-type probing + hub_content_name alias support. |
| 89 | +# |
| 90 | +# A private hub can contain either a ModelReference (a pointer to a public |
| 91 | +# model) or a privately-owned Model. get_hub_content_and_document() must not |
| 92 | +# guess from the hub name; it probes ModelReference first, then falls back to |
| 93 | +# Model. The public hub only holds Models. It also honors hub_content_name when |
| 94 | +# the content is filed under an alias differing from model_id. |
| 95 | +# |
| 96 | +# Note: distinct model_id / hub_name values are used per test to avoid the |
| 97 | +# module-level lru_cache on get_hub_content_and_document returning a stale |
| 98 | +# result across tests. |
| 99 | +# --------------------------------------------------------------------------- |
| 100 | + |
| 101 | + |
| 102 | +def _hub_content(hub_name, name, content_type, doc): |
| 103 | + return HubContent( |
| 104 | + hub_name=hub_name, |
| 105 | + hub_content_name=name, |
| 106 | + hub_content_version="1.0.0", |
| 107 | + hub_content_type=content_type, |
| 108 | + hub_content_document=json.dumps(doc), |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +def _not_found(): |
| 113 | + return ClientError( |
| 114 | + error_response={"Error": {"Code": "ResourceNotFound"}}, |
| 115 | + operation_name="DescribeHubContent", |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def _load_doc(): |
| 120 | + cur_dir = os.path.dirname(os.path.abspath(__file__)) |
| 121 | + with open(os.path.join(cur_dir, "hub_content_document.json"), "r") as f: |
| 122 | + return json.load(f) |
| 123 | + |
| 124 | + |
| 125 | +def test_public_hub_uses_model_type_only(jumpstart_session): |
| 126 | + """Public hub: resolve as Model, and never probe ModelReference.""" |
| 127 | + doc = _load_doc() |
| 128 | + jumpstart_config = JumpStartConfig(model_id="probe-public-model") |
| 129 | + |
| 130 | + with patch("sagemaker.core.jumpstart.document.HubContent.get") as mock_get: |
| 131 | + mock_get.return_value = _hub_content( |
| 132 | + "SageMakerPublicHub", "probe-public-model", "Model", doc |
| 133 | + ) |
| 134 | + hub_content, _ = get_hub_content_and_document( |
| 135 | + jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session |
| 136 | + ) |
| 137 | + |
| 138 | + assert hub_content.hub_content_type == "Model" |
| 139 | + # Public hub must be looked up exactly once, as Model. |
| 140 | + assert mock_get.call_count == 1 |
| 141 | + assert mock_get.call_args.kwargs["hub_content_type"] == "Model" |
| 142 | + |
| 143 | + |
| 144 | +def test_private_hub_resolves_model_reference_first(jumpstart_session): |
| 145 | + """Private hub holding a ModelReference: first probe (ModelReference) hits.""" |
| 146 | + doc = _load_doc() |
| 147 | + jumpstart_config = JumpStartConfig(model_id="probe-ref-model", hub_name="my-private-hub-ref") |
| 148 | + |
| 149 | + with patch("sagemaker.core.jumpstart.document.HubContent.get") as mock_get: |
| 150 | + mock_get.return_value = _hub_content( |
| 151 | + "my-private-hub-ref", "probe-ref-model", "ModelReference", doc |
| 152 | + ) |
| 153 | + hub_content, _ = get_hub_content_and_document( |
| 154 | + jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session |
| 155 | + ) |
| 156 | + |
| 157 | + assert hub_content.hub_content_type == "ModelReference" |
| 158 | + # ModelReference is tried first and succeeds -> single call. |
| 159 | + assert mock_get.call_count == 1 |
| 160 | + assert mock_get.call_args.kwargs["hub_content_type"] == "ModelReference" |
| 161 | + |
| 162 | + |
| 163 | +def test_private_hub_falls_back_to_model(jumpstart_session): |
| 164 | + """Private hub holding a privately-owned Model: ModelReference misses, then |
| 165 | + the Model fallback resolves it (the core of the fix).""" |
| 166 | + doc = _load_doc() |
| 167 | + jumpstart_config = JumpStartConfig( |
| 168 | + model_id="probe-private-model", hub_name="my-private-hub-model" |
| 169 | + ) |
| 170 | + |
| 171 | + with patch("sagemaker.core.jumpstart.document.HubContent.get") as mock_get: |
| 172 | + mock_get.side_effect = [ |
| 173 | + _not_found(), # ModelReference lookup misses |
| 174 | + _hub_content( # Model fallback resolves |
| 175 | + "my-private-hub-model", "probe-private-model", "Model", doc |
| 176 | + ), |
| 177 | + ] |
| 178 | + hub_content, _ = get_hub_content_and_document( |
| 179 | + jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session |
| 180 | + ) |
| 181 | + |
| 182 | + assert hub_content.hub_content_type == "Model" |
| 183 | + # Two probes: ModelReference (miss) then Model (hit). |
| 184 | + assert mock_get.call_count == 2 |
| 185 | + assert [c.kwargs["hub_content_type"] for c in mock_get.call_args_list] == [ |
| 186 | + "ModelReference", |
| 187 | + "Model", |
| 188 | + ] |
| 189 | + |
| 190 | + |
| 191 | +def test_private_hub_honors_hub_content_name_alias(jumpstart_session): |
| 192 | + """When hub_content_name is set (alias differs from model_id), the lookup |
| 193 | + must use the alias, not the model_id.""" |
| 194 | + doc = _load_doc() |
| 195 | + jumpstart_config = JumpStartConfig( |
| 196 | + model_id="probe-alias-public-id", |
| 197 | + hub_name="my-private-hub-alias", |
| 198 | + hub_content_name="the-alias-name", |
| 199 | + ) |
| 200 | + |
| 201 | + with patch("sagemaker.core.jumpstart.document.HubContent.get") as mock_get: |
| 202 | + mock_get.return_value = _hub_content( |
| 203 | + "my-private-hub-alias", "the-alias-name", "ModelReference", doc |
| 204 | + ) |
| 205 | + get_hub_content_and_document( |
| 206 | + jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session |
| 207 | + ) |
| 208 | + |
| 209 | + # Lookup used the alias, not the model_id. |
| 210 | + assert mock_get.call_args.kwargs["hub_content_name"] == "the-alias-name" |
| 211 | + |
| 212 | + |
| 213 | +def test_private_hub_not_found_as_either_type_raises(jumpstart_session): |
| 214 | + """Private hub where neither ModelReference nor Model exists: raise.""" |
| 215 | + jumpstart_config = JumpStartConfig( |
| 216 | + model_id="probe-missing-model", hub_name="my-private-hub-missing" |
| 217 | + ) |
| 218 | + |
| 219 | + with patch("sagemaker.core.jumpstart.document.HubContent.get") as mock_get: |
| 220 | + mock_get.side_effect = [_not_found(), _not_found()] |
| 221 | + with pytest.raises(ClientError): |
| 222 | + get_hub_content_and_document( |
| 223 | + jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session |
| 224 | + ) |
| 225 | + # Both content types were attempted before giving up. |
| 226 | + assert mock_get.call_count == 2 |
0 commit comments