Skip to content

fix: estimator hyperparameters in script mode #3344

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 7 commits into from
Sep 12, 2022
12 changes: 10 additions & 2 deletions src/sagemaker/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,8 @@ def __init__(
**kwargs,
)

self.set_hyperparameters(**self._hyperparameters)

def training_image_uri(self):
"""Returns the docker image to use for training.

Expand All @@ -2644,9 +2646,15 @@ def set_hyperparameters(self, **kwargs):
training code on SageMaker. For convenience, this accepts other types
for keys and values, but ``str()`` will be called to convert them before
training.

If a source directory is specified, this method escapes the dict argument as JSON,
and updates the private hyperparameter attribute.
"""
for k, v in kwargs.items():
self._hyperparameters[k] = v
if self.source_dir:
Copy link
Member

Choose a reason for hiding this comment

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

is this a strict contract between source_dir and use of _json_encode_hyperparameters?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. If we always encode the hyper parameters, some unit tests will fail.

self._hyperparameters.update(EstimatorBase._json_encode_hyperparameters(kwargs))
else:
for k, v in kwargs.items():
self._hyperparameters[k] = v

def hyperparameters(self):
"""Returns the hyperparameters as a dictionary to use for training.
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4394,3 +4394,51 @@ def test_insert_invalid_source_code_args():
assert (
"The entry_point should not be a pipeline variable " "when source_dir is a local path"
) in str(err.value)


@patch("time.time", return_value=TIME)
@patch("sagemaker.estimator.tar_and_upload_dir")
@patch("sagemaker.model.Model._upload_code")
def test_script_mode_estimator_escapes_hyperparameters_as_json(
patched_upload_code, patched_tar_and_upload_dir, sagemaker_session
):
patched_tar_and_upload_dir.return_value = UploadedCode(
s3_prefix="s3://%s/%s" % ("bucket", "key"), script_name="script_name"
)
sagemaker_session.boto_region_name = REGION

instance_type = "ml.p2.xlarge"
instance_count = 1

training_data_uri = "s3://bucket/mydata"

jumpstart_source_dir = f"s3://{list(JUMPSTART_BUCKET_NAME_SET)[0]}/source_dirs/source.tar.gz"

hyperparameters = {
"int_hyperparam": 1,
"string_hyperparam": "hello",
"stringified_numeric_hyperparam": "44",
"float_hyperparam": 1.234,
}

generic_estimator = Estimator(
entry_point=SCRIPT_PATH,
role=ROLE,
region=REGION,
sagemaker_session=sagemaker_session,
instance_count=instance_count,
instance_type=instance_type,
source_dir=jumpstart_source_dir,
image_uri=IMAGE_URI,
model_uri=MODEL_DATA,
hyperparameters=hyperparameters,
)
generic_estimator.fit(training_data_uri)

formatted_hyperparams = EstimatorBase._json_encode_hyperparameters(hyperparameters)

assert (
set(formatted_hyperparams.items())
- set(sagemaker_session.train.call_args_list[0][1]["hyperparameters"].items())
== set()
)