Skip to content

Contextualizer

The contextualizer (paper module C) lets the signal content of each patch flow across the other channels and over time, into the past of its own channel. A patch on its own is a short window of one channel; after the contextualizer, it carries the state of the other channels at that moment and the history of its channel, which is what captures the spatio-temporal structure of the recording. Its output, the contextualized patches z, is the input of the encoder and of the TACO decoder.

What it does

The contextualizer factorises the attention over the two dimensions of the patch grid (channel, patch). Each of its num_layers layers (3 in the paper) runs:

  • an attention over channels at each patch time: all the channels attend to each other, at a given time;
  • a attention over time for each channel: the patches of a channel attend along time, and with is_causal=True a patch only sees the patches before it;

followed by a feed-forward block. Factorising the two passes, instead of attending over every pair of patches, reduces the cost of the attention from O(N_c^2 N_t^2) to O(N_t N_c^2 + N_c N_t^2): with the paper's sizes (1,024 channels, 15 patches) it is about a 15-fold speed-up. The two passes use the same attention parameters by default (share_attention_weights=True) and run in the order attention_order ("channel_time": channels first, as in the paper).

The inputs

Each patch enters the contextualizer as two projections that are summed: a projection of the signal of the patch (patch_length samples to hidden_dim) and a projection of its position. The position is the channel embedding, and, when a time embedding is configured, the time embedding too (see Tokenizer). The two projections are summed rather than concatenated, so that masked patches can drop their whole signal term, bias included.

Whether the position includes time depends on the objective:

  • for TACO, as in the paper, the position is the channel embedding only: the temporal order is inferred from the causal attention, and no time-position embedding is given;
  • for the vanilla reconstruction objective, the paper adds sinusoidal time-position embeddings, and removes the causal mask, since the time order is then given explicitly.

Options that matter

Option Default Experiments Effect
hidden_dim 128 128 Width of the layers (D).
num_layers 3 3 Number of channel and time layers (L_pre); 0 removes the attention.
num_heads 4 4 Attention heads per pass.
ffn_ratio 2 2 Widening of the feed-forward blocks.
is_causal True True Causal attention along time: required by TACO, see Decoders.
attention_order "channel_time" "channel_time" Which pass comes first in each layer.
share_attention_weights True True One attention module for both passes.
use_gradient_checkpointing False False Recompute the layers in the backward pass to save memory.

Reference

timber.modules.contextualizer

Contextualizer (paper module C): factorised spatial / temporal attention over patches.

Each patch is embedded as signal_projection(signal) + position_projection(position) (position = channel and time embeddings concatenated), then every layer attends over the channel axis at each patch time and over the patch (time) axis at each channel, followed by a feed-forward block. The pass over time can be causal, so that a contextualised patch only depends on the past (needed for the TACO forecasting objective).

Contextualizer

Bases: Module

Embed patches with their positions and contextualise them with factorised attention.

Parameters:

Name Type Description Default
patch_length int

Samples per patch.

required
position_dim int

Channels of the position embeddings (channel + time).

required
hidden_dim int

Channels of the layers.

required
output_dim int | None

Channels of the output projection, or None for no projection (output has hidden_dim channels).

None
num_layers int

Number of ContextualizerLayer. 0 keeps only the input embedding (the "no contextualizer" ablation).

3
num_heads int

See ContextualizerLayer.

1
ffn_ratio int

See ContextualizerLayer.

2
dropout float

See ContextualizerLayer.

0.0
attention_order AttentionOrder

See ContextualizerLayer.

'channel_time'
is_causal bool

See ContextualizerLayer.

True
share_attention_weights bool

See ContextualizerLayer.

True
use_pre_norm bool

See ContextualizerLayer.

False
use_gradient_checkpointing bool

Recompute each layer in the backward pass instead of storing its activations (training only). Saves memory at the cost of a second forward pass.

False
output_dim property
output_dim: int

Channels of the contextualised patches (patch_dim in the encoder).

forward
forward(
    patches: Tensor,
    position_embeddings: Tensor,
    patch_keep: Tensor | None = None,
) -> torch.Tensor

Contextualise patches.

Parameters:

Name Type Description Default
patches Tensor

(batch, channel, patch, patch_length).

required
position_embeddings Tensor

(batch, channel, patch, position_dim).

required
patch_keep Tensor | None

Optional (batch, channel, patch) boolean mask; the signal of masked (False) patches is hidden, only their position embedding is kept. Used when no encoder masks the patches downstream (no-encoder ablation).

None

Returns:

Type Description
Tensor

(batch, channel, patch, output_dim).

ContextualizerLayer

Bases: Module

One factorised layer: attention over one axis, then over the other, then a feed-forward block.

Each sub-layer is a residual block x + dropout(f(x)) normalised after the sum (post-norm, default) or before f (use_pre_norm).

Parameters:

Name Type Description Default
hidden_dim int

Channels of the patch embeddings.

required
num_heads int

Attention heads (both passes).

1
ffn_ratio int

Hidden size of the feed-forward block as a multiple of hidden_dim.

2
dropout float

Dropout on attention probabilities, feed-forward activations and residual branches.

0.0
attention_order AttentionOrder

"channel_time" (channels first) or "time_channel" (patches first).

'channel_time'
is_causal bool

Causal mask on the pass over time.

True
share_attention_weights bool

Use one attention module for both passes.

True
use_pre_norm bool

Pre-norm instead of post-norm residual blocks.

False
forward
forward(x: Tensor) -> torch.Tensor

x (batch, channel, patch, hidden_dim) -> same shape.