Skip to content

Add Support to Dicts and Strings in ONNX for Inputs and Outputs#25889

Closed
lara-hdr wants to merge 11 commits intopytorch:masterfrom
lara-hdr:lahaidar/support_dict_in_onnx
Closed

Add Support to Dicts and Strings in ONNX for Inputs and Outputs#25889
lara-hdr wants to merge 11 commits intopytorch:masterfrom
lara-hdr:lahaidar/support_dict_in_onnx

Conversation

@lara-hdr
Copy link
Copy Markdown
Contributor

ONNX does not support dictionaries for inputs and output. The reason is that the arg flattening and unflattening does not handle Dictionary types.
This PR adds flattening/unflattening support for dictionaries and strings.
However this feature should be handled with caution for input dictionaries; and users need to verify their dict inputs carefully, and keep in mind that dynamic lookups are not available.

This PR will allow exporting cases where models have dictionnary outputs (detection and segmentation models in torchvision), and where dictionary inputs are used for model configurations (MultiScaleRoiAlign in torchvision).

@lara-hdr lara-hdr requested a review from apaszke as a code owner September 10, 2019 02:15
@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: onnx Related to torch.onnx module: pybind Related to our Python bindings / interactions with other Python libraries labels Sep 10, 2019
Comment thread torch/csrc/jit/python_arg_flatten.cpp Outdated
flatten_rec(item.ptr(), args);
}
structure.push_back(D::DictClose);
std::string msg =
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This warning prints each time flatten() is called in the code.
We can keep it this way (and have the warning print multiple times in the code), or I an add a boolean arg to flatten() to only print the warnings if the caller is LegacyTracedModule() (and thus print the warning once).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the only limitation that the keys must remain the same during the function?

I don't think we should warn at all unless we can confirm that the user is doing something wrong. We don't want to teach people that they should ignore the warnings they see because they are expected. Can we validate after tracing that the dictionary keys are identical?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's correct, I will then remove the warnings.

I am not sure to understand your comment "Can we validate after tracing that the dictionary keys are identical?".
you mean by that, that we should verify that the dictionary keys are unchanged during the export?

@lara-hdr
Copy link
Copy Markdown
Contributor Author

@houseroad, this is required for some pending PRs in torchvision to export the detection models.
cc @BowenBao who reviewed internally.

@BowenBao
Copy link
Copy Markdown
Collaborator

@houseroad, this is required for some pending PRs in torchvision to export the detection models.
cc @BowenBao who reviewed internally.

A little more context. The dictionary usages in torchvision detection models is only for model graph configuration, so we are only expecting static usages: dictionary keys are always the same. Likewise how lists are expected as static, and flattened/unflattened, we do the same for dictionaries.

std::string msg =
"Only tuples, lists and Variables supported as JIT inputs, but got ";
"Only tuples, lists and Variables supported as JIT inputs/outputs. "
"Dictionaries and strings are also accepted but their usage is not "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@suo does this extension look good to you?

@houseroad houseroad requested a review from suo September 13, 2019 21:38
@lara-hdr
Copy link
Copy Markdown
Contributor Author

@houseroad @suo any updates on this?

@lara-hdr
Copy link
Copy Markdown
Contributor Author

@houseroad @suo, sorry for the spam, but this PR is blocking pytorch/vision#1324.
Could you please help with the review?

@suo
Copy link
Copy Markdown
Member

suo commented Sep 18, 2019

Sorry for the delay, I'll take a look tomorrow morning

Comment thread torch/csrc/jit/python_arg_flatten.cpp Outdated
flatten_rec(item.ptr(), args);
}
structure.push_back(D::DictClose);
std::string msg =
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the only limitation that the keys must remain the same during the function?

I don't think we should warn at all unless we can confirm that the user is doing something wrong. We don't want to teach people that they should ignore the warnings they see because they are expected. Can we validate after tracing that the dictionary keys are identical?

Comment thread torch/csrc/jit/python_arg_flatten.cpp Outdated
string str = THPUtils_unpackString(obj);
args.desc.strings.emplace_back(str);
args.desc.structure.push_back(D::String);
std::string msg =
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same note about warnings here

