Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ce3d4e4
New Question Set
Exorust Feb 15, 2025
5b1d80a
Init Multi-Head-Attention
Exorust Feb 17, 2025
882df6d
Sinusoidal Embeddings
Exorust Feb 18, 2025
f855415
Update README.md
Exorust Apr 15, 2025
6712c9a
Rope
Exorust May 8, 2025
3afa6ec
Changes
Exorust May 18, 2025
d854e1e
BPE
Exorust May 18, 2025
c082efb
added smollm-q12
bargav25 May 22, 2025
eaae575
added smollm-q12
bargav25 May 22, 2025
179be37
Merge pull request #10 from bargav25/new-llm
Exorust May 22, 2025
7e13622
Update attention-q4.ipynb
AtulAravindDas May 23, 2025
851501b
Update embeddings-q2.ipynb
AtulAravindDas May 23, 2025
469bce5
Update rope-q8.ipynb
AtulAravindDas May 23, 2025
700be16
Update embeddings-q2.ipynb
AtulAravindDas May 23, 2025
ba43be7
Update sinusoidal-q7.ipynb
AtulAravindDas May 23, 2025
a387e95
Update grouped-query-attention.ipynb
AtulAravindDas May 23, 2025
77b4993
Update multi-head-attention-q5.ipynb
AtulAravindDas May 23, 2025
ff5b96d
Update attention-q4.ipynb
AtulAravindDas May 23, 2025
bb029f9
Merge pull request #11 from AtulAravindDas/new-llm
Exorust May 23, 2025
f7ccb8c
Update README.md
AtulAravindDas May 23, 2025
52fc438
Added files into required folders, updated the README.md, created que…
AtulAravindDas May 24, 2025
f3ca103
Update README.md
AtulAravindDas May 24, 2025
9677cd1
Added the three dots
AtulAravindDas May 24, 2025
25304cd
Merge pull request #12 from AtulAravindDas/new-llm
Exorust May 24, 2025
ced93d6
Clean up
Exorust May 24, 2025
3d574f6
Cleanup
Exorust May 24, 2025
bdacc7e
Cleanup lstm problem
Exorust May 24, 2025
6934127
Question Update
Exorust May 25, 2025
b14d9e4
new q
Exorust May 25, 2025
463e87d
From Scratch Implementation RNN
Exorust May 25, 2025
5bd47c5
Updated Question Set
Exorust May 25, 2025
9283b67
New Ordering in README
Exorust May 26, 2025
ea57d47
Refactoring
Exorust May 26, 2025
c06e1ce
Adding CNN Scratch
Exorust May 26, 2025
1cbe3f7
AlexNet
Exorust May 26, 2025
629e73f
Link updates
Exorust May 26, 2025
2be72e8
Changes
Exorust May 26, 2025
60d933e
Transformer change
Exorust May 26, 2025
2c22fb0
New README
Exorust May 26, 2025
12f27e8
Readme Cleanup
Exorust May 26, 2025
6a8cda9
Cleanup
Exorust May 26, 2025
7295290
Q2 update
Exorust May 26, 2025
b0275e2
Clean up
Exorust May 31, 2025
d2481a4
added flash attention forward
bargav25 Jun 6, 2025
86aa561
Merge pull request #13 from bargav25/new-llm
Exorust Jun 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CheatSheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Cheat Sheet for PyTorch

142 changes: 102 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<div align="center">
<img src="torch.png" alt="Robot Image">
<img src="torchleet-llm.png" alt="Robot Image">
<!-- <h1>TorchLeet</h1> -->
<p align="center">
🐦 <a href="https://twitter.com/charoori_ai">Follow me on Twitter</a> •
➡️ <a href="https://github.com/Exorust/TorchLeet/tree/new-llm?tab=readme-ov-file#llm-set">Jump to LLMs!</a>
📧 <a href="mailto:chandrahas.aroori@gmail.com?subject=LLM%20Cookbook">Feedback</a>
</p>
<p>
Expand All @@ -11,55 +12,118 @@
</div>
<br/>

TorchLeet is a curated set of PyTorch practice problems, inspired by LeetCode-style challenges, designed to enhance your skills in deep learning and PyTorch.
TorchLeet is broken into two sets of questions:
1. **Question Set**: A collection of PyTorch practice problems, ranging from basic to hard, designed to enhance your skills in deep learning and PyTorch.
2. **LLM Set**: A new set of questions focused on understanding and implementing Large Language Models (LLMs) from scratch, including attention mechanisms, embeddings, and more.
3.
> [!NOTE]
> Avoid using GPT. Try to solve these problems on your own. The goal is to learn and understand PyTorch concepts deeply.

