Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Infer dtype in SymbolBlock import from input symbol #12412

Merged

Conversation

sandeep-krishnamurthy
Copy link
Contributor

@sandeep-krishnamurthy sandeep-krishnamurthy commented Aug 30, 2018

Description

  • Fix for the issue - gluon.SymbolBlock cannot imports resnet trained with dtype="float16" #11849
  • Currently, Gluon symbol block cannot import any symbol with type other than fp32. All the parameters are created as FP32 leading to failure in importing the params when it is of type fp16, fp64 etc,
  • In this PR, we infer the type of the symbol being imported and create the Symbol Block Parameters with that inferred type.

Created this PR for getting the early feedback. I am working on adding test cases for this and will update the PR soon
Added the tests

@szha @hetong007 @apeforest - Can you please take a look at this?
@Roshrini - I think this is important fix to be picked for 1.3. @szha ?

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • In this PR, we infer the type of the symbol being imported and create the Symbol Block Parameters with that inferred type.
  • If we cannot infer the type, we go by usual route using default dtype (fp32) for creating the parameters.

Comments

Below is the issue:

When you create mx.gluon.SymbolBlock(sm, input). It creates the parameters in the Block. Type is not passed for creating the parameter.
See here - https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/gluon/block.py#L1058 and the behavior is "If there is not parameter to get, it creates one and uses default type (fp32) https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/gluon/parameter.py#L688

@sandeep-krishnamurthy sandeep-krishnamurthy added Gluon pr-awaiting-review PR is waiting for code review pr-work-in-progress PR is still work in progress labels Aug 30, 2018
@sandeep-krishnamurthy sandeep-krishnamurthy changed the title [WIP] Infer dtype in SymbolBlock import from input symbol Infer dtype in SymbolBlock import from input symbol Aug 30, 2018
@sandeep-krishnamurthy sandeep-krishnamurthy removed the pr-work-in-progress PR is still work in progress label Aug 30, 2018
Copy link
Contributor

@apeforest apeforest left a comment

Choose a reason for hiding this comment

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

Left a few small comments. Otherwise looks great. Thanks for the quick fix!

self.params.get(aux, grad_req='null', allow_deferred_init=True, dtype=aux_types[i])
else:
# Use default types for params
for i, arg in enumerate(arg_params):
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe no enumerate is needed since you don't use index i in this loop?

for i in out.list_auxiliary_states():
if i not in input_names:
self.params.get(i, grad_req='null', allow_deferred_init=True)
for i, aux in enumerate(aux_params):
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, no need to get index i

Copy link
Contributor Author

Choose a reason for hiding this comment

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

N/A with latest refactoring.

python/mxnet/gluon/block.py Outdated Show resolved Hide resolved
Copy link
Contributor

@lupesko lupesko left a comment

Choose a reason for hiding this comment

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

Suggest to add a test for float16 as well as float64

ctx = mx.cpu(0)

net_fp32 = mx.gluon.model_zoo.vision.resnet34_v2(pretrained=True, ctx=ctx)
net_fp32.cast('float64')
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we add another test that casts to float16?
The original issue reported import from float16 failing, and it might be appropriate to cover it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test for fp16.

@sandeep-krishnamurthy
Copy link
Contributor Author

@apeforest @szha @lupesko - Thanks for your time. Addressed your comments. Please have a look.

tmpfile = os.path.join(tmp, 'resnet34_fp64')
ctx = mx.cpu(0)

net_fp32 = mx.gluon.model_zoo.vision.resnet34_v2(pretrained=True, ctx=ctx, root=tmp)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the model name in model zoo going to be maintained? If there is any change in the name, it would break this unit test. Not sure if we want to keep this dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resnet34_v2 is a public function exposed through model_zoo.vision module. I think it is ok because we are not using string based selection of the model.

@sandeep-krishnamurthy
Copy link
Contributor Author

@szha @apeforest - Is this good to go?

Copy link
Contributor

@apeforest apeforest left a comment

Choose a reason for hiding this comment

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

LGTM

# other than fp32 param dtype.

# 1. Load a resnet model, cast it to fp64 and export
tmp = tempfile.mkdtemp()
Copy link
Contributor

Choose a reason for hiding this comment

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

should we delete the temporary directory when done with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Temp gets automatically cleaned up.

Copy link
Contributor

@stu1130 stu1130 Sep 7, 2018

Choose a reason for hiding this comment

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

According to python docs, it seems that it should be deleted by the user. The user of mkdtemp() is responsible for deleting the temporary directory and its contents when done with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Thanks. I meant, temp gets automatically cleaned up after all the tests (test session).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marcoabreu - Can you please confirm if my understanding is correct? If not, I will add code to delete the temp directory created in the tests. Also, I see similar behavior in all other tests, where it creates temp dir, but, assumes, it will be cleaned up the system.

@sandeep-krishnamurthy
Copy link
Contributor Author

@zhreshold - Can you help look at this once, and merge if looks good?

for i in out.list_auxiliary_states():
if i not in input_names:
self.params.get(i, grad_req='null', allow_deferred_init=True)
arg_types, aux_types = _infer_param_types(inputs[0], out, arg_params, aux_params)
Copy link
Member

Choose a reason for hiding this comment

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

this does not handle Grouped Symbol because you are only slicing [0] from the symbol.

Copy link
Member

Choose a reason for hiding this comment

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

also, I think the type inference should occur in cast as well, otherwise it's buggy when user is trying to cast dtype of a cascaded network with symbolBlock inside.

for i in out.list_auxiliary_states():
if i not in input_names:
self.params.get(i, grad_req='null', allow_deferred_init=True)
arg_types, aux_types = _infer_param_types(inputs[0], out, arg_params, aux_params)
Copy link
Member

Choose a reason for hiding this comment

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

also, I think the type inference should occur in cast as well, otherwise it's buggy when user is trying to cast dtype of a cascaded network with symbolBlock inside.

break
assert np.dtype(net_fp64.params[param_name].dtype) == np.dtype(np.float64)

# Cast the symbol block to FP32 and try to forward a FP32 data.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhreshold - Added a test here to verify the SymbolBlock.cast functionality.

@sandeep-krishnamurthy
Copy link
Contributor Author

@zhreshold - Can you please take a look at this? Thanks.

@sandeep-krishnamurthy sandeep-krishnamurthy merged commit acf309e into apache:master Sep 18, 2018
anirudh2290 pushed a commit to anirudh2290/mxnet that referenced this pull request Sep 19, 2018
* Infer dtype in SymbolBlock import from input symbol

* Fix lint issues and make existing tests pass

* Add tests for importing a fp64 model into symbol block

* Fixing failing test for test symbol block

* Set context in unit tests

* Add tests for fp16, add default dtype in infer_param_types

* Use tmp directory as root for loading from model zoo to avoid race condition

* Fixing naming and parameter selection in test case

* Fixing failing GPU tests

* Make unit test more deterministic to get param name

* Override cast in symbol block, handle grouped symbol

* Handle multiple symbolic input usecase

* Add tests to verify behavior of SymbolBlock.cast
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Gluon pr-awaiting-review PR is waiting for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants