Repository navigation
Mvs functions - #36
Conversation
|
|
||
|
|
||
| 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. |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
i like it better as well. Thanks
|
|
||
| def signature(fn): # type: (function) -> ([], [], []) | ||
| """Given a function fn, returns the function args, vargs and kwargs | ||
|
|
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That PR includes this branch, you can see the code here: https://github.com/mvsusp/sagemaker-containers/blob/924446d2325be7b5b634c9d4ee72e9fc3d49b6c1/test/functional/test_keras_framework.py#L72
| fn (function): a function | ||
|
|
||
| Returns: | ||
| ([], [], []): a tuple containing the function args, vargs and kwargs. |
There was a problem hiding this comment.
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).
| """ | ||
| if six.PY2: | ||
| arg_spec = inspect.getargspec(fn) | ||
| return arg_spec.args, arg_spec.varargs, arg_spec.keywords |
There was a problem hiding this comment.
Continuing with the above comment - I think we should just return the ArgSpec namedtuple as-is.
| arg_spec = inspect.getargspec(fn) | ||
| return arg_spec.args, arg_spec.varargs, arg_spec.keywords | ||
| elif six.PY3: | ||
| sig = inspect.signature(fn) |
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
Great point, I will do that.
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
|
|
||
| RESOURCE_CONFIG = dict(current_host='algo-1', hosts=['algo-1']) | ||
|
|
||
| INPUT_DATA_CONFIG = {'training': {'ContentType': 'trainingContentType', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
inputdataconfig.json is a default file from SageMaker training: https://github.com/aws/sagemaker-containers/blob/r2.0/src/sagemaker_containers/environment.py#L122
The difference of these setting can be find in the docstrings of the environment.py, aws docs (https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo.html#your-algorithms-training-algo-running-container-dist-training)
| 'S3DistributionType': 'FullyReplicated', | ||
| 'RecordWrapperType': 'None'}} | ||
|
|
||
| hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2', |
There was a problem hiding this comment.
what is the naming rule in the team? Why not HYPER_PARAMETERS like others?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
But shouldn't it be capitalized for that exact reason?
There was a problem hiding this comment.
It should! Good call. Renamed to HYPERPARAMETERS
| import collections | ||
|
|
||
| def split_by_criteria(dictionary, keys): # type: (dict, set) -> (dict, dict) | ||
| SplitByCriteriaSpec = collections.namedtuple('SplitByCriteriaSpec', 'included not_included') |
There was a problem hiding this comment.
How about "SplitResult" instead?
And "not_included" should be "excluded".
There was a problem hiding this comment.
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', [ |
There was a problem hiding this comment.
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
| if arg_spec.keywords: | ||
| return dictionary | ||
|
|
||
| return smc.collections.split_by_criteria(dictionary, set(arg_spec.args)).included |
There was a problem hiding this comment.
Is set() needed here? It's not possible for a function to have duplicate argument names, is it?
There was a problem hiding this comment.
It could be a list but split_by_criteria only is receiving sets for now. I will change it to accept lists as well.
|
|
||
| extras_require={ | ||
| 'test': ['tox', 'flake8', 'pytest', 'pytest-cov', 'mock', 'sagemaker'] | ||
| 'test': ['tox', 'flake8', 'pytest', 'pytest-cov', 'mock', 'sagemaker', 'keras', 'tensorflow', 'numpy'] |
There was a problem hiding this comment.
shouldn't this package be agnostic of any frameworks?
There was a problem hiding this comment.
It is, but the tests need to be tested with a framework. So I picked Keras.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
| pragma: py2 no cover | ||
| if six.PY2 | ||
| elif six.PY2 | ||
| if six.PY3 |
There was a problem hiding this comment.
should it really be 'six.PY3' in both places?
just curious: how does this logic works?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 'S3DistributionType': 'FullyReplicated', | ||
| 'RecordWrapperType': 'None'}} | ||
|
|
||
| hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2', |
There was a problem hiding this comment.
shouldn't region be configurable or we are running this test purely locally?
There was a problem hiding this comment.
I don't think we need to test regions here. There is no container running in this tests.
| 'S3DistributionType': 'FullyReplicated', | ||
| 'RecordWrapperType': 'None'}} | ||
|
|
||
| hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2', |
There was a problem hiding this comment.
But shouldn't it be capitalized for that exact reason?
| 'S3DistributionType': 'FullyReplicated', | ||
| 'RecordWrapperType': 'None'}} | ||
|
|
||
| hyperparameters = dict(training_data_file='training_data.npz', sagemaker_region='us-west-2', |
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.