TorchCode — PyTorch Interview Practice
Skill by ara.so — Daily 2026 Skills collection.
TorchCode is a Jupyter-based, self-hosted coding practice environment for ML engineers. It provides 40 curated problems covering PyTorch fundamentals and architectures (softmax, LayerNorm, MultiHeadAttention, GPT-2, etc.) with an automated judge that gives instant pass/fail feedback, gradient verification, and timing — like LeetCode but for tensors.
Installation & Setup
Option 1: Online (zero install)
- Hugging Face Spaces: https://huggingface.co/spaces/duoan/TorchCode
- Google Colab: Every notebook has an "Open in Colab" badge
Option 2: pip (for use inside Colab or existing environment)
Option 3: Docker (pre-built image)
Option 4: Build locally
make run auto-detects Docker or Podman and falls back to local build if the registry image is unavailable (common on Apple Silicon/arm64).
Judge API
The torch_judge package provides the core API used in every notebook.
check() return values
- Colored pass/fail per test case
- Correctness check against PyTorch reference implementation
- Gradient verification (autograd compatibility)
- Timing measurement
Problem Set Overview
Difficulty levels: Easy → Medium → Hard
Working Through a Problem
Each problem notebook has the same structure:
Typical notebook workflow
Real Implementation Examples
ReLU (Problem 1 — Easy)
Softmax (Problem 2 — Easy, numerically stable)
LayerNorm (Problem 4 — Medium)
RMSNorm (Problem 8 — Medium, LLaMA-style)
Scaled Dot-Product Self-Attention (Problem 5 — Medium)
Multi-Head Attention (Problem 6 — Medium)
Cross-Entropy Loss (Problem 16 — Easy)
Dropout (Problem 17 — Easy)
Kaiming Init (Problem 20 — Easy)
Gradient Clipping (Problem 21 — Easy)
Gradient Accumulation (Problem 31 — Easy)
Common Patterns & Tips
Numerical stability pattern
Always subtract the max before exp():
Causal attention mask (for GPT-style models)
nn.Module skeleton (used in many problems)
Train vs eval mode pattern
Project Structure
Troubleshooting
Docker image not available for Apple Silicon (arm64)
check() not found in Colab
Notebook reset to blank template
Use the toolbar "Reset" button in JupyterLab to reset any notebook to its original blank state — useful for re-practicing a problem.
Gradient check fails but output is correct
Ensure your implementation uses PyTorch operations (not NumPy) so autograd works:
Viewing reference solution
After attempting a problem, open the matching file in solutions/:



