Transformers documentation

모델의 양자화 해제

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.23.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

모델의 양자화 해제

Dequantizing은 양자화된 가중치를 bf16과 같은 더 높은 정밀도의 dtype로 되돌리는 과정입니다. 모델을 완전히 fine-tune하거나, 해당 방법의 커널이 지원하지 않는 하드웨어에서 실행하거나, 다른 형식으로 변환하기 위해 모델을 Dequantize합니다. 결과는 양자화되지 않은 모델과 동일한 양의 메모리를 사용하며, 반올림으로 인해 손실된 정확도를 회복하지 않습니다.

Transformers는 방법에 따라 두 가지 방식으로 dequantize합니다.

메서드양자화 해제 방법
bitsandbytes, HIGGS로드 후 dequantize()를 호출합니다.
compressed-tensors, 세밀한 FP8, GGUF, Metal, MXFP4로드할 때 해당 메서드의 config에 dequantize=True를 전달합니다.

다른 메서드는 dequantize()에서 NotImplementedError를 발생시킵니다.

Dequantize after loading

로드된 모델에 dequantize()를 호출합니다. 기본적으로 모델이 로드된 데이터 유형을 사용합니다. 다른 것을 선택하려면 dtype을 전달하세요.

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen3-0.6B",
    quantization_config=BitsAndBytesConfig(load_in_4bit=True),
    device_map="auto",
)
model = model.dequantize(dtype=torch.bfloat16)

dequantize()는 quantization_config도 제거하므로, save_pretrained()는 일반적인 전체 정밀도 체크포인트를 저장합니다.

Dequantize while loading

해당 기능을 지원하는 메서드의 config에 dequantize=True를 전달합니다. 가중치가 로드되는 시점에 역양자화되므로 양자화 커널은 사용되지 않습니다. 아래 예제는 gpt-oss의 MXFP4 가중치를 bf16으로 로드합니다.

from transformers import AutoModelForCausalLM, Mxfp4Config

model = AutoModelForCausalLM.from_pretrained(
    "openai/gpt-oss-20b",
    quantization_config=Mxfp4Config(dequantize=True),
    device_map="auto",
)

일부 메서드는 커널을 실행할 수 없는 경우 자동으로 dequantize합니다. 예를 들어, GGUF는 파일의 양자화 유형을 지원하는 커널이 없을 때 dequantize로 폴백합니다.

Update on GitHub