gensbi.models.tarflow#
Transformer autoregressive normalizing-flow density model.
Exports TarFlow and its configuration dataclass
TarFlowParams for building autoregressive normalizing flows
based on a stack of causal transformer blocks (MetaBlocks).
Submodules#
Classes#
Transformer autoregressive normalizing flow density model. |
|
Architecture parameters for |
Package Contents#
- class gensbi.models.tarflow.TarFlow(params)[source]#
Bases:
flax.nnx.ModuleTransformer autoregressive normalizing flow density model.
Stacks
MetaBlockbijections with alternating token permutations on top of a tokenizer and an isotropic Gaussian base distribution. Supports both vector and image data, with optional input standardization.- Parameters:
params (TarFlowParams) – Architecture and initialization parameters.
- log_prob(x, cond=None)[source]#
Compute the log-probability of data under the model.
Applies standardization, tokenizes the input, then runs each
MetaBlock’sinverse()transform (data→noise direction), accumulating the log-absolute-determinant terms, and finally evaluates the base Gaussian log-probability.- Parameters:
x (Array) – Data samples of shape
(B, *example_shape)or a single unbatched sample that will be promoted to a batch of one.cond (Array or None, optional) – Conditioning batch of shape
(B, cond_dim)forcond_channels == 1, or(B, cond_dim, C_cond)forcond_channels > 1(flattened internally by the conditioner). PassNonefor an unconditional model.
- Returns:
Log-probabilities of shape
(B,).- Return type:
Array
- sample(key, cond=None, nsamples=None)[source]#
Draw samples from the model.
Samples noise from
N(0, I), then applies eachMetaBlock’sforward()transform (noise→data direction) in reverse block order, detokenizes the result, and applies the inverse standardization.- Parameters:
key (jax.random.PRNGKey) – Random key for noise sampling.
cond (Array or None, optional) – Conditioning batch of shape
(nsamples, cond_dim)forcond_channels == 1, or(nsamples, cond_dim, C_cond)forcond_channels > 1(flattened internally by the conditioner). If provided,nsamplesis inferred fromcond.shape[0].nsamples (int or None, optional) – Number of samples to draw. Required when
condisNone.
- Returns:
Samples of shape
(B, *example_shape).- Return type:
Array
- set_standardization(mean, std)[source]#
Set the mean and standard deviation for input standardization.
Accepts shapes
(dim,)(broadcast to(dim, 1)),(dim, 1),(C,)(per-channel broadcast), or a scalar broadcastable toexample_shape.- Parameters:
mean (Array) – Mean broadcastable to
example_shape.std (Array) – Standard deviation broadcastable to
example_shape.
- Return type:
None
- Raises:
ValueError – If the model was built with
standardize=False.
- F = 1#
- T#
- _standardize#
- blocks#
- cond_dim#
- dim#
- example_shape#
- mean#
- std#
- tokenizer#
- class gensbi.models.tarflow.TarFlowParams[source]#
Architecture parameters for
TarFlow.modeledselects the tokenizer ("vector"or"image");condselects the conditioner ("bias","vector", or"image"). Head sizing follows the Flux1 convention: specifyhead_dimandnum_heads; total widthchannels = head_dim * num_headsis derived in__post_init__.- Parameters:
rngs (nnx.Rngs) – Flax RNG container passed to all sub-modules during construction.
dim (int or None, optional) – Feature dimension of each input vector. Required when
modeled="vector". Default isNone.cond_dim (int, optional) – Dimensionality of the conditioning vector. Set to
0for an unconditional model. Default is0.modeled (str, optional) – Tokenizer type:
"vector"(1-D data) or"image"(spatial data). Default is"vector".img_size (int or None, optional) – Spatial size (height = width) of the modeled image. Required when
modeled="image". Default isNone.patch_size (int or None, optional) – Patch size for the image tokenizer. Required when
modeled="image". Default isNone.img_channels (int, optional) – Number of channels in the modeled image. Default is
1.cond (str, optional) – Conditioning strategy:
"bias"(per-token additive bias viaAdditiveBiasConditioner),"vector"(one condition token per modeled coordinate viaVectorConditioner), or"image"(prefix tokens from an image viaImageConditioner). Default is"bias".cond_img_size (int or None, optional) – Spatial size of the conditioning image. Required when
cond="image". Default isNone.cond_patch_size (int or None, optional) – Patch size for the image conditioning tokenizer. Required when
cond="image". Default isNone.cond_channels (int, optional) – Number of channels in the conditioning image. Default is
1.head_dim (int, optional) – Dimension per attention head. Default is
16.num_heads (int, optional) – Number of attention heads per block. Default is
4.num_blocks (int, optional) – Number of
MetaBlocklayers. Default is8.layers_per_block (int, optional) – Number of
AttentionBlocklayers inside eachMetaBlock. Default is2.block_size (int, optional) – Token grouping factor for the vector tokenizer. Default is
1.permutation (str, optional) – Token permutation strategy per block:
"flip"(alternate forward/reverse order) or"random"(independently sampled per block). Default is"flip".standardize (bool, optional) – If
True(default), apply mean/std standardization to inputs and outputs. EnablesTarFlow.set_standardization().zero_init (bool, optional) – If
True(default), initializeproj_outweights to zero so eachMetaBlockstarts as the identity map.use_softplus (bool, optional) – If
True(default), use softplus for the affine scale (numerically stable, bounded tail). IfFalse, useexp(legacy behavior).soft_clip (float, optional) – Soft-clip magnitude applied via
tanhto raw network outputs before splitting into(a, b). Default is4.0.use_rope (bool, optional) – If
True, replace the learned per-tokenpos_embedfor the modeled image tokens with 2D rotary position embeddings (VisionRotaryEmbedding). Prefix (condition) tokens keep their learned embeddings and sit at the identity rotation (zero angles). Requiresmodeled="image"andhead_dimdivisible by4.head_dim >= 32is recommended for image data (more rotary frequencies per axis) but not enforced. Default isFalse.rope_theta (int, optional) – Frequency base for the rotary embedding. Only used when
use_rope=True. Default is10000.param_dtype (DTypeLike, optional) – Dtype for all stored (master) kernel/bias/embedding parameters across the tokenizer, conditioner, and transformer blocks. Default is
float32.dtype (DTypeLike, optional) – Compute dtype knob threaded through the conditioners and
MetaBlock/AttentionBlocklayers. Default isfloat32, matchingparam_dtype, so with default arguments this is a bit-identical no-op cast. Hard-fp32 regardless of this knob: the softplus/soft_clip affine-scale path inMetaBlock._affine, log-det accumulation, themean/stdstandardization buffers, and the KV-cache buffers used during sampling.
- block_size: int = 1#
- cond: str = 'bias'#
- cond_channels: int = 1#
- cond_dim: int = 0#
- cond_img_size: int | None = None#
- cond_patch_size: int | None = None#
- dim: int | None = None#
- dtype: jax.typing.DTypeLike#
- head_dim: int = 16#
- img_channels: int = 1#
- img_size: int | None = None#
- layers_per_block: int = 2#
- modeled: str = 'vector'#
- num_blocks: int = 8#
- num_heads: int = 4#
- param_dtype: jax.typing.DTypeLike#
- patch_size: int | None = None#
- permutation: str = 'flip'#
- rngs: flax.nnx.Rngs#
- rope_theta: int = 10000#
- soft_clip: float = 4.0#
- standardize: bool = True#
- use_rope: bool = False#
- use_softplus: bool = True#
- vec_channels: int = 1#
- zero_init: bool = True#