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

**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=&quot;x&quot;)
 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 &lt;module&gt;
​    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=&quot;x&quot;)
 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接口](https://www.mindspore.cn/docs/api/zh-CN/master/api_python/ops/mindspore.ops.ScatterAdd.html?highlight=scatteradd)

