EmergentTwilight's picture
download
raw
877 Bytes
"""Lightweight lossless packing for grouped 0/1 BSQ tensors."""
from __future__ import annotations
import math
import numpy as np
import torch
def pack_visual_codes(codes: torch.Tensor) -> tuple[torch.Tensor, tuple[int, ...]]:
values = codes.detach().to(torch.uint8).cpu().numpy()
shape = tuple(values.shape)
packed = np.packbits(values.reshape(shape[0], -1), axis=1, bitorder="little")
return torch.from_numpy(packed), shape
def unpack_visual_codes(
packed: torch.Tensor, shape: tuple[int, ...] | list[int]
) -> torch.Tensor:
shape = tuple(int(value) for value in shape)
bits_per_row = math.prod(shape[1:])
values = np.unpackbits(
packed.cpu().numpy(), axis=1, count=bits_per_row, bitorder="little"
)
return torch.from_numpy(values).reshape(shape).to(torch.uint8)
__all__ = ["pack_visual_codes", "unpack_visual_codes"]

Xet Storage Details

Size:
877 Bytes
·
Xet hash:
5dc3c380c6ae99c3ed5514c89f19cc1a7471483fc341457276125f0b406b401e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.