news 2026/5/9 14:42:29

CANN/ops-math矩阵乘法压缩反量化算子

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CANN/ops-math矩阵乘法压缩反量化算子

aclnnMatmulCompressDequant

【免费下载链接】ops-math本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。项目地址: https://gitcode.com/cann/ops-math

📄 查看源码

产品支持情况

产品是否支持
Ascend 950PR/Ascend 950DT×
Atlas A3 训练系列产品/Atlas A3 推理系列产品×
Atlas A2 训练系列产品/Atlas A2 推理系列产品×
Atlas 200I/500 A2 推理产品×
Atlas 推理系列产品
Atlas 训练系列产品×
Atlas 200/300/500 推理产品×

功能说明

  • 接口功能:进行l@r矩阵乘计算时,可先通过msModelSlim工具对r矩阵进行无损压缩,减少r矩阵的内存占用大小,然后通过本接口完成无损解压缩、矩阵乘、反量化计算。

  • 计算公式:

    $$ x2_unzip = unzip(x2, compressIndex, compressInfo)\ result=(x1 @ x2_unzip + bias)*deqScale $$

    其中x2表示r矩阵经过msModelSlim工具进行压缩后的一维数据,compressIndex以及compressInfo表示压缩算法相关的信息,$x2_unzip$是本接口内部进行无损解压缩后的数据(与原始r矩阵数据一致),压缩和调用本接口的详细使用样例参考调用示例。

函数原型

每个算子分为两段式接口,必须先调用“aclnnMatmulCompressDequantGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnMatmulCompressDequant”接口执行计算。

