Bitsandbytes 文档

LLM.int8()

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始使用

LLM.int8()

LLM.int8() 是一种量化方法,旨在使大型语言模型推理更易于访问,而不会显着降低性能。与朴素的 8-bit 量化(可能导致关键信息和准确性损失)不同,LLM.int8() 动态调整以确保计算的敏感组件在需要时保持更高的精度。关键是从输入和权重中提取异常值,并在 16-bit 中将它们相乘。所有其他值在 8-bit 中相乘,然后再反量化回 16-bit。来自 16-bit 和 8-bit 乘法的输出被组合以产生最终输出。

更多资源

Linear8bitLt

class bitsandbytes.nn.Linear8bitLt

< >

( input_features: int output_features: int bias = True has_fp16_weights = True threshold = 0.0 index = None device = None )

此类是 LLM.int8() 算法的基础模块。要了解更多信息,请查看论文。

为了量化线性层,应首先将原始 fp16 / bf16 权重加载到 Linear8bitLt 模块中,然后调用 int8_module.to("cuda") 以量化 fp16 权重。

示例

import torch
import torch.nn as nn

import bitsandbytes as bnb
from bnb.nn import Linear8bitLt

fp16_model = nn.Sequential(
    nn.Linear(64, 64),
    nn.Linear(64, 64)
)

int8_model = nn.Sequential(
    Linear8bitLt(64, 64, has_fp16_weights=False),
    Linear8bitLt(64, 64, has_fp16_weights=False)
)

int8_model.load_state_dict(fp16_model.state_dict())
int8_model = int8_model.to(0) # Quantization happens here

__init__

< >

( input_features: int output_features: int bias = True has_fp16_weights = True threshold = 0.0 index = None device = None )

参数

  • input_features (int) — 线性层的输入特征数量。
  • output_features (int) — 线性层的输出特征数量。
  • bias (bool, 默认为 True) — 线性类是否也使用偏置项。

初始化 Linear8bitLt 类。

Int8Params

class bitsandbytes.nn.Int8Params

< >

( data: typing.Optional[torch.Tensor] = None requires_grad = True has_fp16_weights = False CB: typing.Optional[torch.Tensor] = None SCB: typing.Optional[torch.Tensor] = None )

__init__

( *args **kwargs )

初始化 self。有关准确的签名,请参阅 help(type(self))。

< > 在 GitHub 上更新