Optimum 文档

使用 Brevitas 进行量化

您正在查看 main 版本,该版本需要从源代码安装。如果您想要常规的 pip 安装,请查看最新的稳定版本 (v1.24.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

使用 Brevitas 进行量化

BrevitasQuantizer

class optimum.amd.BrevitasQuantizer

< >

( model: Module model_name_or_path: str )

处理在 huggingface.co/models 上共享的模型的运行时量化过程。

from_pretrained

< >

( model_name_or_path: str subfolder: str = '' revision: typing.Optional[str] = None cache_dir: typing.Optional[str] = None trust_remote_code: bool = False force_download: bool = False local_files_only: bool = False use_auth_token: typing.Union[bool, str, NoneType] = None device_map: typing.Union[typing.Dict, str, torch.device, NoneType] = None **model_kwargs )

参数

  • model_name_or_path (Union[str, Path]) — 可以是 Hugging Face Hub 上模型仓库的模型 ID,也可以是包含模型的本地目录的路径。
  • subfolder (str, 默认为 "") — 如果模型文件位于 Hugging Face Hub 上的模型目录/仓库的子文件夹中,您可以在此处指定子文件夹名称。
  • revision (Optional[str], 可选, 默认为 None) — Revision 是要使用的特定模型版本。它可以是分支名称、标签名称或提交 ID。
  • cache_dir (Optional[str], 可选) — 缓存下载的预训练模型权重的目录路径,如果不想使用标准缓存。
  • trust_remote_code (bool, 默认为 False) — 允许使用模型仓库中托管的自定义代码进行建模。此选项仅应为您信任的仓库设置,并且您已阅读过代码,因为它将在您的本地计算机上执行模型仓库中存在的任意代码。
  • force_download (bool, 默认为 False) — 是否强制(重新)下载模型权重和配置文件,覆盖缓存版本(如果存在)。
  • local_files_only (Optional[bool], 默认为 False) — 是否仅查看本地文件(即,不尝试下载模型)。
  • use_auth_token (Optional[str], 默认为 None) — 用于远程文件的 HTTP Bearer 授权的令牌。如果为 True,将使用运行 transformers-cli login 时生成的令牌(存储在 ~/.huggingface 中)。

加载 BrevitasQuantizer 和模型。

quantize

< >

( quantization_config: BrevitasQuantizationConfig calibration_dataset: typing.Optional[typing.List[typing.Dict]] = None )

参数

  • quantization_config (BrevitasQuantizationConfig) — 用于量化模型的量化配置。
  • calibration_dataset (Optional[List[Dict]], 默认为 None) — 如果量化涉及校准阶段,则需要将此参数指定为模型输入的列表。示例:calibration_dataset = [{"input_ids": torch.tensor([[1, 2, 3, 4]])}, {"input_ids": torch.tensor([[6, 7, 3, 4]])}],这是一个以 input_ids 作为参数的模型的数据集,并且有两个样本。

使用 Brevitas 根据 quantization_config 量化模型。

BrevitasQuantizationConfig

class optimum.amd.BrevitasQuantizationConfig

< >

( weights_bitwidth: int = 8 activations_bitwidth: typing.Optional[int] = 8 weights_only: bool = False weights_param_method: typing.Literal['stats', 'mse'] = 'stats' weights_symmetric: bool = True scale_precision: typing.Literal['float_scale', 'power_of_two_scale'] = 'float_scale' weights_quant_granularity: typing.Literal['per_tensor', 'per_channel', 'per_group'] = 'per_tensor' weights_group_size: typing.Optional[int] = None quantize_zero_point: bool = True activations_param_method: typing.Optional[typing.Literal['stats', 'mse']] = 'stats' is_static: bool = False activations_symmetric: typing.Optional[bool] = False activations_quant_granularity: typing.Optional[typing.Literal['per_tensor', 'per_row', 'per_group']] = 'per_tensor' activations_group_size: typing.Optional[int] = None activations_equalization: typing.Optional[typing.Literal[None, 'layerwise', 'cross_layer']] = 'cross_layer' apply_weight_equalization: bool = False apply_bias_correction: bool = False apply_gptq: bool = False gptq_act_order: typing.Optional[bool] = None device: str = 'auto' layers_to_exclude: typing.Optional[typing.List] = None gpu_device_map: typing.Optional[typing.Dict[int, float]] = None cpu_device_map: typing.Optional[typing.Dict[str, float]] = None )

参数

  • weights_bitwidth (int, 默认为 8) — 权重量化的位宽。例如,当 weights_bitwidth=8 时,每个权重值都以 8 位量化。
  • activations_bitwidth (Optional[int], 默认为 8) — 激活量化的位宽。
  • weights_only (bool, 默认为 False) — 如果设置为 True,则仅量化权重,否则激活也将被量化。
  • weights_param_method (str, 默认为 stats) — 用于估计权重的量化参数(比例、零点)的策略。有两种策略可用:
    • "stats": 使用最小值-最大值来估计要量化的范围。
    • "mse": 使用未量化权重和量化权重之间的均方误差来估计要量化的范围。
  • weights_symmetric (bool, defaults to True) — 是否对权重使用对称量化。
  • scale_precision (str, defaults to "float_scale") — 精确化尺度的约束。可以是 "float_scale" (任意尺度),或 "power_of_two_scale" (尺度约束为 2 的幂)。
  • weights_quant_granularity (str, defaults to "per_tensor") — 权重​​量化的粒度。此参数可以是:
    • "per_tensor":单个尺度(以及可能的零点)用于一个权重矩阵。
    • "per_channel":权重矩阵的每一列(外维度)都有其自己的尺度(以及可能的零点)。
    • "per_group":权重矩阵的每一列可能具有多个尺度,按 weight_group_size 分组。
  • weights_group_size (Optional[int], defaults to None) — 在 weights_quant_granularity="per_group" 的情况下用于权重的分组大小。在这种情况下默认为 128,否则为 None
  • quantize_zero_point (bool, defaults to True) — 当设置为 True 时,未量化的值 0.0 可以精确地表示为量化值:零点。当设置为 False 时,量化范围 [a, b] 可以精确表示(a 和 b 上没有舍入),但未量化的值零不能精确表示。
  • activations_param_method (List[str]) — 用于估计激活的量化参数(尺度、零点)的策略。有两种策略可用:
    • "stats":使用最小值-最大值来估计量化的范围。
    • "mse":使用未量化激活和量化激活之间的均方误差来估计量化的范围。
  • is_static (bool, defaults to False) — 是否应用静态量化或动态量化。
  • activations_symmetric (bool, defaults to False) — 是否对激活使用对称量化。
  • activations_quant_granularity (str, defaults to "per_tensor") — 激活量化的粒度。此参数可以是 "per_tensor""per_row""per_group"。如果使用静态量化 (is_static=True),则只能使用 "per_tensor"
  • activations_group_size (int, defaults to None) — 在 activations_quant_granularity="per_group" 的情况下用于激活的分组大小。在这种情况下默认为 64,否则为 None
  • activations_equalization (Optional[str], defaults to "cross_layer") — 是否应用激活均衡化 (SmoothQuant)。可能的选项有:
    • None:不进行激活均衡化。
    • "layerwise":应用 SmoothQuant,如 https://arxiv.org/abs/2211.10438 中所述。激活重新缩放将作为乘法节点添加,该节点不会融合到前一层中。
    • "cross_layer":应用 SmoothQuant,并在可能的情况下将激活重新缩放融合到前一层中(示例:nn.LayerNorm 后跟 nn.Linear)。这是通过使用 torch.fx 对模型进行图捕获来实现的。
  • apply_weight_equalization (bool, defaults to False) — 跨层应用权重均衡化,遵循 https://arxiv.org/abs/1906.04721。此参数对于激活函数为线性或分段线性(如 OPT 模型中使用的 ReLU)的模型很有用,并且允许通过平衡跨层的尺度来减少权重的量化误差。
  • apply_bias_correction (bool, defaults to False) — 应用偏差校正以补偿由量化引起的激活偏差的变化。
  • apply_gptq (bool, defaults to False) — 是否应用 GPTQ 算法来量化权重。
  • gptq_act_order (Optional[bool], defaults to None) — 当 apply_gptq=True 时,是否使用激活重排序(act-order,也称为 desc-act)。如果 apply_gptq=True,则默认为 False
  • layers_to_exclude (Optional[List], defaults to None) — 指定不应量化的层的名称。这应该只是层名称的最后一部分。如果同一名称在多个层中重复,它们都将被排除。如果留为 None,则会自动识别并排除最后一个线性层。

QuantizationConfig 是处理所有 Brevitas 量化参数的配置类。

< > GitHub 上更新