github-actions[bot] commited on
Commit Β·
1ccc68c
0
Parent(s):
Deploy prebuilt image 8a49ba150f76c92fc6e2fd30631e07b88c803988
Browse files- Dockerfile +1 -0
- README.md +370 -0
Dockerfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
FROM ghcr.io/duoan/torchcode:latest
|
README.md
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: TorchCode
|
| 3 |
+
emoji: π₯
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
<div align="center">
|
| 12 |
+
|
| 13 |
+
# π₯ TorchCode
|
| 14 |
+
|
| 15 |
+
**Crack the PyTorch interview.**
|
| 16 |
+
|
| 17 |
+
Practice implementing operators and architectures from scratch β the exact skills top ML teams test for.
|
| 18 |
+
|
| 19 |
+
*Like LeetCode, but for tensors. Self-hosted. Jupyter-based. Instant feedback.*
|
| 20 |
+
|
| 21 |
+
[](https://pytorch.org)
|
| 22 |
+
[](https://jupyter.org)
|
| 23 |
+
[](https://www.docker.com)
|
| 24 |
+
[](https://python.org)
|
| 25 |
+
[](LICENSE)
|
| 26 |
+
|
| 27 |
+
[](https://github.com/duoan/TorchCode)
|
| 28 |
+
[](https://ghcr.io/duoan/torchcode)
|
| 29 |
+
[](https://huggingface.co/spaces/duoan/TorchCode)
|
| 30 |
+

|
| 31 |
+

|
| 32 |
+
|
| 33 |
+
[](https://star-history.com/#duoan/TorchCode&Date)
|
| 34 |
+
|
| 35 |
+
</div>
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## π― Why TorchCode?
|
| 40 |
+
|
| 41 |
+
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.
|
| 42 |
+
|
| 43 |
+
TorchCode gives you a **structured practice environment** with:
|
| 44 |
+
|
| 45 |
+
| | Feature | |
|
| 46 |
+
|---|---|---|
|
| 47 |
+
| π§© | **40 curated problems** | The most frequently asked PyTorch interview topics |
|
| 48 |
+
| βοΈ | **Automated judge** | Correctness checks, gradient verification, and timing |
|
| 49 |
+
| π¨ | **Instant feedback** | Colored pass/fail per test case, just like competitive programming |
|
| 50 |
+
| π‘ | **Hints when stuck** | Nudges without full spoilers |
|
| 51 |
+
| π | **Reference solutions** | Study optimal implementations after your attempt |
|
| 52 |
+
| π | **Progress tracking** | What you've solved, best times, and attempt counts |
|
| 53 |
+
| π | **One-click reset** | Toolbar button to reset any notebook back to its blank template β practice the same problem as many times as you want |
|
| 54 |
+
| [](#) | **Open in Colab** | Every notebook has an "Open in Colab" badge + toolbar button β run problems in Google Colab with zero setup |
|
| 55 |
+
|
| 56 |
+
No cloud. No signup. No GPU needed. Just `make run` β or try it instantly on Hugging Face.
|
| 57 |
+
|
| 58 |
+
---
|
| 59 |
+
|
| 60 |
+
## π Quick Start
|
| 61 |
+
|
| 62 |
+
### Option 0 β Try it online (zero install)
|
| 63 |
+
|
| 64 |
+
**[Launch on Hugging Face Spaces](https://huggingface.co/spaces/duoan/TorchCode)** β opens a full JupyterLab environment in your browser. Nothing to install.
|
| 65 |
+
|
| 66 |
+
Or open any problem directly in Google Colab β every notebook has an [](https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/01_relu.ipynb) badge.
|
| 67 |
+
|
| 68 |
+
### Option 0b β Use the judge in Colab (pip)
|
| 69 |
+
|
| 70 |
+
In Google Colab, install the judge from PyPI so you can run `check(...)` without cloning the repo:
|
| 71 |
+
|
| 72 |
+
```bash
|
| 73 |
+
!pip install torch-judge
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
Then in a notebook cell:
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
from torch_judge import check, status, hint, reset_progress
|
| 80 |
+
status() # list all problems and your progress
|
| 81 |
+
check("relu") # run tests for the "relu" task
|
| 82 |
+
hint("relu") # show a hint
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### Option 1 β Pull the pre-built image (fastest)
|
| 86 |
+
|
| 87 |
+
```bash
|
| 88 |
+
docker run -p 8888:8888 -e PORT=8888 ghcr.io/duoan/torchcode:latest
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### Option 2 β Build locally
|
| 92 |
+
|
| 93 |
+
```bash
|
| 94 |
+
make run
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
Open **<http://localhost:8888>** β that's it. Works with both Docker and Podman (auto-detected).
|
| 98 |
+
|
| 99 |
+
---
|
| 100 |
+
|
| 101 |
+
## π Problem Set
|
| 102 |
+
|
| 103 |
+
> **Frequency**: π₯ = very likely in interviews, β = commonly asked, π‘ = emerging / differentiator
|
| 104 |
+
|
| 105 |
+
### π§± Fundamentals β "Implement X from scratch"
|
| 106 |
+
|
| 107 |
+
The bread and butter of ML coding interviews. You'll be asked to write these without `torch.nn`.
|
| 108 |
+
|
| 109 |
+
| # | Problem | What You'll Implement | Difficulty | Freq | Key Concepts |
|
| 110 |
+
|:---:|---------|----------------------|:----------:|:----:|--------------|
|
| 111 |
+
| 1 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/01_relu.ipynb" target="_blank">ReLU</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/01_relu.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `relu(x)` |  | π₯ | Activation functions, element-wise ops |
|
| 112 |
+
| 2 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/02_softmax.ipynb" target="_blank">Softmax</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/02_softmax.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)` |  | π₯ | Numerical stability, exp/log tricks |
|
| 113 |
+
| 16 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/16_cross_entropy.ipynb" target="_blank">Cross-Entropy Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/16_cross_entropy.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)` |  | π₯ | Log-softmax, logsumexp trick |
|
| 114 |
+
| 17 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/17_dropout.ipynb" target="_blank">Dropout</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/17_dropout.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `MyDropout` (nn.Module) |  | π₯ | Train/eval mode, inverted scaling |
|
| 115 |
+
| 18 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/18_embedding.ipynb" target="_blank">Embedding</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/18_embedding.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `MyEmbedding` (nn.Module) |  | π₯ | Lookup table, `weight[indices]` |
|
| 116 |
+
| 19 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/19_gelu.ipynb" target="_blank">GELU</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/19_gelu.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `my_gelu(x)` |  | β | Gaussian error linear unit, `torch.erf` |
|
| 117 |
+
| 20 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/20_weight_init.ipynb" target="_blank">Kaiming Init</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/20_weight_init.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `kaiming_init(weight)` |  | β | `std = sqrt(2/fan_in)`, variance scaling |
|
| 118 |
+
| 21 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/21_gradient_clipping.ipynb" target="_blank">Gradient Clipping</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/21_gradient_clipping.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)` |  | β | Norm-based clipping, direction preservation |
|
| 119 |
+
| 31 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/31_gradient_accumulation.ipynb" target="_blank">Gradient Accumulation</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/31_gradient_accumulation.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, ...)` |  | π‘ | Micro-batching, loss scaling |
|
| 120 |
+
| 40 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/40_linear_regression.ipynb" target="_blank">Linear Regression</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/40_linear_regression.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `LinearRegression` (3 methods) |  | π₯ | Normal equation, GD from scratch, nn.Linear |
|
| 121 |
+
| 3 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/03_linear.ipynb" target="_blank">Linear Layer</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/03_linear.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `SimpleLinear` (nn.Module) |  | π₯ | `y = xW^T + b`, Kaiming init, `nn.Parameter` |
|
| 122 |
+
| 4 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/04_layernorm.ipynb" target="_blank">LayerNorm</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/04_layernorm.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, Ξ³, Ξ²)` |  | π₯ | Normalization, running stats, affine transform |
|
| 123 |
+
| 7 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/07_batchnorm.ipynb" target="_blank">BatchNorm</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/07_batchnorm.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, Ξ³, Ξ²)` |  | β | Batch vs layer statistics, train/eval behavior |
|
| 124 |
+
| 8 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/08_rmsnorm.ipynb" target="_blank">RMSNorm</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/08_rmsnorm.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)` |  | β | LLaMA-style norm, simpler than LayerNorm |
|
| 125 |
+
| 15 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/15_mlp.ipynb" target="_blank">SwiGLU MLP</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/15_mlp.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `SwiGLUMLP` (nn.Module) |  | β | Gated FFN, `SiLU(gate) * up`, LLaMA/Mistral-style |
|
| 126 |
+
| 22 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/22_conv2d.ipynb" target="_blank">Conv2d</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/22_conv2d.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, ...)` |  | π₯ | Convolution, unfold, stride/padding |
|
| 127 |
+
|
| 128 |
+
### π§ Attention Mechanisms β The heart of modern ML interviews
|
| 129 |
+
|
| 130 |
+
If you're interviewing for any role touching LLMs or Transformers, expect at least one of these.
|
| 131 |
+
|
| 132 |
+
| # | Problem | What You'll Implement | Difficulty | Freq | Key Concepts |
|
| 133 |
+
|:---:|---------|----------------------|:----------:|:----:|--------------|
|
| 134 |
+
| 23 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/23_cross_attention.ipynb" target="_blank">Cross-Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/23_cross_attention.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `MultiHeadCrossAttention` (nn.Module) |  | β | Encoder-decoder, Q from decoder, K/V from encoder |
|
| 135 |
+
| 5 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/05_attention.ipynb" target="_blank">Scaled Dot-Product Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/05_attention.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)` |  | π₯ | `softmax(QK^T/βd_k)V`, the foundation of everything |
|
| 136 |
+
| 6 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/06_multihead_attention.ipynb" target="_blank">Multi-Head Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/06_multihead_attention.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `MultiHeadAttention` (nn.Module) |  | π₯ | Parallel heads, split/concat, projection matrices |
|
| 137 |
+
| 9 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/09_causal_attention.ipynb" target="_blank">Causal Self-Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/09_causal_attention.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)` |  | π₯ | Autoregressive masking with `-inf`, GPT-style |
|
| 138 |
+
| 10 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/10_gqa.ipynb" target="_blank">Grouped Query Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/10_gqa.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `GroupQueryAttention` (nn.Module) |  | β | GQA (LLaMA 2), KV sharing across heads |
|
| 139 |
+
| 11 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/11_sliding_window.ipynb" target="_blank">Sliding Window Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/11_sliding_window.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)` |  | β | Mistral-style local attention, O(nΒ·w) complexity |
|
| 140 |
+
| 12 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/12_linear_attention.ipynb" target="_blank">Linear Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/12_linear_attention.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)` |  | π‘ | Kernel trick, `Ο(Q)(Ο(K)^TV)`, O(nΒ·dΒ²) |
|
| 141 |
+
| 14 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/14_kv_cache.ipynb" target="_blank">KV Cache Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/14_kv_cache.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `KVCacheAttention` (nn.Module) |  | π₯ | Incremental decoding, cache K/V, prefill vs decode |
|
| 142 |
+
| 24 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/24_rope.ipynb" target="_blank">RoPE</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/24_rope.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)` |  | π₯ | Rotary position embedding, relative position via rotation |
|
| 143 |
+
| 25 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/25_flash_attention.ipynb" target="_blank">Flash Attention</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/25_flash_attention.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)` |  | π‘ | Tiled attention, online softmax, memory-efficient |
|
| 144 |
+
|
| 145 |
+
### ποΈ Architecture & Adaptation β Put it all together
|
| 146 |
+
|
| 147 |
+
| # | Problem | What You'll Implement | Difficulty | Freq | Key Concepts |
|
| 148 |
+
|:---:|---------|----------------------|:----------:|:----:|--------------|
|
| 149 |
+
| 26 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/26_lora.ipynb" target="_blank">LoRA</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/26_lora.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `LoRALinear` (nn.Module) |  | β | Low-rank adaptation, frozen base + `BA` update |
|
| 150 |
+
| 27 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/27_vit_patch.ipynb" target="_blank">ViT Patch Embedding</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/27_vit_patch.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `PatchEmbedding` (nn.Module) |  | π‘ | Image β patches β linear projection |
|
| 151 |
+
| 13 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/13_gpt2_block.ipynb" target="_blank">GPT-2 Block</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/13_gpt2_block.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `GPT2Block` (nn.Module) |  | β | Pre-norm, causal MHA + MLP (4x, GELU), residual connections |
|
| 152 |
+
| 28 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/28_moe.ipynb" target="_blank">Mixture of Experts</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/28_moe.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `MixtureOfExperts` (nn.Module) |  | β | Mixtral-style, top-k routing, expert MLPs |
|
| 153 |
+
|
| 154 |
+
### βοΈ Training & Optimization
|
| 155 |
+
|
| 156 |
+
| # | Problem | What You'll Implement | Difficulty | Freq | Key Concepts |
|
| 157 |
+
|:---:|---------|----------------------|:----------:|:----:|--------------|
|
| 158 |
+
| 29 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/29_adam.ipynb" target="_blank">Adam Optimizer</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/29_adam.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `MyAdam` |  | β | Momentum + RMSProp, bias correction |
|
| 159 |
+
| 30 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/30_cosine_lr.ipynb" target="_blank">Cosine LR Scheduler</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/30_cosine_lr.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, ...)` |  | β | Linear warmup + cosine annealing |
|
| 160 |
+
|
| 161 |
+
### π― Inference & Decoding
|
| 162 |
+
|
| 163 |
+
| # | Problem | What You'll Implement | Difficulty | Freq | Key Concepts |
|
| 164 |
+
|:---:|---------|----------------------|:----------:|:----:|--------------|
|
| 165 |
+
| 32 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/32_topk_sampling.ipynb" target="_blank">Top-k / Top-p Sampling</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/32_topk_sampling.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, ...)` |  | π₯ | Nucleus sampling, temperature scaling |
|
| 166 |
+
| 33 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/33_beam_search.ipynb" target="_blank">Beam Search</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/33_beam_search.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, ...)` |  | π₯ | Hypothesis expansion, pruning, eos handling |
|
| 167 |
+
| 34 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/34_speculative_decoding.ipynb" target="_blank">Speculative Decoding</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/34_speculative_decoding.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, ...)` |  | π‘ | Accept/reject, draft model acceleration |
|
| 168 |
+
|
| 169 |
+
### π¬ Advanced β Differentiators
|
| 170 |
+
|
| 171 |
+
| # | Problem | What You'll Implement | Difficulty | Freq | Key Concepts |
|
| 172 |
+
|:---:|---------|----------------------|:----------:|:----:|--------------|
|
| 173 |
+
| 35 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/35_bpe.ipynb" target="_blank">BPE Tokenizer</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/35_bpe.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `SimpleBPE` |  | π‘ | Byte-pair encoding, merge rules, subword splits |
|
| 174 |
+
| 36 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/36_int8_quantization.ipynb" target="_blank">INT8 Quantization</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/36_int8_quantization.ipynb" target="_blank"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" height="20"></a> | `Int8Linear` (nn.Module) |  | π‘ | Per-channel quantize, scale/zero-point, buffer vs param |
|
| 175 |
+
| 37 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/37_dpo_loss.ipynb" target="_blank">DPO Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/37_dpo_loss.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, ...)` |  | π‘ | Direct preference optimization, alignment training |
|
| 176 |
+
| 38 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/38_grpo_loss.ipynb" target="_blank">GRPO Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/38_grpo_loss.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)` |  | π‘ | Group relative policy optimization, RLAIF, within-group normalized advantages |
|
| 177 |
+
| 39 | <a href="https://github.com/duoan/TorchCode/blob/master/templates/39_ppo_loss.ipynb" target="_blank">PPO Loss</a> <a href="https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/39_ppo_loss.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)` |  | π‘ | PPO clipped surrogate loss, policy gradient, trust region |
|
| 178 |
+
|
| 179 |
+
---
|
| 180 |
+
|
| 181 |
+
## βοΈ How It Works
|
| 182 |
+
|
| 183 |
+
Each problem has **two** notebooks:
|
| 184 |
+
|
| 185 |
+
| File | Purpose |
|
| 186 |
+
|------|---------|
|
| 187 |
+
| `01_relu.ipynb` | βοΈ Blank template β write your code here |
|
| 188 |
+
| `01_relu_solution.ipynb` | π Reference solution β check when stuck |
|
| 189 |
+
|
| 190 |
+
### Workflow
|
| 191 |
+
|
| 192 |
+
```text
|
| 193 |
+
1. Open a blank notebook β Read the problem description
|
| 194 |
+
2. Implement your solution β Use only basic PyTorch ops
|
| 195 |
+
3. Debug freely β print(x.shape), check gradients, etc.
|
| 196 |
+
4. Run the judge cell β check("relu")
|
| 197 |
+
5. See instant colored feedback β β
pass / β fail per test case
|
| 198 |
+
6. Stuck? Get a nudge β hint("relu")
|
| 199 |
+
7. Review the reference solution β 01_relu_solution.ipynb
|
| 200 |
+
8. Click π Reset in the toolbar β Blank slate β practice again!
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
### In-Notebook API
|
| 204 |
+
|
| 205 |
+
```python
|
| 206 |
+
from torch_judge import check, hint, status
|
| 207 |
+
|
| 208 |
+
check("relu") # Judge your implementation
|
| 209 |
+
hint("causal_attention") # Get a hint without full spoiler
|
| 210 |
+
status() # Progress dashboard β solved / attempted / todo
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
---
|
| 214 |
+
|
| 215 |
+
## π
Suggested Study Plan
|
| 216 |
+
|
| 217 |
+
> **Total: ~12β16 hours spread across 3β4 weeks. Perfect for interview prep on a deadline.**
|
| 218 |
+
|
| 219 |
+
| Week | Focus | Problems | Time |
|
| 220 |
+
|:----:|-------|----------|:----:|
|
| 221 |
+
| **1** | π§± Foundations | ReLU β Softmax β CE Loss β Dropout β Embedding β GELU β Linear β LayerNorm β BatchNorm β RMSNorm β SwiGLU MLP β Conv2d | 2β3 hrs |
|
| 222 |
+
| **2** | π§ Attention Deep Dive | SDPA β MHA β Cross-Attn β Causal β GQA β KV Cache β Sliding Window β RoPE β Linear Attn β Flash Attn | 3β4 hrs |
|
| 223 |
+
| **3** | ποΈ Architecture + Training | GPT-2 Block β LoRA β MoE β ViT Patch β Adam β Cosine LR β Grad Clip β Grad Accumulation β Kaiming Init | 3β4 hrs |
|
| 224 |
+
| **4** | π― Inference + Advanced | Top-k/p Sampling β Beam Search β Speculative Decoding β BPE β INT8 Quant β DPO Loss β GRPO Loss β PPO Loss + speed run | 3β4 hrs |
|
| 225 |
+
|
| 226 |
+
---
|
| 227 |
+
|
| 228 |
+
## ποΈ Architecture
|
| 229 |
+
|
| 230 |
+
```text
|
| 231 |
+
ββββββββββββββββββββββββββββββββββββββββββββ
|
| 232 |
+
β Docker / Podman Container β
|
| 233 |
+
β β
|
| 234 |
+
β JupyterLab (:8888) β
|
| 235 |
+
β βββ templates/ (reset on each run) β
|
| 236 |
+
β βββ solutions/ (reference impl) β
|
| 237 |
+
β βββ torch_judge/ (auto-grading) β
|
| 238 |
+
β βββ torchcode-labext (JLab plugin) β
|
| 239 |
+
β β π Reset β restore template β
|
| 240 |
+
β β π Colab β open in Colab β
|
| 241 |
+
β βββ PyTorch (CPU), NumPy β
|
| 242 |
+
β β
|
| 243 |
+
β Judge checks: β
|
| 244 |
+
β β Output correctness (allclose) β
|
| 245 |
+
β β Gradient flow (autograd) β
|
| 246 |
+
β β Shape consistency β
|
| 247 |
+
β β Edge cases & numerical stability β
|
| 248 |
+
ββββββββββββββββββββββββββββββββββββββββββββ
|
| 249 |
+
```
|
| 250 |
+
|
| 251 |
+
Single container. Single port. No database. No frontend framework. No GPU.
|
| 252 |
+
|
| 253 |
+
## π οΈ Commands
|
| 254 |
+
|
| 255 |
+
```bash
|
| 256 |
+
make run # Build & start (http://localhost:8888)
|
| 257 |
+
make stop # Stop the container
|
| 258 |
+
make clean # Stop + remove volumes + reset all progress
|
| 259 |
+
```
|
| 260 |
+
|
| 261 |
+
## π§© Adding Your Own Problems
|
| 262 |
+
|
| 263 |
+
TorchCode uses auto-discovery β just drop a new file in `torch_judge/tasks/`:
|
| 264 |
+
|
| 265 |
+
```python
|
| 266 |
+
TASK = {
|
| 267 |
+
"id": "my_task",
|
| 268 |
+
"title": "My Custom Problem",
|
| 269 |
+
"difficulty": "medium",
|
| 270 |
+
"function_name": "my_function",
|
| 271 |
+
"hint": "Think about broadcasting...",
|
| 272 |
+
"tests": [ ... ],
|
| 273 |
+
}
|
| 274 |
+
```
|
| 275 |
+
|
| 276 |
+
No registration needed. The judge picks it up automatically.
|
| 277 |
+
|
| 278 |
+
---
|
| 279 |
+
|
| 280 |
+
## π¦ Publishing `torch-judge` to PyPI (maintainers)
|
| 281 |
+
|
| 282 |
+
The judge is published as a separate package so Colab/users can `pip install torch-judge` without cloning the repo.
|
| 283 |
+
|
| 284 |
+
### Automatic (GitHub Action)
|
| 285 |
+
|
| 286 |
+
Pushing to `master` after changing the package version triggers [`.github/workflows/pypi-publish.yml`](.github/workflows/pypi-publish.yml), which builds and uploads to PyPI. No git tag is required.
|
| 287 |
+
|
| 288 |
+
1. **Bump version** in `torch_judge/_version.py` (e.g. `__version__ = "0.1.1"`).
|
| 289 |
+
2. **Configure PyPI Trusted Publisher** (one-time):
|
| 290 |
+
- PyPI β Your project **torch-judge** β **Publishing** β **Add a new pending publisher**
|
| 291 |
+
- Owner: `duoan`, Repository: `TorchCode`, Workflow: `pypi-publish.yml`, Environment: (leave empty)
|
| 292 |
+
- 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.
|
| 293 |
+
3. **Release**: commit the version bump and `git push origin master`.
|
| 294 |
+
|
| 295 |
+
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.
|
| 296 |
+
|
| 297 |
+
### Manual
|
| 298 |
+
|
| 299 |
+
```bash
|
| 300 |
+
pip install build twine
|
| 301 |
+
python -m build
|
| 302 |
+
twine upload dist/*
|
| 303 |
+
```
|
| 304 |
+
|
| 305 |
+
Version is in `torch_judge/_version.py`; bump it before each release.
|
| 306 |
+
|
| 307 |
+
---
|
| 308 |
+
|
| 309 |
+
## β FAQ
|
| 310 |
+
|
| 311 |
+
<details>
|
| 312 |
+
<summary><b>Do I need a GPU?</b></summary>
|
| 313 |
+
<br>
|
| 314 |
+
No. Everything runs on CPU. The problems test correctness and understanding, not throughput.
|
| 315 |
+
</details>
|
| 316 |
+
|
| 317 |
+
<details>
|
| 318 |
+
<summary><b>Can I keep my solutions between runs?</b></summary>
|
| 319 |
+
<br>
|
| 320 |
+
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.
|
| 321 |
+
</details>
|
| 322 |
+
|
| 323 |
+
<details>
|
| 324 |
+
<summary><b>Can I use Google Colab instead?</b></summary>
|
| 325 |
+
<br>
|
| 326 |
+
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.
|
| 327 |
+
</details>
|
| 328 |
+
|
| 329 |
+
<details>
|
| 330 |
+
<summary><b>How are solutions graded?</b></summary>
|
| 331 |
+
<br>
|
| 332 |
+
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.
|
| 333 |
+
</details>
|
| 334 |
+
|
| 335 |
+
<details>
|
| 336 |
+
<summary><b>Who is this for?</b></summary>
|
| 337 |
+
<br>
|
| 338 |
+
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.
|
| 339 |
+
</details>
|
| 340 |
+
|
| 341 |
+
---
|
| 342 |
+
|
| 343 |
+
## π€ Contributors
|
| 344 |
+
|
| 345 |
+
Thanks to everyone who has contributed to TorchCode.
|
| 346 |
+
|
| 347 |
+
<!-- readme: contributors -start -->
|
| 348 |
+
<!-- readme: contributors -end -->
|
| 349 |
+
|
| 350 |
+
Auto-generated from the [GitHub contributors graph](https://github.com/duoan/TorchCode/graphs/contributors) with avatars and GitHub usernames.
|
| 351 |
+
|
| 352 |
+
---
|
| 353 |
+
|
| 354 |
+
<div align="center">
|
| 355 |
+
|
| 356 |
+
**Built for engineers who want to deeply understand what they build.**
|
| 357 |
+
|
| 358 |
+
If this helped your interview prep, consider giving it a β
|
| 359 |
+
|
| 360 |
+
---
|
| 361 |
+
|
| 362 |
+
### β Buy Me a Coffee
|
| 363 |
+
|
| 364 |
+
<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>
|
| 365 |
+
|
| 366 |
+
<img src="./bmc_qr.png" alt="BMC QR Code" width="150" height="150">
|
| 367 |
+
|
| 368 |
+
*Scan to support*
|
| 369 |
+
|
| 370 |
+
</div>
|