Skip to Content
LLMOps
book

LLMOps

by Abi Aryan
July 2025
Beginner to intermediate
284 pages
3h 23m
Chinese
O'Reilly Media, Inc.
Content preview from LLMOps

第 5 章 基于 LLM 应用程序的模型域适应 基于 LLM 应用程序的模型域适配

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在上一章中,我们讨论了模型部署的不同架构。在本章中,我们将讨论如何对模型进行域适配。实践者经常把领域适应称为 "微调",但微调实际上只是使模型在领域中良好运行的众多方法之一。

在本章中,我们将介绍几种模型适应方法,包括提示工程、微调和检索增强生成(RAG)。

我们还将研究如何优化 LLM,使其在需要压缩模型的资源受限环境中运行。最后,我们将讨论最佳实践和扩展法则,向您展示如何确定 LLMs 需要多少数据才能有效运行。

从零开始训练 LLMs

从头开始训练 LLMs 可以是简单的,也可以是资源密集型的,具体取决于您的应用。对于大多数应用而言,使用现有的开源 LLM 或专有 LLM 是有意义的。另一方面,学习 LLM 如何工作的最佳方法莫过于从头开始训练一个 LLM。

从头开始训练 LLM 是一项复杂的资源密集型任务,需要一个全面的管道,其中包括数据准备、模型架构选择、训练配置和监控。让我们来看看从头开始训练 LLM 的结构化方法。

第 1 步:选择任务

确定 建立该模型的原因、它将服务的领域以及它将执行的任务(如文本生成、摘要或代码生成)。决定成功的标准,如复杂性、准确性或其他特定领域的评估指标。

第 2 步:准备数据

在 将数据输入模型之前,模型预处理步骤要确保输入的数据是模型能够有效处理的形式。这包括标记文本、去除噪音、规范格式,有时还包括将复杂结构简化为模型更容易理解的组件。预处理还包括特征选择,即挑选最相关的数据,使模型的 "焦点 "集中在真正重要的数据上。这一步包括

收集大规模文本数据

高质量来源包括书籍、文章、网站、研究论文、代码库以及特定领域的文本(如果模型是专业的(如用于法律或医学领域))。

清理数据

包括删除无用元素(如广告或格式化人工痕迹)和处理拼写错误。这项工作可以使用 Hugging Face 等库。

对数据进行标记

您可以使用 字节对编码 (BPE) 或 SentencePiece 等子词标记化方法来完成这项工作,如 BERT 和 GPT-3 等模型所做的那样。您也可以使用 Hugging Face 的AutoTokenizer 来完成这项任务。标记化对于处理大型词汇表和避免过多参数的需要至关重要。

第 3 步:决定模型架构

选择 适合您的数据、资源和目标的模型大小。模型配置的范围从较小的模型(数亿个参数)到全面的 LLMs(数十亿甚至数万亿个参数)。正如第 1 章中所讨论的,调整基本架构以适应您的特定需求,无论是改变层数、改变注意机制,还是添加专门组件(如检索增强 机制 ,如果专注于知识密集型任务)。图 5-1 显示了三种一般类型的架构。

图 5-1. 三种 LLM 架构(来源:Abhinav Kimothi)

第 4 步:建立训练基础设施

训练 大型模型通常需要在多个 GPU 或 TPU 上进行分布式训练, ,最好配备大内存(16 GB 以上)和快速互连(如 NVLink)。PyTorch 的分布式数据并行(DDP)或 TensorFlow 的MultiWorkerMirroredStrategy ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

LLMOps

LLMOps

Abi Aryan
Agentic GraphRAG

Agentic GraphRAG

Anthony Alcaraz, Sam Julien
INSPIRED

INSPIRED

Marty Cagan
Accelerate

Accelerate

Nicole Forsgren, Jez Humble, Gene Kim

Publisher Resources

ISBN: 9798341663862