gensbi.models.core.tokenizers#
Invertible tokenizers — the modeled-variable reshape seam.
Adapted from apple/ml-tarflow (TarFlow); see models/tarflow/LICENSE.apple.
A tokenizer maps the modeled variable to a token sequence (B, T, F) and back.
It MUST be volume-preserving (a fixed invertible reshape, log-det 0) — never a
learned lossy encoder — so the change-of-variables stays exact. Pure reshape, no
parameters, which is why it is a shared core primitive.
Classes#
Patchify a 2D image into a token sequence via |
|
Reshape a channel-carrying vector into a token sequence. |
Module Contents#
- class gensbi.models.core.tokenizers.ImageTokenizer(height, width, channels, patch_size)[source]#
Patchify a 2D image into a token sequence via
patchify_2d().Maps
(B, H, W, C)images to(B, T, F)token sequences whereT = (H // patch_size) * (W // patch_size)andF = C * patch_size * patch_size. Pure reshape: volume-preserving (log-det 0, no learned parameters). Tokens are in raster (row-major) causal order as fixed bypatchify_2d().- Parameters:
height (int) – Image height in pixels. Must be divisible by
patch_size.width (int) – Image width in pixels. Must be divisible by
patch_size.channels (int) – Number of image channels.
patch_size (int) – Patch edge length in pixels. Must divide both
heightandwidth.
- Raises:
ValueError – If
patch_sizedoes not divideheightorwidth.
- detokenize(tokens)[source]#
Reconstruct an image from a token sequence.
- Parameters:
tokens (Array) – Token sequence of shape
(B, T, F).- Returns:
Image of shape
(B, H, W, C).- Return type:
Array
- class gensbi.models.core.tokenizers.VectorTokenizer(dim, block_size=1, channels=1)[source]#
Reshape a channel-carrying vector into a token sequence.
Maps
(B, dim, C)tensors to(B, T, F)token sequences via a volume-preserving reshape (log-det 0, no learned parameters). The number of tokens isT = dim // block_sizeand each token hasF = block_size * channelsfeatures.C = 1gives(B, dim, 1)input — a trailing channel axis is always required.example_shapeis always(dim, channels)(e.g.(dim, 1)for the standard tabular path).detokenizealways returns(B, dim, channels); the channel axis is never collapsed.- Parameters:
dim (int) – Total feature dimension of the input vector.
block_size (int, optional) – Number of features per token. Must divide
dim. Default is 1.channels (int, optional) – Number of channels. Default is 1 (
C = 1 → (dim, 1)shape). Each token carriesF = block_size * channelsfeatures.
- Raises:
ValueError – If
block_sizedoes not dividedim, or ifchannels < 1.
- detokenize(tokens)[source]#
Flatten a token sequence back into a channel-carrying vector.
- Parameters:
tokens (Array) – Token sequence of shape
(B, T, F).- Returns:
Vector of shape
(B, dim, channels)for allC >= 1(C = 1gives(B, dim, 1); never collapsed to(B, dim)).- Return type:
Array
- tokenize(x)[source]#
Reshape a channel-carrying vector into a token sequence.
- Parameters:
x (Array) – Input of shape
(B, dim, C)whereCis the channel count (C = 1for the standard tabular path gives(B, dim, 1)).- Returns:
Token sequence of shape
(B, T, F)whereT = dim // block_sizeandF = block_size * channels.- Return type:
Array