MindSpore报错:The supported input and output data types for the current operator are: node is Default/BitwiseAnd

1 报错描述

1.1 系统环境

Hardware Environment: Ascend
Software Environment:
-- MindSpore version: 2.3.0
--Python version: Python 3.8.15
--OS platform and distribution: EulerOS 2.0 (SP8)
--GCC/Compiler version (if compiled from source):

2. 报错信息

2.1 问题描述

TypeError: The supported input and output data types for the current operator are: node is Default/BitwiseAnd-op0

cke_248737.gif2.2 报错信息

Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
  File "/home/user/miniconda3/envs/ms/lib/python3.8/site-packages/mindspore/common/_stub_tensor.py", line 48, in fun  
    arg = (stub.stub_sync(),) + arg[1:]  
  File "/home/user/miniconda3/envs/ms/lib/python3.8/site-packages/mindspore/common/_stub_tensor.py", line 159, in stub_sync  
    val = self.stub.get_value()  
TypeError: The supported input and output data types for the current operator are: node is Default/BitwiseAnd-op0  
InputDesc [0] support {int16,int32,int64,int8,uint16,uint32,uint64,uint8,}  
InputDesc [1] support {int16,int32,int64,int8,uint16,uint32,uint64,uint8,}  
OutputDesc [0] support {int16,int32,int64,int8,uint16,uint32,uint64,uint8,}  
  
But current operator's input and output data types is:  
InputDesc [0] is Bool  
InputDesc [1] is Bool  
OutputDesc [0] is Bool

训练脚本如下:

mask = (label_inds == gti) & (pos_mask > 0)

3. 根因分析

根据报错信息可以知道,BitwiseAnd算子不支持bool数据类型,因此
1)首先确认BitwiseAnd是哪个运算符输入的,通过将代码拆分,再次执行可以看到是 & 符号引入的

x = (label_inds == gti)  
y = (pos_mask > 0)  
mask = x & y

2)接下来确认数据类型:打印x和y的类型,可以看到x和y是bool类型的Tensor

3 解决方法

基于上面已知的原因,很容易做出如下修改:

import mindspore as ms  
x = (label_inds == gti).to(ms.int32)  
y = (pos_mask > 0).to(ms.int32)  
mask = (x & y).to(ms.bool_)

再次执行,模型可以顺利跑通