Skip to content

Commit 37d2b09

Browse files
author
Rohan Gujarathi
committed
fix: propagate deploy tags on the model-customization endpoint paths
Tags passed to ModelBuilder.deploy() were accepted but never reached the endpoints created on the model-customization paths, so they could not be used for tag-based resource association. deploy() branches into _deploy_model_customization() for fine-tuned and Nova models and returns before reaching _deploy_core_endpoint(), which is the only place that applies the deploy-time tags kwarg. Those paths built their endpoint tags from self._tags alone, so they saw only the tags ModelBuilder had added itself. Resolve the endpoint tags once on entry from both sources and use them for whichever path creates the endpoint. The restricted-model-package path created its endpoint with no tags at all; it now receives them like the other two. Adds _tags_as_key_value_list() to normalize tags into the lowercase key/value list the sagemaker.core.resources create() calls are typed for. self._tags can hold either casing, Tag objects, or a bare {key: value} mapping, and the previous inline reads indexed tag["Key"] directly after format_tags(), which returns a list unchanged whatever its casing -- so a lowercase list raised KeyError. That was already reachable through build(tags=...) and add_tags(). The merge deliberately does not go through add_tags(): tag_exists() compares tags as tag["Key"] == curr_tag["Key"], so writing the lowercase form these create() calls require back onto the accumulator raises KeyError as soon as self._tags is non-empty, which is the normal state after build(). Resolving into a local list instead also keeps deploy() from mutating builder state as a side effect. SageMaker rejects duplicate tag keys, so on a collision the tag ModelBuilder manages is kept and the caller's duplicate is dropped rather than displacing a tag the SDK relies on.
1 parent a7bf7ea commit 37d2b09

2 files changed

Lines changed: 247 additions & 19 deletions

File tree

