@Huahuazo: Normally when using PyTorch's high-level API, everything works smoothly, but the moment you step away from the framework and write raw operators, you're quickly exposed — formulas swirl in your head but get stuck when turned into code; you can talk a good game, but freeze when you actually code. There's an open-source coding platform on GitHub called TorchCode, designed to fix this, turning "writing deep learning operators by hand" into a LeetCode-style practice...

X AI KOLs Timeline Tools

Summary

TorchCode is an open-source coding platform that turns manual deep learning operator implementation into a LeetCode-style experience. It includes 40 high-frequency interview questions, provides automated evaluation and hints, and supports one-click Docker deployment and online use via Hugging Face Spaces.

Normally when using PyTorch's high-level API, everything works smoothly, but the moment you step away from the framework and write raw operators, you're quickly exposed — formulas swirl in your head but get stuck when turned into code; you can talk a good game, but freeze when you actually code. There's an open-source coding platform on GitHub called TorchCode, designed to fix this, turning "writing deep learning operators by hand" into a LeetCode-style practice. Based on the Jupyter environment, it carefully selects 40 high-frequency questions covering basic operators, attention mechanisms, complete architectures, training optimization, and other interview pain points. Each question comes with automatic evaluation: real-time verification of output, gradient flow, and numerical stability. Color-coded pass/fail feedback makes it clear where you went wrong. Stuck? There are hints. Finished? Reference implementations are available. Progress tracking is also clearly arranged. Deployment is extremely friendly: one-click with Docker, or directly use Hugging Face Spaces to practice online. Each question also supports opening with one click in Google Colab, saving you the trouble of setting up a local environment. If you truly want to understand the underlying principles of large models, or prepare for AI job interviews, this set of questions is worth going through from start to finish. GitHub: https://github.com/duoan/TorchCode
Original Article
View Cached Full Text

Cached at: 07/09/26, 03:48 PM

Normally you breeze through PyTorch’s high‑level APIs, but the moment you have to write operators from scratch without a framework, your true colors show—formulas swirl in your head but get stuck when translated into code; you can talk through the logic but freeze up when you start coding.

There’s an open‑source practice platform on GitHub called TorchCode, specifically designed to fix this problem. It turns handwritten deep learning operators into a LeetCode‑style experience.

Built on a Jupyter environment, it curates 40 high‑frequency problems covering foundational operators, attention mechanisms, full architectures, and training optimization—the heavy‑hitters in interviews.

Each problem comes with an automatic judge: real‑time verification of outputs, gradient flow, and numerical stability, with clear color‑coded pass/fail feedback. You’ll know immediately where things went wrong.

Stuck? There are hints. Finished? There are reference solutions. Progress tracking is laid out clearly.

Deployment is extremely friendly: Docker one‑click startup, or directly use Hugging Face Spaces to start practicing online. Each problem also supports one‑click opening in Google Colab, saving you the hassle of local environment setup.

If you want to truly master the underlying mechanics of large models or prepare for AI position interviews, this set of problems is worth going through from start to finish.

GitHub: https://github.com/duoan/TorchCode


duoan/TorchCode

Source: https://github.com/duoan/TorchCode


title: TorchCode emoji: 🔥 colorFrom: red colorTo: yellow sdk: docker app_port: 7860 pinned: false


🔥 TorchCode

Crack the PyTorch interview. Practice implementing operators and architectures from scratch — the exact skills top ML teams test for.

An interactive coding platform, but for tensors. Self-hosted. Jupyter-based. Instant feedback.

