-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: repack_model support dependencies and code location #821
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
Changes from 5 commits
6fed597
2861285
53fa83a
aa4056f
fa31ff1
0c4face
f3a779b
f2713d7
6ed1f5a
0e83796
89b72a5
bb97d64
797bfd6
89504b8
79efec2
89afc9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from __future__ import absolute_import | ||
|
||
import logging | ||
import os | ||
|
||
import sagemaker | ||
from sagemaker.content_types import CONTENT_TYPE_JSON | ||
|
@@ -128,10 +129,17 @@ def prepare_container_def(self, instance_type, accelerator_type=None): | |
env = self._get_container_env() | ||
|
||
if self.entry_point: | ||
model_data = sagemaker.utils.repack_model(self.entry_point, | ||
self.source_dir, | ||
self.model_data, | ||
self.sagemaker_session) | ||
key_prefix = sagemaker.fw_utils.model_code_key_prefix(self.key_prefix, self.name, image) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see there are unit tests added in test_mxnet, but do we need to have unit test for serving.py and model.py class as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, I will do it. |
||
|
||
bucket = self.bucket or self.sagemaker_session.default_bucket() | ||
model_data = 's3://' + os.path.join(bucket, key_prefix, 'model.tar.gz') | ||
|
||
sagemaker.utils.repack_model(self.entry_point, | ||
self.source_dir, | ||
self.dependencies, | ||
self.model_data, | ||
model_data, | ||
self.sagemaker_session) | ||
else: | ||
model_data = self.model_data | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,6 @@ | |
|
||
import six | ||
|
||
import sagemaker | ||
|
||
ECR_URI_PATTERN = r'^(\d+)(\.)dkr(\.)ecr(\.)(.+)(\.)(amazonaws.com|c2s.ic.gov)(/)(.*:.*)$' | ||
|
||
|
||
|
@@ -300,7 +298,12 @@ def _tmpdir(suffix='', prefix='tmp'): | |
shutil.rmtree(tmp) | ||
|
||
|
||
def repack_model(inference_script, source_directory, model_uri, sagemaker_session): | ||
def repack_model(inference_script, | ||
source_directory, | ||
dependencies, | ||
model_uri, | ||
repacked_model_uri, | ||
sagemaker_session): | ||
"""Unpack model tarball and creates a new model tarball with the provided code script. | ||
|
||
This function does the following: | ||
|
@@ -311,60 +314,90 @@ def repack_model(inference_script, source_directory, model_uri, sagemaker_sessio | |
Args: | ||
inference_script (str): path or basename of the inference script that will be packed into the model | ||
source_directory (str): path including all the files that will be packed into the model | ||
dependencies (list[str]): A list of paths to directories (absolute or relative) with | ||
any additional libraries that will be exported to the container (default: []). | ||
The library folders will be copied to SageMaker in the same folder where the entrypoint is copied. | ||
Example: | ||
|
||
The following call | ||
>>> Estimator(entry_point='train.py', dependencies=['my/libs/common', 'virtual-env']) | ||
results in the following inside the container: | ||
|
||
>>> $ ls | ||
|
||
>>> opt/ml/code | ||
>>> |------ train.py | ||
>>> |------ common | ||
>>> |------ virtual-env | ||
|
||
repacked_model_uri (str): path or file system location where the new model will be saved | ||
model_uri (str): S3 or file system location of the original model tar | ||
sagemaker_session (:class:`sagemaker.session.Session`): a sagemaker session to interact with S3. | ||
|
||
Returns: | ||
str: path to the new packed model | ||
""" | ||
new_model_name = 'model-%s.tar.gz' % sagemaker.utils.sagemaker_short_timestamp() | ||
dependencies = dependencies or [] | ||
|
||
with _tmpdir() as tmp: | ||
tmp_model_dir = os.path.join(tmp, 'model') | ||
os.mkdir(tmp_model_dir) | ||
model_dir = _extract_model(model_uri, sagemaker_session, tmp) | ||
|
||
model_from_s3 = model_uri.lower().startswith('s3://') | ||
if model_from_s3: | ||
local_model_path = os.path.join(tmp, 'tar_file') | ||
download_file_from_url(model_uri, local_model_path, sagemaker_session) | ||
_update_code(model_dir, inference_script, source_directory, dependencies, sagemaker_session, tmp) | ||
|
||
new_model_path = os.path.join(tmp, new_model_name) | ||
else: | ||
local_model_path = model_uri.replace('file://', '') | ||
new_model_path = os.path.join(os.path.dirname(local_model_path), new_model_name) | ||
tmp_model_path = os.path.join(tmp, 'temp-model.tar.gz') | ||
with tarfile.open(tmp_model_path, mode='w:gz') as t: | ||
t.add(model_dir, arcname=os.path.sep) | ||
|
||
with tarfile.open(name=local_model_path, mode='r:gz') as t: | ||
t.extractall(path=tmp_model_dir) | ||
_save_model(repacked_model_uri, tmp_model_path, sagemaker_session) | ||
|
||
code_dir = os.path.join(tmp_model_dir, 'code') | ||
if os.path.exists(code_dir): | ||
shutil.rmtree(code_dir, ignore_errors=True) | ||
|
||
if source_directory and source_directory.lower().startswith('s3://'): | ||
local_code_path = os.path.join(tmp, 'local_code.tar.gz') | ||
download_file_from_url(source_directory, local_code_path, sagemaker_session) | ||
def _save_model(repacked_model_uri, tmp_model_path, sagemaker_session): | ||
if repacked_model_uri.lower().startswith('s3://'): | ||
url = parse.urlparse(repacked_model_uri) | ||
bucket, key = url.netloc, url.path.lstrip('/') | ||
new_key = key.replace(os.path.basename(key), os.path.basename(repacked_model_uri)) | ||
|
||
with tarfile.open(name=local_code_path, mode='r:gz') as t: | ||
t.extractall(path=code_dir) | ||
sagemaker_session.boto_session.resource('s3').Object(bucket, new_key).upload_file( | ||
tmp_model_path) | ||
else: | ||
shutil.move(tmp_model_path, repacked_model_uri.replace('file://', '')) | ||
|
||
elif source_directory: | ||
shutil.copytree(source_directory, 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) | ||
def _update_code(model_dir, inference_script, source_directory, dependencies, sagemaker_session, tmp): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method name doesn't seem entirely indicative of what is going on. I don't have any suggestions, but this method is mainly copying files to a certain directory and not necessarily updating 'code'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Renamed to |
||
code_dir = os.path.join(model_dir, 'code') | ||
if os.path.exists(code_dir): | ||
shutil.rmtree(code_dir, ignore_errors=True) | ||
if source_directory and source_directory.lower().startswith('s3://'): | ||
local_code_path = os.path.join(tmp, 'local_code.tar.gz') | ||
download_file_from_url(source_directory, local_code_path, sagemaker_session) | ||
|
||
with tarfile.open(name=local_code_path, mode='r:gz') as t: | ||
t.extractall(path=code_dir) | ||
|
||
if model_from_s3: | ||
url = parse.urlparse(model_uri) | ||
bucket, key = url.netloc, url.path.lstrip('/') | ||
new_key = key.replace(os.path.basename(key), new_model_name) | ||
elif source_directory: | ||
shutil.copytree(source_directory, code_dir) | ||
else: | ||
os.mkdir(code_dir) | ||
shutil.copy2(inference_script, code_dir) | ||
|
||
sagemaker_session.boto_session.resource('s3').Object(bucket, new_key).upload_file(new_model_path) | ||
return 's3://%s/%s' % (bucket, new_key) | ||
for dependency in dependencies: | ||
if os.path.isdir(dependency): | ||
shutil.copytree(dependency, code_dir) | ||
else: | ||
return 'file://%s' % new_model_path | ||
shutil.copy2(dependency, code_dir) | ||
|
||
|
||
def _extract_model(model_uri, sagemaker_session, tmp): | ||
tmp_model_dir = os.path.join(tmp, 'model') | ||
os.mkdir(tmp_model_dir) | ||
if model_uri.lower().startswith('s3://'): | ||
local_model_path = os.path.join(tmp, 'tar_file') | ||
download_file_from_url(model_uri, local_model_path, sagemaker_session) | ||
else: | ||
local_model_path = model_uri.replace('file://', '') | ||
with tarfile.open(name=local_model_path, mode='r:gz') as t: | ||
t.extractall(path=tmp_model_dir) | ||
return tmp_model_dir | ||
|
||
|
||
def download_file_from_url(url, dst, sagemaker_session): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this assignment happen after the repack_model function finishes successfully?
This can be a problem in a notebook, where state is stored even with exceptions happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call.