aclnnStatus aclnnMatmulCompressDequantGetWorkspaceSize( const aclTensor* x1, const aclTensor* x2, const aclTensor* compressIndex, const aclTensor* bias, const aclTensor* deqScale, const aclTensor* offsetW, int offsetX, const aclIntArray* compressInfo, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
aclnnStatus aclnnMatmulCompressDequant( void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)

aclnnMatmulCompressDequantGetWorkspaceSize

  • 参数说明

    参数名输入/输出描述使用说明数据类型数据格式维度(shape)非连续tensor
    x1输入表示矩阵乘的左输入。-INT8ND2-
    x2输入表示压缩后的矩阵乘的右输入,为通过msModelSlim工具中weight_compression模块压缩后的输入。-INT8ND1-
    compressIndex输入表示矩阵乘右输入的压缩索引表。通过示例中的msModelSlim工具中获取INT8ND1-
    bias输入参与矩阵乘计算的偏置项。支持空指针传入。INT8ND2维,shape仅支持(1, n)或者(n),其中n为输出shape(m, n)的n-
    deqScale输入表示反量化参数。tensor中的值为float通过下述示例中转换后的UINT64的数据。UINT64ND2维,shape支持(1, n)或者(1, 1), 其中n为输出shape(m, n)中的n。-
    offsetW输入标量,表示矩阵乘右输入的偏移量。当前仅支持空指针传入。INT8-与x2_unzip一致。-
    offsetX输入标量,表示矩阵乘左输入的偏移量。当前仅支持0。INT32---
    compressInfo输入整型数据列表,数据类型为INT64。其中包括压缩块信息tilingN、tilingK(通过msModelSlim工具中weight_compression模块压缩后获取,分别表示压缩前shape(n, k)在n方向和k方向上一个基本压缩块的大小),压缩前x2矩阵原始shape(shape为2维,用(n, k)表示),以及压缩块遍历方向的标识。-INT64---
    out输出计算输出。-FLOAT16ND2-
    workspaceSize出参返回需要在Device侧申请的workspace大小。-----
    executor出参返回op执行器,包含了算子计算流程。-----
  • 返回值

    aclnnStatus:返回状态码,具体参见aclnn返回码。

    第一段接口完成入参校验,出现以下场景时报错:

    返回值错误码描述
    ACLNN_ERR_PARAM_NULLPTR161001传入的x1、x2或out是空指针。
    ACLNN_ERR_PARAM_INVALID161002x1或x2的数据类型和数据格式不在支持的范围之内。
    x1或x2无法做数据类型推导。
    推导出的数据类型无法转换为指定输出out的类型。

aclnnMatmulCompressDequant

  • 参数说明

    参数名输入/输出描述
    workspace输入在Device侧申请的workspace内存地址。
    workspaceSize输入在Device侧申请的workspace大小,由第一段接口aclnnMatmulCompressDequantGetWorkspaceSize获取。
    executor输入op执行器,包含了算子计算流程。
    stream输入指定执行任务的Stream。
  • 返回值

    aclnnStatus:返回状态码,具体参见aclnn返回码。

约束说明

  • 确定性计算:
    • aclnnMatmulCompressDequant默认确定性实现。

调用示例

  • 准备压缩前的数据

    假设通过脚本gen_data.py生成输入数据,示例如下,仅供参考:

    import numpy as np import os import sys from numpy import random def write2file(data, path): with open(path, 'wb') as f: data.tofile(f) if not os.path.exists("./data"): os.mkdir("./data") if len(sys.argv) != 4: print("Usage: python gen_data.py m k n") sys.exit(1) m = int(sys.argv[1]) k = int(sys.argv[2]) n = int(sys.argv[3]) if m <= 0 or k <= 0 or n <= 0: print("Error: m, k and n must be positive integers.") sys.exit(1) # 随机生成矩阵mat1,shape为(m,k ) mat1 = random.randn(m, k).astype(np.int8) write2file(mat1, "./data/mat1.bin") # 随机生成矩阵mat2,shape为(n, k) mat2 = random.randint(0, 100, size=(n, k)).astype(np.int8) np.save("./data/weight.npy", {'weight': mat2}) os.chmod("./data/weight.npy", 0o0640) # 生成output output = np.random.randn(m, n).astype(np.float16) write2file(output, "./data/output.bin") # 生成bias bias = random.randn(n).astype(np.float32) write2file(bias, "./data/bias.bin") # 生成deq_scale deq_scale = random.randn(n).astype(np.float32) write2file(deq_scale, "./data/deqScale_ori.bin") deq_scale_int64 = np.fromfile("./data/deqScale_ori.bin", dtype=np.int32).astype(np.int64) deq_scale_int64.tofile("./data/deqScale.bin")

    执行gen_data.py,假设mat1和mat2的shape入参为m=512、k=1024、n=1024。

    python3 gen_data.py 512 1024 1024
  • 对数据进行预处理

    • 原始权重通过msModelSlim压缩工具生成压缩后的x2、compressIndex以及compressInfo

      使用以下接口时,需对CANN包中msModelSlim压缩工具进行编译,具体操作参考Gitee msit仓中msmodelslim/pytorch/weight_compression目录下的README.md。

      from msmodelslim.pytorch.weight_compression import CompressConfig, Compressor compress_config = CompressConfig(do_pseudo_sparse=False, sparse_ratio=1) compressor = Compressor(compress_config, weight_path=weight_path) compress_weight, compress_index, compress_info = compressor.run() # 压缩后的权重,对应aclnnMatmulCompressDequantGetWorkspaceSize接口的x2 compressor.export(compress_weight, './data/weight') # 压缩权重的索引,对应aclnnMatmulCompressDequantGetWorkspaceSize接口的compressIndex compressor.export(compress_index, './data/index') # 压缩数据的相关信息,对应aclnnMatmulCompressDequantGetWorkspaceSize接口的compressInfo compressor.export(compress_info, './data/compress_info')
    • 将原始float类型的反量化参数deqscale进行转换, 得到aclnn接口需要的uint64数据

      deqScale原始为float类型,以int32读取并转换为int64

      import numpy as np data = np.fromfile('./deqScale_original.bin', dtype=np.int32).astype(np.int64) data.tofile('./deqScale.bin')
  • 调用aclnn接口运算

    示例代码如下,仅供参考,具体编译和执行过程请参考编译与运行样例。

#include <iostream> #include <vector> #include <acl/acl.h> #include <aclnnop/aclnn_matmul_compress_dequant.h> #include <fstream> #include <unistd.h> #include <sys/stat.h> #include <stdio.h> #include <cstdlib> #include <string> #define CHECK_RET(cond, return_expr) \ do { \ if (!(cond)) { \ return_expr; \ } \ } while (0) #define LOG_PRINT(message, ...) \ do { \ printf(message, ##__VA_ARGS__); \ } while (0) int64_t GetShapeSize(const std::vector<int64_t>& shape) { int64_t shapeSize = 1; for (auto i : shape) { shapeSize *= i; } return shapeSize; } int Init(int32_t deviceId, aclrtStream* stream) { // 固定写法,资源初始化 auto ret = aclInit(nullptr); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); ret = aclrtSetDevice(deviceId); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); ret = aclrtCreateStream(stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); return 0; } int ReadBinFileNNop(std::string filePath, void* buffer, size_t bufferSize) { struct stat sBuf; int fileStatus = stat(filePath.data(), &sBuf); CHECK_RET(fileStatus == ACL_SUCCESS, LOG_PRINT("Failed to get file %s\n", filePath); return -1); std::ifstream file; file.open(filePath, std::ios::binary); CHECK_RET(file.is_open(), LOG_PRINT("Open file failed.\n"); return -1); file.seekg(0, file.end); uint64_t binFileBufferLen = file.tellg(); CHECK_RET(binFileBufferLen > 0, std::cout<<"File size is 0.\n"; file.close(); return -1); file.seekg(0, file.beg); file.read(static_cast<char *>(buffer), binFileBufferLen); file.close(); return ACL_SUCCESS; } int CreateAclTensor(std::string filePath, const std::vector<int64_t>& shape, int typeSize, void** deviceAddr, aclDataType dataType, aclTensor** tensor) { auto size = GetShapeSize(shape) * typeSize; // 调用aclrtMalloc申请device侧内存 auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); // 调用aclrtMallocHost申请host侧内存 void* binBufferHost = nullptr; ret = aclrtMallocHost(&binBufferHost, size); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMallocHost failed. ERROR: %d\n", ret); return ret); // 读取文件 ret = ReadBinFileNNop(filePath, binBufferHost, size); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("ReadBinFileNNop failed. ERROR: %d\n", ret); return ret); // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 ret = aclrtMemcpy(*deviceAddr, size, binBufferHost, size, ACL_MEMCPY_HOST_TO_DEVICE); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); // 计算连续tensor的strides std::vector<int64_t> strides(shape.size(), 1); for (int64_t i = shape.size() - 2; i >= 0; i--) { strides[i] = shape[i + 1] * strides[i + 1]; } // 调用aclCreateTensor接口创建aclTensor *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); return 0; } int main(int argc, char* argv[]) { // 1. (固定写法)device/stream初始化,参考acl API手册 // 根据自己的实际device填写deviceId int32_t deviceId = 0; aclrtStream stream; auto ret = Init(deviceId, &stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); if (argc != 6) { std::cerr << "Error: Invalid number of arguments. Usage: <program> m k n wCompressedSize indexSize" << std::endl; return -1; } // 2. 构造输入与输出,需要根据API的接口自定义构造 int m = atoi(argv[1]); int k = atoi(argv[2]); int n = atoi(argv[3]); // wShape是右矩阵压缩后数据的大小 int wCompressedSize = atoi(argv[4]); // indexShape是压缩索引数据的大小 int indexSize = atoi(argv[5]); if (m <= 0 || k <= 0 || n <= 0 || wCompressedSize <= 0 || indexSize <= 0) { std::cerr << "Error: m, k, n, wCompressedSize and indexSize must be positive integers." << std::endl; return -1; } std::vector<int64_t> mat1Shape = {m, k}; std::vector<int64_t> mat2CompressedShape = {wCompressedSize}; std::vector<int64_t> indexShape = {indexSize}; std::vector<int64_t> biasShape = {n}; std::vector<int64_t> deqScaleShape = {n}; std::vector<int64_t> outputShape = {m, n}; std::vector<int64_t> compressInfoHostData = {8, 8, k, n, 1}; void* mat1DeviceAddr = nullptr; void* mat2CompressedDeviceAddr = nullptr; void* indexDeviceAddr = nullptr; void* biasDeviceAddr = nullptr; void* deqScaleDeviceAddr = nullptr; void* outputDeviceAddr = nullptr; aclTensor* mat1 = nullptr; aclTensor* mat2Compressed = nullptr; aclTensor* index = nullptr; aclTensor* bias = nullptr; aclTensor* deqScale = nullptr; aclTensor* output = nullptr; aclIntArray* compressInfo = nullptr; std::string rootPath = "./data/"; // 创建mat1 aclTensor std::string mat1FilePath = rootPath + "mat1.bin"; ret = CreateAclTensor(mat1FilePath, mat1Shape, sizeof(int8_t), &mat1DeviceAddr, aclDataType::ACL_INT8, &mat1); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create mat1 tensor failed. ERROR: %d\n", ret); return ret); // 创建mat2Compressed aclTensor std::string mat2FilePath = rootPath + "weight/weight.dat"; ret = CreateAclTensor(mat2FilePath, mat2CompressedShape, sizeof(int8_t), &mat2CompressedDeviceAddr, aclDataType::ACL_INT8, &mat2Compressed); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create mat2 tensor failed. ERROR: %d\n", ret); return ret); // 创建index aclTensor std::string indexFilePath = rootPath + "index/weight.dat"; ret = CreateAclTensor(indexFilePath, indexShape, sizeof(int8_t), &indexDeviceAddr, aclDataType::ACL_INT8, &index); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create index tensor failed. ERROR: %d\n", ret); return ret); // 创建bias aclTensor std::string biasFilePath = rootPath + "bias.bin"; ret = CreateAclTensor(biasFilePath, biasShape, sizeof(int32_t), &biasDeviceAddr, aclDataType::ACL_INT32, &bias); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create bias tensor failed. ERROR: %d\n", ret); return ret); // 创建deqScale aclTensor std::string deqScaleFilePath = rootPath + "deqScale.bin"; ret = CreateAclTensor(deqScaleFilePath, deqScaleShape, sizeof(int32_t), &deqScaleDeviceAddr, aclDataType::ACL_UINT64, &deqScale); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create deqScale tensor failed. ERROR: %d\n", ret); return ret); // 创建compressInfo compressInfo = aclCreateIntArray(compressInfoHostData.data(), aclDataType::ACL_INT64); // 创建out aclTensor std::string outputFilePath = rootPath + "output.bin"; ret = CreateAclTensor(outputFilePath, outputShape, 2, &outputDeviceAddr, aclDataType::ACL_FLOAT16, &output); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create output tensor failed. ERROR: %d\n", ret); return ret); int32_t offsetX = 0; // 3. 调用CANN算子库API,需要修改为具体的Api名称 uint64_t workspaceSize = 0; aclOpExecutor* executor; // 调用aclnnMm第一段接口 ret = aclnnMatmulCompressDequantGetWorkspaceSize(mat1, mat2Compressed, index, bias, deqScale, nullptr, offsetX, compressInfo, output, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMatmulCompressDequantGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); // 根据第一段接口计算出的workspaceSize申请device内存 void* workspaceAddr = nullptr; if (workspaceSize > 0) { ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); } // 调用aclnnMm第二段接口 ret = aclnnMatmulCompressDequant(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMatmulCompressDequant failed. ERROR: %d\n", ret); return ret); // 4. (固定写法)同步等待任务执行结束 ret = aclrtSynchronizeStream(stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 auto size = GetShapeSize(outputShape); std::vector<float> resultData(size, 0); ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outputDeviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); } // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 aclDestroyTensor(mat1); aclDestroyTensor(mat2Compressed); aclDestroyTensor(index); aclDestroyTensor(bias); aclDestroyTensor(deqScale); aclDestroyTensor(output); aclDestroyIntArray(compressInfo); // 7.释放硬件资源,需要根据具体API的接口定义修改 aclrtFree(mat1DeviceAddr); aclrtFree(mat2CompressedDeviceAddr); aclrtFree(indexDeviceAddr); aclrtFree(biasDeviceAddr); aclrtFree(deqScaleDeviceAddr); aclrtFree(outputDeviceAddr); if (workspaceSize > 0) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }

【免费下载链接】ops-math本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。项目地址: https://gitcode.com/cann/ops-math

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 14:39:34

CANN/asc-tools msopgen算子模板样例

【免费下载链接】asc-tools Ascend C Tools仓是CANN基于Ascend C编程语言推出的配套调试工具仓。 项目地址: https://gitcode.com/cann/asc-tools msopgen使用新自定义算子工程模板样例 概述 本样例介绍msopgen工具按照新算子工程模板生成新自定义算子工程。以AddCust…

作者头像 李华
网站建设 2026/5/9 14:37:45

工程教育AI能力框架:角色化教学破解AI融合难题

1. 项目概述&#xff1a;为什么工程教育需要一套“角色化”的AI能力框架&#xff1f;如果你是一位机械、电气或土木工程专业的教授&#xff0c;面对“在你的专业课里融入人工智能内容”这个要求&#xff0c;第一反应是什么&#xff1f;是觉得自己的AI知识储备不够&#xff0c;无…

作者头像 李华
网站建设 2026/5/9 14:36:37

CANN/pyasc稀疏矩阵乘加API

asc.language.basic.mmad_with_sparse 【免费下载链接】pyasc 本项目为Python用户提供算子编程接口&#xff0c;支持在昇腾AI处理器上加速计算&#xff0c;接口与Ascend C一一对应并遵守Python原生语法。 项目地址: https://gitcode.com/cann/pyasc asc.language.basic.…

作者头像 李华
网站建设 2026/5/9 14:34:44

DoWhy因果推断库实战:从理论到业务评估的完整指南

1. 项目概述&#xff1a;为什么我们需要一个专门的因果推断库&#xff1f;在数据科学和机器学习领域&#xff0c;预测模型已经相当成熟了。我们能用XGBoost精准预测用户流失&#xff0c;用神经网络识别图像&#xff0c;但当我们被问到“如果我们把产品价格降低10%&#xff0c;销…

作者头像 李华
网站建设 2026/5/9 14:33:42

PyTorch、TensorFlow与Keras工程选型实操指南

1. 这不是“选哪个更好”的站队指南&#xff0c;而是三年踩坑后我画的三张实操地图你点开这个标题&#xff0c;大概率正站在一个真实而具体的十字路口&#xff1a;手头有个图像分类项目要启动&#xff0c;老板说“尽快出效果”&#xff0c;数据集刚清洗完还带着热气&#xff0c…

作者头像 李华
网站建设 2026/5/9 14:29:30

谷歌智能眼镜2026年将问世,Gemini驱动,多品牌合作亮点多!

谷歌智能眼镜即将登场去年12月&#xff0c;有人试用了尚在开发阶段的多款Google Glasses。很快&#xff0c;最终版本就能买到。具体推出时间和售价&#xff0c;再过几天或许会有更多消息。尽管Meta是致力于以眼镜形式抢占人们面部设备市场的最大科技公司&#xff0c;但并非唯一…

作者头像 李华