Advertisement

MindSpore报错:type conversion of ‘Parameter‘ is not supported,so data type int32 cannot be converted..

阅读量:

1 报错描述

1.1 系统环境

硬件平台(加速器/GPU/CPU):GPU
软件环境:

  • MindSpore版本(源代码或二进制文件):1.6
  • Python版本(例如:Python 3.8):3.7
  • 操作系统及版本(例如:Linux Ubuntu 18.04):Ubuntu 2204-LTS-x86_64
  • 编译器或编译环境(若从源码编译):

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 &quot;demo.py&quot;, 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)

原因分析

在MindSpore 1.6版本中,在construct模块中生成和应用Tensor。如脚本中第11行代码所示。

接下来看报错信息,在RuntimeError异常中指出 Data type conversion of ‘Parameter’ is not supported, so data type int32 cannot be converted to data type float32 automatically ,其含义是Parameter的数据类型不具备自动转为float32的支持性问题。并结合Parameter和代码 output = \text{self.scatter\_nd\_update}(\text{self.x}, \text{indices}, \text{updates}) ,根据输入参数 input\_x = \text{Tensor}(\text{np.array}([[-2, 3, 6], [4, 5, -3]]), \text{mindspore.int32}) 的信息可知,在该处的数据类型处理过程中需要将数据类型由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 = net(indices, updates) ;

Data-type conversions for the parameter are not supported, thus data-type int32 cannot be directly converted to data-type float32 without intervention.

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

4 参考文档

4.1 construct方法

全部评论 (0)

还没有任何评论哟~