1 报错描述
1.1 系统环境
Hardware Environment(Ascend/GPU/CPU): GPU
Software Environment:
– MindSpore version (source or binary): 1.5.2
– Python version (e.g., Python 3.7.5): 3.7.6
– OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic
– GCC/Compiler version (if compiled from source):
1.2 基本信息
1.2.1 脚本
训练脚本是通过构建Minimum的单算子网络,计算输入Tensor对应元素的最小值。脚本如下:
01 class Net(nn.Cell):
02 def __init__(self):
03 super(Net, self).__init__()
04 self.minimum = ops.minumum()
05 def construct(self, x,y):
06 output = self.minimum(x, y)
07 return output
08 net = Net()
09 x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float64) .astype(mindspore.float32)
10 y = Tensor(np.array([3.0, 5.0, 6.0]), mindspore.float64) .astype(mindspore.float32)
11 output = net(x, y)
12 print('output', output)
1.2.2 报错
这里报错信息如下:
[EXCEPTION] PYNATIVE(95109,7fe22ea22740,python):2022-06-14-21:59:02.837.339 [mindspore/ccsrc/pipeline/pynative/pynative_execute.cc:1331] SetImplicitCast] Minimum inputs size 0 does not match the requires signature size 2
Traceback (most recent call last):
File "182168.py", line 36, in
net = Net()
File "182168.py", line 24, in init
self.minimum = ops.minimum()
File "/data2/llj/mindspores/r1.5/build/package/mindspore/ops/primitive.py", line 247, in call
return _run_op(self, self.name, args)
File "/data2/llj/mindspores/r1.5/build/package/mindspore/common/api.py", line 78, in wrapper
results = fn(*arg, **kwargs)
File "/data2/llj/mindspores/r1.5/build/package/mindspore/ops/primitive.py", line 682, in _run_op
output = real_run_op(obj, op_name, args)
ValueError: mindspore/ccsrc/pipeline/pynative/pynative_execute.cc:1331 SetImplicitCast] Minimum inputs size 0 does not match the requires signature size 2
原因分析
我们看报错信息,在ValueError中,写到Minimum inputs size 0 does not match the requires signature size 2,大概意思是需要两个输入, 但实际没有输入。结合第11行发现其实是传入了两个输入的。所以问题可能就不是出在这一行, 我们可以在3, 5, 11行后面分别加上一个打印标记, 看程序执行到了哪个位置。执行后发现只打印了1, 说明问题就出在第四行, 仔细检查发现第四行初始化Minimum算子的时候minimum用的小写, 相当于直接调用了函数式接口minimum算子, 所以报错没有传入参数。
2 解决方法
基于上面已知的原因,很容易做出如下修改:
01 class Net(nn.Cell):
02 def __init__(self):
03 super(Net, self).__init__()
04 self.minimum = ops.Minimum()
05 def construct(self, x,y):
06 output = self.minimum(x, y)
07 return output
08 net = Net()
09 x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float64) .astype(mindspore.float32)
10 y = Tensor(np.array([3.0, 5.0, 6.0]), mindspore.float64) .astype(mindspore.float32)
11 output = net(x, y)
12 print('output', output)
此时执行成功,输出如下:
output [1. 2. 3.]
3 总结
定位报错问题的步骤:
1、找到报错的用户代码行:self.minimum = ops.minimum();
2、 根据日志报错信息中的关键字,缩小分析问题的范围Minimum inputs size 0 does not match the requires signature size 2 ;
3、需要重点关注变量定义、初始化的正确性。
4 参考文档
4.1 Minimum算子API接口