TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

236. SiLU / Swish (PyTorch)

Easy

Implement SiLU (also known as Swish) in PyTorch using primitive tensor ops only.

Signature: def silu(x: torch.Tensor) -> torch.Tensor

The rule: you may NOT call F.silu, nn.SiLU, torch.sigmoid, or F.sigmoid. Implement sigmoid yourself inline. We verify your output matches F.silu(x).

Allowed primitives: .exp(), basic arithmetic.

Formula:

silu(x) = x * sigmoid(x) = x / (1 + exp(-x))

PyTorch idioms vs the NumPy version:

  • The NumPy reference uses np.exp(-x) directly without overflow handling, relying on float64. In float32 you need to be careful: (-x).exp() for large negative x (very negative input) would overflow. For the test inputs here, the simple x / (1 + (-x).exp()) form is fine; for production you'd use the same where-based stable pattern as the sigmoid problem.
  • This is the activation used in LLaMA, EfficientNet, and most modern vision/language models.

Math

SiLU(x)=x⋅σ(x)=1+e−xx​

Related problems

  • SiLU / Swish ActivationeasyNumPy

Asked at

NumPy

import numpy as np

 

def silu(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?