MCPcopy Index your code
hub / github.com/DevTechJr/turboquant_cutile / LloydMaxCodebook

Class LloydMaxCodebook

turboquant_cutile/codebook.py:61–81  ·  view source on GitHub ↗

Pre-solved Lloyd-Max codebook for a given (d, bits) pair.

Source from the content-addressed store, hash-verified

59
60
61class LloydMaxCodebook:
62 """Pre-solved Lloyd-Max codebook for a given (d, bits) pair."""
63
64 def __init__(self, d: int, bits: int):
65 self.d = d
66 self.bits = bits
67 self.n_levels = 1 << bits
68 self.centroids, self.boundaries = solve_lloyd_max(d, bits)
69
70 def quantize(self, x: torch.Tensor) -> torch.Tensor:
71 diffs = x.unsqueeze(-1) - self.centroids.to(x.device)
72 return diffs.abs().argmin(dim=-1)
73
74 def dequantize(self, indices: torch.Tensor) -> torch.Tensor:
75 return self.centroids.to(indices.device)[indices.long()]
76
77 def __repr__(self) -> str:
78 return (
79 f"LloydMaxCodebook(d={self.d}, bits={self.bits}, "
80 f"levels={self.n_levels})"
81 )

Callers 8

test_correct_num_levelsFunction · 0.90
test_symmetryFunction · 0.90
test_sortedFunction · 0.90
test_roundtrip_identityFunction · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 7

test_correct_num_levelsFunction · 0.72
test_symmetryFunction · 0.72
test_sortedFunction · 0.72
test_roundtrip_identityFunction · 0.72