I'm trying to implement the Nunchaku quantization. In @quantizeqwenimage.py , I've…
- User
I'm trying to implement the Nunchaku quantization. In @quantize_qwen_image.py , I've tested that the recounstruction error of
quantize_awq_layer_naiveis small, but that ofquantize_awq_layer_babaiis large. Is there any problem inquantize_awq_layer_babai? --- Content from referenced files --- Content from @quantize_qwen_image.py: #!/usr/bin/env python3import argparse
import safetensors import safetensors.torch import torch from tqdm import tqdm
def pack_scale(scale: torch.Tensor, group_size: int) -> torch.Tensor: warp_n = 128 num_lanes = 32 n = scale.shape[0] s_pack_size = min(max(warp_n // num_lanes, 2), 8) num_s_lanes = min(num_lanes, warp_n // s_pack_size) num_s_packs = warp_n // (s_pack_size * num_s_lanes) warp_s = num_s_packs * num_s_lanes * s_pack_size scale = scale.view( n // warp_s, num_s_packs, num_s_lanes // 4, s_pack_size // 2, 4, 2, -1 ) scale = scale.permute(0, 6, 1, 2, 4, 3, 5).contiguous() return scale.view(-1) if group_size == -1 else scale.view(-1, n)
def unpack_scale(packed: torch.Tensor, n: int, m: int) -> torch.Tensor: warp_n = 128 num_lanes = 32 s_pack_size = min(max(warp_n // num_lanes, 2), 8) num_s_lanes = min(num_lanes, warp_n // s_pack_size) num_s_packs = warp_n // (s_pack_size * num_s_lanes) warp_s = num_s_packs * num_s_lanes * s_pack_size packed = packed.view(m, n) scale = packed.view( n // warp_s, m, num_s_packs, num_s_lanes // 4, 4, s_pack_size // 2, 2 ) scale = scale.permute(0, 2, 3, 5, 4, 6, 1).contiguous() return scale.view(n, m)
def pack_lowrank_weight(weight: torch.Tensor, down: bool) -> torch.Tensor: lane_n = 1 n_pack_size = 2 num_n_lanes = 8 lane_k = 2 k_pack_size = 2 num_k_lanes = 4 frag_n = n_pack_size * num_n_lanes * lane_n frag_k = k_pack_size * num_k_lanes * lane_k if down: r, c = weight.shape r_frags = r // frag_n c_frags = c // frag_k weight = weight.view(r_frags, frag_n, c_frags, frag_k) weight = weight.permute(2, 0, 1, 3).contiguous() else: c, r = weight.shape c_frags = c // frag_n r_frags = r // frag_k weight = weight.view(c_frags, frag_n, r_frags, frag_k) weight = weight.permute(0, 2, 1, 3).contiguous() weight = weight.view( c_frags, r_frags, n_pack_size, num_n_lanes, k_pack_size, num_k_lanes, lane_k ) weight = weight.permute(0, 1, 3, 5, 2, 4, 6).contiguous() return weight.view(c, r)
def pack_svdq_qweight(weight: torch.Tensor) -> torch.Tensor: n, k = weight.shape device = weight.device assert weight.dtype == torch.int32
code Assistant
I will create a reproduction script
test_babai.pyto verify the correctness of thebabai_roundingfunction by comparing its loss against naive rounding on a synthetic problem, which will help determine if the reported large reconstruction error is due to the algorithm implementation.Assistant
I will execute the
test_babai.pyscript to compare the loss of Babai rounding versus naive rounding and check if the Babai implementation performs as expected.