gensbi.models.core.patching#

Invertible 2D patchify/depatchify — pure einops reshapes (no learned state).

Moved out of recipes.utils so model/flow code can depend on it without pulling in the recipes package (which imports gensbi.models, creating a cycle).

Functions#

depatchify_2d(x[, size, grid])

Inverse of patchify_2d().

patchify_2d(x[, size])

Patchify a 2D image into a sequence of non-overlapping patches.

Module Contents#

gensbi.models.core.patching.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. If None, a square grid (h == w) is assumed.

Returns:

Image of shape (B, H, W, C) where H = h * size and W = w * size.

Return type:

Array

Raises:

ValueError – If grid is None and the token count is not a perfect square.

gensbi.models.core.patching.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). H and W must each be divisible by size.

  • size (int, optional) – Patch edge length. Default is 2.

Returns:

Token sequence of shape (B, T, F) where T = (H // size) * (W // size) and F = C * size * size. Tokens are in raster (row-major) order.

Return type:

Array