TensorTonic
Study Plans
Study Plan
Parameter-Matched SwiGLU Block
Language ModelsActivation Functions
Medium

Implement a bias-free SwiGLU block with a hidden width selected to match a target parameter budget. The input's final dimension is d, the supplied weight pool has maximum hidden width H, and P is base_params.

h=min⁡(H,max⁡(1,⌊P3d+0.5⌋)).h=\min\left(H,\max\left(1,\left\lfloor\frac{P}{3d}+0.5\right\rfloor\right)\right).h=min(H,max(1,⌊3dP​+0.5⌋)). SiLU⁡(z)=z1+e−z.\operatorname{SiLU}(z)=\frac{z}{1+e^{-z}}.SiLU(z)=1+e−zz​. y=(SiLU⁡(xWg)⊙(xWv))Wo.y=\left(\operatorname{SiLU}(xW_g)\odot(xW_v)\right)W_o.y=(SiLU(xWg​)⊙(xWv​))Wo​.

Here h is the selected hidden width; floor rounds down, giving upward rounding at half-way budgets. The gate and value projections use the first h columns of w_g and w_v; the output projection uses the first h rows of w_o. These are the three W matrices in the equation. The circled-dot operator is elementwise multiplication, z is an activation value, e is Euler's number, and y is the block output. The three selected matrices contain 3dh parameters.

Return a Python dictionary with output as a PyTorch tensor matching x's shape, dtype, and device, hidden_width as the selected Python integer h, and parameter_count as the Python integer number of parameters used.

Loading visualization...

Examples

Input: x = [[1,2]], w_g = [[1,0,1],[0,1,1]], w_v = [[1,1,0],[1,0,1]], w_o = [[1,0],[0,1],[1,1]], base_params = 12

Output: {"output":[[2.193176,1.761594]],"hidden_width":2,"parameter_count":12}

Explanation: The target selects hidden width 2. The first two columns of the gate and value weights and the first two rows of the output weight form the budget-matched SwiGLU block.

Input: x = [[2]], w_g = [[1,0]], w_v = [[3,1]], w_o = [[2],[4]], base_params = 3

Output: {"output":[[21.13913]],"hidden_width":1,"parameter_count":3}

Input: x = [[1,-1]], w_g = [[1,0],[0,1]], w_v = [[1,0],[0,1]], w_o = [[1,0],[0,1]], base_params = 100

Output: {"output":[[0.731059,0.268941]],"hidden_width":2,"parameter_count":12}

Hint 1

math.floor avoids Python round's ties-to-even behavior.

Hint 2

A two-axis slice selects columns or rows without changing their order.

Hint 3

torch.nn.functional.silu provides the gate activation.

Requirements

  • Use PyTorch and the supplied weights without biases or newly initialized parameters.
  • Compute projections and the gated activation in float32, preserving x's dtype in the returned tensor.
  • Supported tensor dtypes are float32 and bfloat16; public cases and custom Run inputs use float32.
  • Leave every input tensor unchanged.

Constraints

  • x is a floating-point PyTorch tensor whose final dimension d is positive.
  • w_g and w_v are PyTorch tensors of shape (d, H), and w_o has shape (H, d), with positive H.
  • base_params is a positive Python integer.
  • All tensors have float32 or bfloat16 dtype, share a device, and contain finite values.
Try Similar Problems
RoPE: Rotary Query and Key EmbeddingsMediumCausal Grouped-Query AttentionHardRMSNorm Forward PassEasyTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumApply Ranked BPE MergesMediumParallel and Recurrent Linear AttentionMediumImplement Prefix Cache Matching and ReuseMediumContinuity of Activation FunctionsMediumSigmoid Boundedness via Squeeze TheoremMediumN-gram Language ModelMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Parameter-Matched SwiGLU Block

Language ModelsActivation Functions
Medium

Implement a bias-free SwiGLU block with a hidden width selected to match a target parameter budget. The input's final dimension is d, the supplied weight pool has maximum hidden width H, and P is base_params.

h=min⁡(H,max⁡(1,⌊P3d+0.5⌋)).h=\min\left(H,\max\left(1,\left\lfloor\frac{P}{3d}+0.5\right\rfloor\right)\right).h=min(H,max(1,⌊3dP​+0.5⌋)). SiLU⁡(z)=z1+e−z.\operatorname{SiLU}(z)=\frac{z}{1+e^{-z}}.SiLU(z)=1+e−zz​. y=(SiLU⁡(xWg)⊙(xWv))Wo.y=\left(\operatorname{SiLU}(xW_g)\odot(xW_v)\right)W_o.y=(SiLU(xWg​)⊙(xWv​))Wo​.

Here h is the selected hidden width; floor rounds down, giving upward rounding at half-way budgets. The gate and value projections use the first h columns of w_g and w_v; the output projection uses the first h rows of w_o. These are the three W matrices in the equation. The circled-dot operator is elementwise multiplication, z is an activation value, e is Euler's number, and y is the block output. The three selected matrices contain 3dh parameters.

Return a Python dictionary with output as a PyTorch tensor matching x's shape, dtype, and device, hidden_width as the selected Python integer h, and parameter_count as the Python integer number of parameters used.

Loading visualization...

Examples

Input: x = [[1,2]], w_g = [[1,0,1],[0,1,1]], w_v = [[1,1,0],[1,0,1]], w_o = [[1,0],[0,1],[1,1]], base_params = 12

Output: {"output":[[2.193176,1.761594]],"hidden_width":2,"parameter_count":12}

Explanation: The target selects hidden width 2. The first two columns of the gate and value weights and the first two rows of the output weight form the budget-matched SwiGLU block.

Input: x = [[2]], w_g = [[1,0]], w_v = [[3,1]], w_o = [[2],[4]], base_params = 3

Output: {"output":[[21.13913]],"hidden_width":1,"parameter_count":3}

Input: x = [[1,-1]], w_g = [[1,0],[0,1]], w_v = [[1,0],[0,1]], w_o = [[1,0],[0,1]], base_params = 100

Output: {"output":[[0.731059,0.268941]],"hidden_width":2,"parameter_count":12}

Hint 1

math.floor avoids Python round's ties-to-even behavior.

Hint 2

A two-axis slice selects columns or rows without changing their order.

Hint 3

torch.nn.functional.silu provides the gate activation.

Requirements

  • Use PyTorch and the supplied weights without biases or newly initialized parameters.
  • Compute projections and the gated activation in float32, preserving x's dtype in the returned tensor.
  • Supported tensor dtypes are float32 and bfloat16; public cases and custom Run inputs use float32.
  • Leave every input tensor unchanged.

Constraints

  • x is a floating-point PyTorch tensor whose final dimension d is positive.
  • w_g and w_v are PyTorch tensors of shape (d, H), and w_o has shape (H, d), with positive H.
  • base_params is a positive Python integer.
  • All tensors have float32 or bfloat16 dtype, share a device, and contain finite values.
Try Similar Problems
RoPE: Rotary Query and Key EmbeddingsMediumCausal Grouped-Query AttentionHardRMSNorm Forward PassEasyTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumApply Ranked BPE MergesMediumParallel and Recurrent Linear AttentionMediumImplement Prefix Cache Matching and ReuseMediumContinuity of Activation FunctionsMediumSigmoid Boundedness via Squeeze TheoremMediumN-gram Language ModelMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.