Skip to content

feature: make estimator accept json file as modelparallel config #3265

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 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions src/sagemaker/fw_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Utility methods used by framework classes"""
from __future__ import absolute_import

import json
import logging
import os
import re
Expand Down Expand Up @@ -234,6 +235,41 @@ def validate_source_code_input_against_pipeline_variables(
)


def parse_mp_parameters(params):
"""Parse the model parallelism parameters provided by the user.

Args:
params: a string representing path to an existing config, or
a config dict.

Returns:
parsed: a dict of parsed config.

Raises:
ValueError: if params is not a string or a dict, or
the config file cannot be parsed as json.
"""
parsed = None
if isinstance(params, dict):
parsed = params
elif os.path.exists(params):
try:
with open(params, "r") as fp:
parsed = json.load(fp)
except json.decoder.JSONDecodeError:
pass
else:
raise ValueError(
f"Expected a string path to an existing modelparallel config, or a dictionary. "
f"Received: {params}."
)

if parsed is None:
raise ValueError(f"Cannot parse {params} as a json file.")

return parsed


def get_mp_parameters(distribution):
"""Get the model parallelism parameters provided by the user.

Expand All @@ -250,6 +286,7 @@ def get_mp_parameters(distribution):
mp_dict = {}
if mp_dict.get("enabled", False) is True:
params = mp_dict.get("parameters", {})
params = parse_mp_parameters(params)
validate_mp_config(params)
return params
return None
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_fw_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import absolute_import

import inspect
import json
import os
import tarfile
from contextlib import contextmanager
Expand Down Expand Up @@ -192,6 +193,43 @@ def test_validate_source_dir_file_not_in_dir():
fw_utils.validate_source_dir(script, directory)


def test_parse_mp_parameters_input_dict():
mp_parameters = {
"partitions": 1,
"tensor_parallel_degree": 2,
"microbatches": 1,
"optimize": "speed",
"pipeline": "interleaved",
"ddp": 1,
"auto_partition": False,
"default_partition": 0,
}
assert mp_parameters == fw_utils.parse_mp_parameters(mp_parameters)


def test_parse_mp_parameters_input_str_json():
mp_parameters = {
"partitions": 1,
"tensor_parallel_degree": 2,
"microbatches": 1,
"optimize": "speed",
"pipeline": "interleaved",
"ddp": 1,
"auto_partition": False,
"default_partition": 0,
}
json_file_path = "./params.json"
with open(json_file_path, "x") as fp:
json.dump(mp_parameters, fp)
assert mp_parameters == fw_utils.parse_mp_parameters(json_file_path)
os.remove(json_file_path)


def test_parse_mp_parameters_input_not_exit():
with pytest.raises(ValueError):
fw_utils.parse_mp_parameters(" !@#$%^&*()path probably in not there.!@#$%^&*()")


def test_tar_and_upload_dir_not_s3(sagemaker_session):
bucket = "mybucket"
s3_key_prefix = "something/source"
Expand Down