🚀 LLaMA-2-7B-32K
LLaMA-2-7B-32K 是一款開源的長上下文語言模型,由 Together 基於 Meta 原始的 Llama-2 7B 模型進行微調。該模型致力於推動大語言模型開源生態的快速發展,通過位置插值將上下文長度擴展至 32K,可應用於多文檔問答、長文本摘要等場景。
🚀 快速開始
你可以使用 Together API 對 LLaMA-2-7B-32K 進行推理。更新後的推理棧可實現高效推理。
若要在本地運行該模型,強烈建議安裝 Flash Attention V2,這是獲得最佳性能的必要條件:
# 請更新 `CUDA_HOME` 的路徑
export CUDA_HOME=/usr/local/cuda-11.8
pip install transformers==4.31.0
pip install sentencepiece
pip install ninja
pip install flash-attn --no-build-isolation
pip install git+https://github.com/HazyResearch/flash-attention.git#subdirectory=csrc/rotary
你可以直接從 Hugging Face 模型中心使用該模型,也可以使用 OpenChatKit 在自己的數據上對其進行微調。
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("togethercomputer/LLaMA-2-7B-32K")
model = AutoModelForCausalLM.from_pretrained("togethercomputer/LLaMA-2-7B-32K", trust_remote_code=True, torch_dtype=torch.float16)
input_context = "Your text here"
input_ids = tokenizer.encode(input_context, return_tensors="pt")
output = model.generate(input_ids, max_length=128, temperature=0.7)
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(output_text)
或者,如果你不想使用 flash attention,可以將 trust_remote_code
設置為 False
。
✨ 主要特性
擴展上下文
該模型經過訓練,可處理長達 32K 的上下文,相較於之前的版本有了顯著提升。
預訓練和指令微調
共享了數據配方,包含預訓練和指令微調數據的混合。
微調示例
提供瞭如何針對特定應用微調模型的示例,包括書籍摘要和長上下文問答。
軟件支持
更新了推理和訓練棧,以實現 32K 上下文的高效推理和微調。
📦 安裝指南
若要在本地運行該模型,需安裝 Flash Attention V2 以獲得最佳性能:
# 請更新 `CUDA_HOME` 的路徑
export CUDA_HOME=/usr/local/cuda-11.8
pip install transformers==4.31.0
pip install sentencepiece
pip install ninja
pip install flash-attn --no-build-isolation
pip install git+https://github.com/HazyResearch/flash-attention.git#subdirectory=csrc/rotary
💻 使用示例
基礎用法
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("togethercomputer/LLaMA-2-7B-32K")
model = AutoModelForCausalLM.from_pretrained("togethercomputer/LLaMA-2-7B-32K", trust_remote_code=True, torch_dtype=torch.float16)
input_context = "Your text here"
input_ids = tokenizer.encode(input_context, return_tensors="pt")
output = model.generate(input_ids, max_length=128, temperature=0.7)
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(output_text)
高級用法
長上下文問答
以論文 “Lost in the Middle: How Language Models Use Long Contexts” 中的多文檔問答任務為例。模型的輸入包括(i)一個需要回答的問題和(ii)k 個文檔,這些文檔是從維基百科中提取的段落。值得注意的是,只有其中一個文檔包含問題的答案,其餘 k - 1 個文檔(稱為“干擾”文檔)不包含答案。為了成功完成此任務,模型必須從其輸入上下文中識別並利用包含答案的文檔。
使用 OpenChatKit(OCK),只需運行以下命令進行微調:
bash training/finetune_llama-2-7b-32k-mqa.sh
摘要
另一個示例是 BookSum,這是一個專門用於解決長篇敘事摘要挑戰的獨特數據集。該數據集的源文檔來自文學領域,包括小說、戲劇和故事,並提供了人工編寫的高度抽象的摘要。這裡主要關注章節級別的數據。BookSum 帶來了一系列獨特的挑戰,要求模型全面閱讀每個章節。
使用 OCK,只需運行以下命令進行微調:
bash training/finetune_llama-2-7b-32k-booksum.sh
📚 詳細文檔
模型架構
該模型遵循 Llama-2-7B 的架構,並對其進行擴展以處理更長的上下文。它利用了最近發佈的 FlashAttention-2 和一系列其他優化,以提高推理和訓練的速度和效率。
訓練和微調
模型使用預訓練和指令微調數據的混合進行訓練:
- 在持續預訓練的第一階段,數據混合包含 25% 的 RedPajama 書籍、25% 的 RedPajama ArXiv(包括摘要)、25% 的 RedPajama 其他數據和 25% 的 UL2 Oscar 數據(屬於 OIG,即 Open-Instruction-Generalist),要求模型填充缺失的部分或完成文本。為了增強長上下文能力,排除了字數少於 2K 的數據。包含 UL2 Oscar 數據能有效促使模型閱讀和利用長距離上下文。
- 然後對模型進行微調,使其專注於長上下文中的小樣本能力,包括 20% 的 Natural Instructions(NI)、20% 的 Public Pool of Prompts(P3)、20% 的 The Pile。對所有數據進行了針對 HELM 核心場景的淨化處理。通過將示例打包成一個 32K 標記的序列,教導模型利用上下文示例。為了保留從第一部分數據中學到的知識,納入了 20% 的 RedPajama-Data 書籍和 20% 的 RedPajama-Data ArXiv。
示例數據集位於 togethercomputer/Long-Data-Collections。你可以使用 OpenChatKit 在 LLaMA-2-7B-32K 上微調自己的 32K 模型。具體步驟請參考 OpenChatKit。
🔧 技術細節
訓練數據
屬性 |
詳情 |
模型類型 |
LLaMA-2-7B-32K |
訓練數據 |
togethercomputer/RedPajama-Data-1T、togethercomputer/RedPajama-Data-Instruct、EleutherAI/pile、togethercomputer/Long-Data-Collections |
侷限性和偏差
與所有語言模型一樣,LLaMA-2-7B-32K 可能會生成不正確或有偏差的內容。使用該模型時請牢記這一點。
📄 許可證
本模型使用 LLaMA-2 許可證。
👥 社區
加入我們的 Together Discord 社區。