Skip to content

fix: repack model function works without source directory #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ def repack_model(inference_script, source_directory, model_uri, sagemaker_sessio

dirname = source_directory if source_directory else os.path.dirname(inference_script)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope of this PR, but I want to know what the expected behavior should be for the following parameter:

If one specifies:

inference_script = 'path/to/inference.py'
source_dir = None

should we be copying in the entire leaf directory to the path of the script or just the script?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


shutil.copytree(dirname, code_dir)
if dirname:
shutil.copytree(dirname, code_dir)
else:
os.mkdir(code_dir)
shutil.copy2(inference_script, code_dir)

with tarfile.open(new_model_path, mode='w:gz') as t:
t.add(tmp_model_dir, arcname=os.path.sep)
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,41 @@ def test_repack_model_without_source_dir(tmpdir):
assert re.match(r'^s3://fake/model-\d+-\d+.tar.gz$', new_model_uri)


def test_repack_model_with_entry_point_without_path_without_source_dir(tmpdir):

tmp = str(tmpdir)

model_path = os.path.join(tmp, 'model')
write_file(model_path, 'model data')

source_dir = os.path.join(tmp, 'source-dir')
os.mkdir(source_dir)
script_path = os.path.join(source_dir, 'inference.py')
write_file(script_path, 'inference script')

contents = [model_path]

sagemaker_session = MagicMock()
mock_s3_model_tar(contents, sagemaker_session, tmp)
fake_upload_path = mock_s3_upload(sagemaker_session, tmp)

model_uri = 's3://fake/location'

cwd = os.getcwd()
try:
os.chdir(source_dir)

new_model_uri = sagemaker.utils.repack_model('inference.py',
None,
model_uri,
sagemaker_session)
finally:
os.chdir(cwd)

assert list_tar_files(fake_upload_path, tmpdir) == {'/code/inference.py', '/model'}
assert re.match(r'^s3://fake/model-\d+-\d+.tar.gz$', new_model_uri)


def test_repack_model_from_s3_saved_model_to_s3(tmpdir):

tmp = str(tmpdir)
Expand Down