20 多個高效能法學碩士,提供大規模預訓練、微調和部署的方法。
✅ 從頭開始實現 ✅ 無抽象 ✅ 初學者友好 ✅ Flash 注意力 ✅ FSDP ✅ LoRA、QLoRA、適配器 ✅ 減少 GPU 記憶體 (fp4/8/16/32) ✅ 1-1000+ GPU/TPU ✅ 20+ LLM
快速入門 • 模型 • Finetune • 部署 • 所有工作流程 • 功能 • 配方 (YAML) • Lightning AI • 教程
每個法學碩士都是從頭開始實施的,沒有抽象和完全控制,這使得它們在企業規模上速度極快、極簡且高效能。
✅企業就緒 - Apache 2.0 可供無限的企業使用。
✅開發人員友好 -輕鬆調試,沒有抽象層和單一文件實現。
✅優化性能 -模型旨在最大限度地提高性能、降低成本並加快訓練速度。
✅經過驗證的食譜 -在企業規模上測試的高度優化的培訓/微調食譜。
安裝 LitGPT
pip install 'litgpt[all]'
載入並使用 20 多個 LLM 中的任一個:
from litgpt import LLM
llm = LLM . load ( "microsoft/phi-2" )
text = llm . generate ( "Fix the spelling: Every fall, the familly goes to the mountains." )
print ( text )
# Corrected Sentence: Every fall, the family goes to the mountains.
✅ 針對快速推理進行了最佳化
✅ 量化
✅ 在低記憶體 GPU 上運行
✅ 沒有內部抽象層
✅ 針對生產規模進行最佳化
從源安裝:
git clone https://github.com/Lightning-AI/litgpt
cd litgpt
pip install -e ' .[all] '
探索完整的 Python API 文件。
每個模型都是從頭開始編寫的,以最大限度地提高效能並消除抽象層:
模型 | 型號尺寸 | 作者 | 參考 |
---|---|---|---|
駱駝 3、3.1、3.2 | 1B、3B、8B、70B、405B | 元人工智慧 | 元人工智慧 2024 |
程式碼駱駝 | 7B、13B、34B、70B | 元人工智慧 | 羅齊埃等人。 2023年 |
混合教育部 | 8x7B | 米斯特拉爾人工智慧 | 米斯特拉爾人工智慧 2023 |
米斯特拉爾 | 7B、123B | 米斯特拉爾人工智慧 | 米斯特拉爾人工智慧 2023 |
代碼吉瑪 | 7B | 谷歌團隊,Google Deepmind | |
傑瑪2號 | 2B、9B、27B | 谷歌團隊,Google Deepmind | |
Φ3 & 3.5 | 3.8B | 微軟 | 阿卜丁等人。 2024年 |
… | … | … | … |
模型 | 型號尺寸 | 作者 | 參考 |
---|---|---|---|
代碼吉瑪 | 7B | 谷歌團隊,Google Deepmind | |
程式碼駱駝 | 7B、13B、34B、70B | 元人工智慧 | 羅齊埃等人。 2023年 |
鷸 | 7B、40B、180B | 阿聯酋 TII | 2023年TII |
FreeWilly2(穩定白鯨 2) | 70B | 穩定性人工智慧 | 穩定性人工智慧 2023 |
函數呼叫 Llama 2 | 7B | 特雷利斯 | 特雷利斯等人。 2023年 |
芽 | 2B、7B | 谷歌團隊,Google Deepmind | |
傑瑪2號 | 9B、27B | 谷歌團隊,Google Deepmind | |
駱駝2 | 7B、13B、70B | 元人工智慧 | 圖夫龍等人。 2023年 |
駱駝3.1 | 8B、70B | 元人工智慧 | 元人工智慧 2024 |
駱駝3.2 | 1B、3B | 元人工智慧 | 元人工智慧 2024 |
數學斯特拉爾 | 7B | 米斯特拉爾人工智慧 | 米斯特拉爾人工智慧 2024 |
小羊駝 | 300M | 王肯 | MicroLama 倉庫 |
混合教育部 | 8x7B | 米斯特拉爾人工智慧 | 米斯特拉爾人工智慧 2023 |
米斯特拉爾 | 7B、123B | 米斯特拉爾人工智慧 | 米斯特拉爾人工智慧 2023 |
OLMo | 1B、7B | 艾倫人工智慧研究所 (AI2) | 格羅內費爾德等。 2024年 |
開放駱駝 | 3B、7B、13B | OpenLM 研究 | 耿劉2023 |
Φ1.5&2 | 1.3B、2.7B | 微軟研究院 | 李等人。 2023年 |
Φ3 | 3.8B | 微軟研究院 | 阿卜丁等人。 2024年 |
鴨嘴獸 | 7B、13B、70B | 李等人。 | 李、亨特和魯伊斯 2023 |
皮提亞 | {14,31,70,160,410}M, {1,1.4,2.8,6.9,12}B | 埃魯瑟人工智慧 | 比德曼等人。 2023年 |
穩定程式碼 | 3B | 穩定性人工智慧 | 穩定性人工智慧 2023 |
穩定LM | 3B、7B | 穩定性人工智慧 | 穩定性人工智慧 2023 |
穩定LM Zephyr | 3B | 穩定性人工智慧 | 穩定性人工智慧 2023 |
小羊駝 | 1.1B | 張等人。 | 張等人。 2023年 |
提示:您可以透過執行litgpt download list
指令列出所有可用型號。
Finetune • 預訓練 • 持續預訓練 • 評估 • 部署 • 測試
使用命令列介面運行高級工作流程,例如對您自己的資料進行預先訓練或微調。
安裝 LitGPT 後,選擇要執行的模型和工作流程(微調、預訓練、評估、部署等...):
# ligpt [action] [model]
litgpt serve meta-llama/Llama-3.2-3B-Instruct
litgpt finetune meta-llama/Llama-3.2-3B-Instruct
litgpt pretrain meta-llama/Llama-3.2-3B-Instruct
litgpt chat meta-llama/Llama-3.2-3B-Instruct
litgpt evaluate meta-llama/Llama-3.2-3B-Instruct
微調是採用預先訓練的人工智慧模型,並在針對特定任務或應用程式量身定制的較小的專業資料集上對其進行進一步訓練的過程。
# 0) setup your dataset
curl -L https://huggingface.co/datasets/ksaw008/finance_alpaca/resolve/main/finance_alpaca.json -o my_custom_dataset.json
# 1) Finetune a model (auto downloads weights)
litgpt finetune microsoft/phi-2
--data JSON
--data.json_path my_custom_dataset.json
--data.val_split_fraction 0.1
--out_dir out/custom-model
# 2) Test the model
litgpt chat out/custom-model/final
# 3) Deploy the model
litgpt serve out/custom-model/final
閱讀完整的微調文檔
部署預先訓練或微調的 LLM 以在實際應用中使用它。部署,自動設定可供網站或應用程式存取的 Web 伺服器。
# deploy an out-of-the-box LLM
litgpt serve microsoft/phi-2
# deploy your own trained model
litgpt serve path/to/microsoft/phi-2/checkpoint
在單獨的終端機中測試伺服器並將模型 API 整合到您的 AI 產品中:
# 3) Use the server (in a separate Python session)
import requests , json
response = requests . post (
"http://127.0.0.1:8000/predict" ,
json = { "prompt" : "Fix typos in the following sentence: Exampel input" }
)
print ( response . json ()[ "output" ])
閱讀完整的部署文件。
評估法學碩士以測試其在各種任務上的表現,看看它理解和生成文本的能力如何。簡而言之,我們可以評估它在大學化學、編碼等方面的表現如何(MMLU、真實的 QA 等)
litgpt evaluate microsoft/phi-2 --tasks ' truthfulqa_mc2,mmlu '
閱讀完整的評估文件。
透過互動式聊天測試模型的運作。使用chat
指令進行聊天、提取嵌入等...
以下範例展示如何使用 Phi-2 LLM:
litgpt chat microsoft/phi-2
>> Prompt: What do Llamas eat ?
# 1) List all supported LLMs
litgpt download list
# 2) Use a model (auto downloads weights)
litgpt chat microsoft/phi-2
>> Prompt: What do Llamas eat ?
某些模型的下載需要額外的存取令牌。您可以在下載文件中閱讀有關此內容的更多資訊。
閱讀完整的聊天文件。
預訓練是在針對特定任務進行微調之前,透過將其暴露於大量資料來訓練人工智慧模型的過程。
mkdir -p custom_texts
curl https://www.gutenberg.org/cache/epub/24440/pg24440.txt --output custom_texts/book1.txt
curl https://www.gutenberg.org/cache/epub/26393/pg26393.txt --output custom_texts/book2.txt
# 1) Download a tokenizer
litgpt download EleutherAI/pythia-160m
--tokenizer_only True
# 2) Pretrain the model
litgpt pretrain EleutherAI/pythia-160m
--tokenizer_dir EleutherAI/pythia-160m
--data TextFiles
--data.train_data_path " custom_texts/ "
--train.max_tokens 10_000_000
--out_dir out/custom-model
# 3) Test the model
litgpt chat out/custom-model/final
閱讀完整的預訓練文檔
持續預訓練是另一種微調方法,透過對自訂資料進行訓練來專門化已經預先訓練的模型:
mkdir -p custom_texts
curl https://www.gutenberg.org/cache/epub/24440/pg24440.txt --output custom_texts/book1.txt
curl https://www.gutenberg.org/cache/epub/26393/pg26393.txt --output custom_texts/book2.txt
# 1) Continue pretraining a model (auto downloads weights)
litgpt pretrain EleutherAI/pythia-160m
--tokenizer_dir EleutherAI/pythia-160m
--initial_checkpoint_dir EleutherAI/pythia-160m
--data TextFiles
--data.train_data_path " custom_texts/ "
--train.max_tokens 10_000_000
--out_dir out/custom-model
# 2) Test the model
litgpt chat out/custom-model/final
閱讀完整的持續預訓練文檔
✅ 最先進的最佳化:Flash Attention v2、透過完全分片資料並行的多 GPU 支援、可選的 CPU 卸載以及 TPU 和 XLA 支援。
✅ 預訓練、微調與部署
✅ 透過低精度設定降低運算要求:FP16、BF16 和 FP16/FP32 混合。
✅ 透過量化降低記憶體需求:4 位元浮點數、8 位元整數和雙量化。
✅ 設定檔可實現出色的開箱即用效能。
✅ 參數高效率的微調:LoRA、QLoRA、Adapter 和 Adapter v2。
✅ 匯出為其他流行的模型重量格式。
✅ 許多流行的資料集用於預訓練和微調,並支援自訂資料集。
✅ 可讀且易於修改的程式碼來嘗試最新的研究想法。
LitGPT 隨附經過驗證的配方(YAML 配置),可在不同條件下訓練模型。我們根據我們發現的在不同訓練條件下表現最佳的參數來產生了這些配方。
在此瀏覽所有訓練食譜。
litgpt finetune
--config https://raw.githubusercontent.com/Lightning-AI/litgpt/main/config_hub/finetune/llama-2-7b/lora.yaml
配置可讓您自訂所有細粒度參數的訓練,例如:
# The path to the base model's checkpoint directory to load for finetuning. (type: <class 'Path'>, default: checkpoints/stabilityai/stablelm-base-alpha-3b)
checkpoint_dir : checkpoints/meta-llama/Llama-2-7b-hf
# Directory in which to save checkpoints and logs. (type: <class 'Path'>, default: out/lora)
out_dir : out/finetune/qlora-llama2-7b
# The precision to use for finetuning. Possible choices: "bf16-true", "bf16-mixed", "32-true". (type: Optional[str], default: null)
precision : bf16-true
...
# The path to the base model's checkpoint directory to load for finetuning. (type: <class 'Path'>, default: checkpoints/stabilityai/stablelm-base-alpha-3b)
checkpoint_dir : checkpoints/meta-llama/Llama-2-7b-hf
# Directory in which to save checkpoints and logs. (type: <class 'Path'>, default: out/lora)
out_dir : out/finetune/qlora-llama2-7b
# The precision to use for finetuning. Possible choices: "bf16-true", "bf16-mixed", "32-true". (type: Optional[str], default: null)
precision : bf16-true
# If set, quantize the model with this algorithm. See ``tutorials/quantize.md`` for more information. (type: Optional[Literal['nf4', 'nf4-dq', 'fp4', 'fp4-dq', 'int8-training']], default: null)
quantize : bnb.nf4
# How many devices/GPUs to use. (type: Union[int, str], default: 1)
devices : 1
# How many nodes to use. (type: int, default: 1)
num_nodes : 1
# The LoRA rank. (type: int, default: 8)
lora_r : 32
# The LoRA alpha. (type: int, default: 16)
lora_alpha : 16
# The LoRA dropout value. (type: float, default: 0.05)
lora_dropout : 0.05
# Whether to apply LoRA to the query weights in attention. (type: bool, default: True)
lora_query : true
# Whether to apply LoRA to the key weights in attention. (type: bool, default: False)
lora_key : false
# Whether to apply LoRA to the value weights in attention. (type: bool, default: True)
lora_value : true
# Whether to apply LoRA to the output projection in the attention block. (type: bool, default: False)
lora_projection : false
# Whether to apply LoRA to the weights of the MLP in the attention block. (type: bool, default: False)
lora_mlp : false
# Whether to apply LoRA to output head in GPT. (type: bool, default: False)
lora_head : false
# Data-related arguments. If not provided, the default is ``litgpt.data.Alpaca``.
data :
class_path : litgpt.data.Alpaca2k
init_args :
mask_prompt : false
val_split_fraction : 0.05
prompt_style : alpaca
ignore_index : -100
seed : 42
num_workers : 4
download_dir : data/alpaca2k
# Training-related arguments. See ``litgpt.args.TrainArgs`` for details
train :
# Number of optimizer steps between saving checkpoints (type: Optional[int], default: 1000)
save_interval : 200
# Number of iterations between logging calls (type: int, default: 1)
log_interval : 1
# Number of samples between optimizer steps across data-parallel ranks (type: int, default: 128)
global_batch_size : 8
# Number of samples per data-parallel rank (type: int, default: 4)
micro_batch_size : 2
# Number of iterations with learning rate warmup active (type: int, default: 100)
lr_warmup_steps : 10
# Number of epochs to train on (type: Optional[int], default: 5)
epochs : 4
# Total number of tokens to train on (type: Optional[int], default: null)
max_tokens :
# Limits the number of optimizer steps to run (type: Optional[int], default: null)
max_steps :
# Limits the length of samples (type: Optional[int], default: null)
max_seq_length : 512
# Whether to tie the embedding weights with the language modeling head weights (type: Optional[bool], default: null)
tie_embeddings :
# (type: float, default: 0.0003)
learning_rate : 0.0002
# (type: float, default: 0.02)
weight_decay : 0.0
# (type: float, default: 0.9)
beta1 : 0.9
# (type: float, default: 0.95)
beta2 : 0.95
# (type: Optional[float], default: null)
max_norm :
# (type: float, default: 6e-05)
min_lr : 6.0e-05
# Evaluation-related arguments. See ``litgpt.args.EvalArgs`` for details
eval :
# Number of optimizer steps between evaluation calls (type: int, default: 100)
interval : 100
# Number of tokens to generate (type: Optional[int], default: 100)
max_new_tokens : 100
# Number of iterations (type: int, default: 100)
max_iters : 100
# The name of the logger to send metrics to. (type: Literal['wandb', 'tensorboard', 'csv'], default: csv)
logger_name : csv
# The random seed to use for reproducibility. (type: int, default: 1337)
seed : 1337
litgpt finetune
--config https://raw.githubusercontent.com/Lightning-AI/litgpt/main/config_hub/finetune/llama-2-7b/lora.yaml
--lora_r 4
LitGPT 為許多偉大的人工智慧計畫、倡議、挑戰,當然還有企業提供支援。請提交拉取請求以考慮某個功能。
微軟研究人員的 Samba 專案建立在 LitGPT 程式碼庫之上,將狀態空間模型與滑動視窗注意力結合,其效能優於純狀態空間模型。
LitGPT 儲存庫是 NeurIPS 2023 LLM 效率挑戰賽的官方入門套件,該競賽的重點是在單一 GPU 上對現有非指令調整的 LLM 進行 24 小時的微調。
LitGPT 為 TinyLlama 計畫和 TinyLlama:開源小語言模型研究論文提供支援。
MicroLlama 是一個 300M Llama 模型,在 TinyLlama 和 LitGPT 提供支援的 50B 代幣上進行預訓練。
研究論文「用更少的令牌預訓練小型基礎 LM」利用 LitGPT,透過從較大模型繼承一些 Transformer 區塊並在較大模型使用的一小部分資料上進行訓練來開發較小的基礎語言模型。它表明,儘管使用的訓練資料和資源少得多,但這些較小的模型可以與較大的模型相媲美。
我們歡迎所有個人貢獻者,無論他們的經驗或硬體水平如何。您的貢獻很有價值,我們很高興看到您在這個協作和支持的環境中能夠取得什麼成就。
開始使用
⚡️ 微調,包括。 LoRA、QLoRA 和適配器
?預訓練
模型評估
支援的和自訂的資料集
?量化
?處理記憶體不足 (OOM) 錯誤的提示
?? 使用雲端TPU
此實作在 Lit-LLaMA 和 nanoGPT 上進行了擴展,並由Lightning Fabric ⚡ 提供支援。
LitGPT 在 Apache 2.0 許可證下發布。
如果您在研究中使用 LitGPT,請引用以下工作:
@misc { litgpt-2023 ,
author = { Lightning AI } ,
title = { LitGPT } ,
howpublished = { url{https://github.com/Lightning-AI/litgpt} } ,
year = { 2023 } ,
}