【Mindspore产品】【自定义Ascend算子功能】使用export导出调用自定义算子的模型出错

问题描述:

【功能模块】

Mindspore.export

【环境说明】

ModelArts NoteBook

镜像:tensorflow1.15-mindspore1.5.1-cann5.0.3-euler2.8-aarch64

【操作步骤&问题现象】

1、使用TIK实现自定义CusAdd算子

2、构建使用了CusAdd算子的网络Net

3、使用export把Net导出成AIR格式

4、出现报错,显示找不到自定义算子CusAdd

【相关代码说明】

add_impl.py #tik实现自定义算子

cus_add.py #kernel definition & kernel binding

test_add.py # 实际调用CusAdd算子

add_impl.py

from tbe import tik

from mindspore.ops import op_info_register, TBERegOp, DataType



# Define the kernel info of CusAdd.

cus_add_op_info = TBERegOp("CusAdd") \

    .fusion_type("OPAQUE") \

    .partial_flag(True) \

    .async_flag(False) \

    .binfile_name("add.so") \

    .compute_cost(10) \

    .kernel_name("CusAddImpl") \

    .input(0, "x", False, "required", "all") \

    .input(0, "y", False, "required", "all") \

    .output(0, "z", False, "required", "all") \

    .dtype_format(DataType.F32_Default, DataType.F32_Default, DataType.F32_Default) \

    .dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.F16_Default) \

    .get_op_info()



# Binding kernel info with the kernel implementation.

@op_info_register(cus_add_op_info)

def CusAddImpl(input_x, input_y, output_z, kernel_name="CusAddImpl"):

    """

    The entry function of the CusAdd implementation.

    """



    x_shape = input_x.get("shape")

    x_dtype = input_x.get("dtype").lower()



    y_shape = input_y.get("shape")

    y_dtype = input_y.get("dtype").lower()



    z_shape = output_z.get("shape")

    z_dtype = output_z.get("dtype").lower()



    assert x_shape == y_shape == z_shape

    assert x_dtype == y_dtype == z_dtype



    tik_instance = tik.Tik()

    gm_x = tik_instance.Tensor(x_dtype, x_shape , name="gm_x", scope=tik.scope_gm)

    gm_y = tik_instance.Tensor(y_dtype, y_shape , name="gm_y", scope=tik.scope_gm)

    gm_z = tik_instance.Tensor(z_dtype, z_shape , name="gm_z", scope=tik.scope_gm)



    ubuf_x = tik_instance.Tensor(x_dtype, x_shape , name="ubuf_x", scope=tik.scope_ubuf)

    ubuf_y = tik_instance.Tensor(y_dtype, y_shape , name="ubuf_y", scope=tik.scope_ubuf)

    ubuf_z = tik_instance.Tensor(z_dtype, z_shape , name="ubuf_z", scope=tik.scope_ubuf)



    tik_instance.h_data_move(ubuf_x, gm_x)

    tik_instance.h_data_move(ubuf_y, gm_y)

    tik_instance.h_add(ubuf_z,ubuf_x, ubuf_y)

    tik_instance.h_data_move(gm_z, ubuf_z)

    tik_instance.BuildCCE(kernel_name=kernel_name, inputs=[gm_x, gm_y], outputs=[gm_z])

    return tik_instance



if __name__ == "__main__":

    import numpy as np

    data_x = np.ones((128,)).astype("float16")

    data_y = np.ones((128,)).astype("float16")

    data_z = np.ones((128,)).astype("float16")

    ADD_INSTANCE = CusAddImpl(data_x,data_y,data_z)

    feed_dict = {

        'gm_x': data_x,

        'gm_y': data_y,

    }

    data = ADD_INSTANCE.tikdb.start_debug(feed_dict=feed_dict, interactive=True)

    print(data)

Cus_Add.py



from mindspore.ops import prim_attr_register, PrimitiveWithInfer

import mindspore.ops as ops



# z = x + y

class CusAdd(PrimitiveWithInfer):

    @prim_attr_register

    def __init__(self):

        self.init_prim_io_names(inputs=['x', 'y'],outputs=['z'])

        from add_impl import CusAddImpl



    def infer_shape(self, x_data_shape, y_data_shape):

        return x_data_shape



    def infer_dtype(self, x_data_dtype, y_data_dtype):

        return x_data_dtype

test_add.py

import numpy as np

import mindspore.nn as nn

import mindspore.context as context

from mindspore import Tensor, export



from cus_add import CusAdd

context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")



class Net(nn.Cell):

    def __init__(self):

        super(Net, self).__init__()

        self.add = CusAdd()



    def construct(self, data_x, data_y):

        return self.add(data_x, data_y)



def test_net():

    x = np.ones((128)).astype(np.float32)

    y = np.ones((128)).astype(np.float32)

    add = Net()

    export(add, Tensor(x), Tensor(y), file_name="add", file_format="AIR")

    output = add(Tensor(x),Tensor(y))

    print("x: ", x)

    print("y: ", y)

    print("output: ", output)



test_net()

【截图信息】

 【日志信息】(可选,上传日志内容或者附件)

[ma-user my_operator]$python3 test_add.py

[EXCEPTION] GE_ADPT(1522,ffff9f3445d0,python3):2022-04-06-14:09:15.711.373 [mindspore/ccsrc/transform/graph_ir/convert.cc:124] FindAdapter] Can't find OpAdapter for CusAdd

Traceback (most recent call last):

  File "test_add.py", line 27, in <module>

    test_net()

  File "test_add.py", line 21, in test_net

    export(add, Tensor(x), Tensor(y), file_name="add", file_format="AIR")

  File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/train/serialization.py", line 751, in export

    _export(net, file_name, file_format, *inputs, **kwargs)

  File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/train/serialization.py", line 777, in _export

    graph_id, _ = _executor.compile(net, *inputs, phase=phase_name)

  File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/common/api.py", line 569, in compile

    self._build_data_graph(obj, phase)

  File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/common/api.py", line 499, in _build_data_graph

    self._graph_executor.build_data_graph(obj.parameters_dict(), phase, obj.parameters_broadcast_dict())

RuntimeError: mindspore/ccsrc/transform/graph_ir/convert.cc:124 FindAdapter] Can't find OpAdapter for CusAdd

解答:

自定义算子不支持310推理。910可以直接使用MindIR进行推理,不需要导出AIR,910上推理MindIR可以看下这个https://www.mindspore.cn/docs/programming_guide/zh-CN/master/multi_platform_inference_ascend_910.html。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值