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

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 "C:/Users/l30026544/PycharmProjects/q2_map/new/160945-strided_slice.py", line 20, in <module>
    out = Net()(input_x,  (1, 0, 2), (3, 1, 3), (1, 1, 1))
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 586, in __call__
    out = self.compile_and_run(*args)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 964, in compile_and_run
    self.compile(*inputs)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 937, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\common\api.py", line 1006, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
TypeError: mindspore\ccsrc\plugin\device\cpu\hal\device\kernel_select_cpu.cc:226 KernelNotSupportException] Operator[StridedSlice]  input(kNumberTypeUInt16) output(kNumberTypeUInt16) 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(kNumberTypeUInt16) output(kNumberTypeUInt16) 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)

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

output 

[[[3]]

 [[5]]]

3 总结

定位报错问题的步骤:

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

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

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

4 参考文档

4.1 StridedSlice算子API接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值