gensbi.models.core#
Shared, model-agnostic primitives (the reuse home across architectures).
Submodules#
Classes#
Patchify a 2D image into a token sequence via |
|
Reshape a channel-carrying vector into a token sequence. |
Functions#
|
Inverse of |
|
Patchify a 2D image into a sequence of non-overlapping patches. |
Package Contents#
- class gensbi.models.core.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
- tokenize(x)[source]#
Patchify an image into a token sequence.
- Parameters:
x (Array) – Image of shape
(B, H, W, C).- Returns:
Token sequence of shape
(B, T, F)whereT = (H // patch_size) * (W // patch_size)andF = C * patch_size * patch_size.- Return type:
Array
- F#
- T#
- channels#
- example_shape#
- grid#
- height#
- patch_size#
- width#
- class gensbi.models.core.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
- F = 1#
- T#
- channels = 1#
- dim#
- example_shape#
- gensbi.models.core.depatchify_2d(x, size=2, grid=None)[source]#
Inverse of
patchify_2d().- Parameters:
x (Array) – Patchified tensor of shape
(B, h*w, C*size*size).size (int) – Patch edge length used by
patchify_2d().grid (tuple of int, optional) – The
(h, w)patch grid. The grid cannot be inferred from the token count alone, so it is required for non-square grids. IfNone, a square grid (h == w) is assumed.
- Returns:
Image of shape
(B, H, W, C)whereH = h * sizeandW = w * size.- Return type:
Array
- Raises:
ValueError – If
gridisNoneand the token count is not a perfect square.
- gensbi.models.core.patchify_2d(x, size=2)[source]#
Patchify a 2D image into a sequence of non-overlapping patches.
Invertible 2D patchify via einops reshape. The inverse operation is
depatchify_2d().- Parameters:
x (Array) – Image of shape
(B, H, W, C).HandWmust each be divisible bysize.size (int, optional) – Patch edge length. Default is 2.
- Returns:
Token sequence of shape
(B, T, F)whereT = (H // size) * (W // size)andF = C * size * size. Tokens are in raster (row-major) order.- Return type:
Array