# triton **Repository Path**: shengbao/triton ## Basic Information - **Project Name**: triton - **Description**: triton learn - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-16 - **Last Updated**: 2026-02-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 01triton # Triton基础 ## Triton是什么 Triton:An Intermediate Language and Compiler for Tiled Neural Network Computations(一种用于分块神经网络计算的中间语言与编译器。) 关键字:基于python的DSL,用于GPU的分开神经网络,编译器。 ![image-20260216075853526](./README.assets/image-20260216075853526.png) 其中triton处于Kernel环节。triton的编译过程如下: ![image-20260216080158513](./README.assets/image-20260216080158513.png) Triton编译器可以兼容Pytorch等框架,通过inductor后端降级为Triton kernel,Pytorch中深入学习模型经过TorchDynamo捕获得到FX计算图,再经过inductor后端生成triton代码。 > Philippe Tillet;H. T. Kung;David Cox.Triton: an intermediate language and compiler for tiled neural network computations[A].MAPL 2019: Proceedings of the 3rd ACM SIGPLAN International Workshop on Machine Learning and Programming Languages[C],2019 > https://www.jokeren.tech/slides/triton_next.pdf > https://www.jokeren.tech/slides/Tritonbsc.pdf > https://openai.com/index/triton/ > https://superjomn.github.io/posts/triton-mlir-publish/ ## Triton安装 环境:Ubuntu 22.04,Python 3.10,Cuda 12.1,以安装目前最新版本triton3.0.0为例。 1. pip 安装:pip install triton ![image-20260216081522349](./README.assets/image-20260216081522349.png) 2. 源码安装 ``` git clone https://github.com/triton-lang/triton.git cd triton pip install ninja cmake wheel; # build-time dependencies pip install -e python ``` ![image-20260216081902263](./README.assets/image-20260216081902263.png) ![image-20260216081940530](./README.assets/image-20260216081940530.png) 注:需要在官网找到对应llvm的版本 ![image-20260216082117589](./README.assets/image-20260216082117589.png) ![image-20260216082135643](./README.assets/image-20260216082135643.png) ![image-20260216082152679](./README.assets/image-20260216082152679.png) # Triton编写程序 ## Triton语法 参考:https://triton-lang.org/main/index.html 常见的triton语法: ``` jit: Decorator for JIT-compiling a function using the Triton compiler. autotune: Decorator for auto-tuning a triton. jit 'd function. heuristics: Decorator for specifying how the values of certain meta-parameters may be computed. config: An object that represents a possible kernel configuration for the auto-tuner to try. ``` **jit** 一个jit装饰器,用于使用triton编译器,对函数进行jit编译。该函数将在gpu上运行。使用jit装饰的函数只能访问python基元、triton包里的内置函数、该函数参数、其他jit函数。 **Triton.autotune** 用于评估所有配置,内核将使用多次,最终使用最好的配置进行运行。Decorator for auto-tuning a triton.jit'd function. ``` @triton.autotune(configs=[ triton.Config(kwargs={'BLOCK_SIZE': 128}, num_warps=4), triton.Config(kwargs={'BLOCK_SIZE': 1024}, num_warps=8), ], key=['x_size'] # the two above configs will be evaluated anytime # the value of x_size changes ) @triton.jit def kernel(x_ptr, x_size, **META): BLOCK_SIZE = META['BLOCK_SIZE'] ```