RoPE(Rotary Position Embedding)编码是一种高效的位置编码方式,有以下特点:
通过RoPE编码与RazorAttention结合,可分析注意力头对位置编码的依赖性,来决定哪些头可以被压缩,以优化模型的存储、传输和计算效率,提高模型在实际应用中的可部署性和实用性。
目前支持对表1中RoPE编码的大模型进行长序列压缩(包括但不限于)。
以下命令若使用非root用户安装,需要在安装命令后加上--user,例如:pip3 install numpy==1.25.2 --user。
pip3 install numpy==1.26.4 pip3 install transformers==4.43.1 pip3 install torch==2.1.0 # 安装CPU版本的PyTorch 2.1.0(依赖torch_npu) pip3 install torch_npu-2.1.0.xxx.whl # xxx需要根据实际情况进行选择,具体请参见安装torch_npu插件
关键步骤说明如下:
config.json generation_config.json merges.txt model-00001-of-00037.safetensors ...... model-00037-of-00037.safetensors model.safetensors.index.json tokenizer.json tokenizer config.json vocab.json
import torch from msmodelslim.pytorch.ra_compression import RARopeCompressConfig, RARopeCompressor from transformers import AutoTokenizer, AutoModelForCausalLM import torch_npu torch.npu.set_compile_mode(jit_compile=False) config = RARopeCompressConfig(induction_head_ratio=0.14, echo_head_ratio=0.01) save_path = "./win.pt" model_path = "./Qwen2-72B-Instruct/" model = AutoModelForCausalLM.from_pretrained( pretrained_model_name_or_path=model_path, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto", ).eval() tokenizer = AutoTokenizer.from_pretrained( pretrained_model_name_or_path=model_path, pad_token='<|extra_0|>', eos_token='<|endoftext|>', padding_side='left', trust_remote_code=True ) ra = RARopeCompressor(model, tokenizer, config) ra.get_compress_heads(save_path)
python3 run.py