Genomics and RNA

Real-imaginary modal filters and recurrent state

MediumRaw PyTorchAbout 20 min2 public examples plus private tests

Evo 1StripedHyena

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Your task

Apply a complex-valued modal recurrence represented by real/imaginary pairs. Return the sequence output, final recurrent state, and implied causal filter.

Function signature

modal_filter_recurrence(
    u: torch.Tensor,
    poles: torch.Tensor,
    residues: torch.Tensor,
    state: torch.Tensor,
    skip: torch.Tensor,
) -> dict[str, torch.Tensor]

Worked example

An imaginary pole and a real impulse

The pole is 0+0.5i, and the input is an impulse. Starting from zero, the complex states are 1, 0.5i, and -0.25. Their real parts give y=[1, 0, -0.25], also the first three filter coefficients. The final real/imaginary state is [-0.25, 0]; using an already-updated real state when computing the imaginary part changes this result.

import torch

u = torch.tensor([[[1.0], [0.0], [0.0]]], dtype=torch.float64, requires_grad=True)
poles = torch.tensor([[[0.0, 0.5]]], dtype=torch.float64, requires_grad=True)
residues = torch.tensor([[[1.0, 0.0]]], dtype=torch.float64, requires_grad=True)
state = torch.tensor([[[[0.0, 0.0]]]], dtype=torch.float64, requires_grad=True)
skip = torch.tensor([0.0], dtype=torch.float64, requires_grad=True)

result = modal_filter_recurrence(u=u, poles=poles, residues=residues, state=state, skip=skip)

Expected return

{
    'y': torch.tensor([[[1.0], [0.0], [-0.25]]], dtype=torch.float64),
    'state': torch.tensor([[[[-0.25, 0.0]]]], dtype=torch.float64),
    'filter': torch.tensor([[1.0, 0.0, -0.25]], dtype=torch.float64),
}

Parameters

ParameterShape and meaning
utorch.Tensor[B, L, D]float [B,L,D], real driving sequence
polestorch.Tensor[D, S, 2]float [D,S,2], complex poles stored as [\mathrm{real},\mathrm{imag}]
residuestorch.Tensor[D, S, 2]float [D,S,2], readout residues stored as [\mathrm{real},\mathrm{imag}]
statetorch.Tensor[B, D, S, 2]float [B,D,S,2], complex modal state before the chunk
skiptorch.Tensor[D]float [D], diagonal instantaneous input skip

Return

dict[str, torch.Tensor]

Return exactly the named fields applicable to this operation.

Required behavior

  • All transported tensors are real. poles [D,S,2], residues [D,S,2] and state [B,D,S,2] store [\mathrm{real},\mathrm{imag}] for p,c,z; u [B,L,D] is real and skip [D] is supplied. State precedes the first chunk token.
  • Simultaneously update z_t=pz_{t-1}+u_t, meaning \mathrm{next\_real}=\mathrm{pr}\,\mathrm{real}-\mathrm{pi}\,\mathrm{imag}+u and \mathrm{next\_imag}=\mathrm{pr}\,\mathrm{imag}+\mathrm{pi}\,\mathrm{real} using the OLD real/imag values. Output y_t=\operatorname{Re}\left(\sum_s c z_t\right)+\mathrm{skip}\,u_t.
  • Also construct the zero-state impulse filter h_l=\operatorname{Re}\left(\sum_s c p^l\right), l=0..L-1, so h_0=\operatorname{Re}\left(\sum_s\mathrm{residues}_s\right), and return final state as real/imag pairs.
  • Nonzero initial state contributes a homogeneous p^{t+1}\cdot z_{\mathrm{initial}} term; do not pretend it is captured by zero-state convolution alone. L=0 preserves state and returns empty y/filter. Stable |p|<1 modes are used in fixtures; finite input and output are required.
  • No complex input/output tensors or hidden state are permitted.

Why this matters

Each pole carries a decaying oscillatory state; its residues combine those states into a real output and a matching zero-state impulse response.

Starter code

You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.

import torch


def modal_filter_recurrence(
    u: torch.Tensor,  # (B, L, D)
    poles: torch.Tensor,  # (D, S, 2)
    residues: torch.Tensor,  # (D, S, 2)
    state: torch.Tensor,  # (B, D, S, 2)
    skip: torch.Tensor,  # (D,)
) -> dict[str, torch.Tensor]:
    """Return dict[str, torch.Tensor]:
    
    y: (B, L, D).
    state: (B, D, S, 2).
    filter: (D, L)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement modal_filter_recurrence")

Ready to implement it?

3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Sources and further reading

A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.