PyTorch (https://pytorch.org) Jupyter (https://jupyter.org) Docker (https://www.docker.com) Python (https://python.org)

License: MIT

GitHub stars (https://github.com/duoan/TorchCode) · GitHub Container Registry (https://ghcr.io/duoan/torchcode) · Hugging Face Spaces (https://huggingface.co/spaces/duoan/TorchCode)

Problems · GPU · Star History Chart (https://star-history.com/#duoan/TorchCode&Date)


🎯 Why TorchCode?

Top companies (Meta, Google DeepMind, OpenAI, etc.) expect ML engineers to implement core operations from memory on a whiteboard. Reading papers isn’t enough — you need to write softmax, LayerNorm, MultiHeadAttention, and full Transformer blocks from scratch.

TorchCode gives you a structured practice environment with:

FeatureDescription
🧩40 curated problemsThe most frequently asked PyTorch interview topics
⚖️Automated judgeCorrectness checks, gradient verification, and timing
🎨Instant feedbackColored pass/fail per test case, just like competitive programming
💡Hints when stuckNudges without full spoilers
📖Reference solutionsStudy optimal implementations after your attempt
📊Progress trackingWhat you’ve solved, best times, and attempt counts
🔄One-click resetToolbar button to reset any notebook back to its blank template — practice the same problem as many times as you want
🔗Open in ColabEvery notebook has an “Open in Colab” badge + toolbar button — run problems in Google Colab with zero setup

No cloud. No signup. No GPU needed. Just make run — or try it instantly on Hugging Face.


🚀 Quick Start

Option 0 — Try it online (zero install)

Launch on Hugging Face Spaces (https://huggingface.co/spaces/duoan/TorchCode) — opens a full JupyterLab environment in your browser. Nothing to install.

Or open any problem directly in Google Colab — every notebook has an Open In Colab (https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/01_relu.ipynb) badge.

Option 0b — Use the judge in Colab (pip)

In Google Colab, install the judge from PyPI so you can run check(...) without cloning the repo:

!pip install torch‑judge

Then in a notebook cell:

from torch_judge import check, status, hint, reset_progress
status()           # list all problems and your progress
check("relu")      # run tests for the "relu" task
hint("relu")       # show a hint

Option 1 — Pull the pre‑built image (fastest)

docker run -p 8888:8888 -e PORT=8888 ghcr.io/duoan/torchcode:latest

If the registry image is unavailable for your platform, use Option 2 instead. This is the common path on Apple Silicon / arm64.

Option 2 — Build locally

make run

make run will try the prebuilt image first and automatically fall back to a local build when needed.

Open http://localhost:8888 — that’s it. Works with both Docker and Podman (auto‑detected).

Option 3 — Standalone Web UI (Next.js + FastAPI)

For a modern, standalone coding experience with an integrated IDE and dual‑pane layout:

  1. Start Backend (FastAPI):

    pip install -r api/requirements.txt
    python -m uvicorn api.main:app --port 8000 --reload
    
  2. Start Frontend (Next.js):

    cd web
    npm install
    npm run dev
    
  3. Open http://localhost:3000 in your browser.


📋 Problem Set

Frequency: 🔥 = very likely in interviews, ⭐ = commonly asked, 💡 = emerging / differentiator

🧱 Fundamentals — “Implement X from scratch”

The bread and butter of ML coding interviews. You’ll be asked to write these without torch.nn.

#ProblemWhat You’ll ImplementDifficultyFreqKey Concepts
1ReLUrelu(x)Easy🔥Activation functions, element‑wise ops
2Softmaxmy_softmax(x, dim)Easy🔥Numerical stability, exp/log tricks
16Cross‑Entropy Losscross_entropy_loss(logits, targets)Easy🔥Log‑softmax, logsumexp trick
17DropoutMyDropout (nn.Module)Easy🔥Train/eval mode, inverted scaling
18EmbeddingMyEmbedding (nn.Module)Easy🔥Lookup table, weight[indices]
19GELUmy_gelu(x)EasyGaussian error linear unit, torch.erf
20Kaiming Initkaiming_init(weight)Easystd = sqrt(2/fan_in), variance scaling
21Gradient Clippingclip_grad_norm(params, max_norm)EasyNorm‑based clipping, direction preservation
31Gradient Accumulationaccumulated_step(model, opt, ...)Easy💡Micro‑batching, loss scaling
40Linear RegressionLinearRegression (3 methods)Medium🔥Normal equation, GD from scratch, nn.Linear
3Linear LayerSimpleLinear (nn.Module)Medium🔥y = xW^T + b, Kaiming init, nn.Parameter
4LayerNormmy_layer_norm(x, γ, β)Medium🔥Normalization, running stats, affine transform
7BatchNormmy_batch_norm(x, γ, β)MediumBatch vs layer statistics, train/eval behavior
8RMSNormrms_norm(x, weight)MediumLLaMA‑style norm, simpler than LayerNorm
15SwiGLU MLPSwiGLUMLP (nn.Module)MediumGated FFN, SiLU(gate) * up, LLaMA/Mistral‑style
22Conv2dmy_conv2d(x, weight, ...)Medium🔥Convolution, unfold, stride/padding

🧠 Attention Mechanisms — The heart of modern ML interviews

If you’re interviewing for any role touching LLMs or Transformers, expect at least one of these.

#ProblemWhat You’ll ImplementDifficultyFreqKey Concepts
23Cross‑AttentionMultiHeadCrossAttention (nn.Module)MediumEncoder‑decoder, Q from decoder, K/V from encoder
5Scaled Dot‑Product Attentionscaled_dot_product_attention(Q, K, V)Hard🔥softmax(QK^T/√d_k)V, the foundation of everything
6Multi‑Head AttentionMultiHeadAttention (nn.Module)Hard🔥Parallel heads, split/concat, projection matrices
9Causal Self‑Attentioncausal_attention(Q, K, V)Hard🔥Autoregressive masking with -inf, GPT‑style
10Grouped Query AttentionGroupQueryAttention (nn.Module)HardGQA (LLaMA 2), KV sharing across heads
11Sliding Window Attentionsliding_window_attention(Q, K, V, w)HardMistral‑style local attention, O(n·w) complexity
12Linear Attentionlinear_attention(Q, K, V)Hard💡Kernel trick, φ(Q)(φ(K)^TV), O(n·d²)
14KV Cache AttentionKVCacheAttention (nn.Module)Hard🔥Incremental decoding, cache K/V, prefill vs decode
24RoPEapply_rope(q, k)Hard🔥Rotary position embedding, relative position via rotation
25Flash Attentionflash_attention(Q, K, V, block_size)Hard💡Tiled attention, online softmax, memory‑efficient

🏗️ Architecture & Adaptation — Put it all together

#ProblemWhat You’ll ImplementDifficultyFreqKey Concepts
26LoRALoRALinear (nn.Module)MediumLow‑rank adaptation, frozen base + BA update
27ViT Patch EmbeddingPatchEmbedding (nn.Module)Medium💡Image → patches → linear projection
13GPT‑2 BlockGPT2Block (nn.Module)HardPre‑norm, causal MHA + MLP (4×, GELU), residual connections
28Mixture of ExpertsMixtureOfExperts (nn.Module)HardMixtral‑style, top‑k routing, expert MLPs

⚙️ Training & Optimization

#ProblemWhat You’ll ImplementDifficultyFreqKey Concepts
29Adam OptimizerMyAdamMediumMomentum + RMSProp, bias correction
30Cosine LR Schedulercosine_lr_schedule(step, ...)MediumLinear warmup + cosine annealing

🎯 Inference & Decoding

#ProblemWhat You’ll ImplementDifficultyFreqKey Concepts
32Top‑k / Top‑p Samplingsample_top_k_top_p(logits, ...)Medium🔥Nucleus sampling, temperature scaling
33Beam Searchbeam_search(log_prob_fn, ...)Medium🔥Hypothesis expansion, pruning, eos handling
34Speculative Decodingspeculative_decode(target, draft, ...)Hard💡Accept/reject, draft model acceleration

🔬 Advanced — Differentiators

#ProblemWhat You’ll ImplementDifficultyFreqKey Concepts
35BPE TokenizerSimpleBPEHard💡Byte‑pair encoding, merge rules, subword splits
36INT8 QuantizationInt8Linear (nn.Module)Hard💡Per‑channel quantize, scale/zero‑point, buffer vs param
37DPO Lossdpo_loss(chosen, rejected, ...)Hard💡Direct preference optimization, alignment training
38GRPO Lossgrpo_loss(logps, rewards, group_ids, eps)Hard💡Group relative policy optimization, RLAIF, within‑group normalized advantages
39PPO Lossppo_loss(new_logps, old_logps, advantages, clip_ratio)Hard💡PPO clipped surrogate loss, policy gradient, trust region

⚙️ How It Works

Each problem has two notebooks:

FilePurpose
01_relu.ipynb✏️ Blank template — write your code here
01_relu_solution.ipynb📖 Reference solution — check when stuck

Workflow

1. Open a blank notebook → Read the problem description
2. Implement your solution → Use only basic PyTorch ops
3. Debug freely → print(x.shape), check gradients, etc.
4. Run the judge cell → check("relu")
5. See instant colored feedback → ✅ pass / ❌ fail per test case
6. Stuck? Get a nudge → hint("relu")
7. Review the reference solution → 01_relu_solution.ipynb
8. Click 🔄 Reset in the toolbar → Blank slate — practice again!

In‑Notebook API

from torch_judge import check, hint, status

check("relu")                # Judge your implementation
hint("causal_attention")     # Get a hint without full spoiler
status()                     # Progress dashboard — solved / attempted / todo

📅 Suggested Study Plan

Total: ~12–16 hours spread across 3–4 weeks. Perfect for interview prep on a deadline.

WeekFocusProblemsTime
1🧱 FoundationsReLU → Softmax → CE Loss → Dropout → Embedding → GELU → Linear → LayerNorm → BatchNorm → RMSNorm → SwiGLU MLP → Conv2d2–3 hrs
2🧠 Attention Deep DiveSDPA → MHA → Cross‑Attn → Causal → GQA → KV Cache → Sliding Window → RoPE → Linear Attn → Flash Attn3–4 hrs
3🏗️ Architecture + TrainingGPT‑2 Block → LoRA → MoE → ViT Patch → Adam → Cosine LR → Grad Clip → Grad Accumulation → Kaiming Init3–4 hrs
4🎯 Inference + AdvancedTop‑k/p Sampling → Beam Search → Speculative Decoding → BPE → INT8 Quant → DPO Loss → GRPO Loss → PPO Loss + speed run3–4 hrs

🏛️ Architecture

┌──────────────────────────────────────────┐
│ Docker / Podman Container                │
│                                          │
│ JupyterLab (:8888)                       │
│ ├── templates/ (reset on each run)       │
│ ├── solutions/ (reference impl)          │
│ ├── torch_judge/ (auto‑grading)          │
│ ├── torchcode-labext (JLab plugin)       │
│ │     🔄 Reset — restore template        │
│ │     🔗 Colab — open in Colab           │
│ └── PyTorch (CPU), NumPy                 │
│                                          │
│ Judge checks:                            │
│ ✓ Output correctness (allclose)          │
│ ✓ Gradient flow (autograd)               │
│ ✓ Shape consistency                      │
│ ✓ Edge cases & numerical stability       │
└──────────────────────────────────────────┘

Single container. Single port. No database. No frontend framework. No GPU.

🛠️ Commands

make run    # Build & start (http://localhost:8888)
make stop   # Stop the container
make clean  # Stop + remove volumes + reset all progress

🧩 Adding Your Own Problems

TorchCode uses auto‑discovery — just drop a new file in torch_judge/tasks/:

TASK = {
    "id": "my_task",
    "title": "My Custom Problem",
    "difficulty": "medium",
    "function_name": "my_function",
    "hint": "Think about broadcasting...",
    "tests": [ ... ],
}

No registration needed. The judge picks it up automatically.


📦 Publishing torch‑judge to PyPI (maintainers)

The judge is published as a separate package so Colab/users can pip install torch‑judge without cloning the repo.

Automatic (GitHub Action)

Pushing to master after changing the package version triggers .github/workflows/pypi-publish.yml, which builds and uploads to PyPI. No git tag is required.

  1. Bump version in torch_judge/_version.py (e.g. __version__ = "0.1.1").
  2. Configure PyPI Trusted Publisher (one‑time):
    • PyPI → Your project torch‑judgePublishingAdd a new pending publisher
    • Owner: duoan, Repository: TorchCode, Workflow: pypi-publish.yml, Environment: (leave empty)
    • Run the workflow once (push a version bump to master or Actions → Publish torch‑judge to PyPI → Run workflow); PyPI will then link the publisher.
  3. Release: commit the version bump and git push origin master.

Alternatively, use an API token: add repository secret PYPI_API_TOKEN (value = pypi‑... from PyPI) and set TWINE_USERNAME=__token__ and TWINE_PASSWORD from that secret in the workflow if you prefer not to use Trusted Publishing.

Manual

pip install build twine
python -m build
twine upload dist/*

Version is in torch_judge/_version.py; bump it before each release.


❓ FAQ

Do I need a GPU?
No. Everything runs on CPU. The problems test correctness and understanding, not throughput.

Can I keep my solutions between runs?
Blank templates reset on every make run so you practice from scratch. Save your work under a different filename if you want to keep it. You can also click the 🔄 Reset button in the notebook toolbar at any time to restore the blank template without restarting.

Can I use Google Colab instead?
Yes! Every notebook has an “Open in Colab” badge at the top. Click it to open the problem directly in Google Colab — no Docker or local setup needed. You can also use the Colab toolbar button inside JupyterLab.

How are solutions graded?
The judge runs your function against multiple test cases using torch.allclose for numerical correctness, verifies gradients flow properly via autograd, and checks edge cases specific to each operation.

Who is this for?
Anyone preparing for ML/AI engineering interviews at top tech companies, or anyone who wants to deeply understand how PyTorch operations work under the hood.


🤝 Contributors

Thanks to everyone who has contributed to TorchCode.

duoan · Ando233 · abhijitmjj · HareshKarnan · ThierryHJ

Auto‑generated from the GitHub contributors graph with avatars and GitHub usernames.


Built for engineers who want to deeply understand what they build.
If this helped your interview prep, consider giving it a ⭐


☕ Buy Me a Coffee

Scan to support

Similar Articles

@Ellieorange8: During a large model algorithm interview, the interviewer smiled and handed me a pen: Write down the Attention mechanism on the whiteboard. I nodded pretending to be confident, took the pen—then my mind went blank. Normally, I just call `F.softmax` in PyTorch and that's it, but when I have to hand-code the underlying operators, I remember the formula but forget all the details. Don't go into the exam unprepared anymore...

X AI KOLs Timeline

TorchCode is an open-source question bank that turns handwritten deep learning operators (such as Attention, Transformer) into LeetCode-style training. It is based on Jupyter and supports auto-grading, hints, and reference implementations, helping you prepare for large model algorithm interviews.

@NFTCPS: You keep talking about AI, but can't even explain what a Transformer is? There's a repo that goes all out — builds a GPT from scratch without using any high-level libraries. It lays out exactly how Attention, Multi-Head, Feed-Forward, Embedding, Residual connections, and Layer Norm are pieced together. And it's not just the model; the entire pipeline is covered…

X AI KOLs Timeline

A GitHub open-source project that implements the complete GPT training pipeline from scratch, including data preprocessing, pretraining, SFT, and RLHF post-training, all based on native PyTorch. Ideal for developers who want to deeply understand the Transformer architecture.

@QingQ77: 'Dive into Deep Learning' is an excellent introductory book, but its update speed struggles to keep pace with the field's development. Since the Transformer, content like CLIP, Diffusion, vLLM, and more has proliferated. While online resources are abundant, they are highly fragmented—today you study Attention, tomorrow LoRA, the day after...

X AI KOLs Timeline

This project is a systematic deep learning notes repository covering PyTorch, Transformers, generative models, and more. It aims to address the fragmentation of learning materials and provides code implementations along with practical guides.

@axichuhai: Found a project on GitHub specifically teaching how to use Claude Code, now up to 4w stars. It's not an official document, but a complete learning path from entry to advanced agents. Slash commands, MCP configuration, hook scripts, sub-agent orchestration — each module has visual diagrams, teaching you "how" and also "why."

X AI KOLs Timeline

Introduces a GitHub project with 40K stars for learning Claude Code, offering a complete learning path from beginner to advanced agents, visual diagrams, and reusable templates.

@GitTrend0x: Claude Code Codebase Smart Brain — 27× Token Savings Killer Open-Source Tool https://github.com/repowise-dev/repowise… This is Repowise, a codebase intelligence platform built for AI-assisted engineering teams! It turns...

X AI KOLs Timeline

Repowise is an open-source tool that indexes codebases into four intelligence layers (dependency graph, git history, auto-documentation, architectural decisions) and exposes them via seven MCP tools to AI coding agents like Claude Code, achieving up to 27× token savings while maintaining answer quality.