Skip to content
Qubitium edited this page Dec 1, 2026 · 2 revisions

TWLA

🏠 Home · 🧭 Choose a method · 🚀 Quickstart · 🧰 Help

On this page

TWLA is a calibrated post-training quantization method for linear projections. Each weight uses a ternary code (-1, 0, or 1) with a scale and shift shared by an input group within an output row. The ternary alphabet has about 1.58 bits of capacity per weight. The checkpoint stores each code in a two-bit lane and adds group and rotation metadata.

Quantize, save, and load

Supply representative text in calibration_texts. The example applies four-bit activation quantization to the packed projections. Use TWLAConfig() to keep activations in the model dtype.

import torch
from gptqmodel import BACKEND, CalibrationConfig, GPTQModel, TWLAConfig

model = GPTQModel.load(
    "Qwen/Qwen2.5-1.5B-Instruct",
    quantize_config=TWLAConfig(act_bits=4),
    device="cuda",
    dtype=torch.float16,
)
model.quantize(CalibrationConfig(dataset=calibration_texts))
model.save("qwen2.5-1.5b-twla")

quantized = GPTQModel.load(
    "qwen2.5-1.5b-twla",
    backend=BACKEND.TWLA_TORCH,
    device="cuda",
    dtype=torch.float16,
)

Configuration

Use TWLAConfig(...) or QuantizeConfig(method="twla", ...). The format requires bits=2, sym=False, and desc_act=False. Selected projections must be torch.nn.Linear with input width divisible by 16.

Field Default Behavior
group_size 128 A positive multiple of 16, or -1 for one group per output row.
e2m_iters 15 Number of metric-aware relocation iterations in the ternary fit.
kotms, kotms_guard True, True Learn a Kronecker rotation to shape weights for ternarization. The guard keeps the shaped rotation only when it improves calibrated reconstruction.
kotms_iters, kotms_lr 100, 0.01 Rotation optimization iterations and learning rate.
kotms_pi0, kotms_rho, kotms_beta 1/3, 1/3, 0.1 Center-component mixture weight, target center-component responsibility, and balance penalty weight.
kotms_sigma_min, kotms_sigma0_ratio, kotms_sigma1_ratio 1e-6, 0.25, 0.25 Minimum standard deviation and relative widths of the center and outer mixture components.
kotms_seed 0 Seed for the rotation optimizer.
act_bits 16 16 leaves activations in the model dtype. Choose 2, 4, 6, or 8 for fixed-width activation quantization.
act_clip 1.0 Per-group activation clipping multiplier in (0, 1].
ila_amp False Search for a bit width per decoder layer after weight packing.
ila_amp_candidates, ila_amp_baseline_bits (2, 4, 6, 8), 8 Candidate widths and the baseline width used when scoring. The baseline must be a candidate.
ila_amp_avg_bits, ila_amp_pair_beta 4.0, 1.0 Average bit budget across decoder layers and the weight given to adjacent-layer interactions.
ila_amp_rows, ila_amp_seq_len 4, 1024 Maximum number of calibration sequences used to score the assignments and maximum tokens per sequence.

The weight solve uses a calibration Hessian to fit ternary codes and compensate for errors in later input groups. For layer-wise mixed precision, try TWLAConfig(ila_amp=True, ila_amp_avg_bits=4.0). Leave act_bits=16 so the search can choose each width. Enabling ILA-AMP disables quantization disk offload. The search scores the packed model on one device and needs the model materialized there.

Checkpoint and inference

The safetensors checkpoint saves method and format twla. Each packed projection stores an INT32 qweight with 16 lanes per word, FP16 scales and shifts, the g_idx group map, FP32 Kronecker factors, and the saved rotation and activation settings.

Use GPTQModel.load with BACKEND.AUTO, BACKEND.TWLA_TORCH, or its method-specific BACKEND.TORCH alias. The Torch runtime declares CPU, NVIDIA CUDA, and AMD ROCm support with FP16, BF16, or FP32 activations. It applies the saved rotation and optional activation quantization, decodes the weights one group at a time, and uses FP32 Torch matrix multiplies and accumulation before returning the input dtype.

TwlaTorchLinear declares opt-in Inductor compilation through model.optimize(). On CPU, the default kotms=True, act_bits=16 path passed Dynamo, AOT Eager, and Inductor probes without graph breaks. Lower-bit activation paths can break full-graph tracing on Tensor.item(). Static-cache generation stays eager by default, and the kernel declares no eager CUDA-graph capture support. See the kernel compile matrix.

Source and further reading

Clone this wiki locally