#12 fix CrossEntropyLoss tensor args

Merged
sunxiyin merged 1 commits from sunxiyin/MSAdapterUT:master into master 1 year ago
  1. +7
    -0
      solution_test/README.md
  2. +14
    -18
      solution_test/common/base.py
  3. +5
    -5
      solution_test/pytorch_cases/demo/CrossEntropyLoss/test_msadapter_ops_crossentropyloss.yaml

+ 7
- 0
solution_test/README.md View File

@@ -74,6 +74,13 @@ args: 对应各个算子入参
support_type: ["int", "tuple"] #表示支持int、tuple类型
support_value_range: {"int": [1, 10], "tuple": [1, 10]} #表示各个类型取值范围是[1, 10]
support_value_length: {"tuple": {2}} #表示tuple类型只支持2维
支持参数配置指定tensor数据和类型:
args:
weight: #参数名weight
support_type: ["numpy"] #参数weight支持tensor
numpy_shape: [[5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]#参数weight支持tensor的shape
numpy_type: ["float32", "float16"] #表示支持float32、float16类型
具体使用示例可参考用例demo/CrossEntropyLoss/
# 对比精度,指定精度配置,可选
默认精度为:
"float16": 1e-3,"float32": 1e-4,"float64": 1e-5,


+ 14
- 18
solution_test/common/base.py View File

@@ -29,6 +29,8 @@ class SolutionTestBase:
self.init_success_flag = False
self.base_dir = os.getcwd()
self.case_name = case_name
self.args_dict = dict()
self.input_dict = dict()
self.params_dict = dict()
# 从环境获取执行模式
self.execute_mode = os.environ.get("TRAIN_MODE", None)
@@ -80,8 +82,6 @@ class SolutionTestBase:
return True

def generate_case_params(self):
args_dict = dict()
input_dict = dict()
yaml_content = yaml_read(self.yaml_path)
args = yaml_content.get("args")
variation = yaml_content.get("variation")
@@ -106,13 +106,13 @@ class SolutionTestBase:
if args is not None:
for key in args.keys():
if self.params_dict.get(key) is not None:
args_dict[key] = self.params_dict.get(key)
self.args_dict[key] = self.params_dict.get(key)
if input_shape is not None:
for key in input_shape.keys():
if self.params_dict.get(key) is not None:
input_dict[key] = self.params_dict.get(key)
self.ms_log.info("params_dict: %s, args_dict: %s", self.params_dict, args_dict)
return self.params_dict, args_dict, input_shape
self.input_dict[key] = self.params_dict.get(key)
self.ms_log.info("params_dict: %s, args_dict: %s", self.params_dict, self.args_dict)
return self.params_dict, self.args_dict, self.input_dict

def generate_input_shape(self):
"""
@@ -120,33 +120,29 @@ class SolutionTestBase:
"""
ms_input_shape_list = list()
torch_input_shape_list = list()
_, args_dict, input_dict = self.generate_case_params()
_, _, input_dict = self.generate_case_params()
for key, value in input_dict.items():
random_shape = np.random.randn(*input_dict.get(key).get("input_shape"))
torch_input_shape = torch.tensor(random_shape, dtype=input_dict.get("torch_dtype"))
ms_input_shape = ms_pytorch.tensor(random_shape, dtype=input_dict.get("ms_dtype"))
ms_input_shape_list.append(ms_input_shape)
torch_input_shape_list.append(torch_input_shape)

return torch_input_shape_list, ms_input_shape_list

def generate_args_with_tensor(self, args):
"""
生成参数中的tensor类型数据
:param args: 设为tesnor数据的参数名
:param frame_name: 使用的框架名,取值分别为:"torch"或"ms_pytorch"
"""
_, args_dict, _ = self.generate_case_params()
numpy_shape = args_dict.get(args).get("numpy_shape")
numpy_type = args_dict.get(args).get("numpy_type")
random_shape = np.random.randn(*numpy_shape)

numpy_shape = self.args_dict.get(args).get("numpy_shape")
numpy_type = self.args_dict.get(args).get("numpy_type")
random_shape = np.random.randn(numpy_shape)
torch_tensor = torch.tensor(random_shape, dtype=eval(".".join(["torch", numpy_type])))
args_dict["weight"] = torch_tensor
torch_args = args_dict.copy()
self.args_dict["weight"] = torch_tensor
torch_args = copy.deepcopy(self.args_dict)
ms_tensor = ms_pytorch.tensor(random_shape, dtype=eval(".".join(["ms_pytorch", numpy_type])))
args_dict["weight"] = ms_tensor
ms_args = args_dict
self.args_dict["weight"] = ms_tensor
ms_args = self.args_dict
self.ms_log.info("func generate_args_with_tensor get torch_args: %s\n ms_args: %s\n", torch_args, ms_args)
return torch_args, ms_args



+ 5
- 5
solution_test/pytorch_cases/demo/CrossEntropyLoss/test_msadapter_ops_crossentropyloss.yaml View File

@@ -2,19 +2,19 @@ op_name: CrossEntropyLoss
# 输入shape
input_shape:
input:
input_shape: [[256], [5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]
input_shape: [[5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]
dtype: float32

target:
input_shape: [[256], [5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]
input_shape: [[5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]
dtype: float32

# 参数
args:
weight:
support_type: ["numpy"]
numpy_shape: [[256], [5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]
numpy_type: ["float32", "float16"]
numpy_shape: [[5, 10], [10, 30, 256], [1, 10, 10, 10, 10], [1, 10, 128, 128]]
numpy_type: ["float32"]
size_average:
support_type: ["bool"]
support_value_range: {"bool": [True, False]}
@@ -34,4 +34,4 @@ args:
# 参数间约束规则,可选
variation:
variation1: params["target"]["input_shape"]=params["input"]["input_shape"]
variation2: params["weight"]["numpy_shape"]=params["input"]["input_shape"]
variation2: params["weight"]["numpy_shape"]=params["input"]["input_shape"][1]

Loading…
Cancel
Save