Skip to content

Commit

Permalink
[PyTorch] Guarantee data input is the first argument (#7592)
Browse files Browse the repository at this point in the history
  • Loading branch information
comaniac authored Mar 5, 2021
1 parent d7f5753 commit d5cb3cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion python/tvm/relay/frontend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3216,5 +3216,16 @@ def from_pytorch(script_module, input_infos, custom_convert_map=None, default_dt
# ListConstruct kept original python list. Convert to tuple.
ret = _expr.Tuple(ret)

mod["main"] = tvm.relay.Function(_analysis.free_vars(ret), ret)
# Separate data inputs and parameters to make sure data inputs are always in the beginning.
func_args = []
data_inputs = []
for arg in _analysis.free_vars(ret):
if arg.name_hint not in tvm_params.keys():
data_inputs.append(arg)
else:
func_args.append(arg)
func_args = data_inputs + func_args

mod["main"] = tvm.relay.Function(func_args, ret)

return transform.RemoveUnusedFunctions()(mod), tvm_params
2 changes: 2 additions & 0 deletions tests/python/frontend/pytorch/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ def verify_model(model_name, input_data=[], custom_convert_map={}, rtol=1e-5, at
input_names = ["input{}".format(idx) for idx, inp in enumerate(baseline_input)]
input_shapes = list(zip(input_names, [inp.shape for inp in baseline_input]))
mod, params = relay.frontend.from_pytorch(trace, input_shapes, custom_convert_map)
for arg in mod["main"].params[: len(input_names)]:
assert arg.name_hint in input_names
compiled_input = dict(zip(input_names, [inp.clone().cpu().numpy() for inp in baseline_input]))

with tvm.transform.PassContext(opt_level=3):
Expand Down

0 comments on commit d5cb3cb

Please sign in to comment.