Skip to content

Commit 7d97024

Browse files
authored
Update README.md
1 parent 2ff3ef1 commit 7d97024

File tree

1 file changed

+81
-18
lines changed

1 file changed

+81
-18
lines changed

08 Triton/README.md

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,90 @@
1-
# Triton
1+
# Triton: High-Level Deep Learning Programming Language
22

3-
## Design
3+
Triton is an advanced programming language designed to simplify GPU kernel development, especially for deep learning operations. By providing a higher level of abstraction compared to traditional CUDA, Triton allows Python developers to write highly optimized GPU kernels without delving into the complexities of CUDA's low-level programming. The following sections will explore Triton's design, how it compares to CUDA, and why it’s a valuable tool for deep learning practitioners.
44

5-
- CUDA -> scalar program + blocked threads
6-
- Triton -> blocked program + scalar threads
5+
## Design Philosophy
76

8-
![](../assets/triton1.png)
9-
![](../assets/triton2.png)
7+
### CUDA vs Triton: Key Differences
108

11-
Blocked program + scalar threads (Triton) vs scalar program + blocked threads (CUDA)
12-
- cuda is a scalar program with blocked threads because we write a kernel to operate at the level of threads (scalars), whereas triton is abstracted up to thread blocks (compiler takes care of thread level operations for us)
13-
- cuda has blocked threads in the context of "worrying" about inter-thread at the level of blocks, whereas triton has scalar threads in the context of "not worrying" about inter-thread at the level of threads (compiler also takes care of this)
9+
#### CUDA:
10+
- **Scalar Program + Blocked Threads**: CUDA programs operate at the level of scalar threads. We write kernels for individual threads, and manage thread blocks ourselves. The execution of thread blocks and synchronization between them is left to the programmer, resulting in low-level control over GPU hardware but also requiring manual optimization and management.
1411

15-
Why does this actually mean on an intuitive level?
12+
#### Triton:
13+
- **Blocked Program + Scalar Threads**: Triton abstracts away the complexity of thread block management. Instead, it allows you to write high-level "blocked programs" while treating individual threads as "scalars." This means the programmer focuses only on high-level operations, while the compiler handles the underlying thread-level optimizations such as memory access, tiling, and parallelization.
1614

17-
- higher level of abstract for deep learning operations (activations functions, convolutions, matmul, etc)
18-
- the compiler will take care of boilerplate complexities of load and store instructions, tiling, SRAM caching, etc
19-
- python programmers can write kernels comparable to cuBLAS, cuDNN (very difficult for most CUDA/GPU programmers)
15+
### Visual Comparison
2016

21-
So can't we just skip CUDA and go straight to Triton.
17+
Below is a comparison of how the execution models differ between CUDA and Triton:
2218

23-
- Triton is an abstraction on top of CUDA
24-
- you may want to optimize your own kernels in CUDA
25-
- you need to understand the paradigms used in CUDA and related topics to understand how to build on top of triton.
19+
![Triton Design](../assets/triton1.png)
20+
![CUDA Design](../assets/triton2.png)
2621

