Skip to content

Conv

conv

CausalConv3d

CausalConv3d(in_channels: int, out_channels: int, kernel_size: int | tuple[int, int, int], stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int] = 0, dilation: int | tuple[int, int, int] = 1, bias: bool = True, pad_mode: Literal['zeros', 'replicate'] = 'zeros')

Bases: Module

3D convolution that pads the time axis on the left only.

Layout is channels-last: input (B, T, H, W, C_in), output (B, T_out, H_out, W_out, C_out). The weight is stored directly as weight with MLX layout (C_out, kT, kH, kW, C_in) (plus bias), so PyTorch CausalConv3d(nn.Conv3d) state dicts load with only the usual conv-weight transpose and no key remapping.

The temporal receptive field before the current frame is context_frames = dilation_t * (kT - 1). In a single call those frames are filled with zeros (pad_mode="zeros", Wan / Qwen-Image style) or with copies of the first frame (pad_mode="replicate", CogVideoX / LTX style). With :meth:step, they come from the previous chunk instead, and concatenating the chunk outputs reproduces the single-call output exactly.

Parameters:

Name Type Description Default
in_channels int

Input channels.

required
out_channels int

Output channels.

required
kernel_size int | tuple[int, int, int]

kT or (kT, kH, kW).

required
stride int | tuple[int, int, int]

Stride, int or (sT, sH, sW). Chunked :meth:step calls require sT == 1.

1
padding int | tuple[int, int]

Symmetric spatial padding, int or (pH, pW). Temporal padding is always causal and derived from the kernel.

0
dilation int | tuple[int, int, int]

Dilation, int or (dT, dH, dW).

1
bias bool

Whether to add a learned bias.

True
pad_mode Literal['zeros', 'replicate']

How to fill the causal context on the first chunk: "zeros" or "replicate" (first frame).

'zeros'

A conv whose temporal padding is not dilation_t * (kT - 1), such as the strided time_conv of Wan's encoder downsample (p_t = 0, the caller prepends one cached frame), is a plain nn.Conv3d, not this class.

step

step(x: array, cache: array | None = None) -> tuple[array, array | None]

Process one chunk and return the cache for the next one.

Calling step on consecutive chunks, threading the returned cache, gives the same frames as one __call__ on the whole clip, for any chunk sizes (including 1-frame chunks).

Parameters:

Name Type Description Default
x array

Chunk (B, T, H, W, C_in).

required
cache array | None

Cache returned by the previous step (None for the first chunk).

None

Returns:

Type Description
array

(y, next_cache). next_cache holds the last

array | None

context_frames input frames seen so far (fewer at the start of

tuple[array, array | None]

a clip), or None when the kernel has no temporal context.

Raises:

Type Description
ValueError

If the temporal stride is not 1.

WeightNorm

WeightNorm(module: Module, weight_name: str = 'weight', dim: int = 0)

Bases: Module

Weight normalization wrapper for any module with a weight parameter.

Decomposes the weight into a magnitude (g) and direction (v): weight = g * (v / ||v||)

Parameters:

Name Type Description Default
module Module

The module to wrap (e.g., nn.Conv1d, nn.Linear).

required
weight_name str

Name of the weight parameter to normalize.

'weight'
dim int

Dimension over which to compute the norm. Default 0 normalizes per output channel.

0

causal

Causal 3D convolution with a temporal cache for chunked video processing.

Causal video VAEs (Wan 2.x, Qwen-Image, LongCat, CogVideoX, LTX) pad the time axis on the left only, so output frame t depends on input frames <= t. To encode or decode a long clip in chunks, each convolution keeps the last few input frames of the previous chunk and prepends them to the next one. This module implements that pattern once.

CausalConv3d

CausalConv3d(in_channels: int, out_channels: int, kernel_size: int | tuple[int, int, int], stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int] = 0, dilation: int | tuple[int, int, int] = 1, bias: bool = True, pad_mode: Literal['zeros', 'replicate'] = 'zeros')

Bases: Module

3D convolution that pads the time axis on the left only.

Layout is channels-last: input (B, T, H, W, C_in), output (B, T_out, H_out, W_out, C_out). The weight is stored directly as weight with MLX layout (C_out, kT, kH, kW, C_in) (plus bias), so PyTorch CausalConv3d(nn.Conv3d) state dicts load with only the usual conv-weight transpose and no key remapping.

The temporal receptive field before the current frame is context_frames = dilation_t * (kT - 1). In a single call those frames are filled with zeros (pad_mode="zeros", Wan / Qwen-Image style) or with copies of the first frame (pad_mode="replicate", CogVideoX / LTX style). With :meth:step, they come from the previous chunk instead, and concatenating the chunk outputs reproduces the single-call output exactly.

Parameters:

Name Type Description Default
in_channels int

Input channels.

required
out_channels int

Output channels.

required
kernel_size int | tuple[int, int, int]

kT or (kT, kH, kW).

required
stride int | tuple[int, int, int]

Stride, int or (sT, sH, sW). Chunked :meth:step calls require sT == 1.

1
padding int | tuple[int, int]

Symmetric spatial padding, int or (pH, pW). Temporal padding is always causal and derived from the kernel.

0
dilation int | tuple[int, int, int]

Dilation, int or (dT, dH, dW).

1
bias bool

Whether to add a learned bias.

True
pad_mode Literal['zeros', 'replicate']

How to fill the causal context on the first chunk: "zeros" or "replicate" (first frame).

'zeros'

A conv whose temporal padding is not dilation_t * (kT - 1), such as the strided time_conv of Wan's encoder downsample (p_t = 0, the caller prepends one cached frame), is a plain nn.Conv3d, not this class.

step
step(x: array, cache: array | None = None) -> tuple[array, array | None]

Process one chunk and return the cache for the next one.

Calling step on consecutive chunks, threading the returned cache, gives the same frames as one __call__ on the whole clip, for any chunk sizes (including 1-frame chunks).

Parameters:

Name Type Description Default
x array

Chunk (B, T, H, W, C_in).

required
cache array | None

Cache returned by the previous step (None for the first chunk).

None

Returns:

Type Description
array

(y, next_cache). next_cache holds the last

array | None

context_frames input frames seen so far (fewer at the start of

tuple[array, array | None]

a clip), or None when the kernel has no temporal context.

Raises:

Type Description
ValueError

If the temporal stride is not 1.

weight_norm

Weight normalization for MLX modules.

Implements weight normalization (Salimans & Kingma, 2016) as a wrapper that reparameterizes a weight tensor as w = g * (v / ||v||).

WeightNorm

WeightNorm(module: Module, weight_name: str = 'weight', dim: int = 0)

Bases: Module

Weight normalization wrapper for any module with a weight parameter.

Decomposes the weight into a magnitude (g) and direction (v): weight = g * (v / ||v||)

Parameters:

Name Type Description Default
module Module

The module to wrap (e.g., nn.Conv1d, nn.Linear).

required
weight_name str

Name of the weight parameter to normalize.

'weight'
dim int

Dimension over which to compute the norm. Default 0 normalizes per output channel.

0

weight_norm

weight_norm(module: Module, weight_name: str = 'weight', dim: int = 0) -> WeightNorm

Apply weight normalization to a module.

Parameters:

Name Type Description Default
module Module

Module to wrap (e.g., nn.Conv1d, nn.Linear).

required
weight_name str

Name of the weight parameter.

'weight'
dim int

Dimension for per-channel normalization.

0

Returns:

Type Description
WeightNorm

WeightNorm wrapper around the module.

Example::

conv = weight_norm(nn.Conv1d(16, 32, 3))
out = conv(x)