模型简介
模型特点
模型能力
使用案例
🚀 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 许可协议。
🔗 参考资料
📞 联系我们
如果你对该模型有任何疑问或建议,请在社区板块发布。