## Table of Contents
- [TorchLeet](#torchleet)
- [Table of Contents](#table-of-contents)
- [Question Set](#question-set)
- [🟢Easy](#easy)
- [🟡Medium](#medium)
- [🔴Hard](#hard)
- [Getting Started](#getting-started)
- [1. Install Dependencies](#1-install-dependencies)
- [2. Structure](#2-structure)
- [3. How to Use](#3-how-to-use)
- [Question Set](#question-set)
- [🔵Basic](#basic)
- [🟢Easy](#easy)
- [🟡Medium](#medium)
- [🔴Hard](#hard)
- [LLM Set](#llm-set)
- [Getting Started](#getting-started)
- [1. Install Dependencies](#1-install-dependencies)
- [2. Structure](#2-structure)
- [3. How to Use](#3-how-to-use)
- [Contribution](#contribution)
- [Authors:](#authors)


## Question Set

### 🟢Easy
1. [Implement linear regression](https://github.com/Exorust/TorchLeet/blob/main/e1/lin-regression.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e1/lin-regression_SOLN.ipynb)
2. [Write a custom Dataset and Dataloader to load from a CSV file](https://github.com/Exorust/TorchLeet/blob/main/e2/custom-dataset.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e2/custom-dataset_SOLN.ipynb)
3. [Write a custom activation function (Simple)](https://github.com/Exorust/TorchLeet/blob/main/e3/custom-activation.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e3/custom-activation_SOLN.ipynb)
4. [Implement Custom Loss Function (Huber Loss)](https://github.com/Exorust/TorchLeet/blob/main/e4/custom-loss.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e4/custom-loss_SOLN.ipynb)
5. [Implement a Deep Neural Network](https://github.com/Exorust/TorchLeet/blob/main/e5/custon-DNN.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e5/custon-DNN_SOLN.ipynb)
6. [Visualize Training Progress with TensorBoard in PyTorch](https://github.com/Exorust/TorchLeet/blob/main/e6/tensorboard.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e6/tensorboard_SOLN.ipynb)
7. [Save and Load Your PyTorch Model](https://github.com/Exorust/TorchLeet/blob/main/e7/save_model.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/e7/save_model_SOLN.ipynb)
### 🔵Basic
Mostly for beginners to get started with PyTorch.

1. [Implement linear regression](torch/basic/lin-regression/lin-regression.ipynb) [(Solution)](torch/basic/lin-regression/lin-regression_SOLN.ipynb)
2. [Write a custom Dataset and Dataloader to load from a CSV file](torch/basic/custom-dataset/custom-dataset.ipynb) [(Solution)](torch/basic/custom-dataset/custom-dataset_SOLN.ipynb)
3. [Write a custom activation function (Simple)](torch/easy/e3/custom-activation.ipynb) [(Solution)](torch/easy/e3/custom-activation_SOLN.ipynb)
4. [Implement Custom Loss Function (Huber Loss)](torch/easy/e4/custom-loss.ipynb) [(Solution)](torch/easy/e4/custom-loss_SOLN.ipynb)
5. [Implement a Deep Neural Network](torch/easy/e5/custon-DNN.ipynb) [(Solution)](torch/easy/e5/custon-DNN_SOLN.ipynb)
6. [Visualize Training Progress with TensorBoard in PyTorch](torch/easy/e6/tensorboard.ipynb) [(Solution)](torch/easy/e6/tensorboard_SOLN.ipynb)
7. [Save and Load Your PyTorch Model](torch/easy/e7/save_model.ipynb) [(Solution)](torch/easy/e7/save_model_SOLN.ipynb)
10. Implement Softmax function from scratch

---

### 🟢Easy
Recommended for those who have a basic understanding of PyTorch and want to practice their skills.
1. [Implement a CNN on CIFAR-10](torch/medium/m2/CNN.ipynb) [(Solution)](torch/medium/m2/CNN_SOLN.ipynb)
2. [Implement an RNN from Scratch](torch/medium/m3/RNN.ipynb) [(Solution)](torch/medium/m3/RNN_SOLN.ipynb)
3. [Use `torchvision.transforms` to apply data augmentation](torch/medium/m4/augmentation.ipynb) [(Solution)](torch/medium/m4/augmentation_SOLN.ipynb)
4. [Add a benchmark to your PyTorch code](torch/medium/m5/bench.ipynb) [(Solution)](torch/medium/m5/bench_SOLN.ipynb)
5. [Train an autoencoder for anomaly detection](torch/medium/m6/autoencoder.ipynb) [(Solution)](torch/medium/m6/autoencoder_SOLN.ipynb)
6. [Quantize your language model](torch/hard/h6/quantize-language-model.ipynb) [(Solution)](torch/hard/h6/quantize-language-model_SOLN.ipynb)
7. [Implement Mixed Precision Training using torch.cuda.amp](torch/hard/h9/cuda-amp.ipynb) [(Solution)](torch/hard/h9/cuda-amp_SOLN.ipynb)

---

### 🟡Medium
1. [Implement an LSTM](https://github.com/Exorust/TorchLeet/blob/main/m1/LSTM.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/m1/LSTM_SOLN.ipynb)
2. [Implement a CNN on CIFAR-10](https://github.com/Exorust/TorchLeet/blob/main/m2/CNN.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/m2/CNN_SOLN.ipynb)
3. [Implement parameter initialization for a CNN]() [(Solution)]()
4. [Implement an RNN](https://github.com/Exorust/TorchLeet/blob/main/m3/RNN.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/m3/RNN_SOLN.ipynb)
5. [Use `torchvision.transforms` to apply data augmentation](https://github.com/Exorust/TorchLeet/blob/main/m4/augmentation.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/m4/augmentation_SOLN.ipynb)
6. [Add a benchmark to your PyTorch code](https://github.com/Exorust/TorchLeet/blob/main/m5/bench.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/m5/bench_SOLN.ipynb)
7. [Train an autoencoder for anomaly detection](https://github.com/Exorust/TorchLeet/blob/main/m6/autoencoder.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/m6/autoencoder_SOLN.ipynb)
These problems are designed to challenge your understanding of PyTorch and deep learning concepts. They require you to implement things from scratch or apply advanced techniques.
1. [Implement parameter initialization for a CNN]() [(Solution)]()
2. [Implement a CNN from Scratch]()
3. [Implement an LSTM from Scratch](torch/medium/m1/LSTM.ipynb) [(Solution)](torch/medium/m1/LSTM_SOLN.ipynb)
4. Implement AlexNet from scratch
5. Build a Dense Retrieval System using PyTorch
6. Implement KNN from scratch in PyTorch

---

### 🔴Hard
1. [Write a custom Autograd function for activation (SILU)](https://github.com/Exorust/TorchLeet/blob/main/h1/custom-autgrad-function.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h1/custom-autgrad-function_SOLN.ipynb)
These problems are for advanced users who want to push their PyTorch skills to the limit. They involve complex architectures, custom layers, and advanced techniques.
1. [Write a custom Autograd function for activation (SILU)](torch/hard/h1/custom-autgrad-function.ipynb) [(Solution)](torch/hard/h1/custom-autgrad-function_SOLN.ipynb)
2. Write a Neural Style Transfer
3. [Write a Transformer](https://github.com/Exorust/TorchLeet/blob/main/h3/transformer.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h3/transformer_SOLN.ipynb)
4. [Write a GAN](https://github.com/Exorust/TorchLeet/blob/main/h4/GAN.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h4/GAN_SOLN.ipynb)
5. [Write Sequence-to-Sequence with Attention](https://github.com/Exorust/TorchLeet/blob/main/h5/seq-to-seq-with-Attention.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h5/seq-to-seq-with-Attention_SOLN.ipynb)
6. [Quantize your language model](https://github.com/Exorust/TorchLeet/blob/main/h6/quantize-language-model.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h6/quantize-language-model_SOLN.ipynb)
7. [Enable distributed training in pytorch (DistributedDataParallel)]
8. [Work with Sparse Tensors]
9. [Implement Mixed Precision Training using torch.cuda.amp](https://github.com/Exorust/TorchLeet/blob/main/h9/cuda-amp.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h9/cuda-amp_SOLN.ipynb)
10. [Add GradCam/SHAP to explain the model.](https://github.com/Exorust/TorchLeet/blob/main/h10/xai.ipynb) [(Solution)](https://github.com/Exorust/TorchLeet/blob/main/h10/xai_SOLN.ipynb)
3. Build a Graph Neural Network (GNN) from scratch
4. Build a Graph Convolutional Network (GCN) from scratch
5. [Write a Transformer](torch/hard/h3/transformer.ipynb) [(Solution)](torch/hard/h3/transformer_SOLN.ipynb)
6. [Write a GAN](torch/hard/h4/GAN.ipynb) [(Solution)](torch/hard/h4/GAN_SOLN.ipynb)
7. [Write Sequence-to-Sequence with Attention](torch/hard/h5/seq-to-seq-with-Attention.ipynb) [(Solution)](torch/hard/h5/seq-to-seq-with-Attention_SOLN.ipynb)
8. [Enable distributed training in pytorch (DistributedDataParallel)]
9. [Work with Sparse Tensors]
10. [Add GradCam/SHAP to explain the model.](torch/hard/h10/xai.ipynb) [(Solution)](torch/hard/h10/xai_SOLN.ipynb)
11. Linear Probe on CLIP Features
12. Add Cross Modal Embedding Visualization to CLIP (t-SNE/UMAP)
13. Implement a Vision Transformer
14. Implement a Variational Autoencoder

---

## LLM Set

**An all new set of questions to help you understand and implement Large Language Models from scratch.**

Each question is designed to take you one step closer to building your own LLM.

1. Implement KL Divergence Loss
2. Implement RMS Norm
3. [Implement Byte Pair Encoding from Scratch](llm/Byte-Pair-Encoding/BPE-q3-Question.ipynb) [(Solution)](llm/Byte-Pair-Encoding/BPE-q3.ipynb)
4. Create a RAG Search of Embeddings from a set of Reviews
5. Implement Predictive Prefill with Speculative Decoding
6. [Implement Attention from Scratch](llm/Implement-Attention-from-Scratch/attention-q4-Question.ipynb) [(Solution)](llm/Implement-Attention-from-Scratch/attention-q4.ipynb)
7. [Implement Multi-Head Attention from Scratch](llm/Multi-Head-Attention/multi-head-attention-q5-Question.ipynb) [(Solution)](llm/Multi-Head-Attention/multi-head-attention-q5.ipynb)
8. [Implement Grouped Query Attention from Scratch](llm/Grouped-Query-Attention/grouped-query-attention-Question.ipynb) [(Solution)](llm/Grouped-Query-Attention/grouped-query-attention.ipynb)
9. Implement KV Cache in Multi-Head Attention from Scratch
10. [Implement Sinusoidal Embeddings](llm/Sinusoidal-Positional-Embedding/sinusoidal-q7-Question.ipynb) [(Solution)](llm/Sinusoidal-Positional-Embedding/sinusoidal-q7.ipynb)
11. [Implement ROPE Embeddings](llm/Rotary-Positional-Embedding/rope-q8-Question.ipynb) [(Solution)](llm/Rotary-Positional-Embedding/rope-q8.ipynb)
12. [Implement SmolLM from Scratch](llm/SmolLM/smollm-q12-Question.ipynb) [(Solution)](llm/SmolLM/smollm-q12.ipynb)
13. Implement Quantization of Models
1. GPTQ
14. Implement Beam Search atop LLM for decoding
15. Implement Top K Sampling atop LLM for decoding
16. Implement Top p Sampling atop LLM for decoding
17. Implement Temperature Sampling atop LLM for decoding
18. Implement LoRA on a layer of an LLM
1. QLoRA
19. Mix two models to create a mixture of Experts
20. Apply SFT on SmolLM
21. Apply RLHF on SmolLM
22. Implement DPO based RLHF
23. Add continous batching to your LLM
24. Chunk Textual Data for Dense Passage Retreival
25. Implement Lage scale Training => 5D Parallelism

**What's cool? 🚀**
- **Diverse Questions**: Covers beginner to advanced PyTorch concepts (e.g., tensors, autograd, CNNs, GANs, and more).
Expand All @@ -84,10 +148,8 @@ TorchLeet is a curated set of PyTorch practice problems, inspired by LeetCode-st
**Happy Learning! 🚀**




# Contribution
Feel free to contribute by adding new questions or improving existing ones. Ensure that new problems are well-documented and follow the project structure.
Feel free to contribute by adding new questions or improving existing ones. Ensure that new problems are well-documented and follow the project structure. Submit a PR and tag the authors.

# Authors

Expand Down
9 changes: 9 additions & 0 deletions Tricks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Tricks

List of tricks in pytorch


## TorchScript
@torch.jit.scriptorch.jit.script

## Unit Tests in PyTorch
176 changes: 0 additions & 176 deletions h3/transformer_SOLN.ipynb

This file was deleted.

Loading
Loading