# Knorm
## 论文
[A Simple and Effective L_2 Norm-Based Strategy for KV Cache Compression](https://arxiv.org/pdf/2406.11430)
## 模型简介
Knorm将key进行L_2 Norm获得scores实现极简方式剪枝,英伟达官方默认的基准算法,速度快但精度不好。
## 环境依赖
| 软件 | 版本 |
| :------: |:---------:|
| DTK | dtk25.04.1 |
| python | 3.10.12 |
| transformers | 5.3.0 |
| torch | 2.5.1+das.opt1.dtk25042 |
环境配置:
```
mv kvpress-Knorm-Qwen3-8B_pytorch kvpress_pytorch # 去框架名后缀
```
### Docker(方法一)
```
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.5.1-ubuntu22.04-dtk25.04.2-py3.10
# 为以上拉取的docker的镜像ID替换,本镜像为:8ba136d0c0ab
docker run -it --shm-size=64G -v $PWD/kvpress_pytorch:/home/kvpress_pytorch -v /opt/hyhal:/opt/hyhal:ro --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name kvpress bash
cd /home/kvpress_pytorch/kvpress/
pip install -e . -i https://mirrors.aliyun.com/pypi/simple
```
更多镜像可前往[光源](https://modelzoo.sourcefind.cn/#/service-list)下载使用;
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://das.sourcefind.cn:55011/portal/#/home)开发者社区下载安装;
## 数据集
`无`
## 训练
`无`
## 推理
预训练权重目录结构:
```
/home/kvpress_pytorch
└── Qwen/Qwen3-8B
# 设置HF下载镜像:
export HF_ENDPOINT=https://hf-mirror.com
```
### 单机多卡
```bash
cd /home/kvpress_pytorch
python test.py # 本介绍以Qwen3-8B为示例进行步骤说明,需提前将Qwen3-8B下载到对应目录。
```
## 效果展示
`输入: `
```
context = "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."
question = "美国面积多大?"
```
`输出:`
```
answer: The area of the United States is about 777,777 square miles. This is a large area. The United States is a large country. The United States is a large country. The United States is a large country. The United States is a large country. The United States is a large country
```
此算法精度相对较差,常用于速度参考基准。
### 精度
DCU与GPU精度一致,推理框架:pytorch。
## 预训练权重
| 模型名称 | 权重大小 | DCU型号 | 最低卡数需求 |下载地址|
|:----------------------:|:----:|:------:|:------:|:----------:|
| Qwen3-8B | 8B | K100AI | 4 | [Qwen/Qwen3-8B](https://huggingface.co/Qwen/Qwen3-8B) |
## 源码仓库及问题反馈
- http://developer.sourcefind.cn/codes/modelzoo/kvpress-Knorm-Qwen3-8B_pytorch.git
## 参考资料
- https://github.com/NVIDIA/kvpress/blob/main/kvpress/presses/knorm_press.py