MindSpore报错“operation does not support the type kMetaTypeNone“

系统环境

Hardware Environment(Ascend/GPU/CPU): All
Software Environment:
MindSpore version (source or binary): 1.6.0 & Earlier versions
Python version (e.g., Python 3.7.5): 3.7.6
OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu
GCC/Compiler version (if compiled from source): gcc 9.4.0

python代码样例

from mindspore import nn

class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()

    def construct(self, x):
        return self.y + x

net = Net()
output = net(1)

报错信息

Traceback (most recent call last):
  File "test_self.py", line 11, in <module>
    output = net(1)
  File "mindspore\nn\cell.py", line 477, in __call__
    out = self.compile_and_run(*args)
  File "mindspore\nn\cell.py", line 803, in compile_and_run
    self.compile(*inputs)
  File "mindspore\nn\cell.py", line 790, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "mindspore\common\api.py", line 632, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
RuntimeError: mindspore\ccsrc\frontend\operator\composite\multitype_funcgraph.cc:162 GenerateFromTypes] The 'add' operation does not support the type [kMetaTypeNone, Int64].
The supported types of overload function `add` is: [Tuple, Tuple], [RowTensor, Tensor], [Tensor, Tensor], [List, List], [Tensor, List], [List, Tensor], [String, String], [Tuple, Tensor], [kMetaTypeNone, kMetaTypeNone], [Number, Number], [Number, Tensor], [Tensor, Number], [Tensor, Tuple].

The function call stack (See file 'rank_0/om/analyze_fail.dat' for more details):
# 0 In file test_self.py(8)
        return self.y + x

原因分析

首先看报错日志信息:

RuntimeError: The 'add' operation does not support the type [kMetaTypeNone, Int64].
The supported types of overload function `add` is: [Tuple, Tuple],[RowTensor, Tensor],  [Tensor, Tensor],  [List, List],[Tensor, List], [List, Tensor], [String, String], [Tuple, Tensor], [kMetaTypeNone, kMetaTypeNone], [Number, Number], [Number, Tensor], [Tensor, Number], [Tensor, Tuple].

上述日志信息的大致意思是add加法操作符不支持类型[kMetaTypeNone, Int64], 此外,还给出了该操作符支持的参数类型列表:

[Tuple, Tuple], [RowTensor, Tensor], [Tensor, Tensor], [List, List], [Tensor, List], [List, Tensor], [String, String], [Tuple, Tensor], [kMetaTypeNone, kMetaTypeNone], [Number, Number], [Number, Tensor], [Tensor, Number], [Tensor, Tuple]

在这里的疑问是,kMetaTypeNone什么类型, 哪里引入的类型报错?我们继续看报错信息。

The function call stack:后面打印了函数的调用栈,对应的是用户代码行return self.y + x。就是说,该加法操作引入了不支持的参数类型。

在mindspore中,construct函数里使用未定义的变量将作为None处理,对应的类型也就是kMetaTypeNone。关于mindspore中变量的定义与使用,详情可参考官网:https://www.mindspore.cn/docs/note/zh-CN/r1.6/static_graph_syntax_support.html?highlight=kMetaTypeNone。

在样例源码中,我们可以发现,变量self.y并没有定义,使用未定义的变量导致了编译报错。

解决方法

由于执行报错是因使用未定义变量引起,因此,解决的方法便是在网络的初始化函数__init__(self):中定义该变量。需要注意的是,只有将变量定义为self的成员变量,才能够在construct(self, x)方法中使用。

from mindspore import nn

class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.y = 1.0

    def construct(self, x):
        return self.y + x

net = Net()
output = net(1)

如果将self.y = 1.0写成y = 1.0, 同样会因为变量未定义而报错。

总结

MindSpore前端语法是Python语法的子集, 撰写模型时需要遵循Python语法。学习掌握一些python语言的特性,对于MindSpore运行机制理解和问题定位很有帮助。

参考文档

mindspore静态语法:静态图语法支持 — MindSpore master documentation

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值