LLaMA-Factory-310P3/mindie/examples/models/bge/large-zh-v1.5
wql faa909dcc3 add: add mindie file 2024-09-10 15:38:33 +08:00
..
README.md add: add mindie file 2024-09-10 15:38:33 +08:00
bin2onnx.py add: add mindie file 2024-09-10 15:38:33 +08:00
config_bge.json add: add mindie file 2024-09-10 15:38:33 +08:00
configuration_bert.py add: add mindie file 2024-09-10 15:38:33 +08:00
convert.sh add: add mindie file 2024-09-10 15:38:33 +08:00
demo.py add: add mindie file 2024-09-10 15:38:33 +08:00
eval_cmteb.py add: add mindie file 2024-09-10 15:38:33 +08:00
eval_performance.py add: add mindie file 2024-09-10 15:38:33 +08:00
infer.py add: add mindie file 2024-09-10 15:38:33 +08:00
main.py add: add mindie file 2024-09-10 15:38:33 +08:00
modeling_bert_ascend.py add: add mindie file 2024-09-10 15:38:33 +08:00
ops_info.json add: add mindie file 2024-09-10 15:38:33 +08:00
requirements.txt add: add mindie file 2024-09-10 15:38:33 +08:00

README.md

README

特性矩阵

  • 此矩阵罗列了各bge-large-zh模型支持的特性
模型及参数量 800I A2 Tensor Parallelism 300I DUO Tensor Parallelism FP16 BF16 Flash Attention Paged Attention W8A8量化 W8A16量化 W4A16量化 KV cache量化 稀疏量化 MOE量化 MindIE Service TGI 长序列
bge-large-zh 支持world size 1 支持world size 1 × × × × × × × × × × × ×

离线模型版本

模型介绍

bge-large-zh是由智源研究院研发的中文版文本表示模型可将任意文本映射为低维稠密向量以用于检索、分类、聚类或语义匹配等任务并可支持为大模型调用外部知识。其中1.5版本的相似度分布更加合理

开源模型地址

Commit-id 79e7739b6ab944e86d6171e44d24c997fc1e0116

模型转换流程

首先获取huggingface开源模型将其转换为ONNX格式再使用Ascend ATC工具将ONNX格式的模型转换为om格式我们主要关注该模型在昇腾设备上的精度和性能表现。

变量名称解释

变量名 含义
save_directory onnx模型以及转换后om离线模型存放目录
soc_version 昇腾AI处理器的版本可以通过执行npu-smi info 命令查询在查询到的型号前加Ascend信息例如Ascend910B4、Ascend310P3
precision_mode_v2 设置网络模型的精度模式。例如:fp16、mixed_float16、origin
cur_dir 运行指令或执行脚本时的路径(当前目录)
device_id npu芯片的id,在装了CANN驱动的服务器上使用npu-smi info查看可用的npu芯片的id

安装python依赖

cd ${cur_dir}
pip install -r requirements.txt

安装ais_bench推理工具

ais_bench推理工具使用指南

  • 需安装aclruntime包和ais_bench推理程序包

开源模型转换onnx格式

cd ${cur_dir}
python bin2onnx.py --model_path ${save_directory}

onnx转换om离线模型

在环境上使用昇腾ATC将onnx格式转换为om格式的离线模型

  • ATC工具集成在CANN中source相应的环境变量即可
source /usr/local/Ascend/ascend-toolkit/set_env.sh

在 ${cur_dir} 下运行脚本

atc --model=${save_directory}/model.onnx --framework=5 --output=${save_directory}/bge --soc_version=${soc_version} --input_shape="input_ids:-1,-1;attention_mask:-1,-1;token_type_ids:-1,-1" --optypelist_for_implmode="Gelu" --op_select_implmode=high_performance --input_format=ND --precision_mode_v2=${precision_mode} --modify_mixlist=${cur_dir}/ops_info.json

参数说明

  • bert模型的三个输入依次为input_idsattention_masktoken_type_ids 按顺序指定模型输入数据的shape。

  • 参照ATC说明文档设置shape范围时若设置为 -1表示此维度可以使用 >=0 的任意取值,该场景下取值上限为 int64 数据类型表达范围但受限于host和device侧物理内存的大小用户可以通过增大内存来支持。

  • Gelu算子在不影响精度的情况下开启高性能模式提升模型性能

  • 所配置的精度模式不同,网络模型精度以及性能有所不同,具体为:

精度高低排序:origin>mixed_float16>fp16

性能优劣排序:fp16>=mixed_float16>origin

推荐配置: mixed_float16

  • modify_mixlist参数为配置混合精度下的黑白灰名单目的是控制在fp16精度溢出的算子保持原精度格式避免其溢出这里定义了一个将算子写入黑名单的json文件

获取测试数据集

cd ${cur_dir}
mkdir dataset
cd dataset

corpus、queriesdev下载到该路径下

离线模型推理脚本指南

  • om模型推理脚本的启动路径为${cur_dir}/infer.py
  • hf开源模型推理脚本的启动路径为${cur_dir}/demo.py

在昇腾机器上运行python infer.py --model-path ${save_directory} --device ${device_id}

或者GPU的权重存放路径上运行python demo.py

  • **说明:**执行infer.py时脚本会运行模型存放的目录的第一个以.om为结尾的模型若想指定某个om模型可以在infer.py中修改 session = InferSession(device_id=device, model_path=model_path)model_path 为${save_directory}/*.om

其中,*为OM离线模型文件名。

精度 & 性能测试

  • 修改Config_bge.json内的模型路径为各模型所在的相应路径

  • 精度测试脚本

python eval_cmteb.py --model_type_or_path om --device ${device_id}
  • 性能测试脚本
python eval_performance.py --model_type_or_path om --input_shape [batch_size, seq_len] --device ${device_id}

模型推理性能

性能验证NPU环境使用 OM 模型GPU环境使用 ONNX 模型

吞吐率1000 * batch_size / compute_time

环境 芯片型号 batch_size seq_len 吞吐率fps
NPU Ascend310P3 8 100 449.22
NPU Ascend310P3 20 512 39.40
NPU Ascend310P3 128 512 39.63
GPU NVIDIA A10 8 100 149.93
GPU NVIDIA A10 20 512 48.21
GPU NVIDIA A10 128 512 49.38

说明Atlas 300I Duo 推理卡为单卡双芯比较吞吐率时需要×2

环境 芯片型号 batch_size seq_len 吞吐率fps
NPU Ascend910B4 8 100 696.06
NPU Ascend910B4 20 512 132.96
NPU Ascend910B4 128 512 123.94
GPU NVIDIA L20 8 100 384.60
GPU NVIDIA L20 20 512 112.80
GPU NVIDIA L20 128 512 104.37

模型推理精度

精度验证NPU环境使用 OM 模型GPU环境使用 ONNX 模型

环境 芯片型号 ndcg@10%
NPU Ascend310P3 83.66
GPU Nvidia A10 83.67
环境 芯片型号 ndcg@10%
NPU Ascend910B4 83.86
GPU Nvidia L20 83.67

Ascend310P3性能说明

在昇腾310P3上需要进行一项操作来发挥出算子更好的性能

  1. SoftmaxV2使能VectorCore需要在以下路径的json文件中找到SoftmaxV2
/usr/local/Ascend/ascend-toolkit/latest/opp/built-in/op_impl/ai_core/tbe/config/ascend310p/aic-ascend310p-ops-info.json

加入使能VectorCore

"enableVectorCore":{
        "flag":"true"
}
  1. 并且在以下路径中把已经存在的softmax_v2改为其它名称否则使能不生效
ascend-toolkit/latest/opp/built-in/op_impl/ai_core/tbe/kernel/ascend310p
  1. 重新进行ATC转换再进行性能测试即可

加速库版本

离线模型推理脚本指南

  • 接入FA加速库模型推理脚本的启动路径为${cur_dir}/main.py
  1. modeling_bert_ascend.py 的代码替换原生transformers内的 modeling_bert.py 的代码

路径为

/miniconda/envs/${conda_name}/lib/python3.10/site-packages/transformers/models/bert/modeling_bert.py
  1. 在昇腾机器上运行python main.py

精度 & 性能测试

  • 修改Config_bge.json内的模型路径为各模型所在的相应路径

  • 精度测试脚本

python eval_cmteb.py --model_type_or_path pytorch --device ${device_id}
  • 性能测试脚本
python eval_performance.py --model_type_or_path pytorch --input_shape [batch_size, seq_len] --device ${device_id}

模型推理性能

性能验证NPU环境使用 PYTORCH 模型GPU环境使用 PYTORCH 模型

吞吐率1000 * batch_size / compute_time

环境 芯片型号 batch_size seq_len 吞吐率fps
NPU Ascend910B4 8 100 486.66
NPU Ascend910B4 20 512 1100.48
NPU Ascend910B4 128 512 4885.53
GPU NVIDIA L40 8 100 453.42
GPU NVIDIA L40 20 512 575.13
GPU NVIDIA L40 128 512 2104.04

模型推理精度

精度验证NPU环境使用 PYTORCH 模型GPU环境使用 PYTORCH 模型

环境 芯片型号 ndcg@10%
NPU Ascend910B4 (fp16) 83.67
GPU Nvidia L40 (fp32) 83.67
  • Ascend310P3待测试