‎sagemaker-serve/src/sagemaker/serve/model_builder.py‎

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
from sagemaker.core.enums import EndpointType
119119
from sagemaker.core.common_utils import (
120120
Tags,
121+
TagsDict,
121122
ModelApprovalStatusEnum,
122123
_resolve_routing_config,
123124
format_tags,
@@ -180,6 +181,61 @@
180181
SAGEMAKER_OUTPUT_LOCATION = "sagemaker_s3_output"
181182

182183

184+
def _tags_as_key_value_list(tags: Optional[Tags]) -> List[TagsDict]:
185+
"""Normalize any accepted tag form into the ``{"key": ..., "value": ...}`` list form.
186+
187+
``deploy()`` and ``add_tags()`` accept tags either as a list of dicts using
188+
``key``/``value`` or ``Key``/``Value``, or as a single ``{key: value}`` mapping, so
189+
``self._tags`` can hold any of those. The ``sagemaker.core.resources`` ``create()``
190+
calls are typed ``List[Tag]`` whose fields are lowercase and which forbid extra
191+
fields, so they reject the capitalized form and reject a bare mapping outright.
192+
193+
Note this differs from :func:`format_tags`, which emits the capitalized form the
194+
legacy ``sagemaker_session.create_*`` APIs expect and passes a list through
195+
unchanged whatever its casing.
196+
197+
Args:
198+
tags: Tags in any accepted form, or None.
199+
200+
Returns:
201+
The tags as a list of lowercase key/value dicts; empty when none were supplied.
202+
Entries missing a key or value are skipped, since SageMaker rejects them.
203+
"""
204+
if not tags:
205+
return []
206+
if isinstance(tags, dict):
207+
return [{"key": k, "value": v} for k, v in tags.items()]
208+
209+
normalized = []
210+
for tag in tags:
211+
if isinstance(tag, dict):
212+
key = tag.get("key", tag.get("Key"))
213+
value = tag.get("value", tag.get("Value"))
214+
else:
215+
key = getattr(tag, "key", None)
216+
value = getattr(tag, "value", None)
217+
if key is not None and value is not None:
218+
normalized.append({"key": key, "value": value})
219+
return normalized
220+
221+
222+
def _merge_tags(*tag_sets: Optional[Tags]) -> List[TagsDict]:
223+
"""Merge tag sets into one key/value list, keeping the first entry for a repeated key.
224+
225+
SageMaker rejects duplicate tag keys, so a later set cannot override an earlier one.
226+
Callers pass the tags SageMaker manages first so a caller-supplied tag cannot displace
227+
one the SDK relies on (for example the model-source tag used for resource reuse).
228+
"""
229+
merged: List[TagsDict] = []
230+
seen = set()
231+
for tags in tag_sets:
232+
for tag in _tags_as_key_value_list(tags):
233+
if tag["key"] not in seen:
234+
merged.append(tag)
235+
seen.add(tag["key"])
236+
return merged
237+
238+
183239
@dataclass
184240
class ModelBuilder(_InferenceRecommenderMixin, _ModelBuilderServers, _ModelBuilderUtils):
185241
"""Unified interface for building and deploying machine learning models.
@@ -6185,6 +6241,14 @@ def _deploy_model_customization(
61856241
from sagemaker.core.resources import InferenceComponent
61866242
from sagemaker.core.resources import Tag as CoreTag
61876243

6244+
# Tags passed to deploy() arrive here in kwargs, and the tags ModelBuilder
6245+
# manages itself are on self._tags. Merge both for the endpoint created
6246+
# below, whichever path creates it. This deliberately avoids add_tags():
6247+
# tag_exists() indexes tags as tag["Key"], so writing the lowercase form
6248+
# the resource create() calls require back onto the accumulator raises
6249+
# KeyError once self._tags is non-empty (which build() leaves it).
6250+
endpoint_tags = _merge_tags(getattr(self, "_tags", None), kwargs.get("tags"))
6251+
61886252
# An inference_config of ResourceRequirements requests an inference
61896253
# component deployment; otherwise the model is placed directly on the
61906254
# production variant.
@@ -6201,6 +6265,7 @@ def _deploy_model_customization(
62016265
endpoint_name=endpoint_name,
62026266
initial_instance_count=initial_instance_count,
62036267
wait=kwargs.get("wait", True),
6268+
tags=endpoint_tags,
62046269
)
62056270

62066271
# The model package may be absent (e.g. a Nova CPTTrainer or raw-S3
@@ -6226,7 +6291,9 @@ def _deploy_model_customization(
62266291
],
62276292
)
62286293
endpoint = Endpoint.create(
6229-
endpoint_name=endpoint_name, endpoint_config_name=endpoint_name
6294+
endpoint_name=endpoint_name,
6295+
endpoint_config_name=endpoint_name,
6296+
tags=endpoint_tags or None,
62306297
)
62316298
if kwargs.get("wait", True):
62326299
endpoint.wait_for_status("InService")
@@ -6266,10 +6333,6 @@ def _deploy_model_customization(
62666333
# tag) to the endpoint so it is discoverable. Stored tags are in
62676334
# {"Key":..,"Value":..} form; normalize to the key/value form the
62686335
# core resource expects.
6269-
endpoint_tags = [
6270-
{"key": tag["Key"], "value": tag["Value"]}
6271-
for tag in format_tags(getattr(self, "_tags", None) or [])
6272-
]
62736336
endpoint = Endpoint.create(
62746337
endpoint_name=endpoint_name,
62756338
endpoint_config_name=endpoint_name,
@@ -6500,6 +6563,7 @@ def _deploy_nova_model(
65006563
endpoint_name: str,
65016564
initial_instance_count: int = 1,
65026565
wait: bool = True,
6566+
tags: Optional[Tags] = None,
65036567
) -> Endpoint:
65046568
"""Deploy a Nova model directly to an endpoint without inference components.
65056569
@@ -6528,29 +6592,28 @@ def _deploy_nova_model(
65286592
# The jumpstart-model-id tag always applies (resolved from the model
65296593
# package or the trainer's base_model_name). The recipe-name tag is only
65306594
# available when a model package is present.
6531-
tags = [
6595+
nova_tags = [
65326596
{"key": "sagemaker-sdk:jumpstart-model-id", "value": self._base_model_name()},
65336597
]
65346598
model_package = self._fetch_model_package()
65356599
if model_package is not None:
65366600
base_model = model_package.inference_specification.containers[0].base_model
65376601
if base_model is not None and base_model.recipe_name:
6538-
tags.append({"key": "sagemaker-sdk:recipe-name", "value": base_model.recipe_name})
6539-
6540-
# Merge tags accumulated via add_tags (e.g. the model-source reuse tag).
6541-
# Those are stored in {"Key": ..., "Value": ...} form, so normalize to the
6542-
# {"key": ..., "value": ...} form Endpoint.create expects and de-duplicate.
6543-
existing_keys = {tag["key"] for tag in tags}
6544-
for tag in format_tags(getattr(self, "_tags", None) or []):
6545-
key = tag["Key"]
6546-
if key not in existing_keys:
6547-
tags.append({"key": key, "value": tag["Value"]})
6548-
existing_keys.add(key)
6602+
nova_tags.append(
6603+
{"key": "sagemaker-sdk:recipe-name", "value": base_model.recipe_name}
6604+
)
6605+
6606+
# ``tags`` carries the tags resolved by the caller: those ModelBuilder
6607+
# manages plus any passed to deploy(). When this method is called directly,
6608+
# fall back to the accumulator so its tags are still applied.
6609+
endpoint_tags = _merge_tags(
6610+
nova_tags, tags if tags is not None else getattr(self, "_tags", None)
6611+
)
65496612

65506613
endpoint = Endpoint.create(
65516614
endpoint_name=endpoint_name,
65526615
endpoint_config_name=endpoint_name,
6553-
tags=tags,
6616+
tags=endpoint_tags,
65546617
)
65556618

65566619
if wait:

‎sagemaker-serve/tests/unit/test_model_builder_deploy.py‎

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from unittest.mock import Mock, patch, MagicMock, call
88
import tempfile
99

10-
from sagemaker.serve.model_builder import ModelBuilder
10+
from sagemaker.serve.model_builder import ModelBuilder, _tags_as_key_value_list
1111
from sagemaker.serve.utils.types import ModelServer
1212
from sagemaker.serve.mode.function_pointers import Mode
1313
from sagemaker.serve.constants import Framework
@@ -602,3 +602,168 @@ def test_reset_build_state_clears_upload_state(self):
602602

603603
if __name__ == "__main__":
604604
unittest.main()
605+
606+
607+
class TestTagsAsKeyValueList(unittest.TestCase):
608+
"""Tests for the tag normalization used by the model-customization deploy paths."""
609+
610+
def test_lowercase_list_passes_through(self):
611+
self.assertEqual(
612+
_tags_as_key_value_list([{"key": "a", "value": "b"}]), [{"key": "a", "value": "b"}]
613+
)
614+
615+
def test_capitalized_list_is_lowercased(self):
616+
self.assertEqual(
617+
_tags_as_key_value_list([{"Key": "a", "Value": "b"}]), [{"key": "a", "value": "b"}]
618+
)
619+
620+
def test_bare_mapping_is_expanded(self):
621+
self.assertEqual(_tags_as_key_value_list({"a": "b"}), [{"key": "a", "value": "b"}])
622+
623+
def test_none_and_empty_yield_empty_list(self):
624+
self.assertEqual(_tags_as_key_value_list(None), [])
625+
self.assertEqual(_tags_as_key_value_list([]), [])
626+
627+
def test_empty_value_is_kept(self):
628+
"""Tag.Value has a minimum length of 0, so an empty value is a legal tag."""
629+
self.assertEqual(
630+
_tags_as_key_value_list([{"key": "a", "value": ""}]), [{"key": "a", "value": ""}]
631+
)
632+
633+
def test_entry_missing_key_or_value_is_skipped(self):
634+
self.assertEqual(
635+
_tags_as_key_value_list([{"key": "a"}, {"value": "b"}, {"key": "c", "value": "d"}]),
636+
[{"key": "c", "value": "d"}],
637+
)
638+
639+
def test_output_validates_as_core_tag(self):
640+
"""The output must satisfy the List[Tag] shape the create() calls are typed with."""
641+
from pydantic import TypeAdapter
642+
from typing import List as TypingList
643+
from sagemaker.core.shapes import Tag
644+
645+
coerced = TypeAdapter(TypingList[Tag]).validate_python(
646+
_tags_as_key_value_list([{"Key": "a", "Value": "b"}])
647+
)
648+
self.assertEqual([(t.key, t.value) for t in coerced], [("a", "b")])
649+
650+
651+
class TestDeployNovaModelTags(unittest.TestCase):
652+
"""Tag propagation through the Nova model-customization deploy path.
653+
654+
``deploy(tags=...)`` previously never reached ``self._tags`` on the
655+
model-customization paths, so user tags were dropped before Endpoint.create.
656+
"""
657+
658+
PROJECT_TAG = {"key": "sagemaker:project-id", "value": "p-12345"}
659+
660+
def _make_builder(self, tags=None):
661+
builder = ModelBuilder.__new__(ModelBuilder)
662+
builder.instance_type = "ml.g5.12xlarge"
663+
builder.built_model = MagicMock(model_name="model-1")
664+
builder._tags = None
665+
base_model = MagicMock(hub_content_name="amazon-nova-lite-v1", recipe_name="nova-sft")
666+
package = MagicMock()
667+
package.inference_specification.containers = [MagicMock(base_model=base_model)]
668+
builder._fetch_model_package = MagicMock(return_value=package)
669+
builder._base_model_name = MagicMock(return_value="amazon-nova-lite-v1")
670+
builder._is_raw_s3_model = MagicMock(return_value=False)
671+
if tags:
672+
builder.add_tags(_tags_as_key_value_list(tags))
673+
return builder
674+
675+
def _created_tags(self, builder):
676+
with patch("sagemaker.serve.model_builder.EndpointConfig"), patch(
677+
"sagemaker.serve.model_builder.Endpoint"
678+
) as mock_endpoint:
679+
mock_endpoint.create.return_value = MagicMock()
680+
builder._deploy_nova_model(endpoint_name="endpoint-1", wait=False)
681+
return mock_endpoint.create.call_args.kwargs["tags"]
682+
683+
def test_user_tags_merged_with_jumpstart_tags(self):
684+
tags = self._created_tags(self._make_builder(tags=[self.PROJECT_TAG]))
685+
self.assertIn(self.PROJECT_TAG, tags)
686+
self.assertTrue(any("jumpstart-model-id" in tag["key"] for tag in tags))
687+
688+
def test_capitalized_user_tags_accepted(self):
689+
builder = self._make_builder(tags=[{"Key": "sagemaker:project-id", "Value": "p-12345"}])
690+
self.assertIn(self.PROJECT_TAG, self._created_tags(builder))
691+
692+
def test_no_user_tags_leaves_jumpstart_tags_only(self):
693+
tags = self._created_tags(self._make_builder())
694+
self.assertEqual(len(tags), 2)
695+
self.assertTrue(all(tag["key"].startswith("sagemaker-sdk:") for tag in tags))
696+
697+
def test_deploy_tags_reach_the_endpoint_through_model_customization(self):
698+
"""Covers the full chain: deploy(tags=...) -> kwargs -> Endpoint.create.
699+
700+
_deploy_model_customization() is the entry point the customization deploy path
701+
takes, and it is where the deploy-time tags are resolved for every endpoint
702+
branch below it.
703+
"""
704+
builder = self._make_builder()
705+
builder._is_nova_model = MagicMock(return_value=True)
706+
707+
with patch("sagemaker.serve.model_builder.EndpointConfig"), patch(
708+
"sagemaker.serve.model_builder.Endpoint"
709+
) as mock_endpoint:
710+
mock_endpoint.create.return_value = MagicMock()
711+
builder._deploy_model_customization(
712+
endpoint_name="endpoint-1", wait=False, tags=[self.PROJECT_TAG]
713+
)
714+
tags = mock_endpoint.create.call_args.kwargs["tags"]
715+
716+
self.assertIn(self.PROJECT_TAG, tags)
717+
self.assertTrue(any("jumpstart-model-id" in tag["key"] for tag in tags))
718+
719+
def test_deploy_tags_merge_with_tags_already_on_the_builder(self):
720+
"""The realistic build()-then-deploy() flow: self._tags is already populated.
721+
722+
build() leaves capitalized entries on self._tags (JumpStart tags) and can leave
723+
Tag objects there for trainer-backed models, so the deploy-time merge has to cope
724+
with a non-empty accumulator in a different form than the caller's tags.
725+
"""
726+
from sagemaker.core.shapes import Tag as CoreTag
727+
728+
builder = self._make_builder()
729+
builder._is_nova_model = MagicMock(return_value=True)
730+
builder._tags = [
731+
{"Key": "sagemaker-sdk:jumpstart-model-id", "Value": "llama"},
732+
CoreTag(key="from-trainer", value="yes"),
733+
]
734+
735+
with patch("sagemaker.serve.model_builder.EndpointConfig"), patch(
736+
"sagemaker.serve.model_builder.Endpoint"
737+
) as mock_endpoint:
738+
mock_endpoint.create.return_value = MagicMock()
739+
builder._deploy_model_customization(
740+
endpoint_name="endpoint-1", wait=False, tags=[self.PROJECT_TAG]
741+
)
742+
tags = mock_endpoint.create.call_args.kwargs["tags"]
743+
744+
self.assertIn(self.PROJECT_TAG, tags)
745+
self.assertIn({"key": "from-trainer", "value": "yes"}, tags)
746+
self.assertTrue(any(tag["key"] == "sagemaker-sdk:jumpstart-model-id" for tag in tags))
747+
748+
def test_a_managed_tag_is_not_displaced_by_a_colliding_deploy_tag(self):
749+
"""SageMaker rejects duplicate keys, so the tag the SDK relies on must win."""
750+
builder = self._make_builder()
751+
builder._is_nova_model = MagicMock(return_value=True)
752+
builder._tags = [{"Key": "sagemaker-sdk:jumpstart-model-id", "Value": "managed"}]
753+
754+
with patch("sagemaker.serve.model_builder.EndpointConfig"), patch(
755+
"sagemaker.serve.model_builder.Endpoint"
756+
) as mock_endpoint:
757+
mock_endpoint.create.return_value = MagicMock()
758+
builder._deploy_model_customization(
759+
endpoint_name="endpoint-1",
760+
wait=False,
761+
tags=[{"key": "sagemaker-sdk:jumpstart-model-id", "value": "user-override"}],
762+
)
763+
tags = mock_endpoint.create.call_args.kwargs["tags"]
764+
765+
keys = [tag["key"] for tag in tags]
766+
self.assertEqual(keys.count("sagemaker-sdk:jumpstart-model-id"), 1)
767+
self.assertNotIn(
768+
{"key": "sagemaker-sdk:jumpstart-model-id", "value": "user-override"}, tags
769+
)

0 commit comments

Comments
 (0)