CoolFace
Apppublic

zhangliu12/TorchCode

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

<div align="center">

๐Ÿ”ฅ TorchCode

Crack the PyTorch interview.

Practice implementing operators and architectures from scratch โ€” the exact skills top ML teams test for.

Like LeetCode, 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](LICENSE)

![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)

</div>


๐ŸŽฏ 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 code.

TorchCode gives you a structured practice environment with:

Feature
๐Ÿงฉ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 Colab](#)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:

bash
!pip install torch-judge

Then in a notebook cell:

python
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)

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

Option 2 โ€” Build locally

bash
make run

Open <http://localhost:8888> โ€” that's it. Works with both Docker and Podman (auto-detected).


๐Ÿ“‹ 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
1<a href="https://github.com/duoan/TorchCode/blob/master/templates/01relu.ipynb" target="blank">ReLU</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/01relu.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>relu(x)Easy๐Ÿ”ฅActivation functions, element-wise ops
2<a href="https://github.com/duoan/TorchCode/blob/master/templates/02softmax.ipynb" target="blank">Softmax</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/02softmax.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>my_softmax(x, dim)Easy๐Ÿ”ฅNumerical stability, exp/log tricks
16<a href="https://github.com/duoan/TorchCode/blob/master/templates/16crossentropy.ipynb" target="blank">Cross-Entropy Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/16crossentropy.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>cross_entropy_loss(logits, targets)Easy๐Ÿ”ฅLog-softmax, logsumexp trick
17<a href="https://github.com/duoan/TorchCode/blob/master/templates/17dropout.ipynb" target="blank">Dropout</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/17dropout.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>MyDropout (nn.Module)Easy๐Ÿ”ฅTrain/eval mode, inverted scaling
18<a href="https://github.com/duoan/TorchCode/blob/master/templates/18embedding.ipynb" target="blank">Embedding</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/18embedding.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>MyEmbedding (nn.Module)Easy๐Ÿ”ฅLookup table, weight[indices]
19<a href="https://github.com/duoan/TorchCode/blob/master/templates/19gelu.ipynb" target="blank">GELU</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/19gelu.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>my_gelu(x)EasyโญGaussian error linear unit, torch.erf
20<a href="https://github.com/duoan/TorchCode/blob/master/templates/20weightinit.ipynb" target="blank">Kaiming Init</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/20weightinit.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>kaiming_init(weight)Easyโญstd = sqrt(2/fan_in), variance scaling
21<a href="https://github.com/duoan/TorchCode/blob/master/templates/21gradientclipping.ipynb" target="blank">Gradient Clipping</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/21gradientclipping.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>clip_grad_norm(params, max_norm)EasyโญNorm-based clipping, direction preservation
31<a href="https://github.com/duoan/TorchCode/blob/master/templates/31gradientaccumulation.ipynb" target="blank">Gradient Accumulation</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/31gradientaccumulation.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>accumulated_step(model, opt, ...)Easy๐Ÿ’กMicro-batching, loss scaling
40<a href="https://github.com/duoan/TorchCode/blob/master/templates/40linearregression.ipynb" target="blank">Linear Regression</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/40linearregression.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>LinearRegression (3 methods)Medium๐Ÿ”ฅNormal equation, GD from scratch, nn.Linear
3<a href="https://github.com/duoan/TorchCode/blob/master/templates/03linear.ipynb" target="blank">Linear Layer</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/03linear.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>SimpleLinear (nn.Module)Medium๐Ÿ”ฅy = xW^T + b, Kaiming init, nn.Parameter
4<a href="https://github.com/duoan/TorchCode/blob/master/templates/04layernorm.ipynb" target="blank">LayerNorm</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/04layernorm.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>my_layer_norm(x, ฮณ, ฮฒ)Medium๐Ÿ”ฅNormalization, running stats, affine transform
7<a href="https://github.com/duoan/TorchCode/blob/master/templates/07batchnorm.ipynb" target="blank">BatchNorm</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/07batchnorm.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>my_batch_norm(x, ฮณ, ฮฒ)MediumโญBatch vs layer statistics, train/eval behavior
8<a href="https://github.com/duoan/TorchCode/blob/master/templates/08rmsnorm.ipynb" target="blank">RMSNorm</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/08rmsnorm.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>rms_norm(x, weight)MediumโญLLaMA-style norm, simpler than LayerNorm
15<a href="https://github.com/duoan/TorchCode/blob/master/templates/15mlp.ipynb" target="blank">SwiGLU MLP</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/15mlp.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>SwiGLUMLP (nn.Module)MediumโญGated FFN, SiLU(gate) * up, LLaMA/Mistral-style
22<a href="https://github.com/duoan/TorchCode/blob/master/templates/22conv2d.ipynb" target="blank">Conv2d</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/22conv2d.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>my_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
23<a href="https://github.com/duoan/TorchCode/blob/master/templates/23crossattention.ipynb" target="blank">Cross-Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/23crossattention.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>MultiHeadCrossAttention (nn.Module)MediumโญEncoder-decoder, Q from decoder, K/V from encoder
5<a href="https://github.com/duoan/TorchCode/blob/master/templates/05attention.ipynb" target="blank">Scaled Dot-Product Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/05attention.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>scaled_dot_product_attention(Q, K, V)Hard๐Ÿ”ฅsoftmax(QK^T/โˆšd_k)V, the foundation of everything
6<a href="https://github.com/duoan/TorchCode/blob/master/templates/06multiheadattention.ipynb" target="blank">Multi-Head Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/06multiheadattention.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>MultiHeadAttention (nn.Module)Hard๐Ÿ”ฅParallel heads, split/concat, projection matrices
9<a href="https://github.com/duoan/TorchCode/blob/master/templates/09causalattention.ipynb" target="blank">Causal Self-Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/09causalattention.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>causal_attention(Q, K, V)Hard๐Ÿ”ฅAutoregressive masking with -inf, GPT-style
10<a href="https://github.com/duoan/TorchCode/blob/master/templates/10gqa.ipynb" target="blank">Grouped Query Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/10gqa.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>GroupQueryAttention (nn.Module)HardโญGQA (LLaMA 2), KV sharing across heads
11<a href="https://github.com/duoan/TorchCode/blob/master/templates/11slidingwindow.ipynb" target="blank">Sliding Window Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/11slidingwindow.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>sliding_window_attention(Q, K, V, w)HardโญMistral-style local attention, O(nยทw) complexity
12<a href="https://github.com/duoan/TorchCode/blob/master/templates/12linearattention.ipynb" target="blank">Linear Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/12linearattention.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>linear_attention(Q, K, V)Hard๐Ÿ’กKernel trick, ฯ†(Q)(ฯ†(K)^TV), O(nยทdยฒ)
14<a href="https://github.com/duoan/TorchCode/blob/master/templates/14kvcache.ipynb" target="blank">KV Cache Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/14kvcache.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>KVCacheAttention (nn.Module)Hard๐Ÿ”ฅIncremental decoding, cache K/V, prefill vs decode
24<a href="https://github.com/duoan/TorchCode/blob/master/templates/24rope.ipynb" target="blank">RoPE</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/24rope.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>apply_rope(q, k)Hard๐Ÿ”ฅRotary position embedding, relative position via rotation
25<a href="https://github.com/duoan/TorchCode/blob/master/templates/25flashattention.ipynb" target="blank">Flash Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/25flashattention.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>flash_attention(Q, K, V, block_size)Hard๐Ÿ’กTiled attention, online softmax, memory-efficient

