-
Notifications
You must be signed in to change notification settings - Fork 283
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
Refactor onnx conversion #100
Conversation
This pull request was exported from Phabricator. Differential Revision: D21072663 |
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
e90237a
to
de27bbc
Compare
This pull request was exported from Phabricator. Differential Revision: D21072663 |
crypten/nn/onnx_converter.py
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
de27bbc
to
473a18c
Compare
This pull request was exported from Phabricator. Differential Revision: D21072663 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect for me !
This pull request has been merged in 1aa8ced. |
Summary:
This diff refactors CrypTen's conversion of onnx models to allow for future alterations of the imported onnx graph. Changes include
onnx_converter.py
) with its own testsfrom_pytorch
andfrom_onnx
using helpers for easier testings / debuggingio.BytesIO()
streams to ensure resource is releasedDifferential Revision: D21072663