Skip to content

Commit f67a2ab

Browse files
committed
fix: resolve private hub Models and aliased references for ModelTrainer
1 parent eb0f09e commit f67a2ab

4 files changed

Lines changed: 786 additions & 42 deletions

File tree

‎sagemaker-core/src/sagemaker/core/jumpstart/document.py‎

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""This module contains utilites for JumpStart model metadata."""
14+
1415
from __future__ import absolute_import
1516

1617
import json
@@ -47,26 +48,54 @@ def get_hub_content_and_document(
4748
logger.debug("No sagemaker session provided. Using default session.")
4849

4950
hub_name = jumpstart_config.hub_name if jumpstart_config.hub_name else SAGEMAKER_PUBLIC_HUB
50-
hub_content_type = "Model" if hub_name == SAGEMAKER_PUBLIC_HUB else "ModelReference"
5151

5252
region = sagemaker_session.boto_region_name
5353

54-
try:
55-
hub_content = HubContent.get(
56-
hub_name=hub_name,
57-
hub_content_name=jumpstart_config.model_id,
58-
hub_content_version=jumpstart_config.model_version,
59-
hub_content_type=hub_content_type,
60-
session=sagemaker_session.boto_session,
61-
region=region,
62-
)
63-
except ClientError as e:
64-
if e.response["Error"]["Code"] == "ResourceNotFound":
65-
logger.error(
66-
f"Hub content {jumpstart_config.model_id} not found in {hub_name}.\n"
67-
"Please check that the Model ID is availble in the specified hub."
54+
# The hub content may be filed under an alias that differs from the public
55+
# model_id, so honor hub_content_name when provided.
56+
hub_content_name = (
57+
jumpstart_config.hub_content_name
58+
if getattr(jumpstart_config, "hub_content_name", None)
59+
else jumpstart_config.model_id
60+
)
61+
62+
# A private hub can contain either a ModelReference (a pointer to a public
63+
# JumpStart model) or a privately-owned Model authored directly into the
64+
# hub. We cannot tell which from the name alone, so probe: try
65+
# ModelReference first, then fall back to Model. The public hub only holds
66+
# Models. This mirrors ModelBuilder's resolution in accessors.py.
67+
if hub_name == SAGEMAKER_PUBLIC_HUB:
68+
content_types_to_try = ["Model"]
69+
else:
70+
content_types_to_try = ["ModelReference", "Model"]
71+
72+
hub_content = None
73+
last_error: Optional[ClientError] = None
74+
for content_type in content_types_to_try:
75+
try:
76+
hub_content = HubContent.get(
77+
hub_name=hub_name,
78+
hub_content_name=hub_content_name,
79+
hub_content_version=jumpstart_config.model_version,
80+
hub_content_type=content_type,
81+
session=sagemaker_session.boto_session,
82+
region=region,
6883
)
69-
raise e
84+
break
85+
except ClientError as e:
86+
if e.response["Error"]["Code"] == "ResourceNotFound":
87+
last_error = e
88+
continue
89+
raise e
90+
91+
if hub_content is None:
92+
logger.error(
93+
f"Hub content {hub_content_name} not found in {hub_name} as any of "
94+
f"{content_types_to_try}.\n"
95+
"Please check that the Model ID (or hub_content_name) is available "
96+
"in the specified hub."
97+
)
98+
raise last_error
7099

71100
logger.info(
72101
f"hub_content_name: {hub_content.hub_content_name}, "

‎sagemaker-core/tests/unit/jumpstart/test_document.py‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Test for JumpStart Document."""
14+
1415
from __future__ import absolute_import
1516

1617
import json
@@ -81,3 +82,145 @@ def test_get_hub_content_document_failure(jumpstart_session):
8182
get_hub_content_and_document(
8283
jumpstart_config=jumpstart_config, sagemaker_session=jumpstart_session
8384
)
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

Comments
 (0)