[Solved] MindSpore Error: “operation does not support the type kMetaTypeNone“

Environment:
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 code examples

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)

Error reporting information

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

 

Solution:

Since the execution error is caused by using an undefined variable, the solution is to define the variable in the network’s initialization function __init__(self):. Note that only variables defined as members of self can be used in the construct(self, x) method.

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)

If you write self.y = 1.0 instead of y = 1.0, you will also get an error because the variable is undefined.

Read More: