![]() |
||
---|---|---|
.. | ||
models | ||
README.md | ||
config.json | ||
convert.sh | ||
eval_performance.py | ||
eval_precision.py | ||
requirements.txt | ||
run.py |
README.md
README
特性矩阵
- 此矩阵罗列了各bge-reranker-large模型支持的特性
模型及参数量 | 800I A2 Tensor Parallelism | 300I DUO Tensor Parallelism | FP16 | BF16 | Flash Attention | Paged Attention | W8A8量化 | W8A16量化 | W4A16量化 | KV cache量化 | 稀疏量化 | MOE量化 | MindIE Service | TGI | 长序列 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
bge-reranker-large | 支持world size 1 | 支持world size 1 | √ | × | × | × | × | × | × | × | × | × | × | × | × |
bge-reranker-large模型-推理指导
概述
模型介绍
bge-reranker-large
是由智源研究院研发的交叉编码器重排模型,可对查询和答案实时计算相关性分数,这比向量模型(即双编码器)更准确,但比向量模型更耗时。
开源模型地址
url=https://huggingface.co/BAAI/bge-reranker-large
commit_id=bc0c7056d15eaea221616887bf15da63743d19e1
model_name=bge-reranker-large
路径变量解释
{cur_dir}
├─ .cache
│ ├─ huggingface
│ │ └─ datasets
│ │ └─ C-MTEB
│ │ └─ T2Reranking
│ │ └─ dev-00000-of-00001-65d96bde8023d9b9.parquet
├─ models
│ ├─ om
│ │ ├─ bge-reranker-large_{soc_version}_{precision_mode}_linux_aarch64.om
│ ├─ onnx
│ │ ├─ model.onnx
│ └─ pytorch
│ └─ pytorch_model.bin
├─ eval_performance.py
├─ eval_precision.py
└─ run.py
变量名 | 含义 |
---|---|
soc_version | npu芯片的处理器的版本,可以使用 npu-smi info 查询 |
precision_mode | 转换的om模型的精度模式,参考ATC工具参数 |
输入输出数据
输入数据
输入数据 | 数据类型 | 大小 | 数据排布格式 |
---|---|---|---|
input_ids | INT64 | batch_size * seq_len | ND |
attention_mask | INT64 | batch_size * seq_len | ND |
输出数据
输出数据 | 数据类型 | 大小 | 数据排布格式 |
---|---|---|---|
output | FLOAT32 | batch_size * class | ND |
推理环境准备
该模型需要以下插件与驱动
配套 | 版本 | 环境准备指导 |
---|---|---|
固件与驱动 | 23.0.RC3 | Pytorch框架推理环境准备 |
CANN | 7.0.RC1 | - |
Python | 3.10 | - |
Pytorch | 2.1.0 | - |
说明:Atlas 300I Duo 推理卡请以 CANN 版本选择实际固件与驱动版本。
快速上手
获取源码
-
获取本项目源码
git clone https://gitee.com/ascend/MindIE-LLM.git # 克隆本仓库代码 git checkout master # 切换对应分支 cd examples/atb_models/pytorch/examples/BAAI/bge-reranker-large # 打开工作(当前)目录 {cur_dir}
-
安装依赖
安装python依赖
pip install -r requirements.txt
下载安装
ais_bench
推理工具pip install ./aclruntime-{version}-{python_version}-linux_{arch}.whl pip install ./ais_bench-{version}-py3-none-any.whl # {version}表示软件版本号,{python_version}表示Python版本号,{arch}表示CPU架构
-
获取开源模型
git lfs install GIT_LFS_SKIP_SMUDGE=1 git clone https://gitee.com/ascend/MindIE-LLM.git
-
准备数据集
下载 C-MTEB/T2Reranking 数据集
mkdir .cache/huggingface/datasets/C-MTEB/ cd .cache/huggingface/datasets/C-MTEB/ git clone https://huggingface.co/datasets/C-MTEB/T2Reranking mv T2Reranking/data/dev-00000-of-00001-65d96bde8023d9b9.parquet T2Reranking/
模型转换
-
获取开源模型 pytorch 权重文件 pytorch_model.bin,放在
models/pytorch
目录中 -
获取开源模型 onnx 权重文件 model.onnx,放在
models/onnx
目录中 -
运行脚本转换模型
bash ${cur_dir}/convert.sh ${onnx} ${om} ${precision_mode}
- 参数说明,参考 ATC工具参数
onnx
:转换后的onnx模型文件路径om
:转换后的om模型文件路径precision_mode
:模型精度模式,精度高低排序origin>mixed_float16>fp16
,性能优劣排序fp16>=mixed_float16>origin
,推荐使用mixed_float16
以在保证精度的前提下获得最大性能,默认为mixed_float16
- 参数说明,参考 ATC工具参数
模型推理
-
执行推理
python run.py \ --model_type_or_path=${model_type} or ${model_path} --device=${device}
- 参数说明
model_type_or_path
:选择需要推理的模型类型或模型文件路径device
:选择加载模型的芯片id
- 参数说明
-
性能测试
python eval_performance.py \ --model_type_or_path=${model_type} or ${model_path} \ --input_shape=${batch_size},${seq_len} \ --device=${device} \ --loop=${loop}
- 参数说明
model_type_or_path
:选择需要推理的模型类型或模型文件路径batch_size
:选择每次推理时加载的数据集长度seq_len
:选择每次推理时加载的文本长度device
:选择加载模型的芯片idloop
:验证循环次数
- 参数说明
-
精度测试
python eval_precision.py \ --model_type_or_path=${model_type} or ${model_path} \ --batch_size=${batch_size} \ --device=${device}
- 参数说明
model_type_or_path
:选择需要推理的模型类型或模型文件路径batch_size
:选择每次推理时加载的数据集长度device
:选择加载模型的芯片id
- 参数说明
模型推理性能&精度
模型推理性能
吞吐率:1000 * batch_size / compute_time
环境 | 芯片型号 | batch_size | seq_len | 吞吐率(fps) |
---|---|---|---|---|
NPU | Ascend310P3 | 20 | 512 | 43.84 |
NPU | Ascend310P3 | 50 | 512 | 44.23 |
GPU | NVIDIA A10 | 20 | 512 | 46.43 |
GPU | NVIDIA A10 | 50 | 512 | 49.16 |
说明:Atlas 300I Duo 推理卡为单卡双芯,比较吞吐率时需要×2
环境 | 芯片型号 | batch_size | seq_len | 吞吐率(fps) |
---|---|---|---|---|
NPU | Ascend910B4 | 20 | 512 | 144.02 |
NPU | Ascend910B4 | 50 | 512 | 135.82 |
GPU | NVIDIA L40S | 20 | 512 | 119.75 |
GPU | NVIDIA L40S | 50 | 512 | 113.42 |
模型推理精度
精度验证NPU环境使用 OM
模型,GPU环境使用 ONNX
模型
有数据集精度验证选择 C-MTEB/T2Reranking 任务,开源模型在该任务下 MAP 分数为 67.28
环境 | 芯片型号 | MAP(%) | MRR@10(%) | 执行时间(s) |
---|---|---|---|---|
NPU | Ascend310P3 | 67.60 | 77.68 | 4496.25 |
GPU | Nvidia A10 | 67.61 | 77.66 | 2216.56 |
环境 | 芯片型号 | MAP(%) | MRR@10(%) | 执行时间(s) |
---|---|---|---|---|
NPU | Ascend910B4 | 67.60 | 77.66 | 985.30 |
GPU | Nvidia L40S | 67.61 | 77.66 | 991.57 |
说明:
- MAP:平均精度均值(Mean Average Precision)
MAP = \frac{1}{|U|} \sum_{i=1}^{|U|} hit(i) \times \frac{1}{P_i}
- MRR:平均倒数排名(Mean Reciprocal Rank)
MRR = \frac{1}{N} \sum_{i=1}^N \frac{1}{p_i}
无数据集精度验证选择输入 [[query, positive], [query, negative]]
文本,torch.allclose
满足1%精度
环境 | 芯片型号 | 推理结果 |
---|---|---|
NPU | Ascend310P3 | tensor([7.5195, 1.3613]) |
GPU | Nvidia A10 | tensor([7.5152, 1.3654]) |
环境 | 芯片型号 | 推理结果 |
---|---|---|
NPU | Ascend910B4 | tensor([7.5195, 1.3779]) |
GPU | Nvidia L40S | tensor([7.5140, 1.3697]) |