Skip to content

Commit a7e4ec0

Browse files
committed
fix(serve): repack source_code for image_uri/ModelTrainer builds
ModelBuilder classified any image_uri build with a model artifact and source_code (no model/inference_spec) as passthrough and dropped the inference code, so build() produced a model.tar.gz without code/ and register() yielded a package that could not serve. This regressed the classic v2 Model(model_data, entry_point, source_dir) repack behavior. In _build_for_passthrough(), distinguish a pure image-only deployment from an "image + model artifact + source_code" build. For the latter, keep the source code and bridge model_path to s3_model_data_url so the existing _upload_code(repack=True) path repacks the code into the model artifact, producing a self-contained tarball. Pure-image (Nova/BYOC) passthrough behavior is unchanged. ModelTrainer builds are covered too since they are normalized to model_path before validation. Also pass script_dependencies (a list derived from SourceCode) to repack_model instead of the deprecated self.dependencies auto-detect dict, which repack_model would otherwise iterate as filesystem paths.
1 parent 368a1a9 commit a7e4ec0

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,44 @@ def _build_for_passthrough(self) -> Model:
18791879

18801880
self.secret_key = ""
18811881

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+
18821920
if self.model_path and self.model_path.startswith("s3://"):
18831921
self.s3_upload_path = self.model_path
18841922
else:
@@ -2282,21 +2320,27 @@ def _upload_code(self, key_prefix: str, repack: bool = False) -> None:
22822320
script_name=os.path.basename(self.entry_point),
22832321
)
22842322

2323+
# ``script_dependencies`` (derived from SourceCode.requirements) is a
2324+
# list of paths; ``self.dependencies`` is the deprecated auto-detect
2325+
# dict ({"auto": True}) and must not be passed to repack_model, which
2326+
# iterates dependencies as filesystem paths.
2327+
repack_dependencies = self.script_dependencies or []
2328+
22852329
logger.info(
22862330
"Repacking model artifact (%s), script artifact "
22872331
"(%s), and dependencies (%s) "
22882332
"into single tar.gz file located at %s. "
22892333
"This may take some time depending on model size...",
22902334
self.s3_model_data_url,
22912335
self.source_dir,
2292-
self.dependencies,
2336+
repack_dependencies,
22932337
repacked_model_data,
22942338
)
22952339

22962340
repack_model(
22972341
inference_script=self.entry_point,
22982342
source_directory=self.source_dir,
2299-
dependencies=self.dependencies,
2343+
dependencies=repack_dependencies,
23002344
model_uri=self.s3_model_data_url,
23012345
repacked_model_uri=repacked_model_data,
23022346
sagemaker_session=self.sagemaker_session,

0 commit comments

Comments
 (0)