使用 Hugging Face 微调 Gemma 模型

1,749次阅读
没有评论

来自 Google Deepmind 开放权重的语言模型 Gemma 现已通过 Hugging Face 面向更广泛的开源社区开放。该模型提供了两个规模的版本:2B 和 7B 参数,包括预训练版本和经过指令调优的版本。它在 Hugging Face 平台上提供支持,可在 Vertex Model Garden 和 Google Kubernetes Engine 中轻松部署和微调。

使用 Hugging Face 微调 Gemma 模型

Gemma 模型系列同样非常适合利用 Colab 提供的免费 GPU 资源进行原型设计和实验。在这篇文章中,我们将简要介绍如何在 GPU 和 Cloud TPU 上,使用 Hugging Face Transformers 和 PEFT 库对 Gemma 模型进行参数高效微调(PEFT),这对想要在自己的数据集上微调 Gemma 模型的用户尤其有用。

为什么选择 PEFT?

使用 Hugging Face 微调 Gemma 模型

即使对于中等大小的语言模型,常规的全参数训练也会非常占用内存和计算资源。对于依赖公共计算平台进行学习和实验的用户来说,如 Colab 或 Kaggle,成本可能过高。另一方面,对于企业用户来说,调整这些模型以适应不同领域的成本也是一个需要优化的重要指标。参数高效微调(PEFT)是一种以低成本实现这一目标的流行方法。

了解更多 PEFT 请参考文章:🤗 PEFT: 在低资源硬件上对十亿规模模型进行参数高效微调

在 GPU 和 TPU 上使用 PyTorch 进行 Gemma 模型的高效微调

在 Hugging Face 的 transformers 中,Gemma 模型已针对 PyTorch 和 PyTorch/XLA 进行了优化,使得无论是 TPU 还是 GPU 用户都可以根据需要轻松地访问和试验 Gemma 模型。随着 Gemma 的发布,我们还改善了 PyTorch/XLA 在 Hugging Face 上的 FSDP 使用体验。这种 FSDP 通过 SPMD 的集成还让其他 Hugging Face 模型能够通过 PyTorch/XLA 利用 TPU 加速。本文将重点介绍 Gemma 模型的 PEFT 微调,特别是低秩适应(LoRA)。

想要深入了解 LoRA 技术,我们推荐阅读 Lialin 等人的 “Scaling Down to Scale Up” 以及 Belkada 等人的 精彩文章。

使用低秩适应技术 (LoRA) 对大语言模型进行微调

低秩适应(LoRA)是一种用于大语言模型(LLM)的参数高效微调技术。它只针对模型参数的一小部分进行微调,通过冻结原始模型并只训练被分解为低秩矩阵的适配器层。PEFT 库 提供了一个简易的抽象,允许用户选择应用适配器权重的模型层。

from peft import LoraConfig

lora_config = LoraConfig(
    r=8,
    target_modules=[“q_proj”“o_proj”“k_proj”“v_proj”“gate_proj”“up_proj”“down_proj”],
    task_type=“CAUSAL_LM”,
)

在这个代码片段中,我们将所有的 nn.Linear 层视为要适应的目标层。

在以下示例中,我们将利用 QLoRA,出自 Dettmers 等人,通过 4 位精度量化基础模型,以实现更高的内存效率微调协议。通过首先在您的环境中安装 bitsandbytes 库,然后在加载模型时传递 BitsAndBytesConfig 对象,即可加载具有 QLoRA 的模型。

开始之前

使用 Hugging Face 微调 Gemma 模型

要访问 Gemma 模型文件,用户需先填写 同意表格。

我们继续。

微调 Gemma,让它学会并生成一些“名言金句”

假设您已提交同意表格,您可以从 Hugging Face Hub 获取模型文件。

地址:https://hf.co/collections/google/gemma-release-65d5efbccdbb8c4202ec078b

我们首先下载模型和分词器 (tokenizer),其中包含了一个 BitsAndBytesConfig 用于仅限权重的量化。

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig

model_id = “google/gemma-2b”
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type=“nf4”,
    bnb_4bit_compute_dtype=torch.bfloat16
)

tokenizer = AutoTokenizer.from_pretrained(model_id, token=os.environ[‘HF_TOKEN’])
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map={“”:0}, token=os.environ[‘HF_TOKEN’])

在开始微调前,我们先使用一个相当熟知的名言来测试一下 Gemma 模型:

text = "Quote: Imagination is more"
device = "cuda:0"
inputs = tokenizer(text, return_tensors="pt").to(device)

outputs = model.generate(**inputs, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

模型完成了一个合理的补全,尽管有一些额外的 token:

Quote: Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.

-Albert Einstein

I

但这并不完全是我们希望看到的答案格式。我们将尝试通过微调让模型学会以我们期望的格式来产生答案:

Quote: Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.

Author: Albert Einstein

首先,我们选择一个英文“名人名言”数据集:

from datasets import load_dataset

data = load_dataset(“Abirate/english_quotes”)
data = data.map(lambda samples: tokenizer(samples[“quote”]), batched=True)

接下来,我们使用上述 LoRA 配置对模型进行微调:

import transformers
from trl import SFTTrainer

def formatting_func(example):
    text = f”Quote: {example[‘quote’][0]}nAuthor: {example[‘author’][0]}
    return [text]

trainer = SFTTrainer(
    model=model,
    train_dataset=data[“train”],
    args=transformers.TrainingArguments(
        per_device_train_batch_size=1,
        gradient_accumulation_steps=4,
        warmup_steps=2,
        max_steps=10,
        learning_rate=2e-4,
        fp16=True,
        logging_steps=1,
        output_dir=“outputs”,
        optim=“paged_adamw_8bit”
    ),
    peft_config=lora_config,
    formatting_func=formatting_func,
)
trainer.train()

最终,我们再次使用先前的提示词,来测试模型:

text = "Quote: Imagination is"
device = "cuda:0"
inputs = tokenizer(text, return_tensors="pt").to(device)

outputs = model.generate(**inputs, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

这次,我们得到了我们期待的答案格式:

Quote: Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.

Author: Albert Einstein

名言:想象力比知识更重要,因为知识是有限的,而想象力概括着世界的一切.

作者:阿尔伯特·爱因斯坦

在 TPU 环境下微调,可通过 SPMD 上的 FSDP 加速

如前所述,Hugging Face transformers 现支持 PyTorch/XLA 的最新 FSDP 实现,这可以显著加快微调速度。只需在 transformers.Trainer 中添加 FSDP 配置即可启用此功能:

from transformers import DataCollatorForLanguageModeling, Trainer, TrainingArguments

# Set up the FSDP config. To enable FSDP via SPMD, set xla_fsdp_v2 to True.
fsdp_config = {
    “fsdp_transformer_layer_cls_to_wrap”: [“GemmaDecoderLayer”],
    “xla”True,
    “xla_fsdp_v2”True,
    “xla_fsdp_grad_ckpt”True
}

# Finally, set up the trainer and train the model.
trainer = Trainer(
    model=model,
    train_dataset=data,
    args=TrainingArguments(
        per_device_train_batch_size=64,  # This is actually the global batch size for SPMD.
        num_train_epochs=100,
        max_steps=-1,
        output_dir=“./output”,
        optim=“adafactor”,
        logging_steps=1,
        dataloader_drop_last = True,  # Required for SPMD.
        fsdp=“full_shard”,
        fsdp_config=fsdp_config,
    ),
    data_collator=DataCollatorForLanguageModeling(tokenizer, mlm=False),
)
trainer.train()

下一步

通过这个从源笔记本改编的简单示例,我们展示了应用于 Gemma 模型的 LoRA 微调方法。完整的 GPU colab 在 这里 可以找到,完整的 TPU 脚本在 这里可以找到。我们对于这一最新加入我们开源生态系统的成员所带来的无限研究和学习机会感到兴奋。我们鼓励用户也浏览 Gemma 文档 和我们的 发布博客,以获取更多关于训练、微调和部署 Gemma 模型的示例。

查看文内链接,请点击阅读原文在 Hugging Face 博客上查看:

https://hf.co/blog/zh/gemma-peft

 

Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2024 年 2 月
 1234
567891011
12131415161718
19202122232425
26272829  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
共推空天领域智能化升级!趋境科技与金航数码强强联手

共推空天领域智能化升级!趋境科技与金航数码强强联手

共推空天领域智能化升级!趋境科技与金航数码强强联手 十三 2025-12-09 18:18:41 来源:量子位...
起底“豆包手机”:核心技术探索早已开源,GUI Agent布局近两年,“全球首款真正的AI手机”

起底“豆包手机”:核心技术探索早已开源,GUI Agent布局近两年,“全球首款真正的AI手机”

起底“豆包手机”:核心技术探索早已开源,GUI Agent布局近两年,“全球首款真正的AI手机” 西风 202...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
5天连更5次,可灵AI年末“狂飙式”升级

5天连更5次,可灵AI年末“狂飙式”升级

5天连更5次,可灵AI年末“狂飙式”升级 思邈 2025-12-10 14:28:37 来源:量子位 让更大规...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
小冰之父李笛智能体创业,公司取名Nextie!陆奇是股东

小冰之父李笛智能体创业,公司取名Nextie!陆奇是股东

小冰之父李笛智能体创业,公司取名Nextie!陆奇是股东 Jay 2025-12-09 08:26:01 来源...
梁文锋,Nature全球年度十大科学人物!

梁文锋,Nature全球年度十大科学人物!

梁文锋,Nature全球年度十大科学人物! 一水 2025-12-09 09:46:23 来源:量子位 来自安...
起底“豆包手机”:核心技术探索早已开源,GUI Agent布局近两年,“全球首款真正的AI手机”

起底“豆包手机”:核心技术探索早已开源,GUI Agent布局近两年,“全球首款真正的AI手机”

起底“豆包手机”:核心技术探索早已开源,GUI Agent布局近两年,“全球首款真正的AI手机” 西风 202...
摩尔线程新一代GPU架构10天后发布

摩尔线程新一代GPU架构10天后发布

摩尔线程新一代GPU架构10天后发布 思邈 2025-12-09 15:46:09 来源:量子位 国内首个聚焦...
极客公园创新大会 2026在京落幕,罗永浩、张楠、何小鹏、刘靖康等共议 AI 时代「进程由我」

极客公园创新大会 2026在京落幕,罗永浩、张楠、何小鹏、刘靖康等共议 AI 时代「进程由我」

极客公园创新大会 2026在京落幕,罗永浩、张楠、何小鹏、刘靖康等共议 AI 时代「进程由我」 henry 2...