MindSpore报错 ScatterNdUpdate这个算子在Ascend硬件上不支持input0是int32的数据类型

文章描述了在使用MindSpore的ScatterNdUpdate算子时遇到的RuntimeError,错误源于尝试将int32的Parameter自动转换为float32。解决方案是使用Cast算子手动进行数据类型转换。经过修改后的代码能够成功执行,问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 脚本

训练脚本是通过构建ScatterNdUpdate的单算子网络,使用索引和输入张量的值更新张量值。脚本如下:

 01 class Net(nn.Cell):
 02   def __init__(self, x):
 03     super(Net, self).__init__()
 04     self.x = Parameter(x, name="x")
 05     self.scatter_nd_update = ops.ScatterNdUpdate()
 06
 07   def construct(self, indices, updates):
 08     output = self.scatter_nd_update(self.x, indices, updates)
 09     return output
 10 input_x = Tensor(np.array([[-2, 3, 6], [4, 5, -3]]), mindspore.int32)
 11 net = Net(input_x)
 12 indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
 13 updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
 14 output = net(indices, updates)
 15 print('output', output)
复制

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
 File 'demo.py', line 14, in <module>
​    output = net(indices, updates)
…
RuntimeError: mindspore/ccsrc/frontend/operator/composite/do_signature.cc:360 RaiseExceptionForConvertRefDtype] Data type conversion of 'Parameter' is not supported, so data type int32 cannot be converted to data type float32 automatically.
For more details, please refer at https://www.mindspore.cn/docs/note/zh-CN/master/operator_list_implicit.html.
The function call stack (See file ' /rank_0/om/analyze_fail.dat' for more details):
\# 0 In file demo.py(05)
​    output = self.scatter_nd_update(self.x, indices, updates)
复制

原因分析

我们看报错信息,在RuntimeError中,写到Data type conversion of 'Parameter' is not supported, so data type int32 cannot be converted to data type float32 automatically,意思是传入Parameter的数据类型并不支持,因为int32不能转为float32,但是再接着看提示报错提示代码,*output = self.scatter_nd_update(self.x, indices, updates)*,结合Parameter和代码input_x = Tensor(np.array([[-2, 3, 6], [4, 5, -3]]), mindspore.int32)可知,该处数据类型需要进行转换为float32,此时用Cast算子将数据类型int32转为float32即可。

2 解决方法

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

 01 class Net(nn.Cell):
 02   def __init__(self,x):
 03     super(Net, self).__init__()
 04     self.x = Parameter(x,name="x")
 05     self.scatter_nd_update = ops.ScatterNdUpdate(
 06
 07   def construct(self, indices, updates):
 08    output = self.scatter_nd_update(self.x, indices, updates)
 09    return output
 10 input_x = Tensor(np.array([[-2, 3, 6], [4, 5, -3]]), mindspore.int32)
 11 input_x = ops.Cast()(input_x, mindspore.float32)
 12 net = Net(input_x)
 13 indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
 14 updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
 15 output = net( indices, updates)
 16 print('output', output)
复制

此时执行成功,输出如下:

output [[ 1. 3. 6. ]

[ 4. 2.2 -3. ]]

3 总结

定位报错问题的步骤:

1、找到报错的用户代码行:output = self.scatter_nd_update(self.x, indices, updates);

2、 根据日志报错信息中的关键字,缩小分析问题的范围:Data type conversion of 'Parameter' is not supported, so data type int32 cannot be converted to data type float32 automatically;

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

4 参考文档

4.1 ScatterAdd算子API接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值