Improve code for getting example inputs for IPEX backend
import copy
import math
import os
-from collections import OrderedDict, UserDict
+from collections import OrderedDict, UserDict, namedtuple
from packaging.version import Version
import yaml
from functools import partial
@@ -128,6 +128,7 @@ def pytorch_forward_wrapper(model, input, device='cpu', conf=None, running_mode=
def get_example_inputs(model, dataloader): # pragma: no cover
+ version = get_torch_version()
# Suggest set dataloader like calib_dataloader
if dataloader is None:
return None
@@ -136,14 +137,13 @@ def get_example_inputs(model, dataloader): # pragma: no cover
output = pytorch_forward_wrapper(model,
input)
if isinstance(input, dict) or isinstance(input, UserDict):
- input_tensor = []
+ assert version.release >= Version("1.12.0").release, \
+ "INC support IPEX version >= 1.12.0"
if "label" in input.keys():
input.pop("label")
- for key, value in input.items():
- if key == "start_positions" or key == "end_positions":
- continue
- input_tensor.append(value)
- return input_tensor
+ named_input = namedtuple("input", input.keys())
+ input = named_input._make(input.values())
+ return input
if isinstance(input, list) or isinstance(input, tuple):
return input
if isinstance(input, torch.Tensor):
@@ -154,14 +154,13 @@ def get_example_inputs(model, dataloader): # pragma: no cover
output = pytorch_forward_wrapper(model,
input)
if isinstance(input, dict) or isinstance(input, UserDict):
- input_tensor = []
+ assert version.release >= Version("1.12.0").release, \
+ "INC support IPEX version >= 1.12.0"
if "label" in input.keys():
input.pop("label")
- for key, value in input.items():
- if key == "start_positions" or key == "end_positions":
- continue
- input_tensor.append(value)
- return input_tensor
+ named_input = namedtuple("input", input.keys())
+ input = named_input._make(input.values())
+ return input
if isinstance(input, list) or isinstance(input, tuple):
return input
if isinstance(input, torch.Tensor):
