File size: 1,466 Bytes
20857b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Dense bit-packing for neural tokens.

MAGVIT2's vocabulary is 262,144 (2^18), so each token fits in 18 bits.
Storing them as 32-bit integers wastes 44% of bits before entropy coding.

Pack uses numpy vectorized operations for ~7x speedup over Python.
"""

import numpy as np


def pack_tokens(tokens: list[int], bits: int = 18) -> list[int]:
    if not tokens:
        return []
    n = len(tokens)
    total_bits = n * bits
    out_len = (total_bits + 7) // 8
    arr = np.array(tokens, dtype=np.uint64)
    out = np.zeros(out_len, dtype=np.uint8)

    for bit_idx in range(bits):
        bit_val = (arr >> (bits - 1 - bit_idx)) & 1
        positions = np.arange(n, dtype=np.uint64) * bits + bit_idx
        byte_idx = positions >> 3
        bit_in_byte = 7 - (positions & 7)
        out[byte_idx] |= bit_val.astype(np.uint8) << bit_in_byte.astype(np.uint8)

    return out.tolist()


def unpack_tokens(data: list[int], count: int, bits: int = 18) -> list[int]:
    if count == 0:
        return []
    arr = np.array(data, dtype=np.uint8)
    nbits = count * bits
    raw_bits = np.unpackbits(arr, bitorder='big')
    if len(raw_bits) > nbits:
        raw_bits = raw_bits[:nbits]
    elif len(raw_bits) < nbits:
        raw_bits = np.pad(raw_bits, (0, nbits - len(raw_bits)), constant_values=0)
    bits_2d = raw_bits.reshape(count, bits)
    weights = 1 << np.arange(bits - 1, -1, -1, dtype=np.uint64)
    return (bits_2d.astype(np.uint64) @ weights).tolist()