MindSpore报错 StridedSlice这个算子在Ascend硬件上不支持input是uint8的数据类型

发布时间:2023-01-18 14:30

1 报错描述
1.1 系统环境
Hardware Environment(Ascend/GPU/CPU): CPU
Software Environment:
– MindSpore version (source or binary): 1.8.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 脚本
训练脚本是通过构建StridedSlice的单算子网络,对输入Tensor根据步长和索引进行切片提取。脚本如下:

01 class Net(nn.Cell):
02 def __init__(self,):
03 super(Net, self).__init__()
04 self.strided_slice = ops.StridedSlice()
05
06 def construct(self, x, begin, end, strides):
07 out = self.strided_slice(x, begin, end, strides)
08 return out
09 input_x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]],
10 [[5, 5, 5], [6, 6, 6]]], mindspore.uint8)
11 out = Net()(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1))
12 print(out)
1.2.2 报错
这里报错信息如下:

Traceback (most recent call last):
File "160945-strided_slice.py", line 19, in

out = Net()(input_x,  (1, 0, 2), (3, 1, 3), (1, 1, 1))

File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 574, in call

out = self.compile_and_run(*args)

File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 975, in compile_and_run

self.compile(*inputs)

File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 948, in compile

jit_config_dict=self._jit_config_dict)

File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/common/api.py", line 1092, in compile

result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())

TypeError: Operator[StridedSlice] input(UInt8) output(UInt8) is not supported. This error means the current input type is not supported, please refer to the MindSpore doc for supported types.

原因分析

我们看报错信息,在TypeError中,写到Operator[StridedSlice] input(UInt8) output(UInt8) is not supported.. This error means the current input type is not supported, please refer to the MindSpore doc for supported types,主要意思是在对于StridedSlice算子,在cpu上, uint数据类型的输入和输出类型目前是不被支持的。解决方法是切换到ascend/gpu平台上运行。

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

context.set_context(device_target='Ascend')
class Net(nn.Cell):

def __init__(self,):
    super(Net, self).__init__()
    self.strided_slice = ops.StridedSlice()

def construct(self, x, begin, end, strides):
    out = self.strided_slice(x, begin, end, strides)
    return out

input_x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]],

              [[5, 5, 5], [6, 6, 6]]], mindspore.uint8)

out = Net()(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1))
print(out)
此时执行成功,输出如下:

[[[3]]

[[5]]]

3 总结
定位报错问题的步骤:

1、找到报错的用户代码行:out = Net()(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1));

2、 根据日志报错信息中的关键字,缩小分析问题的范围:input(kNumberTypeUInt8) output(kNumberTypeUInt8) is not supported;

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

4 参考文档
4.1 StridedSlice算子API接口

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号