Skip to content
This repository was archived by the owner on Aug 26, 2020. It is now read-only.

Mvs functions - #36

Merged
mvsusp merged 10 commits into
aws:r2.0from
mvsusp:mvs-functions
May 1, 2018
Merged

mvsusp merged 10 commits into
aws:r2.0from
mvsusp:mvs-functions

Conversation

@mvsusp

@mvsusp mvsusp commented Apr 26, 2018

Copy link
Copy Markdown
Contributor
  • add functions to match env attributes with function parameters

If you didn't have opportunity to review #35.
I'm still handling comments even after closed.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

andremoeller
andremoeller previously approved these changes Apr 30, 2018


def matching_args(fn, dictionary): # type: (function, collections.Mapping) -> dict
"""Given a function fn and a dict dictionary, returns the function arguments that match the dict keys.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I found this a bit confusing on first read-through. Maybe something like:

Given a function fn and a dict dictionary, returns a dictionary whose keys match (by name) the parameters of fn.

(I don't know if that's really better.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i like it better as well. Thanks


def signature(fn): # type: (function) -> ([], [], [])
"""Given a function fn, returns the function args, vargs and kwargs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think an example (with all 3: args, vargs, and kwargs) would be helpful here.

features = np.random.random((10, 1))
labels = np.zeros((10, 1))