๐Ÿ—๏ธ Architecture & Adaptation โ€” Put it all together

#ProblemWhat You'll ImplementDifficultyFreqKey Concepts
26<a href="https://github.com/duoan/TorchCode/blob/master/templates/26lora.ipynb" target="blank">LoRA</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/26lora.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>LoRALinear (nn.Module)MediumโญLow-rank adaptation, frozen base + BA update
27<a href="https://github.com/duoan/TorchCode/blob/master/templates/27vitpatch.ipynb" target="blank">ViT Patch Embedding</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/27vitpatch.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>PatchEmbedding (nn.Module)Medium๐Ÿ’กImage โ†’ patches โ†’ linear projection
13<a href="https://github.com/duoan/TorchCode/blob/master/templates/13gpt2block.ipynb" target="blank">GPT-2 Block</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/13gpt2block.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>GPT2Block (nn.Module)HardโญPre-norm, causal MHA + MLP (4x, GELU), residual connections
28<a href="https://github.com/duoan/TorchCode/blob/master/templates/28moe.ipynb" target="blank">Mixture of Experts</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/28moe.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>MixtureOfExperts (nn.Module)HardโญMixtral-style, top-k routing, expert MLPs

โš™๏ธ Training & Optimization

#ProblemWhat You'll ImplementDifficultyFreqKey Concepts
29<a href="https://github.com/duoan/TorchCode/blob/master/templates/29adam.ipynb" target="blank">Adam Optimizer</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/29adam.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>MyAdamMediumโญMomentum + RMSProp, bias correction
30<a href="https://github.com/duoan/TorchCode/blob/master/templates/30cosinelr.ipynb" target="blank">Cosine LR Scheduler</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/30cosinelr.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>cosine_lr_schedule(step, ...)MediumโญLinear warmup + cosine annealing

