Repository navigation
Conversation
|
|
About @jesterhazy considerations:
def train(chanel_dirs, hps):
return {'trained': True, 'saved': False}
|
|
i was looking at |
|
@jesterhazy |
iquintero
left a comment
There was a problem hiding this comment.
the overall design makes sense to me, just have some comments.
|
|
||
|
|
||
| class Training(object): | ||
| def __init__(self, train_fn=None, module_name=None): |
There was a problem hiding this comment.
train_fn and module_name are mutually exclusive. (As inferred from the existance of from_train_fn() and from_module(). This class needs to throw an exception if it is manually constructed and passed both.
There was a problem hiding this comment.
@iquintero, I refactored the class to cover that scenario: https://github.com/mvsusp/sagemaker-container-support/pull/3/files#diff-35452821e8fa0a4ef79ea98492563a4aR35
Please, let me know your thoughts.
| raise e | ||
|
|
||
| def import_user_module(self, code_dir, user_script='user_script'): | ||
| return importlib.import_module(user_script) |
There was a problem hiding this comment.
code_dir is not used, but it probably should right?
There was a problem hiding this comment.
refactored out this function: https://github.com/mvsusp/sagemaker-container-support/pull/3/files#diff-35452821e8fa0a4ef79ea98492563a4aR81
Let me know your thoughts.
| @property | ||
| def input_config_dir(self): | ||
| """The directory where standard SageMaker configuration files are located.""" | ||
| return os.path.join(self.base_dir, 'input/config') |
There was a problem hiding this comment.
I would rewrite this as
os.path.join(self.base_dir, 'input', 'config')while I think os.path.join() will figure out the / in your string correctly (even on Windows), Windows only users can be confused as to why input/config is a path.
|
|
||
| @property | ||
| def model_dir(self): | ||
| return os.path.join(self.base_dir, 'model') |
There was a problem hiding this comment.
Not a big fan of having the getter function of the property calculate its value every time. Can we move to having:
def __init__(self):
self._model_dir = os.path.join(self.base_dir, 'model')
# ... other stuff here
@property
def model_dir(self):
return self._model_dirIf someone else is strongly opposed to this Im fine discussing it though
There was a problem hiding this comment.
moved all logic to the constructor.
| return tmpdir | ||
|
|
||
|
|
||
| def test_training_from_train_fn(opt_ml, model_file): |
There was a problem hiding this comment.
Add a test to ensure that your class handles both a train_fn and a train_module being passed at the same time.
There was a problem hiding this comment.
refactored the classes so we do not have the edge case anymore.
|
|
||
| self.train_fn(user_module, self.environment) | ||
| else: | ||
| subprocess.Popen(['python', '-m', self.module_name]) |
There was a problem hiding this comment.
this asumes that the module is importing this as a library in order to figure out where to put stuff right? or the user just hardcoded all their paths but we don't care how they do it as long as it works right?
There was a problem hiding this comment.
When the user provides a module, we will validate it before starting the training job.
The module will be saved to the code_dir. When a new Environment instance is created, it adds the code dir to PYTHONPATH.
|
@iquintero thanks for the observations. The code here was simplified for design purposes. All your comments will be addressed in the following PRs with the real code implementation. Or would you prefer to address the changes here? |
|
@iquintero maybe I should not even push this PR since the code is not a real implementation? |
laurenyu
left a comment
There was a problem hiding this comment.
are you planning a future PR that fleshes out the documentation in the README? (I'm guessing the current proposed README is just for showing what you're thinking)
also, my usual comments:
- capitalize "Python"
- import statements - I usually just ctrl/cmd-F on the Python Standard Library list to check what goes in the first group.
| @property | ||
| def code_dir(self): | ||
| """The directory where user-supplied code will be staged.""" | ||
| return os.path.join(self.base_dir, 'code') |
There was a problem hiding this comment.
'model' and 'code' can be constants with 'opt/ml'
There was a problem hiding this comment.
created constants with just the base_names
| self.environment.write_failure_file('Uncaught exception during training: %s' % e) | ||
| raise e | ||
|
|
||
| def import_user_module(self, code_dir, user_script='user_script'): |
There was a problem hiding this comment.
should we be giving a default value to user_script? It seems a little strange to me to presume that, especially if code_dir is required. I'd be more inclined to follow the strategy in PythonSDK, where entry_point is required and source_dir defaults to None
There was a problem hiding this comment.
Yeah, I agree that we should not. Just did that to reduce the prototype amount of lines and focus in the overall design.
|
|
||
| @property | ||
| def channel_dirs(self): | ||
| """""" |
There was a problem hiding this comment.
Why bother including the docstring at this point? 😂
|
@laurenyu I agree with all your points here. I'm planning to not push this PR and just use it to gather design feedback. |
owen-t
left a comment
There was a problem hiding this comment.
Looks good - some of the implementations are missing though - let's get them filled out!
Great stuff!
|
|
||
| import sys | ||
|
|
||
| base_dir = 'opt/ml' |
|
|
||
| import multiprocessing | ||
| import os | ||
|
|
There was a problem hiding this comment.
added flake8 so we won't have these error again.
| @property | ||
| def available_gpus(self): | ||
| """The number of gpus available in the current container.""" | ||
| pass |
There was a problem hiding this comment.
Why not provide a default here?
Try to invoke nvidia-smi (per Nacho's comment, probably not in the getter) and return the result or 0.
|
|
||
|
|
||
| class Training(object): | ||
| def __init__(self, train_fn=None, module_name=None): |
There was a problem hiding this comment.
API docs! Particularly so for this class as it's a little esoteric.
|
|
||
| class Training(object): | ||
| def __init__(self, train_fn=None, module_name=None): | ||
| self.environment = env.TrainingEnvironment() |
There was a problem hiding this comment.
Document the constraints on these arguments
|
|
||
| self.train_fn(user_module, self.environment) | ||
| else: | ||
| subprocess.Popen(['python', '-m', self.module_name]) |
There was a problem hiding this comment.
Popen returns immediately and successfully if the process can be created. Should this be a check_call?
There was a problem hiding this comment.
That's correct @owen-t. Using check call. This functional tests shows the functionality:
https://github.com/mvsusp/sagemaker-container-support/pull/3/files#diff-82c8c20785ab069f8c8d755dc15cb636R49
| try: | ||
| if self.train_fn: | ||
|
|
||
| user_module = self.import_user_module(self.environment.code_dir) |
There was a problem hiding this comment.
Should we run the train_fn in a subprocess as well?
| return os.path.join(self.base_dir, 'output') | ||
|
|
||
| @property | ||
| def resource_config(self): |
There was a problem hiding this comment.
Can't we provide defaults for many of these?
| """""" | ||
| pass | ||
|
|
||
| def load_training_parameters(self, fn): |
There was a problem hiding this comment.
Provide doc strings for these undocumented functions
|
@jesterhazy I update the documentation and examples. Please let me know if you still have any |
|
@laurenyu I've found this flake8 extension that inspects the import order for us, so I stop messing up the import order: https://github.com/mvsusp/sagemaker-container-support/pull/3/files#diff-b91f3d5bd63fcd17221b267e851608e8R52 🎉 🍷 |
|
@jesterhazy @owen-t @iquintero @laurenyu I updated the readme and added tox to this PR.
|
jesterhazy
left a comment
There was a problem hiding this comment.
looks good. added some nits. i think the readme docs will need another full pass once the design is settled.
|
|
||
|
|
||
| # TODO (mvsusp) - create a shortcut decorator to allow get/set outside the class, e.g. env.model_dir = 'x' | ||
| base_dir = BASE_PATH |
There was a problem hiding this comment.
why are we exposing as model var at all? it's already a class attribute.
There was a problem hiding this comment.
It is temporary, just to reduce the size of the PR. But I added a TODO in the top to fix it.
| self.environment = env.TrainingEnvironment() | ||
| self.training_process_fn = training_process_fn | ||
|
|
||
| # TODO(mvsusp) - add update_training_environment_fn to allow frameworks to update the training env. |
There was a problem hiding this comment.
what does update mean? user can just update/attributes to the instance without adding a special function (b/c python)
There was a problem hiding this comment.
for the following scenario:
def framework_train_fn(user_module, training_env):
model = user_module.train(training_env.channel_input_dirs, training_env.hyperparameters)
save_model(model, model_file)
def update_training_environment_fn(training_env):
training_env.world_size = len(training_env.hosts * training_env.num_gpus)
training_env.rank = 2
return training_env
Training.from_train_fn(train_fn=framework_train_fn,
environment_fn=update_training_environment_fn)Put it is outside the scope this PR. We can discuss implementation details later.
|
|
||
|
|
||
| def write_user_module(tmpdir, code): | ||
| customer_module = tmpdir.mkdir('code', 'customer_module') |
There was a problem hiding this comment.
customer_module*_path* etc.
| ... | ||
|
|
||
| # TODO (mvsusp) - create env.training_data_dir and env.evaluation_data_dir helpers | ||
| parser.add_argument('--training-data-dir', type=str, default=env.training_data_dir) |
There was a problem hiding this comment.
Does this actually work with the code as is right now?
the training_environment module doesn't have training_data_dir, batch_size, etc. fields. You have to create an instance of TrainingEnvironment, right?
It's worth thinking about whether there's value in needing to create an instance of the class. The functions could all just live in the module rather than the class. Is there a strong reason one way or the other currently?
| _PYTHONPATH = 'PYTHONPATH' | ||
|
|
||
|
|
||
| class Training(object): |
There was a problem hiding this comment.
Two questions:
1). Can we design this so we don't lock users into this "framework"? I want to avoid the same problem we had with our original TensorFlow framework design.
What would it look like if framework authors were implementing their train entrypoint using library functions that we provide? How much complexity would remain that we would still want to abstract away, at the cost of flexibility? I'd like to see us design that "API" first, and see some example user code in that style.
We may still want to offer something like this, which enforces a specific workflow for the entirety of training, which lets us ensure that all of our frameworks behave the same way. Even in that case, I'd argue that that should be written purely by calling those library functions.
2). For the "framework", is a class like this the best implementation? Other potential alternatives:
- A function which takes in a function/module
- A class which can be subclassed
Are there reasons for going this direction over the others?
* add container support code (#1) * Add missing test dependencies to setup.py (#2) * Add entry.py as script in setup.py so it can be invoked from our images (#3) * Add data_files to setup.py to copy nginx config correctly + fix install_requires (#4) * Move dependencies from extras_require to install_requires * Copy in change from internal repo: support pip install requirements.txt (#5) * Use package_data instead of data_files to package config files, refer to (#6) * Add missing conf files (accidentally left out of previous commit) (aws#8) * pip install requirements in training container (aws#11) * Use container exit code to exit Trainer process. (aws#9) * Add gunicorn and gevent as setup.py dependencies (aws#13) * Specify version ranges for install_requires dependencies * Add Apache license headers (aws#14) * Add changelog (aws#15) * Bump version to 1.0.0 (aws#16) * Fix year in license headers (aws#18) * Add README description (aws#20) * Update version to 1.0.1 (aws#21) * Read new TRAINING_JOB_NAME environment variable during training (aws#34) * Bump version to 1.0.2 and update Changelog (aws#37) * Move processing of requirements file out to the specific container. (aws#39) * Bump version to 1.0.3 and update Changelog (aws#42) * Change module name to string in __all__ (aws#50) * Hardcode special case for tuning job hyperparameter (aws#54) * Use strip() for more readability (aws#55) This change is a small improvement upon aws#54 * Bump version to 1.0.4 (aws#56)
Amazon SageMaker Containers
SageMaker Containers is the open source library that the SageMaker team uses to create the open source containers
that run on Amazon SageMaker:
You can use SageMaker Containers as a library with helper functions for your training script or to
create your own ML framework container compatible with SageMaker.
Install
Installing from source
pip install -U .Getting started
Using it as a library
sagemaker_containers.training_environmentcontains information about the training container including:hyperparameters, instance information (number of CPUs, GPUs, host name), input data information, environment
variables, etc.
Let's suppose that you want to train the Keras script below in SageMaker:
my_training_script.pyTo be able to train the script above in SageMaker, you need to provide the parameters
--training-data-dir,--batch-size,--model-dir.training_environmentmakes this process very easy:We can use SageMaker Python SDK to submit
my_training_script.pytotrainining using the
TensorFlowestimator:Creating a new ML framework container compatible with SageMaker Training
The core training structure of SageMaker Containers is
Training. When training starts, e.g.training.run(), theTraininginstance executes the following steps:environment variables, and hyperparameters
Let's suppose that we want to create a Keras Container that trains any Keras model provided by the user script.
Let's suppose that to be able to use our Containers, users need to submit a Python script containing a function with
the following signature:
Implementing our Keras Framework Container
Trainingwill install the user provided script as a module, and prepare the training environment for us. All wehave to do is implement a function,
training_process_fn, which implements the training process of our framework.training_process_fntakes as parameters user module and the training environment, and is responsible tosave the trained model in end of training:
#TODO (mvsusp) - complete the example after implementing support for Docker containers.
An example of user provided script for training:
train.py
Submitting a Python script for training using our Keras container
We can use SageMaker Python SDK to submit a scripts to our
containers:
You can look at more information on how to use
SageMaker Python SDKhere: https://github.com/aws/sagemaker-python-sdk#byo-docker-containers-with-sagemaker-estimators.