Foundations · INTEGRATION
Activations and gated feed-forward transitions
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Implement a raw-tensor activation or a complete feed-forward update. In gated mode, activate the gate projection and multiply it by the unactivated up projection before projecting down.
Function signature
gated_feedforward(
x: torch.Tensor,
w_up: torch.Tensor | None = None,
w_gate: torch.Tensor | None = None,
w_down: torch.Tensor | None = None,
b_up: torch.Tensor | None = None,
b_gate: torch.Tensor | None = None,
b_down: torch.Tensor | None = None,
activation: str = 'silu',
variant: str = 'feedforward',
gated: bool = True,
) -> torch.TensorWorked example
A gate controls each hidden channel
Use variant="feedforward", gated=True and activation="relu". Let x=[1,2], w_up=[[1,0],[0,1]], w_gate=[[2,0],[0,-1]], w_down=[[1,3],[4,2]], with all biases omitted. The up branch is [1,2]; the gate branch is [2,-2], which ReLU maps to [2,0]. Their elementwise product is [2,0], and the output is [2,6].
Parameters
| Parameter | Shape and meaning |
|---|---|
xtorch.Tensor | Any floating shape for variant="activation"; [\ldots, \mathrm{Cin}] for "feedforward".Values to activate, or input feature vectors for the feed-forward projection. |
w_uptorch.Tensor | NoneDefault None | [\mathrm{Cin}, H]Supplied hidden expansion projection parameters; the shape column fixes input/output channel orientation. |
w_gatetorch.Tensor | NoneDefault None | [\mathrm{Cin}, H]Supplied projection that produces the gate preactivations. |
w_downtorch.Tensor | NoneDefault None | [H, \mathrm{Cout}]Supplied output contraction projection parameters; the shape column fixes input/output channel orientation. |
b_uptorch.Tensor | NoneDefault None | [H]Supplied hidden expansion affine offset; add it only at its named projection or normalization stage. |
b_gatetorch.Tensor | NoneDefault None | [H]Supplied gate affine offset; add it only at its named projection or normalization stage. |
b_downtorch.Tensor | NoneDefault None | [\mathrm{Cout}]Optional offset added after the final output projection. |
activationstrDefault 'silu' | Choose "silu", exact erf-based "gelu", "gelu_tanh", "gelu_sigmoid", or "relu". The two GELU approximations are distinct named choices. |
variantstrDefault 'feedforward' | Choose "activation" to return the activated input or "feedforward" to run the up/gate/down projections. |
gatedboolDefault True | Feed-forward mode only: when True, multiply the unactivated up projection by the activated separate gate projection. False activates the up projection directly. |
Return
torch.Tensor · Exactly x.shape in activation mode; [\ldots, \mathrm{Cout}] in feedforward mode.
Return Activation: x's shape; feedforward: [...,Cout], without a residual.
Required behavior
variantis 'feedforward' (default) or 'activation'.activationis 'silu' (default), 'gelu', 'gelu_tanh', 'gelu_sigmoid', or 'relu'.- Define
\operatorname{SiLU}(z)=z/(1+\operatorname{exp}(-z)), evaluated without overflow; exact\operatorname{GELU}(z)=0.5\cdot z\cdot (1+\operatorname{erf}(z/\sqrt{2})); and\operatorname{GELU}_{\tanh}(z)=\tfrac12 z\left[1+\tanh\left(\sqrt{2/\pi}(z+0.044715z^3)\right)\right];\operatorname{GELU}_{\mathrm{sigmoid}}(z)=z/(1+\exp(-1.702z)); and\operatorname{ReLU}(z)=\operatorname{max}(z,0), with zero derivative at exactly zero. - Sigmoid-GELU is Enformer's approximation, not
\operatorname{tanh}-\operatorname{GELU}or exact GELU. - In activation mode,
xis any floating shape and\mathrm{output}=\mathrm{activation}(x). - In feedforward mode
x [\ldots,\mathrm{Cin}],w_up[\mathrm{Cin},H],w_down[H,\mathrm{Cout}], optionalb_up[H]andb_down[\mathrm{Cout}]are supplied. Letu=x\mathbin{@}w_{\mathrm{up}}+b_{\mathrm{up}}. gateddefaults toTrue: thenw_gate[\mathrm{Cin},H]and optionalb_gate[H]are supplied and\mathrm{hidden}=u\cdot \mathrm{activation}(x\mathbin{@}w_{\mathrm{gate}}+b_{\mathrm{gate}}), with multiplication elementwise. Ifgated=False,\mathrm{hidden}=\mathrm{activation}(u)and gate tensors are absent.- The output is
\mathrm{hidden}\mathbin{@}w_{\mathrm{down}}+b_{\mathrm{down}}, omitting missing biases. This is the update only: no residual, normalization or dropout. Leading axes and noncontiguous layout must work; Cin,H,Cout are positive. The gate branch, not the up branch, is activated in gated mode. - All values and weights are supplied, and
outputkeeps their dtype/device.
Why this matters
A gate controls a separate linear feature branch. Activating the wrong branch produces a different operation even when every shape matches.
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 gated_feedforward(
x: torch.Tensor, # Any floating shape for variant="activation"; (..., Cin) for "feedforward".
w_up: torch.Tensor | None = None, # (Cin, H)
w_gate: torch.Tensor | None = None, # (Cin, H)
w_down: torch.Tensor | None = None, # (H, Cout)
b_up: torch.Tensor | None = None, # (H,)
b_gate: torch.Tensor | None = None, # (H,)
b_down: torch.Tensor | None = None, # (Cout,)
activation: str = 'silu',
variant: str = 'feedforward',
gated: bool = True,
) -> torch.Tensor:
"""Return a tensor with shape x.shape in activation mode; (..., Cout) in feedforward
mode."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement gated_feedforward")
Ready to implement it?
3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Related exercises
Sources and further reading
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.