๐ŸŽฏ Inference & Decoding

#ProblemWhat You'll ImplementDifficultyFreqKey Concepts
32<a href="https://github.com/duoan/TorchCode/blob/master/templates/32topksampling.ipynb" target="blank">Top-k / Top-p Sampling</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/32topksampling.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>sample_top_k_top_p(logits, ...)Medium๐Ÿ”ฅNucleus sampling, temperature scaling
33<a href="https://github.com/duoan/TorchCode/blob/master/templates/33beamsearch.ipynb" target="blank">Beam Search</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/33beamsearch.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>beam_search(log_prob_fn, ...)Medium๐Ÿ”ฅHypothesis expansion, pruning, eos handling
34<a href="https://github.com/duoan/TorchCode/blob/master/templates/34speculativedecoding.ipynb" target="blank">Speculative Decoding</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/34speculativedecoding.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>speculative_decode(target, draft, ...)Hard๐Ÿ’กAccept/reject, draft model acceleration

๐Ÿ”ฌ Advanced โ€” Differentiators

#ProblemWhat You'll ImplementDifficultyFreqKey Concepts
35<a href="https://github.com/duoan/TorchCode/blob/master/templates/35bpe.ipynb" target="blank">BPE Tokenizer</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/35bpe.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>SimpleBPEHard๐Ÿ’กByte-pair encoding, merge rules, subword splits
36<a href="https://github.com/duoan/TorchCode/blob/master/templates/36int8quantization.ipynb" target="blank">INT8 Quantization</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/36int8quantization.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>Int8Linear (nn.Module)Hard๐Ÿ’กPer-channel quantize, scale/zero-point, buffer vs param
37<a href="https://github.com/duoan/TorchCode/blob/master/templates/37dpoloss.ipynb" target="blank">DPO Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/37dpoloss.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>dpo_loss(chosen, rejected, ...)Hard๐Ÿ’กDirect preference optimization, alignment training
38<a href="https://github.com/duoan/TorchCode/blob/master/templates/38grpoloss.ipynb" target="blank">GRPO Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/38grpoloss.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>grpo_loss(logps, rewards, group_ids, eps)Hard๐Ÿ’กGroup relative policy optimization, RLAIF, within-group normalized advantages
39<a href="https://github.com/duoan/TorchCode/blob/master/templates/39ppoloss.ipynb" target="blank">PPO Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/39ppoloss.ipynb" target="blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a>ppo_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

text
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

python
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

text
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           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

bash
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/:

python
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. 1.Bump version in torch_judge/_version.py (e.g. __version__ = "0.1.1").
  2. 2.Configure PyPI Trusted Publisher (one-time):
  3. 3.PyPI โ†’ Your project torch-judge โ†’ Publishing โ†’ Add a new pending publisher
  4. 4.Owner: duoan, Repository: TorchCode, Workflow: pypi-publish.yml, Environment: (leave empty)
  5. 5.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.
  6. 6.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

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

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


โ“ FAQ

<details> <summary><b>Do I need a GPU?</b></summary> <br> No. Everything runs on CPU. The problems test correctness and understanding, not throughput. </details>

<details> <summary><b>Can I keep my solutions between runs?</b></summary> <br> Blank templates reset on every <code>make run</code> so you practice from scratch. Save your work under a different filename if you want to keep it. You can also click the <b>๐Ÿ”„ Reset</b> button in the notebook toolbar at any time to restore the blank template without restarting. </details>

<details> <summary><b>Can I use Google Colab instead?</b></summary> <br> Yes! Every notebook has an <b>Open in Colab</b> 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 <b>Colab</b> toolbar button inside JupyterLab. </details>

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

<details> <summary><b>Who is this for?</b></summary> <br> 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. </details>


<div align="center">

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

<a href="https://buymeacoffee.com/duoan" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>

<img src="./bmc_qr.png" alt="BMC QR Code" width="150" height="150">

Scan to support

</div>