In @quantize.py , do not load the whole model A and B because they are very big. Use…
- User
In @quantize.py , do not load the whole model A and B because they are very big. Use
safetensors.safe_opento open the file handle, and get the tensor only when needed. --- Content from referenced files --- Content from @quantize.py: #!/usr/bin/env python3import argparse
import safetensors.torch import torch from tqdm import tqdm
def pack_svdq_weight(weight): """ Pack quantized weight tensor for Nunchaku MMA (SVDQ). Input: Int32 tensor [N, K], values in [0, 15] Output: Int8 tensor [N, K/2] """ assert weight.dtype == torch.int32 n, k = weight.shape
code# Parameters from NunchakuWeightPacker(bits=4, warp_n=128) mem_n = 128 mem_k = 64 num_k_unrolls = 2 # Pad if necessary (though usually models are aligned) # assert n % mem_n == 0 # assert k % (mem_k * num_k_unrolls) == 0 n_tiles = n // mem_n k_tiles = k // mem_k num_n_packs = 8 n_pack_size = 2 num_n_lanes = 8 reg_n = 1 num_k_packs = 1 k_pack_size = 2 num_k_lanes = 4 reg_k = 8 # 32 bits / 4 bits # Reshape weight = weight.reshape( n_tiles, num_n_packs, n_pack_size, num_n_lanes, reg_n, k_tiles, num_k_packs, k_pack_size, num_k_lanes, reg_k, ) # Permute # (n_tiles, num_n_packs, n_pack_size, num_n_lanes, reg_n, k_tiles, num_k_packs, k_pack_size, num_k_lanes, reg_k) # -> (n_tiles, k_tiles, num_k_packs, num_n_packs, num_n_lanes, num_k_lanes, n_pack_size, k_pack_size, reg_n, reg_k) # Indices: 0, 5, 6, 1, 3, 8, 2, 7, 4, 9 weight = weight.permute(0, 5, 6, 1, 3, 8, 2, 7, 4, 9).contiguous() # Pack 8 elements into Int32 # Little endian packing shift = torch.arange(0, 32, 4, dtype=torch.int32, device=weight.device) weight = weight.bitwise_left_shift(shift) weight = weight.sum(dim=-1, dtype=torch.int32) # View as Int8 # Int32 (4 bytes) -> 4 Int8s # Output shape [N, K/2] return weight.view(torch.int8).view(n, -1) Assistant
I will update
quantize.pyto usesafetensors.safe_openfor both models, loading tensors only when needed to save memory.Assistant
I will verify the script's integrity by running it with the
--helpflag.