Bitsandbytes 文档

LLM.int8()

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

LLM.int8()

LLM.int8() 是一种量化方法,旨在使大型语言模型推理在没有显著性能下降的情况下更易于使用。与朴素的 8 位量化(可能导致关键信息和准确性损失)不同,LLM.int8() 动态适应,确保计算的敏感部分在需要时保持更高的精度。其关键是从输入和权重中提取离群值,并以 16 位精度进行乘法运算。所有其他值都以 8 位精度进行乘法运算,然后反量化回 16 位。16 位和 8 位乘法运算的输出相结合,产生最终输出。

更多资源

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 上更新