tvm交叉编译android opencl

模型编译:

#encoding:utf-8

import onnx

import numpy as np

import tvm

import tvm.relay as relay

import os

from tvm.contrib import ndk

onnx_model = onnx.load('mobilenet_v3_small.onnx')

x = np.ones([1,3,224,224])                      

input_name = 'input1'                                              

shape_dict = {input_name: x.shape}

sym, params = relay.frontend.from_onnx(onnx_model, shape_dict)

target = "opencl -device=adreno"

target_host = "llvm -mtriple=arm64-linux-android"

with tvm.transform.PassContext(opt_level=3):

    graph, lib, params = relay.build(sym, target=target, target_host=target_host, params=params)

lib.export_library("deploy.so", cc="/path_to_ndk/26.0.10792818/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android34-clang++")

graph_json_path = "deploy.json"

with open(graph_json_path, 'w') as fo:

    fo.write(graph)

param_path = "deploy.params"

with open(param_path, 'wb') as fo:

    fo.write(relay.save_param_dict(params))

--------------------------------------------------------------------------------------------------------------------------

c++代码:

#include <dlpack/dlpack.h>

#include <tvm/runtime/module.h>

#include <tvm/runtime/registry.h>

#include <tvm/runtime/packed_func.h>

#include <fstream>

#include <iterator>

#include <algorithm>

#include <chrono>

int main()

{

    // tvm module for compiled functions

    tvm::runtime::Module mod_syslib = tvm::runtime::Module::LoadFromFile("deploy.so");

    // json graph

    std::ifstream json_in("deploy.json", std::ios::in);

    std::string json_data((std::istreambuf_iterator<char>(json_in)), std::istreambuf_iterator<char>());

    json_in.close();

    // parameters in binary

    std::ifstream params_in("deploy.params", std::ios::binary);

    std::string params_data((std::istreambuf_iterator<char>(params_in)), std::istreambuf_iterator<char>());

    params_in.close();

    // parameters need to be TVMByteArray type to indicate the binary data

    TVMByteArray params_arr;

    params_arr.data = params_data.c_str();

    params_arr.size = params_data.length();

    int dtype_code = kDLFloat;

    int dtype_bits = 32;

    int dtype_lanes = 1;

    int device_type = kDLOpenCL;

    int device_id = 0;

    // get global function module for graph runtime

    tvm::runtime::Module mod = (*tvm::runtime::Registry::Get("tvm.graph_executor.create"))(json_data, mod_syslib, device_type, device_id);

    DLTensor* x;

    int in_ndim = 4;

    int64_t in_shape[4] = {1, 3, 224, 224};

    TVMArrayAlloc(in_shape, in_ndim, dtype_code, dtype_bits, dtype_lanes, device_type, device_id, &x);

    // load image data saved in binary

    std::ifstream data_fin("cat.bin", std::ios::binary);

    if(data_fin.is_open()){

        ;

    }else{

        std::cout << "@@@@@@@@ Failed to open cat.bin!!!" << std::endl;

    }

    float img[3*224*224] = {0.0f};

    // data_fin.read(static_cast<char*>(x->data), 3 * 224 * 224 * 4);

    data_fin.read((char*)img, 3 * 224 * 224 * sizeof(float));


 

    TVMArrayCopyFromBytes(x, img, 3 * 224 * 224 * sizeof(float));


 

    // get the function from the module(set input data)

    tvm::runtime::PackedFunc set_input = mod.GetFunction("set_input");

    set_input("input", x);

    // get the function from the module(load patameters)

    tvm::runtime::PackedFunc load_params = mod.GetFunction("load_params");

    load_params(params_arr);

    // get the function from the module(run it)

    tvm::runtime::PackedFunc run = mod.GetFunction("run");

    run();

    std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();

    const uint_t loops = 10000;

    for(uint_t i = 0; i < loops; i++){

        run();

    }

    std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now();

    auto elapsed = end_time - start_time;

    long long elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();

    std::cout << "elapsed time in ount: " << elapsed_time << std::endl;

    std::cout << "inference time per image: " << double(elapsed_time) / double(loops) << std::endl;


 

    DLTensor* y;

    int out_ndim = 2;

    int64_t out_shape[2] = {1, 1001};

    TVMArrayAlloc(out_shape, out_ndim, dtype_code, dtype_bits, dtype_lanes, device_type, device_id, &y);

    // get the function from the module(get output data)

    tvm::runtime::PackedFunc get_output = mod.GetFunction("get_output");

    get_output(0, y);

    // get the maximum position in output vector

    // auto y_iter = static_cast<float*>(y->data);

    float y_iter[1001] = {0};

    TVMArrayCopyToBytes(y, y_iter, 1001*sizeof(float));

    auto max_iter = std::max_element(y_iter, y_iter + 1001);

    auto max_index = std::distance(y_iter, max_iter);

    std::cout << "The maximum position in output vector is: " << max_index << std::endl;

    TVMArrayFree(x);

    TVMArrayFree(y);

    return 0;

}

其他的请参考:tvm交叉编译示例——android cpu-CSDN博客

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以按照以下步骤在Ubuntu上编译TVM: 1. 参考官方安装文档,确保您的系统环境是Ubuntu 18.04 LTS 64-bit,并根据文档中的说明进行准备工作。 2. 使用以下命令安装所需的依赖项: ``` sudo apt-get update sudo apt-get install -y python python-dev python-setuptools gcc libtinfo-dev zlib1g-dev build-essential cmake ``` 3. 在终端中使用以下命令下载TVM的源代码,并切换到源代码目录: ``` git clone --recursive https://github.com/apache/incubator-tvm.git cd incubator-tvm ``` 4. 执行以下命令编译TVM和LLVM部分: ``` mkdir build cp cmake/config.cmake build/ cd build cmake .. make -j4 ``` 5. 编译完成后,您可以在build目录中找到编译好的TVM库和可执行文件。 请注意,这里的步骤是基于Ubuntu 18.04 LTS 64-bit的环境进行的,其他环境或需求可能会有所不同。提供了更详细的信息和特定环境下的编译指南,您可以参考官方文档以满足您的需求。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Ubuntu下TVM的编译安装](https://blog.csdn.net/weixin_43953703/article/details/94344889)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [dl-infer-perf:深度学习推理性能分析](https://download.csdn.net/download/weixin_42138545/16493843)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值