模型概述
模型特點
模型能力
使用案例
🚀 replit-code-v1-3b
replit-code-v1-3b
是一個專注於代碼補全的 27 億參數的因果語言模型。該模型基於 Stack Dedup v1.2 數據集 的一個子集進行訓練,可助力開發者更高效地完成代碼編寫。
✨ 主要特性
- 多語言支持:支持
Markdown
、Java
、JavaScript
、Python
等 20 種不同的編程語言。 - 大規模訓練:在包含 1750 億個標記的數據集上進行了 3 個輪次的訓練,總共訓練了 5250 億個標記。
- 先進技術:採用了 Flash Attention、AliBi 位置嵌入、LionW 優化器 等先進的大語言模型技術。
📦 安裝指南
首先,你需要安裝以下依賴項的最新版本:
einops
sentencepiece
torch
transformers
若要在支持 BF16 精度的 GPU 上使用 FlashAttention 的優化 Triton 實現,還需安裝以下依賴項:
flash-attn==0.2.8
triton==2.0.0.dev20221202
若要使用 8 位量化加載模型,需安裝以下額外依賴項:
accelerate
bitsandbytes
若要使用 4 位量化加載模型,需從發佈倉庫的 main
分支安裝依賴項:
pip install git+https://github.com/huggingface/accelerate.git
pip install git+https://github.com/huggingface/transformers.git
💻 使用示例
基礎用法
from transformers import AutoModelForCausalLM
# 加載模型
model = AutoModelForCausalLM.from_pretrained('replit/replit-code-v1-3b', trust_remote_code=True)
高級用法
使用優化的 Triton 實現的 FlashAttention
from transformers import AutoModelForCausalLM, AutoConfig
config = AutoConfig.from_pretrained(
"replit/replit-code-v1-3b",
trust_remote_code=True
)
config.attn_config['attn_impl'] = 'triton'
# 加載模型
model = AutoModelForCausalLM.from_pretrained('replit/replit-code-v1-3b', config=config, trust_remote_code=True)
model.to(device='cuda:0', dtype=torch.bfloat16)
# 前向傳播
x = torch.tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
x = x.to(device='cuda:0')
y = model(x)
分詞器使用
from transformers import AutoTokenizer
# 加載分詞器
tokenizer = AutoTokenizer.from_pretrained('replit/replit-code-v1-3b', trust_remote_code=True)
# 單輸入編碼 + 生成
x = tokenizer.encode('def hello():\n print("hello world")\n', return_tensors='pt')
y = model.generate(x)
# 解碼,clean_up_tokenization_spaces=False 以確保語法正確性
generated_code = tokenizer.decode(y[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
print(generated_code)
代碼生成
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('replit/replit-code-v1-3b', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('replit/replit-code-v1-3b', trust_remote_code=True)
x = tokenizer.encode('def fibonacci(n): ', return_tensors='pt')
y = model.generate(x, max_length=100, do_sample=True, top_p=0.95, top_k=4, temperature=0.2, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
# 解碼,clean_up_tokenization_spaces=False 以確保語法正確性
generated_code = tokenizer.decode(y[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
print(generated_code)
8 位量化加載
model = AutoModelForCausalLM.from_pretrained("replit/replit-code-v1-3b",
trust_remote_code=True,
device_map="auto",
load_in_8bit=True)
4 位量化加載
model = AutoModelForCausalLM.from_pretrained("replit/replit-code-v1-3b",
trust_remote_code=True,
device_map="auto",
load_in_4bit=True)
📚 詳細文檔
模型描述
replit-code-v1-3b
是一個專注於代碼補全的 27 億參數的因果語言模型。該模型在 MosaicML 平臺上使用 256 個 A100 - 40GB GPU 進行訓練。
訓練混合數據集中包含 20 種不同的語言,按標記數量降序排列如下:
Markdown
、Java
、JavaScript
、Python
、TypeScript
、PHP
、SQL
、JSX
、reStructuredText
、Rust
、C
、CSS
、Go
、C++
、HTML
、Vue
、Ruby
、Jupyter Notebook
、R
、Shell
預期用途
Replit 希望該模型能被任何人用作特定應用微調的基礎模型,且對商業用途沒有嚴格限制。
侷限性
預訓練數據集即使在應用數據清理過濾器後,仍可能包含冒犯性或不適當的內容,這些內容可能會反映在模型生成的文本中。建議用戶在生產系統中使用時保持合理的謹慎,不要將其用於可能對個人或群體造成傷害或困擾的任何應用。
後處理
與所有代碼生成模型一樣,對生成的代碼進行後處理非常重要。特別推薦以下後處理步驟:
- 遇到 EOS 標記時停止生成。
- 去除尾隨空格。
- 根據你的補全用例將
max_tokens
設置為合理的值。 - 將生成截斷到
return
、def
、"```"、"\n\n\n
" 等停止詞,以避免在max_tokens
大於預期生成代碼的長度時生成不完整的代碼。
🔧 技術細節
- 訓練數據:模型在 Stack Dedup v1.2 數據集 的一個子集上進行訓練,訓練數據集總共包含 1750 億個標記,重複訓練了 3 個輪次,總共訓練了 5250 億個標記(每個參數約 195 個標記)。
- 訓練平臺:在 MosaicML 平臺上使用 256 個 A100 - 40GB GPU 進行訓練。
- 技術實現:採用了 Flash Attention 實現快速訓練和推理,AliBi 位置嵌入 支持推理時的可變上下文長度,以及 LionW 優化器 等技術。
📄 許可證
模型檢查點和詞彙文件遵循知識共享許可協議 (CC BY - SA 4.0)。在該許可下,你必須向 Replit 致謝,提供許可鏈接,並指明是否進行了更改。你可以以任何合理的方式進行,但不得以任何暗示 Replit 認可你或你的使用的方式進行。
源代碼文件 (*.py
) 遵循 Apache 2.0 許可協議。
🔗 參考資料
📞 聯繫我們
如果你對該模型有任何疑問或建議,請在社區板塊發佈。



