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

© . This site is unofficial and not affiliated with Hugging Face, Inc.