TriangleHyena#
- class TriangleHyena(
- hidden_dim,
- global_conv_cfg,
- short_conv_kernel_size=3,
- short_conv_backend='torch',
- start_node=True,
- right_padding=False,
Bases:
ModuleStarting- or ending-node Hyena mixer for pair tensors
[B, L, L, C].Inner mixer for
QKVSequenceMixer. It setstakes_fused_qkvso the mixer hands it the un-split[B, L, L, 3C]projection;forward(q, k, v, ...)with three tensors works as well.- Parameters:
- short_conv_weight#
Depthwise causal taps
[K, 3C]for thequery,keyandvaluestreams, in that channel order.- Type:
torch.nn.Parameter
- start_node#
Truefor the starting-node update (rows are the sequences),Falsefor the ending-node update (columns).- Type:
- takes_fused_qkv = True#
Opts in to the un-split
[B, L, L, 3C]projection fromQKVSequenceMixer.
- __init__(
- hidden_dim,
- global_conv_cfg,
- short_conv_kernel_size=3,
- short_conv_backend='torch',
- start_node=True,
- right_padding=False,
Build the short-conv taps and instantiate the long convolution.
- Parameters:
hidden_dim (int) – Channel width
Cof the pair tensor and of each projection.global_conv_cfg (LazyConfig) –
LazyConfigfor a 1D non-causalCKConvNDwithhidden_dimchannels (see the module docstring).short_conv_kernel_size (int) – Taps of the depthwise causal short conv.
short_conv_backend (Literal['torch', 'subq_ops']) –
"torch"runs the short conv withF.conv1d;"subq_ops"with the channel-lastsubquadratic_ops_torch.causal_conv1dCUDA kernel. Undertorch.compileboth use the fused stencil form.start_node (bool) –
Truemixes along rows (starting node),Falsealong columns (ending node).right_padding (bool) – Skip zeroing the projections before the short conv.
Trueonly if every padding mask marks a suffix of each sample; other layouts then leak padded values.
- Raises:
ValueError – If
short_conv_backendis not"torch"or"subq_ops", orglobal_conv_cfgis not a 1D non-causal zero-padded double-gridCKConvNDwithhidden_dimchannels, or its kernel is FiLM-conditioned.
- flop_count(spatial_dims, inference=False)#
Count FLOPs for one forward pass over an
L x Lpair tensor.Short conv (three depthwise streams), the two gates, and the long conv once per row; the SIREN filter and its FFT run once per forward, so their cost is counted once.
- forward(
- query,
- key=None,
- value=None,
- cp_group=None,
- padding_mask=None,
- **mixer_kwargs,
Apply the row- or column-wise Hyena update.
- Parameters:
query (Tensor) –
[B, L, L, C]gate-input projection (referencex1), or, whenkeyandvalueareNone, the fused[B, L, L, 3C]projection[query | key | value]asQKVSequenceMixerpasses it (takes_fused_qkv).key (Tensor | None) –
[B, L, L, C]projection whose columns feed the long conv (referencev).value (Tensor | None) –
[B, L, L, C]output-gate projection (referencex2).cp_group (ProcessGroup) – Context-parallel group; only
Noneor a group of size 1 is supported.padding_mask (Tensor | None) – Optional
[B, L]boolean tensor,Trueat padded tokens. The projections are zeroed at every pair position in a padded row or column before the short conv (skipped withright_padding), and the long-conv inputx1 * vand the output gate after it. Padded inputs must be finite for training: valid outputs ignore them, but non-finite inputs make upstream weight gradients (e.g. the input projection) non-finite.**mixer_kwargs – Only
conditioningis accepted, and it is ignored (no FiLM kernel conditioning).
- Returns:
[B, L, L, C]update of the pair tensor.- Raises:
ValueError – If the pair tensor is not square, the channel width does not match
hidden_dim, only one ofkey/valueis given, orcp_grouphas size > 1.TypeError – If
mixer_kwargsholds any key other thanconditioning.
- Return type: