# 报错：The 'add' operation does not support the type \[kMetaTypeNone, Int64\].

**1. 系统环境**

```
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
```

**2. 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)
```

**3. 报错信息**

```
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
```

**4. 原因分析**

首先看报错日志信息：

```
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](https://www.mindspore.cn/docs/note/zh-CN/r1.6/static_graph_syntax_support.html?highlight=kMetaTypeNone)

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

**5. 解决方法**

由于执行报错是因使用未定义变量引起，因此，解决的方法便是在网络的初始化函数\_\_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, 同样会因为变量未定义而报错。

**6. 总结**

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

**7. 参考文档**

mindspore静态语法：[https://www.mindspore.cn/docs/note/zh-CN/r1.6/static\_graph\_syntax\_support.html](https://www.mindspore.cn/docs/note/zh-CN/r1.6/static_graph_syntax_support.html)

