当前位置: 首页 > news >正文

【mllm】——qnn后端解读

llm, qnn, qualcomm
源码:https://github.com/UbiquitousLearning/mllm

1.qnn后端的接入

这里只是给一个核心的思路,不涉及具体的代码实现。

前言:

  • qualcomm htp后端,因为支持的数据类型有限,int8/int16/float16,如果要用int数据类型加速,需要对模型进行量化。mllm中使用的是int8,但是不是整个整个网络使用int8,而是部分使用,同时qnn npu推理时是cpu+npu混合的。

实现:

  • 因为qualcomm sdk使用方式的限制,没有单独使用qualcomm op进行计算的方式,需要构建graph。所以mllm中将decoder部分拆成几个qnn graph。具体代码如下:
Backend::global_backends[device_]->onSetUpStart(inputs_vec, outputs_vec, getUinqueName());

                // for xnnpack currently
                for (auto &i : inputs) {
                    i.uuid() = inputs[0].module()->activation_tensors[i.name()]->uuid();
                }

                auto outputs = Forward(inputs, anyArgs);
                for (auto &output : outputs) {
                    outputs_vec.push_back(inputs[0].module()->activation_tensors[output.name()]);
                }
                Backend::global_backends[device_]->onSetUpEnd(inputs_vec, outputs_vec, getUinqueName());

qnnbackend.onsetupstart中会初始化QnnModel

qnnModels_.push_back(qnn_wrapper_api::QnnModel());
    // create qnn context, assign context to qnn memory manager
    if (StatusCode::SUCCESS != this->createContext()) {
        this->reportError("Context Creation failure");
    }
#ifdef QNN_ARM
    auto qnnMM = std::static_pointer_cast<QNNMemoryManager>(mem_manager_);
    qnnMM->setQnnInterfaceAndContext(m_context);
#endif

    // initialize qnn graph info, set graph info, graph count
    // NOTE: currently not using it
    QnnHtpGraph_CustomConfig_t customConfig;
    // customConfig.option = QNN_HTP_GRAPH_CONFIG_OPTION_NUM_HVX_THREADS;
    // customConfig.numHvxThreads = 4; // set a number. MAX = number of HVX HW blocks for that SoC
    customConfig.option = QNN_HTP_GRAPH_CONFIG_OPTION_VTCM_SIZE;
    customConfig.vtcmSizeInMB = 8;

    QnnGraph_Config_t graphConfig;
    graphConfig.option = QNN_GRAPH_CONFIG_OPTION_CUSTOM;
    graphConfig.customConfig = &customConfig;

    const QnnGraph_Config_t *pGraphConfig[] = {&graphConfig, NULL};

    const QnnGraph_Config_t **graphConfigs = pGraphConfig;

    m_graphConfigsInfoCount = 1;

    qnn_wrapper_api::ModelError_t err = qnn_wrapper_api::getQnnGraphConfigFromInfo(
        graphName.c_str(), (const qnn_wrapper_api::GraphConfigInfo_t **)m_graphConfigsInfo, m_graphConfigsInfoCount, graphConfigs);
    if (err != qnn_wrapper_api::MODEL_NO_ERROR) {
        this->reportError("Graph Config Info failure");
    }

    err = qnnModels_[qnnModelIndex_].initialize(m_backendHandle,
                                                m_qnnFunctionPointers.qnnInterface,
                                                m_context,
                                                graphName.c_str(),
                                                m_debug,
                                                DO_GRAPH_NODE_VALIDATIONS,
                                                graphConfigs);
    if (err != qnn_wrapper_api::MODEL_NO_ERROR) {
        this->reportError("Graph Initialization failure: " + graphName);
    }

后续再通过op.setup方法往QnnModel中添加tensor和op,如QNNLinearINT8

graphAddNode(name() + ".linearint8", "Conv2d", {inputs[0]->name(), weight_.name()}, matmulOut, params_InceptionV3_InceptionV3_Conv2d_1a_3x3_Conv2D);

2. other

这都是qualcomm sdk中op不支持单op后端支持,必须要构建model来调用。不友好。

相关文章:

  • Linux多线程同步与互斥:从互斥锁原理到死锁防范的深度实践
  • Tkinter事件与绑定
  • 计算机组成原理笔记(十五)——3.5指令系统的发展
  • 使用FormData格式上传图片
  • zk(Zookeeper)实现分布式锁
  • Java基本数据类型与包装类的区别
  • Linux安装开源版MQTT Broker——EMQX服务器环境从零到一的详细搭建教程
  • Linux驱动开发-网络设备驱动
  • 游戏引擎学习第216天
  • Python 的安装与快速入门
  • 联想电脑开机出现Defalut Boot Device Missing or Boot Failed怎么办
  • nfs共享目录主配置文件权限参数
  • 从“被动跳闸”到“主动预警”:智慧用电系统守护老旧小区安全
  • 为什么我们需要if __name__ == __main__:
  • 十五届蓝桥杯Scratch03月stema选拔赛真题——回文数
  • HTML5 服务器发送事件(Server-Sent Events)
  • YOLOv11改进——注意力机制优化 | 引入SpatialGroupEnhance空间分组增强模块
  • WebGIS 学习路线分享
  • 第二期:[特殊字符] 深入理解MyBatis[特殊字符]MyBatis基础CRUD操作详解[特殊字符]
  • (二十三)安卓开发中数据存储之Room详解
  • 中国海外发展:今年前4个月销售665.8亿元,花费305亿元拿地
  • 古埃及展进入百天倒计时,闭幕前168小时不闭馆
  • 深圳两家会所涉卖淫嫖娼各被罚7万元逾期未缴,警方发催告书
  • 硅料巨亏后弘元绿能割肉求生:逾12亿元内蒙古公司股权转让协鑫
  • 圆桌丨中俄权威专家详解:两国携手维护战后国际秩序,捍卫国际公平正义
  • 西安碑林博物馆票价将调至85元,工作人员:10元属于改扩建期间惠民票