Skip to content
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

Refactor onnx conversion #100

Closed

Conversation

marksibrahim
Copy link
Contributor

Summary:
This diff refactors CrypTen's conversion of onnx models to allow for future alterations of the imported onnx graph. Changes include

  • moving logic of hairy onnx conversions into a separate module (onnx_converter.py) with its own tests
  • adding several tests for helper functions to map onnx graphs to crypten modules
  • refactoring more complex functions such as from_pytorch and from_onnx using helpers for easier testings / debugging
  • using a context manager for all io.BytesIO() streams to ensure resource is released

Differential Revision: D21072663

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported labels Apr 16, 2020
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D21072663

marksibrahim added a commit to marksibrahim/CrypTen that referenced this pull request Apr 16, 2020
Summary:
Pull Request resolved: facebookresearch#100

This diff refactors CrypTen's conversion of onnx models to allow for future alterations of the imported onnx graph. Changes include

* moving logic of hairy onnx conversions into a separate module (`onnx_converter.py`) with its own tests
* adding several tests for helper functions to map onnx graphs to crypten modules
* refactoring more complex functions such as `from_pytorch` and `from_onnx` using helpers for easier testings / debugging
* using a context manager for all `io.BytesIO()` streams to ensure resource is released

Differential Revision: D21072663

fbshipit-source-id: 03b4957d481a98b986e89859212e2c5a59bcf891
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D21072663

Comment on lines 50 to 59
with io.BytesIO() as f:
_export_pytorch_model(f, pytorch_model, dummy_input)

# update ONNX symbolic registry with CrypTen-specific functions
_update_onnx_symbolic_registry()

# export again so the graph is created with CrypTen-specific registry
with io.BytesIO() as f:
f = _export_pytorch_model(f, pytorch_model, dummy_input)
f.seek(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great if the logic here is put into a separate function. An interesting use-case for PySyft is where we can serialize a pytorch model into this bytes object, send it through the network, then build the crypten model in another machine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, happy to restructure. I'll update the pull request.

Summary:
Pull Request resolved: facebookresearch#100

This diff refactors CrypTen's conversion of onnx models to allow for future alterations of the imported onnx graph. Changes include

* moving logic of hairy onnx conversions into a separate module (`onnx_converter.py`) with its own tests
* adding several tests for helper functions to map onnx graphs to crypten modules
* refactoring more complex functions such as `from_pytorch` and `from_onnx` using helpers for easier testings / debugging
* using a context manager for all `io.BytesIO()` streams to ensure resource is released

Differential Revision: D21072663

fbshipit-source-id: ad8930d470b018f0f2733a0a25705c38affcdfda
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D21072663

Comment on lines +58 to +72
def _from_pytorch_to_bytes(pytorch_model, dummy_input):
"""Returns I/O stream containing onnx graph with crypten specific ops"""
# TODO: Currently export twice because the torch-to-ONNX symbolic registry
# only gets created on the first call.
with io.BytesIO() as f:
_export_pytorch_model(f, pytorch_model, dummy_input)

# update ONNX symbolic registry with CrypTen-specific functions
_update_onnx_symbolic_registry()

# export again so the graph is created with CrypTen-specific registry
f = io.BytesIO()
f = _export_pytorch_model(f, pytorch_model, dummy_input)
f.seek(0)
return f
Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect for me !

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 1aa8ced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants