MindSpore报错TypeError: ScalarAdd不支持bool类型

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): Ascend Software Environment: -- MindSpore version (source or binary): 1.6.0 -- Python version (e.g., Python 3.7.5): 3.7.6 -- OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic -- GCC/Compiler version (if compiled from source):

1.2 基本信息

1.2.1 脚本

训练脚本如下:

01 from mindspore import ops
02 x = False
03 y = True
04 output = ops.scalar_add(x, y)
05 print('output', output)

1.2.2 报错

这里报错信息如下:

[CRITICAL] ANALYZER(143497,ffffb898c010,python):2022-04-07-10:19:58.194.241 [mindspore/ccsrc/frontend/operator/cc_implementations.cc:227] ScalarAdd] Unsupported input type for ScalarAdd, type of x:BoolImm, value of x:false, type of y:BoolImm, value of y:true
Traceback (most recent call last):
  File "demo.py", line 4, in <module>
    output = ops.scalar_add(x, y)
  File "/lib/python3.7/site-packages/mindspore/ops/primitive.py", line 294, in __call__
    return _run_op(self, self.name, args)
  File "/lib/python3.7/site-packages/mindspore/common/api.py", line 89, in wrapper
    results = fn(*arg, **kwargs)
  File "/lib/python3.7/site-packages/mindspore/ops/primitive.py", line 754, in _run_op
    output = real_run_op(obj, op_name, args)
TypeError: mindspore/ccsrc/frontend/operator/cc_implementations.cc:227 ScalarAdd] Unsupported input type for ScalarAdd, type of x:BoolImm, value of x:false, type of y:BoolImm, value of y:true

原因分析
对于MindSpore 1.6版本,通过官方API接口,构建简单单算子用例。在报错信息中,写到 Unsupported input type for ScalarAdd, type of x:BoolImm, value of x:false, type of y:BoolImm, value of y:true ,意思是ScalarAdd不支持bool类型,需要对代码进行修正。在官网对scalar_add也做了相应的描述,即该算子用法和Python中的‘+’,显然,在Python中bool类型是不能进行求和运算。

因此需要对传入的数据进行修改。

2 解决方法

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

此时执行成功,输出如下:

result: 1

3 总结

定位报错问题的步骤:

1、 找到报错的用户代码行:output = ops.scalar_add(x, y)

2、 根据日志报错信息中的关键字,缩小分析问题的范围: Unsupported input type for ScalarAdd, type of x:BoolImm

3、需要重点关注变量定义、初始化的正确性。

4 参考文档

4.1 ScalarAdd算子API接口