Skip to content

Commit 6daed77

Browse files
committed
fix(serve): honor source_code for image_uri/model-server builds
ModelBuilder classified any image_uri build without an in-memory model or inference_spec as passthrough, ignoring source_code. _build_for_ passthrough() then cleared source_dir/entry_point, so custom inference code was silently dropped: no repack occurred, the container pointed at the raw model artifact, register()/deploy() produced a model with no serving code. Regressed from v2 Model(model_data, entry_point, source_dir) + _RepackModelStep; introduced by #5969 (commit ebeb54c) which added the passthrough nulling. Fix the passthrough classification at its source: do not treat a build as passthrough when source_code is set (both 1P and non-1P image branches). Such builds route to the normal model-server path and repack the code into the artifact. Also: - Relax the _build_for_model_server guard to accept s3_model_data_url (a prebuilt training artifact) alongside model/MLflow/inference_spec. - In _upload_code repack, pass script_dependencies (a list) instead of the deprecated self.dependencies auto-detect dict, which repack_model would otherwise iterate as filesystem paths. - Update _build_for_model_server unit tests to set s3_model_data_url so the missing-parameter cases still validate.
1 parent a7e4ec0 commit 6daed77

3 files changed

Lines changed: 22 additions & 40 deletions

File tree

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

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@ def _build_validations(self) -> None:
18511851
and not self.model
18521852
and not self.inference_spec
18531853
and not getattr(self, "_is_mlflow_model", False)
1854+
and not self.source_code
18541855
):
18551856
self._passthrough = True
18561857
return
@@ -1861,6 +1862,7 @@ def _build_validations(self) -> None:
18611862
and not self.model
18621863
and not self.inference_spec
18631864
and not getattr(self, "_is_mlflow_model", False)
1865+
and not self.source_code
18641866
):
18651867
self._passthrough = True
18661868
return
@@ -1879,44 +1881,6 @@ def _build_for_passthrough(self) -> Model:
18791881

18801882
self.secret_key = ""
18811883

1882-
# Resolve the trained model artifact URI (if any). It may arrive either
1883-
# directly as an S3 ``model_path`` or via ``s3_model_data_url``. A
1884-
# ModelTrainer / TrainingJob is normalized to ``model_path`` earlier in
1885-
# the build flow.
1886-
model_artifact_uri = None
1887-
if self.model_path and str(self.model_path).startswith("s3://"):
1888-
model_artifact_uri = self.model_path
1889-
elif isinstance(self.s3_model_data_url, str) and self.s3_model_data_url.startswith(
1890-
"s3://"
1891-
):
1892-
model_artifact_uri = self.s3_model_data_url
1893-
1894-
has_source_code = bool(
1895-
getattr(self, "entry_point", None) and getattr(self, "source_dir", None)
1896-
)
1897-
1898-
# When custom inference source code is supplied alongside a model
1899-
# artifact, this is not a pure image-only passthrough: honor the
1900-
# source code by repacking it into the model tarball (mirroring the
1901-
# classic v2 ``Model`` + ``_RepackModelStep`` behavior) so that
1902-
# ``build()`` produces a self-contained ``model.tar.gz`` (code under
1903-
# ``code/``) that is safe to ``register()``.
1904-
if has_source_code and model_artifact_uri:
1905-
if not (
1906-
isinstance(self.s3_model_data_url, str)
1907-
and self.s3_model_data_url.startswith("s3://")
1908-
):
1909-
# Bridge model_path -> s3_model_data_url so the repack path in
1910-
# _upload_code(repack=True) can locate the original artifact.
1911-
self.s3_model_data_url = model_artifact_uri
1912-
# Let the repacked artifact drive the container ModelDataUrl.
1913-
self.s3_upload_path = None
1914-
1915-
if self.mode in LOCAL_MODES:
1916-
self._prepare_for_mode()
1917-
1918-
return self._create_model()
1919-
19201884
if self.model_path and self.model_path.startswith("s3://"):
19211885
self.s3_upload_path = self.model_path
19221886
else:

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,20 @@ def _build_for_model_server(self) -> Model:
100100
if self.model_metadata:
101101
mlflow_path = self.model_metadata.get(MLFLOW_MODEL_PATH)
102102

103-
if not self.model and not mlflow_path and not self.inference_spec:
104-
raise ValueError("Missing required parameter: model, MLflow path, or inference_spec")
103+
# A model artifact supplied via s3_model_data_url (e.g. a completed
104+
# training job's artifact) together with source_code is a valid input:
105+
# the code is repacked into the artifact and served by the chosen model
106+
# server. Accept it as sufficient alongside model/MLflow/inference_spec.
107+
if (
108+
not self.model
109+
and not mlflow_path
110+
and not self.inference_spec
111+
and not self.s3_model_data_url
112+
):
113+
raise ValueError(
114+
"Missing required parameter: model, MLflow path, inference_spec, "
115+
"or s3_model_data_url"
116+
)
105117

106118
# Route to appropriate model server builder
107119
if self.model_server == ModelServer.TORCHSERVE:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_build_for_model_server_missing_required_params(self):
3131
mock_builder.model = None
3232
mock_builder.model_metadata = None
3333
mock_builder.inference_spec = None
34+
mock_builder.s3_model_data_url = None
3435

3536
with self.assertRaises(ValueError) as context:
3637
_ModelBuilderServers._build_for_model_server(mock_builder)
@@ -313,6 +314,7 @@ def test_build_for_model_server_with_all_params_none(self):
313314
mock_builder.model = None
314315
mock_builder.model_metadata = {} # Empty dict, no MLFLOW_MODEL_PATH
315316
mock_builder.inference_spec = None
317+
mock_builder.s3_model_data_url = None
316318

317319
with self.assertRaises(ValueError) as context:
318320
_ModelBuilderServers._build_for_model_server(mock_builder)
@@ -328,6 +330,7 @@ def test_build_for_model_server_with_empty_mlflow_metadata(self):
328330
mock_builder.model = None
329331
mock_builder.model_metadata = {"other_key": "value"} # No MLFLOW_MODEL_PATH
330332
mock_builder.inference_spec = None
333+
mock_builder.s3_model_data_url = None
331334

332335
with self.assertRaises(ValueError) as context:
333336
_ModelBuilderServers._build_for_model_server(mock_builder)
@@ -469,6 +472,7 @@ def test_model_metadata_with_none_mlflow_path(self):
469472
mock_builder.model = None
470473
mock_builder.model_metadata = {MLFLOW_MODEL_PATH: None} # Explicitly None
471474
mock_builder.inference_spec = None
475+
mock_builder.s3_model_data_url = None
472476

473477
with self.assertRaises(ValueError) as context:
474478
_ModelBuilderServers._build_for_model_server(mock_builder)
@@ -485,6 +489,7 @@ def test_model_metadata_with_empty_string_mlflow_path(self):
485489
mock_builder.model = None
486490
mock_builder.model_metadata = {MLFLOW_MODEL_PATH: ""} # Empty string
487491
mock_builder.inference_spec = None
492+
mock_builder.s3_model_data_url = None
488493

489494
# Empty string is falsy, so should raise ValueError
490495
with self.assertRaises(ValueError) as context:
@@ -501,6 +506,7 @@ def test_model_as_empty_string_is_falsy(self):
501506
mock_builder.model = "" # Empty string is falsy
502507
mock_builder.model_metadata = None
503508
mock_builder.inference_spec = None
509+
mock_builder.s3_model_data_url = None
504510

505511
with self.assertRaises(ValueError) as context:
506512
_ModelBuilderServers._build_for_model_server(mock_builder)

0 commit comments

Comments
 (0)