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

[ONNX] handle aten::_set_item on Dict in convertInplaceOpsAndTrackAlias #58317

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/jit/test_onnx_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import os
import sys
import typing

import torch
import torch.nn as nn
Expand Down Expand Up @@ -381,3 +382,19 @@ def forward(self, x):
f = io.BytesIO()
torch.onnx.export_to_pretty_string(
DynamicSliceExportMod(), (input,), f, example_outputs=example_outs, opset_version=10)

def test_export_dict(self):
class DictModule(torch.nn.Module):
def forward(self, x_in: torch.Tensor) -> typing.Dict[str, torch.Tensor]:
return {"test_key_out": x_in}

x_in = torch.tensor(1)
mod = DictModule()
mod.train(False)

f = io.BytesIO()
torch.onnx.export_to_pretty_string(mod, (x_in,), f)

with self.assertRaisesRegex(RuntimeError, r"DictConstruct.+is not supported."):
torch.onnx.export_to_pretty_string(
torch.jit.script(mod), (x_in,), f, example_outputs=(mod(x_in),))
11 changes: 9 additions & 2 deletions torch/csrc/jit/passes/onnx/remove_inplace_ops_for_onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,15 @@ static std::pair<Value*, Value*> PrepareListAppendAndInsertForONNX(Node* n) {
return std::make_pair(n->input(0), n->output());
}

static std::pair<Value*, Value*> PrepareListSetItemForONNX(Node* n) {
static std::pair<Value*, Value*> PrepareSetItemForONNX(Node* n) {
TORCH_INTERNAL_ASSERT(n->kind() == aten::_set_item);
// It seems the JIT does not always produce an output for _set_item.
// In particular it seems to for list but not for dict.
// So we add one if needed.
if (n->outputs().size() == 0) {
garymm marked this conversation as resolved.
Show resolved Hide resolved
n->addOutput();
n->output()->setType(n->inputs().at(0)->type());
}
return std::make_pair(n->input(0), n->output());
}

Expand Down Expand Up @@ -807,7 +814,7 @@ void InplaceConverter::convertInplaceOpsAndTrackAlias(Block* block) {
} else if (nkind == aten::Delete) {
std::tie(orig_data, new_out) = PrepareListDeleteForONNX(n);
} else if (nkind == aten::_set_item) {
std::tie(orig_data, new_out) = PrepareListSetItemForONNX(n);
std::tie(orig_data, new_out) = PrepareSetItemForONNX(n);
} else {
// Not inplace op.
continue;
Expand Down