Genomics and RNA

Dense 1-D dilated convolution

MediumRaw PyTorchAbout 20 min2 public examples plus private tests

Basenji2EnformerBorzoiHyenaDNA

Write and run your solution Sign in

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

Your task

For each output position and channel, add the bias to a weighted sum of input channels at regularly spaced sequence positions. Dilation sets the spacing within a window; stride sets the spacing between windows. Use channels-last cross-correlation: keep the supplied kernel taps in order, with zeros beyond the padded boundaries.

Function signature

dilated_conv1d(
    x: torch.Tensor,
    weight: torch.Tensor,
    bias: torch.Tensor,
    padding: list[int] | tuple[int, int] = [0, 0],
    dilation: int = 1,
    stride: int = 1,
) -> torch.Tensor

Worked example

Cross-correlation keeps kernel order

The first output is 1\cdot1+2\cdot10=21. Sliding the same ordered taps gives 32 and 43. Reversing the taps would compute a different operation.

import torch

x = torch.tensor([[[1.0], [2.0], [3.0], [4.0]]], dtype=torch.float64, requires_grad=True)
weight = torch.tensor([[[1.0]], [[10.0]]], dtype=torch.float64, requires_grad=True)
bias = torch.tensor([0.0], dtype=torch.float64, requires_grad=True)
padding = [0, 0]
dilation = 1
stride = 1

result = dilated_conv1d(x=x, weight=weight, bias=bias, padding=padding, dilation=dilation, stride=stride)

Expected return

torch.tensor([[[21.0], [32.0], [43.0]]], dtype=torch.float64)

Parameters

ParameterShape and meaning
xtorch.Tensor[B, L, \mathrm{Cin}]Sequence features with channels on the final axis; noncontiguous inputs are supported.
weighttorch.Tensor[K, \mathrm{Cin}, \mathrm{Cout}]Dense cross-correlation weights
biastorch.Tensor[\mathrm{Cout}]Included once per output position
paddinglist[int] | tuple[int, int]Default [0, 0]Pair (left, right) giving the number of padded sequence positions.
dilationintDefault 1Spacing between kernel taps.
strideintDefault 1Distance between successive windows.

Return

torch.Tensor · [B, \mathrm{Lout}, \mathrm{Cout}]

Return [B,Lout,Cout] dense cross-correlation plus bias.

Required behavior

  • Implement dense 1-D cross-correlation using raw tensor operations, without convolution helpers. x is [B,L,\mathrm{Cin}], weight is [K,\mathrm{Cin},\mathrm{Cout}], and bias is [\mathrm{Cout}], all the same floating dtype/device. B,L,Cin,Cout,K are positive.
  • options are dilation (positive int, default 1), stride (positive int, default 1), and padding (two nonnegative ints (\mathrm{left},\mathrm{right}), default (0,0)). No groups/depthwise variant or implicit same-padding rule is included at launch.
  • Let \mathrm{effective\_size}=\mathrm{dilation}\cdot (K-1)+1 and \mathrm{Lout}=\operatorname{max}(0, \operatorname{floor}((L+\mathrm{left}+\mathrm{right}-\mathrm{effective\_size})/\mathrm{stride})+1).
  • Return y [B,\mathrm{Lout},\mathrm{Cout}] with y[b,t,o]=\mathrm{bias}[o]+\sum_{k=0..K-1,c=0..\mathrm{Cin}-1} x_{\mathrm{zero}}[b,t\cdot \mathrm{stride}+k\cdot \mathrm{dilation}-\mathrm{left},c] \cdot \mathrm{weight}[k,c,o], where x_zero is zero outside 0\le \mathrm{position}<L. This is cross-correlation: DO NOT reverse the K axis.
  • The output includes bias but no activation or input residual. Use explicit asymmetric padding, including even kernels.
  • For causal alignment set \mathrm{left}=\mathrm{dilation}\cdot (K-1), \mathrm{right}=0; output t then sees input positions no later than t\cdot \mathrm{stride}. Padding may exceed the input length, as may dilation. Settings with no valid output windows return shape [B,0,\mathrm{Cout}] and zero parameter/input VJPs.
  • No normalization or numerical \epsilon is used.

Why this matters

An index table exposes the receptive field directly: each output selects K padded input positions before mixing input and output channels.

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 dilated_conv1d(
    x: torch.Tensor,  # (B, L, Cin)
    weight: torch.Tensor,  # (K, Cin, Cout)
    bias: torch.Tensor,  # (Cout,)
    padding: list[int] | tuple[int, int] = [0, 0],
    dilation: int = 1,
    stride: int = 1,
) -> torch.Tensor:
    """Return a tensor with shape (B, Lout, Cout)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement dilated_conv1d")

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.