Add Support to Dicts and Strings in ONNX for Inputs and Outputs#25889
Add Support to Dicts and Strings in ONNX for Inputs and Outputs#25889lara-hdr wants to merge 11 commits intopytorch:masterfrom
Conversation
| flatten_rec(item.ptr(), args); | ||
| } | ||
| structure.push_back(D::DictClose); | ||
| std::string msg = |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
|
@houseroad, this is required for some pending PRs in torchvision to export the detection models. |
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. |
…aidar/support_dict_in_onnx
| 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 " |
|
@houseroad @suo any updates on this? |
|
@houseroad @suo, sorry for the spam, but this PR is blocking pytorch/vision#1324. |
|
Sorry for the delay, I'll take a look tomorrow morning |
| flatten_rec(item.ptr(), args); | ||
| } | ||
| structure.push_back(D::DictClose); | ||
| std::string msg = |
There was a problem hiding this comment.
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?
| string str = THPUtils_unpackString(obj); | ||
| args.desc.strings.emplace_back(str); | ||
| args.desc.structure.push_back(D::String); | ||
| 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 " | ||
| "recommended. But got unsupported type "; |
There was a problem hiding this comment.
nit: "But got unsupported type " seems out of place.
|
@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. |
|
@pytorchbot rebase this please |
|
@pytorchbot rebase this please |
|
@pytorchbot rebase this please |
|
tests in test_operaters.py keep failing. @lara-hdr could you take a look? |
|
@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. |
|
@pytorchbot rebase this please |
|
@houseroad. failure is unrelated this time |
|
@pytorchbot rebase this please |
|
Thanks a lot @suo! |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@vincentqb has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
houseroad
left a comment
There was a problem hiding this comment.
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,)) | ||
|
|
There was a problem hiding this comment.
Can we have two one test cases:
- with string as input,
- have some in-place changes, and we get the warning.
|
@houseroad merged this pull request in 614edfc. |
…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
…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
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).