这是Pytorch中大型语言模型(NAACL 2024杰出纸张奖)的论文LM限制的代码:零射击的极端概括。这项工作是由Chi Han,Qifan Wang,Hao Peng,Wenhan Xiong,Yu Chen,Heng Ji,Sinong Wang完成的。
在本文中,作者提出了一种称为LM Infinite的简单方法,以将大语言模型的长度概括提高到200m令牌的极端长度,而没有任何其他培训或参数更新。

我们是由首先确定LLMS长度泛化失败的三个因素的动机: (a)因子1:在代币之间看不见的距离会导致注意力逻辑爆炸。 (b)因子2:看不见的令牌数量会导致注意熵随着长度的增加而超出训练范围。 (c)因子3:启动令牌很少有一个独特的特征区域,不应丢弃。

关键的想法是使用(1)

我们已经实施了LM限制方法作为拥抱面变压器的倒入替换。加载变压器模型后,如果是Llama模型,MPT模型或GPT-J模型,则可以运行以下代码以启用LM Infinite。
对于骆驼模型:
from models.llama import convert_llama_model
model = convert_llama_model(model, 4096, 10)
对于MPT模型:
from models.mpt_7b import convert_mpt_model
model = convert_mpt_model(model, 4096, 10)
对于GPT-J模型:
from models.gpt_j import convert_gpt_j_model
model = convert_gpt_j_model(model, 4096, 10)
然后,您可以照常使用模型!
从requirements.txt角度可以找到Python软件包的详细列表。某些软件包由conda安装,有些则由pip安装。我在Anaconda&Pip环境中安装要求的命令如下:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
conda install -c conda-forge sentencepiece einops cudatoolkit-dev tqdm ipython datasets evaluate rouge-score protobuf accelerate langchain openai
pip install transformers deepspeed
├── LICENSE
├── README.md
├── requirements.txt
├── configs
│ └── zero3_efficient_config.json # config for deepspeed acceleration
├── data
│ ├── generation_metrics.py
│ ├── get_data.py # dataset loading and preprocessing
│ ├── passkey_retrieval
│ │ ├── create_passkey_data.py
│ │ ├── create_passkey_data.sh
│ │ └── passkey_retrieval_accuracy.py
│ └── split_pile_file.py # split the Pile dataset into task-specific files
├── models
│ ├── constant.py # a constant function model
│ ├── get_llama2
│ │ ├── convert_llama_weights_to_hf.py # convert llama-2 weights to huggingface format
│ │ └── download_llama2.sh
│ ├── get_model.py
│ ├── gpt_j.py
│ ├── lambda_attention.py # efficient implementation of lambda attention
│ ├── llama.py
│ ├── model_base.py
│ └── mpt_7b.py
├── scripts
│ ├── combine_evaluate_generation.py
│ ├── combine_results.py
│ ├── eval_downstream_tasks.py # evaluate on passkey retrieval task
│ ├── eval_generation.py # evaluate generation metrics
│ └── eval_ppl_deepspeed.py # evaluate perplexity
├── utils
│ ├── arguments.py
│ └── utils.py
└── visualization
├── plot_nll.py
├── position_pca.py
└── relative_attention_explosion.py
对于数据集,您需要准备一个语料库数据集。如果将原始桩源(https://pile.eleuther.ai)下载到${PILE_PATH}/test.jsonl.zst和${PILE_PATH}/val.jsonl.zst ,请运行以下命令以提取压缩数据集。
cd ${PILE_PATH}
zstd -d ./ test.jsonl.zst
zstd -d ./ val.jsonl.zst
然后运行以下命令将数据集拆分为特定于任务的文件。
cd ${REPOSITORY_ROOT}
mkdir -p ${PILE_PATH}/val
mkdir -p ${PILE_PATH}/test
python data/split_pile_file.py ${PILE_PATH}/val.jsonl ${PILE_PATH}/val
python data/split_pile_file.py ${PILE_PATH}/test.jsonl ${PILE_PATH}/test
但是,官方堆似乎不再可供下载,因此您可能需要找出另一个来源(例如,https://huggingface.co/datasets/arxiv_dataset或https://openwebtext2.creadt.readthedocs.io/en/latest/)。另外,您也可以使用自己的语料库。两个选项都要求您编辑数据/get_data.py。
对于骨干模型,本文使用Llama-2,Llama,GPT-J和MPT-7B。最后3个型号是直接从HuggingFace Model Hub立即获得的,因此事先需要不需要操作。 Llama-2下载密钥需要从Meta AI请求表中请求。然后运行以下命令
bash models/get_llama2/download_llama2.sh
并按照提示将检查点下载到${PATH_TO_LLAMA2_CHECKPOINTS} 。然后运行
python models/get_llama2/convert_llama_weights_to_hf.py
--input_dir ${PATH_TO_LLAMA2_CHECKPOINTS}
--model_size 7B
--output_dir ${PATH_TO_LLAMA2_CHECKPOINTS}/llama-2-7b-hf
将Llama-2-7b检查点转换为拥抱面格式。
这些代码需要${LOG_DIR}存储日志和结果。请选择一个具有足够空间的目录。
评估在ARXIV测试集上Llama-2模型的困惑。
TRIAL=llama2-infinite-ArXiv
mkdir -p $LOG_DIR/$TRIAL
CUDA_VISIBLE_DEVICES=0
MASTER_PORT=$(shuf -i 29500-65535 -n 1)
DS_SKIP_CUDA_CHECK=1 PYTHONPATH=. deepspeed --include localhost:$CUDA_VISIBLE_DEVICES --master_port $MASTER_PORT scripts/eval_ppl_deepspeed.py
--deepspeed_config configs/zero3_efficient_config.json
--model ${PATH_TO_LLAMA2_CHECKPOINTS}/llama-2-7b-hf --tokenizer_path ${PATH_TO_LLAMA2_CHECKPOINTS}
--use_lambda_attention --local_branch 4096 --global_branch 100 --limit_distance 4096
--dataset the_pile --dataset_group ArXiv --split test --dataset_dir ${PILE_PATH}
--max_length 32770
--log_dir $LOG_DIR/$TRIAL
对论点的简要说明:
--model :模型的路径或名称。通过decapoda-research/llama-7b-hf使用Llama, mosaicml/mpt-7b使用MPT-7B,而EleutherAI/gpt-j-6b使用GPT-J-6B。--tokenizer_path :通往令牌的路径。如果不使用Llama-2,请删除此参数。--use_lambda_attention :使用Lambda注意。 (LM无限必需)--local_branch :本地分支大小。 2048年,用于Llama,MPT-7B和GPT-J(LM限制必需)--global_branch :全球分支大小。范围10-100给出了通常相似的效果。 (LM无限必需)--limit_distance :距离限制。 2048年,用于Llama,MPT-7B和GPT-J(LM限制必需)--dataset :数据集名称。请参阅数据/get_data.py来弄清楚如何使用自定义数据集。如果您想在没有LM Infinite的香草型号上进行评估,只需删除--use_lambda_attention --local_branch 4096 --global_branch 100 --limit_distance 4096参数集。
如果您只想在测试集的子集上进行评估,则可以使用--start_data_from参数来指定测试集的起始索引和/或--max_data_num来指定该索引之后的示例数。
TRIAL=llama2-infinite-ArXiv-extreme
CUDA_VISIBLE_DEVICES=0
MASTER_PORT=$(shuf -i 29500-65535 -n 1)
echo port: $MASTER_PORT
mkdir -p $LOG_DIR/$TRIAL
DS_SKIP_CUDA_CHECK=1 PYTHONPATH=. deepspeed --include localhost:$CUDA_VISIBLE_DEVICES --master_port $MASTER_PORT scripts/eval_infinite_ppl.py
--deepspeed_config configs/zero3_efficient_config.json
--model ${PATH_TO_LLAMA2_CHECKPOINTS}/llama-2-7b-hf --tokenizer_path ${PATH_TO_LLAMA2_CHECKPOINTS}
--use_lambda_attention --local_branch 4096 --global_branch 10 --limit_distance 4096
--dataset the_pile --dataset_group ArXiv --split test --dataset_dir ${PILE_PATH}
--streaming_length 200000000 --max_length 128000 --start_data_from 2300
--log_dir $LOG_DIR/$TRIAL
从ARXIV测试集中从Llama-2模型中生成评估。
TRIAL=llama2-infinite-generate-ArXiv
mkdir -p $LOG_DIR/$TRIAL
CUDA_VISIBLE_DEVICES=0
MASTER_PORT=$(shuf -i 29500-65535 -n 1)
DS_SKIP_CUDA_CHECK=1 PYTHONPATH=. deepspeed --include localhost:$CUDA_VISIBLE_DEVICES --master_port $MASTER_PORT scripts/eval_generation.py
--deepspeed_config configs/zero3_efficient_config.json
--model ${PATH_TO_LLAMA2_CHECKPOINTS}/llama-2-7b-hf --tokenizer_path ${PATH_TO_LLAMA2_CHECKPOINTS}
--use_lambda_attention --local_branch 4096 --global_branch 100 --limit_distance 4096
--dataset the_pile --dataset_group ArXiv --split test --dataset_dir ${PILE_PATH}
--max_length 33000
--max_generation_length 100 --evaluate_metrics --evaluate_positions 4096 8192 12288 16384
--log_dir $LOG_DIR/$TRIAL
首先,我们需要准备PassKey检索数据集。
for MAX_LENGTH in 2048 3072 4096 5120 6144 7168 8192 10240 12288 14335 16384; do
echo $MAX_LENGTH
python data/passkey_retrieval/create_passkey_data.py
--token-length $MAX_LENGTH
--dump-file-path ${PASSKEY_DATA}/${MAX_LENGTH}
--tokenizer-path ${PATH_TO_LLAMA2_CHECKPOINTS}
--num-samples 1000
done
然后,让我们评估Passkey检索任务。
CUDA_VISIBLE_DEVICES=0
for MAX_LENGTH in 6144 8192 10240 12288 16384; do
TRIAL=llama2-infinite-passkey-$MAX_LENGTH
mkdir -p $LOG_DIR/$TRIAL
MASTER_PORT=$(shuf -i 29500-65535 -n 1)
DS_SKIP_CUDA_CHECK=1 PYTHONPATH=. deepspeed --master_port $MASTER_PORT --include localhost:$CUDA_VISIBLE_DEVICES scripts/eval_downstream_tasks.py
--deepspeed_config configs/zero3_efficient_config.json
--model ${PATH_TO_LLAMA2_CHECKPOINTS}/llama-2-7b-hf --tokenizer_path ${PATH_TO_LLAMA2_CHECKPOINTS}
--use_lambda_attention --local_branch 4096 --global_branch 10 --limit_distance 4096 --triangle_offset 0
--top_k_attention 5 --top_k_from_layer 4
--dataset passkey_retrieval --dataset_dir ${PASSKEY_DATA} --dataset_group ${MAX_LENGTH}
--max_generation_length 7 --evaluate_metrics
--log_dir $LOG_DIR/$TRIAL
done
运行Qasper任务:
CUDA_VISIBLE_DEVICES=0
DATASET=qasper
TRIAL=llama2-infinite-$DATASET
mkdir -p $LOG_DIR/$TRIAL
MASTER_PORT=$(shuf -i 29500-65535 -n 1)
echo port: $MASTER_PORT
DS_SKIP_CUDA_CHECK=1 PYTHONPATH=. deepspeed --include localhost:$CUDA_VISIBLE_DEVICES --master_port $MASTER_PORT scripts/eval_downstream_tasks.py
--deepspeed_config configs/zero3_efficient_config_large.json
--model ${PATH_TO_LLAMA2_CHECKPOINTS}/llama-2-7b-hf --tokenizer_path ${PATH_TO_LLAMA2_CHECKPOINTS}
--use_lambda_attention --local_branch 4096 --global_branch 10 --limit_distance 4096 --triangle_offset 0
--top_k_attention 5 --top_k_from_layer 4
--dataset $DATASET --split test --evaluate_metrics
--max_length 6144 --truncation_side center
--log_dir $LOG_DIR/$TRIAL
@inproceedings{han2024lm,
title={LM-Infinite: Zero-Shot Extreme Length Generalization for Large Language Models},
author={Han, Chi and Wang, Qifan and Peng, Hao and Xiong, Wenhan and Chen, Yu and Ji, Heng and Wang, Sinong},
booktitle={Proceedings of the 2024 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies (Volume 1: Long Papers)},
pages={3991--4008},
year={2024}
}