MindSpore报RuntimeError:ReduceSum算子不支持8维及以上的输入而报错

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): Ascend Software Environment: -- MindSpore version (source or binary): 1.6.0 -- 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 脚本

训练脚本是通过构建ReduceSum的单算子网络,通过对维度中的所有元素求和来减少张量的维度。脚本如下:

 01 class Net(Cell):
 02  def __init__(self, axis, keep_dims):
 03   super().__init__()
 04   self.reducesum = ops.ReduceSum(keep_dims=keep_dims)
 05   self.axis = axis
 06  def construct(self, input_x):
 07   return self.reducesum(input_x, self.axis)
 08
 09 x = Tensor(np.random.randn(10, 5, 4, 4, 4, 4, 4, 4, 4, 4),mindspore.float32)
 10 net = Net(axis=(1,), keep_dims=True)
 11 out = net(x)
 12 print("out",out.shape)
 13 #print(out)

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
 File "demo4.py", line 11, in <module>
  out = net(x)
  …
RuntimeError: ({'errCode': 'E80012', 'op_name': 'reduce_sum_d', 'param_name': 'x', 'min_value': 0, 'max_value': 8, 'real_value': 10}, 'In op, the num of dimensions of input/output[x] should be inthe range of [0, 8], but actually is [10].')
The function call stack:
In file demo4.py(07)/    return self.reducesum(input_x, self.axis)/

2 原因分析

我们看报错信息:在RuntimeError中,max_value为8,而real_value却为10,显然超过了ReduceSum算子支持的维度,在后续提示中写到,输入参数维度应该在[0,8]之间,在官网中对ReduceSum也做了输入维度限制说明。

检查代码发现,09行代码维度为10,变量定义存在问题。需要将值设定为[0,8]。

3 解决方法

基于上面已知的原因,很容易做出如下修改:

此时执行成功,输出如下: out (10, 1, 4, 4, 4, 4, 4, 4)

4 总结

定位报错问题的步骤:

1、 找到报错的用户代码行: return self.reducesum(input_x, self.axis) ;

2、 根据日志报错信息中的关键字,缩小分析问题的范围: should be in the range of [0, 8], but actually is [10] ;

3、需要重点关注变量定义、初始化的正确性。

5 参考文档

5.1 ReduceSum算子API接口