Skip to content

Commit

Permalink
packs and unpacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Panferov committed Jan 16, 2024
1 parent 0647577 commit f2b27b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/aq.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(
self.scales_clusters = nn.Parameter(
scales_clusters, requires_grad=True
) # [2**self.scale_nbits, num_in_groups] if scale_nbits > 0 else [2**self.scale_nbits, 1]
self.scale_indices = nn.Parameter(scale_indices, requires_grad=False) # [num_out_groups, 1]
self.scales_indices = nn.Parameter(scale_indices, requires_grad=False) # [num_out_groups, 1]

# BIAS
if bias:
Expand Down Expand Up @@ -128,7 +128,7 @@ def initialize(
else:
scales_clusters, scales_indices, _ = fit_kmeans_1d(scales.flatten(1, -1), k=2**self.scale_nbits)
self.scales_clusters.data = scales_clusters
self.scales_indices = pack_int_data(scales_indices, self.scale_nbits)
self.scales_indices.data = pack_int_data(scales_indices, self.scale_nbits)

weight_for_init = (weight_groupwise / scales).swapaxes(1, 2).reshape_as(reference_weight)
del weight_groupwise
Expand Down Expand Up @@ -198,7 +198,9 @@ def get_scales(self) -> torch.Tensor:
dequantized_scales = dequantized_scales + (self.scales - self.scales.detach())
return dequantized_scales
else: # train scale codebook only
return self.scales_clusters.gather(1, self.scales_indices)[:, :, None, None]
return self.scales_clusters.gather(1, unpack_int_data(self.scales_indices, self.scale_nbits))[
:, :, None, None
]

def reconstruct_weight(self, selection: Union[slice, ellipsis, torch.Tensor] = ...):
"""
Expand Down Expand Up @@ -239,13 +241,16 @@ def beam_search_update_codes_(
:param kwargs: any additional keyword arguments are forwarded to beam_search_optimal_codes function
:returns: the updated codes
"""
self.codes[selection] = beam_search_optimal_codes(
XTX=XTX,
reference_weight=reference_weight,
codebooks=self.get_codebooks(),
prev_codes=self.codes[selection],
scales=self.get_scales()[selection],
**kwargs,
self.codes[selection] = pack_int_data(
beam_search_optimal_codes(
XTX=XTX,
reference_weight=reference_weight,
codebooks=self.get_codebooks(),
prev_codes=unpack_int_data(self.codes[selection], self.nbits_per_codebook),
scales=self.get_scales()[selection],
**kwargs,
),
self.nbits_per_codebook,
)
return self.codes[selection]

Expand Down
2 changes: 2 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ def get_int_dtype(nbits: int) -> torch.dtype:
raise ValueError(f"No dtype available for {nbits}-bit codebooks")


@torch.inference_mode()
def pack_int_data(data: torch.IntTensor, nbits: int) -> torch.IntTensor:
data[data >= 2 ** (nbits - 1)] -= 2**nbits
return data.to(get_int_dtype(nbits))


@torch.inference_mode()
def unpack_int_data(data: torch.IntTensor, nbits: int) -> torch.IntTensor:
return data.to(torch.int64) % (2**nbits)

Expand Down

0 comments on commit f2b27b0

Please sign in to comment.