cofai.index_codecs
Codecs for discrete indices.
AdaptiveBitmapIndexCodec
Encode a unique bounded index set using its shorter byte representation.
Sparse selections use a fixed-width list of absolute indices. Dense selections use one bitmap bit per possible index. The serialized stream includes the selected representation, total index count, and retained index count.
decode
decode(encoded: EncodedIndexSet | bytes | bytearray | memoryview) -> list[int]
Decode an encoded object or independently decodable byte stream.
decode_batch
decode_batch(rows: list[list[bytes]]) -> list[list[int]]
Decode canonical stream rows and validate every index set.
encode
encode(indices, universe_size: int) -> EncodedIndexSet
Encode indices using the smaller bitmap or fixed-width index payload.
encode_batch
encode_batch(batch: BoundedIndexSetBatch) -> list[list[bytes]]
Encode a batch into the canonical one-stream-per-sample layout.
BoundedIndexSetBatch
dataclass
BoundedIndexSetBatch(indices: Tensor, universe_size: int)
A batch of unique index sets sharing one finite universe.
EncodedIndexSet
dataclass
EncodedIndexSet(coding: str, universe_size: int, selected_count: int, payload: bytes)
stream_bits
property
stream_bits: int
Return the size of the independently decodable byte stream.
from_bytes
classmethod
from_bytes(stream: bytes | bytearray | memoryview) -> EncodedIndexSet
Parse a self-contained bounded-index-set stream.
to_bytes
to_bytes() -> bytes
Serialize the coding choice, universe, cardinality, and payload.
UniformIndexCodec
UniformIndexCodec(alphabet_size, **kwargs)
Encode discrete indices under a uniform alphabet model.
This codec assumes a uniform distribution over the alphabet and encodes integer indices using the repository's uniform arithmetic-coding helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alphabet_size
|
int
|
Size of the token alphabet (number of possible values). |
required |
**kwargs
|
dict
|
Additional keyword arguments passed to parent class. |
{}
|
compress
compress(tokens)
Compress tokens to bitstring.
Note: tokens should not have batch dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
Tensor
|
Input tokens to compress. Shape should be (H, W, ...) without batch dimension. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coded_unit |
dict
|
Dictionary containing:
|
decompress
decompress(strings, pstate, **kwargs)
Decompress bitstring to tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strings
|
dict
|
Dictionary with key "t" containing nested list with encoded bitstring. Nested structure is for consistent API. |
required |
pstate
|
dict
|
Dictionary with key "t_shape" containing the original shape of tokens as a tuple. |
required |
**kwargs
|
dict
|
Additional keyword arguments (unused). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
task_feats |
dict
|
Dictionary with key "tokens" containing decompressed tokens of shape specified in pstate["t_shape"]. Note: tokens do not have batch dimension. |
forward
forward(tokens)
Forward pass to compute uniform likelihoods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
Tensor
|
Input tokens of any shape. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
output |
dict
|
Dictionary containing: - "likelihoods" (dict): Dictionary with key "t" containing uniform likelihoods of shape matching tokens. - "tokens" (torch.Tensor): Original input tokens. |