MindSpore RuntimeErrorReduceSum算子不支持8维及以上的输入而报错
2023/05/23
99
问题信息
问题来源 | 产品大类 | 产品子类 | 关键字 |
---|---|---|---|
官方 | 模型训练 | MindSpore | 无 |
问题现象描述
系统环境:
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):
训练脚本是通过构建ReduceSum的单算子网络,通过对维度中的所有元素求和来减少张量的维度。脚本如下:
class Net(Cell): def __init__(self, axis, keep_dims): super().__init__() self.reducesum = ops.ReduceSum(keep_dims=keep_dims) self.axis = axis def construct(self, input_x): return self.reducesum(input_x, self.axis) x = Tensor(np.random.randn(10, 5, 4, 4, 4, 4, 4, 4, 4, 4),mindspore.float32) net = Net(axis=(1,), keep_dims=True) out = net(x) print("out",out.shape) #print(out)
报错信息如下:
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)/
原因分析
在RuntimeError中,max_value为8,而real_value为10,超过了ReduceSum算子支持的维度,提示输入参数维度应该在[0,8]之间,在官网中对ReduceSum也做了输入维度限制说明。
检查代码发现,09行代码维度为10,变量定义存在问题。需要将值设定为[0,8]。
参考文档:ReduceSum算子API接口
解决措施
基于以上原因,修改训练脚本:
class Net(Cell): def __init__(self, axis, keep_dims): super().__init__() self.reducesum = ops.ReduceSum(keep_dims=keep_dims) self.axis = axis def construct(self, input_x): return self.reducesum(input_x, self.axis) x = Tensor(np.random.randn(10, 5, 4, 4, 4, 4, 4, 4),mindspore.float32) # 将输入修改为8维 net = Net(axis=(1,), keep_dims=True) out = net(x) print("out",out.shape) #print(out)
执行训练。成功后输出如下:
out (10, 1, 4, 4, 4, 4, 4, 4)
本页内容