"Only tuples, lists and Variables supported as JIT inputs, but got ";
"Only tuples, lists and Variables supported as JIT inputs/outputs. "
"Dictionaries and strings are also accepted but their usage is not "
"recommended. But got unsupported type ";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: "But got unsupported type " seems out of place.

@lara-hdr
Copy link
Copy Markdown
Contributor Author

@suo I added warn_on_static_input_change(), called after tracing, in torch/onnx/utils.py to verify if the dict/str inputs changed, and print the warning in case they are.

Let me know if there are any concerns with the implementation or if you have recommendations for a better design.

@houseroad
Copy link
Copy Markdown
Member

@pytorchbot rebase this please

@houseroad
Copy link
Copy Markdown
Member

@pytorchbot rebase this please

@houseroad houseroad added this to the 1.3 milestone Sep 24, 2019
@lara-hdr
Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase this please

Copy link
Copy Markdown
Member

@houseroad houseroad left a comment

Choose a reason for hiding this comment

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

Looks good. Can we also add one test in test_operators.py? It will help us understand the structure when tracing dict.

@houseroad
Copy link
Copy Markdown
Member

tests in test_operaters.py keep failing. @lara-hdr could you take a look?

@lara-hdr
Copy link
Copy Markdown
Contributor Author

lara-hdr commented Sep 25, 2019

@houseroad the errors are happening on the deepcopy() with the error "Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment" for the tests in test_operaters.py.

I am waiting on a build rn and will commit a fix soon.

@lara-hdr
Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase this please

@lara-hdr
Copy link
Copy Markdown
Contributor Author

@houseroad. failure is unrelated this time

@lara-hdr
Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase this please

@yf225 yf225 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Sep 25, 2019
Copy link
Copy Markdown
Member

@suo suo left a comment

Choose a reason for hiding this comment

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

lgtm, thanks for the changes!

@lara-hdr
Copy link
Copy Markdown
Contributor Author

Thanks a lot @suo!

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@vincentqb has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Copy Markdown
Member

@houseroad houseroad left a comment

Choose a reason for hiding this comment

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

overall, looks good. let's land this PR first, and add two more tests in the following PRs. (check my inline comments)


x = {"test_key_in": torch.randn(1, 2, 3)}
self.assertONNX(MyModel(), (x,))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we have two one test cases:

  1. with string as input,
  2. have some in-place changes, and we get the warning.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@houseroad merged this pull request in 614edfc.

pdlive215 pushed a commit to pdlive215/pytorch that referenced this pull request Nov 27, 2019
…rch#25889)

Summary:
ONNX does not support dictionaries for inputs and output. The reason is that the arg flattening and unflattening does not handle Dictionary types.
This PR adds flattening/unflattening support for dictionaries and strings.
However this feature should be handled with caution for input dictionaries; and users need to verify their dict inputs carefully, and keep in mind that dynamic lookups are not available.

This PR will allow exporting cases where models have dictionnary outputs (detection and segmentation models in torchvision), and where dictionary inputs are used for model configurations (MultiScaleRoiAlign in torchvision).
Pull Request resolved: pytorch#25889

Reviewed By: hl475

Differential Revision: D17613605

Pulled By: houseroad

fbshipit-source-id: c62da4f35e5dc2aa23a85dfd5e2e11f63e9174db
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…rch#25889)

Summary:
ONNX does not support dictionaries for inputs and output. The reason is that the arg flattening and unflattening does not handle Dictionary types.
This PR adds flattening/unflattening support for dictionaries and strings.
However this feature should be handled with caution for input dictionaries; and users need to verify their dict inputs carefully, and keep in mind that dynamic lookups are not available.

This PR will allow exporting cases where models have dictionnary outputs (detection and segmentation models in torchvision), and where dictionary inputs are used for model configurations (MultiScaleRoiAlign in torchvision).
Pull Request resolved: pytorch#25889

Reviewed By: hl475

Differential Revision: D17613605

Pulled By: houseroad

fbshipit-source-id: c62da4f35e5dc2aa23a85dfd5e2e11f63e9174db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: onnx Related to torch.onnx module: pybind Related to our Python bindings / interactions with other Python libraries oncall: jit Add this issue/PR to JIT oncall triage queue open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants