llmcompressor.entrypoints.oneshot
用于对训练后模型进行优化的 Oneshot 压缩入口点。
提供主要的 oneshot 压缩入口点,用于在无需额外训练的情况下对预训练模型应用量化、剪枝和其他压缩技术。支持基于校准的压缩,并提供各种管道配置以实现高效的模型优化。
类
-
Oneshot–负责对预训练模型进行一次性校准的类。
函数
-
oneshot–执行模型的单次校准。
Oneshot
负责对预训练模型进行一次性校准的类。
此类负责一次性校准的整个生命周期,包括预处理(模型和分词器/处理器初始化)、模型优化(量化或稀疏化)以及后处理(保存输出)。可以通过使用配方来指定模型优化指令。
-
输入关键字参数:
kwargs将被解析为model_args:用于加载和配置预训练模型(例如,AutoModelForCausalLM)的参数。dataset_args:用于数据集相关配置的参数,例如校准数据加载器。recipe_args:用于定义和配置指定优化操作的配方的参数。
解析器定义在
src/llmcompressor/args/中。 -
生命周期概述: 一次性校准生命周期包括三个步骤
- 预处理:
- 实例化预训练模型和分词器/处理器。
- 确保输入和输出嵌入层在共享张量时保持未绑定。
- 修补模型以包含用于保存量化配置的附加功能。
- 一次性校准:
- 使用全局
CompressionSession优化模型,并应用配方定义的修改器(例如,GPTQModifier、SparseGPTModifier)。
- 使用全局
- 后处理:
- 将模型、分词器/处理器和配置保存到指定的
output_dir。
- 将模型、分词器/处理器和配置保存到指定的
- 预处理:
-
用法
方法:init(**kwargs):通过解析输入参数、执行预处理和设置实例属性来初始化 Oneshot 对象。
__call__(**kwargs):
Performs the one-shot calibration process by preparing a calibration
dataloader, applying recipe modifiers to the model, and executing
postprocessing steps.
save():
Saves the calibrated model and tokenizer/processor to the specified
`output_dir`. Supports saving in compressed formats based on model
arguments.
apply_recipe_modifiers(calibration_dataloader, **kwargs):
Applies lifecycle actions (e.g., `initialize`, `finalize`) using modifiers
defined in the recipe. Each action is executed via the global
`CompressionSession`.
使用提供的参数初始化 Oneshot 类。
将输入的关键字参数解析为 model_args、dataset_args 和 recipe_args。执行预处理以初始化模型和分词器/处理器。
参数
-
–model_argsModelArguments参数,负责控制模型加载和保存逻辑 -
–dataset_argsDatasetArguments参数,负责控制数据集加载、预处理和数据加载器加载 -
–recipe_argsRecipeArguments参数,负责包含与配方相关的参数 -
–output_dir执行一次性操作后保存输出模型的路径
-
(log_dirstr | None, 默认值:None) –保存一次性运行期间日志的路径。如果为 None,则不向文件记录任何内容。
方法
-
apply_recipe_modifiers–在生命周期中将配方修改器应用于模型。
源文件位于 llmcompressor/entrypoints/oneshot.py
apply_recipe_modifiers
apply_recipe_modifiers(
calibration_dataloader: DataLoader | None,
recipe_stage: str | None = None,
)
在生命周期中将配方修改器应用于模型。
修改器在配方中定义,并通过全局 CompressionSession 通过生命周期操作(initialize、finalize)执行。
源文件位于 llmcompressor/entrypoints/oneshot.py
oneshot
oneshot(
model: str | PreTrainedModel,
config_name: str | None = None,
tokenizer: str | PreTrainedTokenizerBase | None = None,
processor: str | ProcessorMixin | None = None,
use_auth_token: bool = False,
precision: str = "auto",
tie_word_embeddings: bool = True,
trust_remote_code_model: bool = False,
save_compressed: bool = True,
model_revision: str = "main",
recipe: str | list[str] | None = None,
recipe_args: list[str] | None = None,
clear_sparse_session: bool = False,
stage: str | None = None,
dataset: str | Dataset | DatasetDict | None = None,
dataset_config_name: str | None = None,
dataset_path: str | None = None,
splits: str | list[str] | dict[str, str] | None = None,
batch_size: int = 1,
data_collator: str | Callable = "truncation",
num_calibration_samples: int = 512,
shuffle_calibration_samples: bool = False,
max_seq_length: int = 384,
pad_to_max_length: bool = True,
text_column: str = "text",
concatenate_data: bool = False,
streaming: bool = False,
overwrite_cache: bool = False,
preprocessing_num_workers: int | None = None,
min_tokens_per_module: float | None = None,
moe_calibrate_all_experts: bool = True,
quantization_aware_calibration: bool = True,
output_dir: str | None = None,
log_dir: str | None = None,
**kwargs,
) -> PreTrainedModel
执行模型的单次校准。
模型参数
参数
-
(modelstr | PreTrainedModel) –来自 huggingface.co/models 的预训练模型标识符或本地模型路径。必需参数。
-
–distill_teacher用于蒸馏的教师模型(已训练的文本生成模型)。
-
(config_namestr | None, 默认值:None) –如果与 model_name 不同,则为预训练配置名称或路径。
-
(tokenizerstr | PreTrainedTokenizerBase | None, default:None) –如果与 model_name 不同,则为预训练分词器名称或路径。
-
(processorstr | ProcessorMixin | None, default:None) –如果与 model_name 不同,则为预训练处理器名称或路径。
-
(use_auth_tokenbool, 默认值:False) –是否为私有模型使用 Hugging Face 认证令牌。
-
(precisionstr, default:'auto') –将模型权重转换为的精度,默认为 auto。
-
(tie_word_embeddingsbool, 默认值:True) –如果可能,模型的输入和输出词嵌入是否应保持绑定。False 表示始终取消绑定。
-
(trust_remote_code_modelbool, 默认值:False) –是否允许自定义模型执行其自身的建模文件。
-
(save_compressedbool, 默认值:True) –保存期间是否压缩稀疏模型。
-
(model_revisionstr, 默认值:'main') –要使用的具体模型版本(可以是分支名称、标签或提交 ID)。# 配方参数
-
(recipestr | list[str] | None, default:None) –LLM Compressor 配方路径,或指向多个 LLM Compressor 配方的路径列表。
-
(recipe_argslist[str] | None, default:None) –要评估的配方参数列表,格式为“key1=value1”、“key2=value2”。
-
(clear_sparse_sessionbool, 默认值:False) –是否在运行之间清除 CompressionSession/ CompressionLifecycle 数据。
-
(stagestr | None, 默认值:None) –用于一次性校准的配方阶段。# 数据集参数
-
(数据集str | Dataset | DatasetDict | None, default:None) –要使用的数据集名称(通过 datasets 库)。
-
(dataset_config_namestr | None, 默认值:None) –要使用的数据集的配置名称。
-
(dataset_pathstr | None, 默认值:None) –自定义数据集的路径。支持 json、csv、dvc。
-
(splitsstr | list[str] | dict[str, str] | None, default:None) –可选的每个分割的下载百分比。
-
(batch_sizeint, 默认值:1) –校准数据集批次大小。在校准期间,LLM Compressor 会禁用 lm_head 输出计算,以减少大型校准批次大小的内存使用。较大的批次大小可能会导致过度填充或截断,具体取决于 data_collator。
-
(data_collatorstr | Callable, default:'truncation') –用于从数据集中形成批次的函数。也可以指定“truncation”或“padding”来截断或填充批次中不均匀的序列长度。默认为“padding”。
-
(num_calibration_samplesint, default:512) –用于一次性校准的样本数。
-
(shuffle_calibration_samplesbool, 默认值:False) –是否在校准前对数据集进行混洗。
-
(max_seq_lengthint, default:384) –分词后最大总输入序列长度。
-
(pad_to_max_lengthbool, 默认值:True) –是否将所有样本填充到
max_seq_length。 -
(text_columnstr, default:'text') –用作分词器/处理器
text输入的键。 -
(concatenate_databool, 默认值:False) –是否连接数据点以填满 max_seq_length。
-
(streamingbool, 默认值:False) –设置为 True 以从云数据集流式传输数据。
-
(overwrite_cachebool, 默认值:False) –是否覆盖缓存的预处理数据集。
-
(preprocessing_num_workersint | None, 默认值:None) –数据集预处理的进程数。
-
(min_tokens_per_modulefloat | None, 默认值:None) –每个模块的最小令牌百分比,与 MoE 模型相关。
-
(moe_calibrate_all_expertsbool, 默认值:True) –在 MoE 模型校准期间是否校准所有专家。当设置为 True 时,所有专家在校准期间都会看到所有令牌,从而确保正确的量化统计数据。当设置为 False 时,仅使用路由的专家。仅与 MoE 模型相关。默认为 True。
-
(quantization_aware_calibrationbool, 默认值:True) –是否在顺序管道中启用量化感知校准。当设置为 True 时,在校准的前向传递中应用量化。当设置为 False 时,在校准的前向传递中禁用量化。默认为 True。# 杂项参数
-
(output_dirstr | None, 默认值:None) –校准后保存输出模型的路径。如果为 None,则不保存任何内容。
-
(log_dirstr | None, 默认值:None) –保存一次性运行期间日志的路径。如果为 None,则不向文件记录任何内容。
返回
-
PreTrainedModel–已校准的 PreTrainedModel
源文件位于 llmcompressor/entrypoints/oneshot.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |