-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add type annotations to torch.onnx.* modules #45258
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22c31d2
Add type annotations to torch.onnx.* modules
guilhermeleobas 26ef9f4
fix mypy errors
guilhermeleobas 41911bd
fix flake8 error
guilhermeleobas c9653e2
add missing type annotations
guilhermeleobas 6d28a76
update __init__.pyi.in
guilhermeleobas 7c81308
Replace Set[Any] by Set[int]
guilhermeleobas 309facb
remove Any import
guilhermeleobas 1b52305
remove duplicate and add type: ignore
guilhermeleobas File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from torch.jit import _unique_state_dict | ||
from torch.onnx import ONNX_ARCHIVE_MODEL_PROTO_NAME, ExportTypes, OperatorExportTypes, TrainingMode | ||
from torch._C import ListType, OptionalType, _propagate_and_assign_input_shapes, _check_onnx_proto | ||
from typing import Union, Tuple, List | ||
|
||
|
||
# the flag to tell the user whether it's in the middle of ONNX export or not | ||
|
@@ -76,7 +77,7 @@ def export(model, args, f, export_params=True, verbose=False, training=None, | |
if aten or export_raw_ir: | ||
assert operator_export_type is None | ||
assert aten ^ export_raw_ir | ||
operator_export_type = OperatorExportTypes.ATEN if aten else OperatorExportTypes.RAW | ||
operator_export_type = OperatorExportTypes.ONNX_ATEN if aten else OperatorExportTypes.RAW | ||
elif operator_export_type is None: | ||
if torch.onnx.PYTORCH_ONNX_CAFFE2_BUNDLE: | ||
operator_export_type = OperatorExportTypes.ONNX_ATEN_FALLBACK | ||
|
@@ -351,6 +352,7 @@ def _trace_and_get_graph_from_model(model, args): | |
|
||
def _create_jit_graph(model, args, _retain_param_name, use_new_jit_passes): | ||
torch_out = None | ||
params: Union[List, Tuple] | ||
if isinstance(model, torch.jit.ScriptModule): | ||
try: | ||
graph = model.forward.graph | ||
|
@@ -442,7 +444,7 @@ def _model_to_graph(model, args, verbose=False, | |
param_names = input_and_param_names[len(input_and_param_names) - len(params):] | ||
params_dict = dict(zip(param_names, params)) | ||
|
||
if training is None or training == TrainingMode.EVAL or (training == TrainingMode.PRESERVE and not is_originally_training): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if training is None or training == TrainingMode.EVAL: | ||
params_dict = torch._C._jit_pass_onnx_eval_peephole(graph, params_dict) | ||
|
||
if do_constant_folding and _export_onnx_opset_version in torch.onnx.constant_folding_opset_versions: | ||
|
@@ -476,7 +478,7 @@ def export_to_pretty_string(model, args, f, export_params=True, verbose=False, t | |
if aten or export_raw_ir: | ||
assert operator_export_type is None | ||
assert aten ^ export_raw_ir | ||
operator_export_type = OperatorExportTypes.ATEN if aten else OperatorExportTypes.RAW | ||
operator_export_type = OperatorExportTypes.ONNX_ATEN if aten else OperatorExportTypes.RAW | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
elif operator_export_type is None: | ||
operator_export_type = OperatorExportTypes.ONNX | ||
return _export_to_pretty_string(model, args, f, export_params, verbose, training, | ||
|
@@ -1051,6 +1053,10 @@ def _graph_constant(g, value, dims, type, *args, **kwargs): | |
dims = [1] | ||
isscalar = True | ||
type = type.lower() | ||
tensor: Union[torch.CharTensor, torch.ShortTensor, | ||
torch.IntTensor, torch.LongTensor, | ||
torch.HalfTensor, torch.FloatTensor, | ||
torch.DoubleTensor] | ||
if type == "char": | ||
tensor = torch.CharTensor(*dims) | ||
elif type == "short": | ||
|
@@ -1068,7 +1074,7 @@ def _graph_constant(g, value, dims, type, *args, **kwargs): | |
else: | ||
raise ValueError("Unknown type, type should be one of the following strings: " | ||
"char, short, int, long, half, float, double") | ||
tensor.fill_(value) | ||
tensor.fill_(value) # type: ignore | ||
if isscalar: | ||
return g.op("Constant", *args, value_z=tensor, **kwargs) | ||
return g.op("Constant", *args, value_t=tensor, **kwargs) | ||
|
@@ -1141,8 +1147,8 @@ def _validate_dynamic_axes(dynamic_axes, model, input_names, output_names): | |
dynamic_axes[key] = value_dict | ||
|
||
|
||
torch._C.Graph.op = _graph_op | ||
torch._C.Graph.at = _graph_at | ||
torch._C.Block.op = _block_op | ||
torch._C.Graph.constant = _graph_constant | ||
torch._C.Node.__getitem__ = _node_getitem | ||
torch._C.Graph.op = _graph_op # type: ignore | ||
torch._C.Graph.at = _graph_at # type: ignore | ||
torch._C.Block.op = _block_op # type: ignore | ||
torch._C.Graph.constant = _graph_constant # type: ignore | ||
torch._C.Node.__getitem__ = _node_getitem # type: ignore |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is no mention to
OperatorExportTypes.ATEN
in the source code. So, I think this value was supposed to beOperatorExportTypes.ONNX_ATEN
.