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

Generate name for each member of list arg #571

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
25 changes: 19 additions & 6 deletions pytorch_pfn_extras/onnx/export_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,23 @@ def export_testcase(
os.makedirs(out_dir, exist_ok=True)
if isinstance(args, torch.Tensor):
args = args,
input_names = kwargs.pop(
'input_names',
['input_{}'.format(i) for i in range(len(args))])
assert len(input_names) == len(args)

# We unroll list args and generate names for each tensor.
gen_input_names = []
unrolled_args = []

def append_input_name(prefix: str, arg: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

How about supporting also dict and namedtuple types like _normalize_outputs function in _logic.py?

def _normalize_outputs(outputs: Any) -> Dict[str, Any]:
target: Dict[str, Any]
if isinstance(outputs, tuple) and hasattr(outputs, '_fields'):
# namedtuple
target = outputs._asdict() # type: ignore[attr-defined]
elif isinstance(outputs, dict):
target = outputs
elif isinstance(outputs, (list, tuple)):
target = {str(i): out for i, out in enumerate(outputs)}
else:
target = {"0": outputs}
return target

if isinstance(arg, list):
for i, a in enumerate(arg):
append_input_name(prefix + f"_{i}", a)
else:
gen_input_names.append(prefix)
unrolled_args.append(arg)
for i, arg in enumerate(args):
append_input_name(f"input_{i}", arg)

input_names = kwargs.pop('input_names', gen_input_names)
assert len(input_names) == len(unrolled_args)
assert not isinstance(args, torch.Tensor)

onnx_graph, outs = _export(
Expand All @@ -335,7 +348,7 @@ def export_testcase(
if used_input.name not in initializer_names:
used_input_index_list.append(input_names.index(used_input.name))
input_names = [input_names[i] for i in used_input_index_list]
args = [args[i] for i in used_input_index_list]
unrolled_args = [unrolled_args[i] for i in used_input_index_list]

output_path = os.path.join(out_dir, 'model.onnx')
is_on_memory = True
Expand Down Expand Up @@ -374,7 +387,7 @@ def write_to_pb(f: str, tensor: torch.Tensor, name: Optional[str] = None) -> Non
os.makedirs(data_set_path, exist_ok=True)
for pb_name in glob.glob(os.path.join(data_set_path, "*.pb")):
os.remove(pb_name)
for i, (arg, name) in enumerate(zip(args, input_names)):
for i, (arg, name) in enumerate(zip(unrolled_args, input_names)):
f = os.path.join(data_set_path, 'input_{}.pb'.format(i))
write_to_pb(f, arg, name)

Expand Down