27-
> Resources: [Paper](https://www.eecs.harvard.edu/~htk/publication/2019-mapl-tillet-kung-cox.pdf), [Docs](https://triton-lang.org/main/index.html), [OpenAI Blog Post](https://openai.com/index/triton/), [Github](https://github.com/triton-lang/triton)
22+
- **CUDA**: The kernel is written for individual threads (scalars), and these threads are grouped into thread blocks for parallel execution. The programmer must manage block-level synchronization and memory access.
23+
- **Triton**: The kernel is written at the level of thread blocks (blocked program), and the compiler handles thread-level operations for memory access, synchronization, and optimization.
24+
25+
### Intuitive Explanation: How Does This Affect You?
26+
27+
- **High-Level Abstraction for Deep Learning**: Triton simplifies the process of implementing complex operations such as activation functions, convolutions, and matrix multiplications by abstracting low-level details. This makes it easier for deep learning practitioners to write high-performance code without needing extensive knowledge of GPU architecture or CUDA programming.
28+
29+
- **Boilerplate Simplified**: The Triton compiler handles the intricate details of memory management, such as load/store instructions, tiling, and SRAM caching. This allows the developer to focus on the core logic of the computation, improving productivity.
30+
31+
- **Python-Friendly**: Triton allows Python developers to write highly optimized GPU code similar to cuBLAS or cuDNN libraries, making it accessible to those with minimal GPU programming experience.
32+
33+
## Why Not Just Skip CUDA and Use Triton?
34+
35+
While Triton abstracts many of the complexities of CUDA, it does not entirely replace it. Triton operates **on top of CUDA**, leveraging CUDA’s powerful GPU capabilities while simplifying its usage.
36+
37+
### When Should You Use CUDA?
38+
- If you need full control over the GPU, or want to write custom CUDA kernels optimized for your specific problem, CUDA provides the low-level flexibility required.
39+
- For specialized GPU optimization or when implementing non-standard operations, understanding CUDA and its paradigms will give you the power to build highly customized solutions.
40+
41+
### When Should You Use Triton?
42+
- If your goal is to quickly implement and optimize common deep learning operations (e.g., matrix multiplication, convolution) without getting bogged down by the complexities of GPU architecture.
43+
- If you are working in Python and want to leverage GPU acceleration with minimal setup, Triton is the perfect tool.
44+
45+
Triton is designed to let you skip the low-level details while still enabling fast, GPU-accelerated computation. It works well for deep learning tasks and can significantly reduce the time spent on writing custom kernels.
46+
47+
## Key Features
48+
49+
- **Python-Friendly**: Triton integrates well with Python, enabling data scientists and AI researchers to write optimized GPU kernels in a language they are already comfortable with.
50+
- **Simplified GPU Programming**: The language abstracts low-level GPU management, letting developers focus on implementing the logic of deep learning algorithms without worrying about thread-level synchronization and memory access patterns.
51+
- **Automatic Optimization**: The Triton compiler automatically applies optimizations such as loop unrolling, tiling, and memory coalescing, providing performance that’s often comparable to hand-optimized CUDA code.
52+
53+
## Example: Writing a Triton Kernel
54+
55+
```python
56+
import triton
57+
import triton.language as tl
58+
59+
@triton.jit
60+
def matmul_kernel(A, B, C, M, N, K, BLOCK_SIZE: tl.constexpr):
61+
# Define block and thread indexes
62+
pid = tl.program_id(0)
63+
row = pid // N
64+
col = pid % N
65+
66+
# Load matrices A and B into registers
67+
A_block = tl.load(A + row * K + tl.arange(0, K))
68+
B_block = tl.load(B + col * K + tl.arange(0, K))
69+
70+
# Perform matrix multiplication
71+
C_result = tl.dot(A_block, B_block)
72+
73+
# Store the result in C
74+
tl.store(C + row * N + col, C_result)
75+
```
76+
77+
In the example above, the programmer writes a simple matrix multiplication kernel using the Triton language, focusing only on the matrix operations themselves. The complexity of memory access and thread synchronization is automatically managed by the Triton compiler.
78+
79+
## Resources
80+
81+
For more information, you can explore the following resources:
82+
83+
- [Triton Lang Official Docs](https://triton-lang.org/main/index.html)
84+
- [Triton Paper - High-Performance Deep Learning Kernels](https://www.eecs.harvard.edu/~htk/publication/2019-mapl-tillet-kung-cox.pdf)
85+
- [OpenAI Blog Post on Triton](https://openai.com/index/triton/)
86+
- [Triton GitHub Repository](https://github.com/triton-lang/triton)
87+
88+
## Conclusion
89+
90+
Triton is an innovative and powerful tool for deep learning practitioners who want to optimize their GPU workloads with minimal effort. By abstracting the complexities of CUDA, Triton makes it easier to write high-performance, parallelized deep learning kernels in Python. However, understanding CUDA remains valuable for custom optimizations and understanding GPU programming paradigms, especially when working outside of standard deep learning operations.

0 commit comments

Comments
 (0)