MindSpore报错算子AddN的输入类型(kNumberTypeBool,kNumberTypeBool)和输出类型kNumberTypeBool不支持

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

python代码样例

from mindspore import context, ops, ms_function, Tensor, dtype

@ms_function
def test_logic(x, y):
    z = x and y
    return z and x

x = Tensor(True, dtype.bool_)
y = Tensor(True, dtype.bool_)
grad = ops.GradOperation(get_all=True)
grad_net = grad(test_logic)
out = grad_net(x, y)

2 报错信息

Traceback (most recent call last):
  File "test_backward_tensor.py", line 14, in <module>
    out = grad_net(x, y)
  File "C:\Users\46638\miniconda3\envs\mindspore\lib\site-packages\mindspore\common\api.py", line 360, in staging_specialize
    out = _MindsporeFunctionExecutor(func, ms_create_time, input_signature, process_obj)(*args)
  File "C:\Users\46638\miniconda3\envs\mindspore\lib\site-packages\mindspore\common\api.py", line 61, in wrapper
    results = fn(*arg, **kwargs)
  File "C:\Users\46638\miniconda3\envs\mindspore\lib\site-packages\mindspore\common\api.py", line 273, in __call__
    phase = self.compile(args_list, self.fn.__name__)
  File "C:\Users\46638\miniconda3\envs\mindspore\lib\site-packages\mindspore\common\api.py", line 256, in compile
    is_compile = self._graph_executor.compile(self.fn, args_list, phase, True)
TypeError: mindspore\ccsrc\runtime\device\cpu\kernel_select_cpu.cc:217 KernelNotSupportException] Operator[AddN]  input(kNumberTypeBool,kNumberTypeBool) output(kNumberTypeBool) is not supported. This error means the current input type is not supported, please refer to the MindSpore doc for supported types.

The function call stack:
In file C:\Users\46638\miniconda3\envs\mindspore\lib\site-packages\mindspore\ops\composite\multitype_ops\add_impl.py(287)/    return F.addn((x, y))/

报错信息中提示,当前算子AddN的输入类型(kNumberTypeBool,kNumberTypeBool)和输出类型kNumberTypeBool不支持。 同时,定位到报错代码行:return F.addn((x, y)),这里没有定位到用户的代码行。

3 原因分析

MindSpore当前对数据类型为bool的Tensor[后续简称Tensor(bool)]支持能力较弱,仅有少量算子支持Tensor(bool)类型的数据参与运算。若在正向图中使用了支持Tensor(bool)类型的算子且正向图语法正确,由于反向图求解全导数会引入AddNAddN不支持Tensor(bool)类型,反向图运行就会抛出该异常。

上述代码正向处理可以用公式表示为:r = f(z, x), z = z(x, y) 对应的全导数公式为:dr/dx = df/dz * dz/dx + df/dx, 函数f(z,x)z(x,y)均为逻辑运算符and; 正向图中的and算子支持Tensor(bool)类型,反向图求全导数时引入的AddN不支持Tensor(bool) 类型, 此时该错误无法对应到具体的正向代码行。

4 解决方法

遇到这类问题请去除对Tensor(bool)类型的使用,本例中将输入数据Tensor(bool)替换为bool即可解决问题。

from mindspore import context, ops, ms_function, Tensor, dtype

@ms_function
def test_logic(x, y):
    z = x and y
    return z and x

x = True
y = True
grad = ops.GradOperation(get_all=True)
grad_net = grad(test_logic)
out = grad_net(x, y)

总结

MindSpore当前对数据类型支持能力当前还不完善,在使用过程中如果遇到数据类型相关的错误,请参考官方API接口的文档说明。

参考文档

FAQ