create_channel(channel='training', file_name='training_data.npz', data=dict(features=features, labels=labels))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like create_channel, create_training, etc. are being removed (https://github.com/mvsusp/sagemaker-containers/pull/4/files)

Not necessary for this PR, but I'll be curious to see what these look like after.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread src/sagemaker_containers/functions.py Outdated
fn (function): a function

Returns:
([], [], []): a tuple containing the function args, vargs and kwargs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should return NamedTuples instead of regular tuples for all public functions unless we have a strong reason not to, since they still support the functionality of tuples, but gives the user a way to write more readable code if they want by referencing field names instead of tuple indices. An example is in the current matching_args implementation, which currently has:

smc.collections.split_by_criteria(dictionary, set(args))[0]

Which can become:

smc.collections.split_by_criteria(dictionary, set(args)).included (or whatever name we come up with for the field.)

You can see examples of this from the Python standard library, such as inspect.getargspec returning a ArgSpec named tuple.

If we agree on this, we should go back and apply this to the other places in our code where we return tuples (or revisit whether returning tuples at all is the best solution).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@winstonaws great point. Will do that

Comment thread src/sagemaker_containers/functions.py Outdated
"""
if six.PY2:
arg_spec = inspect.getargspec(fn)
return arg_spec.args, arg_spec.varargs, arg_spec.keywords

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Continuing with the above comment - I think we should just return the ArgSpec namedtuple as-is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed.

Comment thread src/sagemaker_containers/functions.py Outdated
arg_spec = inspect.getargspec(fn)
return arg_spec.args, arg_spec.varargs, arg_spec.keywords
elif six.PY3:
sig = inspect.signature(fn)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we should use inspect.signature here if we're throwing away all of the extra information it gives us to return a subset of ArgSpec. We could either just use getargspec for both cases, or use getfullargspec and adapt the return type.

The docs for getfullargspec recommend this as well: https://docs.python.org/3/library/inspect.html#inspect.getfullargspec

"Changed in version 3.6: This method was previously documented as deprecated in favour of signature() in Python 3.5, but that decision has been reversed in order to restore a clearly supported standard interface for single-source Python 2/3 code migrating away from the legacy getargspec() API."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great point, I will do that.

@codecov-io

codecov-io commented May 1, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #36 into r2.0 will increase coverage by 2.44%.
The diff coverage is 95.83%.

Impacted file tree graph

@@            Coverage Diff             @@
##             r2.0      #36      +/-   ##
==========================================
+ Coverage   96.68%   99.13%   +2.44%     
==========================================
  Files           4        5       +1     
  Lines         211      230      +19     
  Branches       13       18       +5     
==========================================
+ Hits          204      228      +24     
+ Misses          5        0       -5     
  Partials        2        2
Impacted Files Coverage Δ
src/sagemaker_containers/environment.py 99.36% <ø> (+3.82%) ⬆️
src/sagemaker_containers/__init__.py 100% <100%> (ø) ⬆️
src/sagemaker_containers/collections.py 100% <100%> (ø) ⬆️
src/sagemaker_containers/functions.py 93.33% <93.33%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b427a68...66257b8. Read the comment docs.


RESOURCE_CONFIG = dict(current_host='algo-1', hosts=['algo-1'])

INPUT_DATA_CONFIG = {'training': {'ContentType': 'trainingContentType',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Might be obvious to you guys, but how to decide which param should be in INPUT_DATA_CONFIG, which should be in HyperParameters? I thought only input data related params will be in INPUT_DATA_CONFIG, but I saw training_data_file in HyperParameters, should it be in INPUT_DATA_CONFIG?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I see

Comment thread test/functional/test_keras_framework.py Outdated
'S3DistributionType': 'FullyReplicated',
'RecordWrapperType': 'None'}}

hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what is the naming rule in the team? Why not HYPER_PARAMETERS like others?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We follow PEP 08 for style guide and naming conventions: https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds

hyperparameters is named like this for historical reasons, which matches the second rule:

To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But shouldn't it be capitalized for that exact reason?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It should! Good call. Renamed to HYPERPARAMETERS

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks

Comment thread src/sagemaker_containers/collections.py Outdated
import collections

def split_by_criteria(dictionary, keys): # type: (dict, set) -> (dict, dict)
SplitByCriteriaSpec = collections.namedtuple('SplitByCriteriaSpec', 'included not_included')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about "SplitResult" instead?

And "not_included" should be "excluded".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, I vote us to put the suffix Spec in all return namedtuples, which is the same convention that stdlib and tensorflow follow.

So I will rename it to SplitResultSpec

assert smc.functions.getargspec(fn) == expected


@pytest.mark.parametrize('fn, environment, expected', [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's add some cases to fully define the behavior of the function (even though they arent new branches):

  • A fn which has both regular args and kwargs
  • A fn which has regular args which aren't in the environment

Comment thread src/sagemaker_containers/functions.py Outdated
if arg_spec.keywords:
return dictionary

return smc.collections.split_by_criteria(dictionary, set(arg_spec.args)).included

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is set() needed here? It's not possible for a function to have duplicate argument names, is it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It could be a list but split_by_criteria only is receiving sets for now. I will change it to accept lists as well.

Comment thread setup.py Outdated

extras_require={
'test': ['tox', 'flake8', 'pytest', 'pytest-cov', 'mock', 'sagemaker']
'test': ['tox', 'flake8', 'pytest', 'pytest-cov', 'mock', 'sagemaker', 'keras', 'tensorflow', 'numpy']

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shouldn't this package be agnostic of any frameworks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is, but the tests need to be tested with a framework. So I picked Keras.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why?
Can it be just any user script?
It's not like it shouldn't work if the user script or container are not related to machine learning at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with you. We can add other examples that are not machine learning related.
But I would ask why not using a machine learning specific example.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional dependency that we don't really need, can be slower than just a fake machine learning script, implies necessity to know keras to anyone who wants to edit this package (it's going to be open sourced after all)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I like the idea to use a real framework in the tests. And code snippets of different frameworks in the documentation.

I agree that I will require longer to install the dependencies, but the required understand of Keras to understand the tests is extremely minimal.

I proposed us to keep the way it is for now. I am happy to change it if evidences that is not a good approach appear or more engineer agree with @nadiaya suggest approach. Please, let me know if that work for you, @nadiaya

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In addition to the points Nadia brought up, I'd also argue that including Keras distracts from the purpose of the test - the test is just for determining if the container code is interacting correctly with the user script. What if there's a bug introduced in Keras (especially since we're not freezing on a specific version of keras here) - should our tests be dependent on that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Keras is frozen.

Comment thread .coveragerc_py27
pragma: py2 no cover
if six.PY2
elif six.PY2
if six.PY3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should it really be 'six.PY3' in both places?
just curious: how does this logic works?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it is a string match. Any code branch that starts with if six.PY3 will be removed from python 2 coverage checking.

USER_SCRIPT = """
import os

import keras

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why do we need to test it for a specific framework?
I don't think we have or should have any keras functionality in the container.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

SageMaker containers is independent of any specific framework. A framework is just needed for testing. These tests don't run in any container as well.

Comment thread test/functional/test_keras_framework.py Outdated
'S3DistributionType': 'FullyReplicated',
'RecordWrapperType': 'None'}}

hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shouldn't region be configurable or we are running this test purely locally?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need to test regions here. There is no container running in this tests.

Comment thread test/functional/test_keras_framework.py Outdated
'S3DistributionType': 'FullyReplicated',
'RecordWrapperType': 'None'}}

hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But shouldn't it be capitalized for that exact reason?

winstonaws
winstonaws previously approved these changes May 1, 2018
winstonaws
winstonaws previously approved these changes May 1, 2018
Comment thread test/functional/test_keras_framework.py Outdated
'S3DistributionType': 'FullyReplicated',
'RecordWrapperType': 'None'}}

hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks

@mvsusp
mvsusp merged commit 6265d40 into aws:r2.0 May 1, 2018
@mvsusp mvsusp mentioned this pull request May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants