Skip to content

Commit 5cd25f3

Browse files
staubhpPayton Staubahsan-z-khan
authored andcommitted
fix: Prevent repack_model script from referencing nonexistent directories (#2755)
Co-authored-by: Payton Staub <[email protected]> Co-authored-by: Ahsan Khan <[email protected]>
1 parent b689689 commit 5cd25f3

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/sagemaker/workflow/_repack_model.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ def repack(inference_script, model_archive, dependencies=None, source_dir=None):
6262
with tarfile.open(name=local_path, mode="r:gz") as tf:
6363
tf.extractall(path=src_dir)
6464

65-
# copy the custom inference script to code/
66-
entry_point = os.path.join("/opt/ml/code", inference_script)
67-
shutil.copy2(entry_point, os.path.join(src_dir, "code", inference_script))
68-
69-
# copy source_dir to code/
7065
if source_dir:
66+
# copy /opt/ml/code to code/
7167
if os.path.exists(code_dir):
7268
shutil.rmtree(code_dir)
73-
shutil.copytree(source_dir, code_dir)
69+
shutil.copytree("/opt/ml/code", code_dir)
70+
else:
71+
# copy the custom inference script to code/
72+
entry_point = os.path.join("/opt/ml/code", inference_script)
73+
shutil.copy2(entry_point, os.path.join(code_dir, inference_script))
7474

7575
# copy any dependencies to code/lib/
7676
if dependencies:
@@ -79,13 +79,16 @@ def repack(inference_script, model_archive, dependencies=None, source_dir=None):
7979
lib_dir = os.path.join(code_dir, "lib")
8080
if not os.path.exists(lib_dir):
8181
os.mkdir(lib_dir)
82-
if os.path.isdir(actual_dependency_path):
83-
shutil.copytree(
84-
actual_dependency_path,
85-
os.path.join(lib_dir, os.path.basename(actual_dependency_path)),
86-
)
87-
else:
82+
if os.path.isfile(actual_dependency_path):
8883
shutil.copy2(actual_dependency_path, lib_dir)
84+
else:
85+
if os.path.exists(lib_dir):
86+
shutil.rmtree(lib_dir)
87+
# a directory is in the dependencies. we have to copy
88+
# all of /opt/ml/code into the lib dir because the original directory
89+
# was flattened by the SDK training job upload..
90+
shutil.copytree("/opt/ml/code", lib_dir)
91+
break
8992

9093
# copy the "src" dir, which includes the previous training job's model and the
9194
# custom inference script, to the output of this training job

tests/unit/sagemaker/workflow/test_repack_model_script.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_repack_with_dependencies(tmp):
9494
_repack_model.repack(
9595
inference_script="inference.py",
9696
model_archive=model_tar_name,
97-
dependencies=["dependencies/a", "bb", "dependencies/some/dir"],
97+
dependencies="dependencies/a bb dependencies/some/dir",
9898
)
9999

100100
# /opt/ml/model should now have the original model and the inference script
@@ -145,7 +145,7 @@ def test_repack_with_source_dir_and_dependencies(tmp):
145145
_repack_model.repack(
146146
inference_script="inference.py",
147147
model_archive=model_tar_name,
148-
dependencies=["dependencies/a", "bb", "dependencies/some/dir"],
148+
dependencies="dependencies/a bb dependencies/some/dir",
149149
source_dir="sourcedir",
150150
)
151151

0 commit comments

Comments
 (0)