Transformers documentation

Compile model

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

and get access to the augmented documentation experience

to get started

Compile model

Quantization menghemat memori, tetapi mendekuantisasi bobot di dalam setiap matmul dapat membuat generasi lebih lambat daripada dalam bf16. torch.compile menggabungkan pekerjaan tersebut ke dalam kernel yang dioptimalkan dan memulihkan sebagian besar kecepatannya.

Compile secara otomatis di generate

generate() mengompilasi jalur maju model secara otomatis ketika semua kondisi berikut terpenuhi.

Lewatkan cache_implementation="static" ke generate(). Contoh di bawah menggunakan kuantisasi int8 weight-only torchao.

import torch
from torchao.quantization import Int8WeightOnlyConfig
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig

model_id = "Qwen/Qwen3-0.6B"
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=TorchAoConfig(quant_type=Int8WeightOnlyConfig()),
    device_map="cuda",
)
tokenizer = AutoTokenizer.from_pretrained(model_id)

inputs = tokenizer("The capital of France is", return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=32, cache_implementation="static")
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Panggilan pertama lambat saat model dikompilasi. Panggilan berikutnya dengan bentuk yang sama akan menggunakan ulang kernel yang telah dikompilasi.

Lewati disable_compile=True ke generate() untuk mematikan kompilasi otomatis.

Update on GitHub