<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Cuda/Triton on moyutianzun's blog</title><link>https://moyutianzun.cn/en/categories/cuda/triton/</link><description>Recent content in Cuda/Triton on moyutianzun's blog</description><generator>Hugo</generator><language>en</language><copyright>moyutianzun</copyright><lastBuildDate>Mon, 20 Oct 2025 15:18:01 +0800</lastBuildDate><atom:link href="https://moyutianzun.cn/en/categories/cuda/triton/index.xml" rel="self" type="application/rss+xml"/><item><title>CUDA profile 大全 —— nsight computer &amp; nsys &amp; pytorch</title><link>https://moyutianzun.cn/en/blog/cuda-profile/</link><pubDate>Mon, 20 Oct 2025 15:18:01 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/cuda-profile/</guid><description>&lt;h1 style="" id="cuda-api"&gt;Cuda API&lt;/h1&gt;&lt;p style=""&gt;创建对象：&lt;/p&gt;&lt;pre&gt;&lt;code&gt;#include &amp;lt;cuda_runtime.h&amp;gt;
#include &amp;lt;cuda.h&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
&lt;p&gt;// 获取当前机器的GPU数量
cudaError_t error_id = cudaGetDeviceCount(&amp;amp;deviceCount);&lt;/p&gt;
&lt;p&gt;for (int dev = 0; dev &amp;lt; deviceCount; ++dev) {
cudaSetDevice(dev);
// 初始化当前device的属性获取对象
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&amp;amp;deviceProp, dev);&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;printf(&amp;quot;\nDevice %d: \&amp;quot;%s\&amp;quot;\n&amp;quot;, dev, deviceProp.name);&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;p style=&amp;quot;&amp;quot;&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;p style=&amp;quot;&amp;quot;&amp;gt;拿到数据后可以查看对应feature&amp;lt;/p&amp;gt;&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;printf(&amp;quot; Total amount of shared memory per block: %zu bytes\n&amp;quot;, deviceProp.sharedMemPerBlock);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;printf(&amp;quot; Total shared memory per multiprocessor: %zu bytes\n&amp;quot;, deviceProp.sharedMemPerMultiprocessor);
printf(&amp;quot; Total number of registers available per block: %d\n&amp;quot;, deviceProp.regsPerBlock);
printf(&amp;quot; Warp size: %d\n&amp;quot;, deviceProp.warpSize);
printf(&amp;quot; Maximum number of threads per multiprocessor: %d\n&amp;quot;, deviceProp.maxThreadsPerMultiProcessor);
printf(&amp;quot; Maximum number of threads per block: %d\n&amp;quot;, deviceProp.maxThreadsPerBlock);
printf(&amp;quot; Max dimension size of a block size (x,y,z): (%d, %d, %d)\n&amp;quot;, deviceProp.maxThreadsDim[0], deviceProp.maxThreadsDim[1],
deviceProp.maxThreadsDim[2]);
printf(&amp;quot; Max dimension size of a grid size (x,y,z): (%d, %d, %d)\n&amp;quot;, deviceProp.maxGridSize[0], deviceProp.maxGridSize[1],
deviceProp.maxGridSize[2]);&lt;/code&gt;&lt;/pre&gt;&lt;p style=""&gt;&lt;/p&gt;</description></item><item><title>算子进阶 —— 通信算子</title><link>https://moyutianzun.cn/en/blog/suan-zi-jin-jie------tong-xin-suan-zi/</link><pubDate>Mon, 22 Sep 2025 09:00:54 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/suan-zi-jin-jie------tong-xin-suan-zi/</guid><description>&lt;p style=""&gt;随着LLM业务的不断发展，我们发现单机单卡无法承载一个模型的训练和推理，故此出现了单机多卡和多机多卡的训练推理算子，这时候每个机和卡之间都需要通信，所以通信算子十分的重要。&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;h1 style="" id="%E5%88%86%E5%B8%83%E5%BC%8F%E5%B9%B6%E8%A1%8C"&gt;分布式并行&lt;/h1&gt;&lt;p style="text-indent: 2em"&gt;下面是传统的四种并行处理架构，常用于大模型训练。&lt;/p&gt;&lt;p style="text-align: center"&gt;&lt;img src="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250921122900313.png" width="991px" height="415px" style="display: inline-block"&gt;&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;h2 style="" id="dp"&gt;DP&lt;/h2&gt;&lt;p style="text-align: center"&gt;&lt;img src="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250921142712073.png" width="70%" height="100%" style="display: inline-block"&gt;&lt;/p&gt;&lt;p style="text-indent: 2em"&gt;每张卡拷贝相同的模型结构，仅对数据做切分。每张卡计算完的梯度也是针对各自数据的，需要做一次allreduce，然后使用优化器更新模型，进入下一次迭代。&lt;/p&gt;</description></item><item><title>算子进阶 —— 通算融合</title><link>https://moyutianzun.cn/en/blog/suan-zi-jin-jie------tong-suan-rong-he/</link><pubDate>Fri, 19 Sep 2025 08:16:51 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/suan-zi-jin-jie------tong-suan-rong-he/</guid><description>&lt;p style=""&gt;（施工ing）&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;h1 style="" id="%E6%A6%82%E8%BF%B0"&gt;概述&lt;/h1&gt;&lt;p style=""&gt;我们知道，算子的作用是计算，那在整个体系中，我们的核心目标是拉满GPU的利用率。&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;在现代分布式体系中，多GPU之间同时存在着计算、内存访问和通信这三种基本活动，为了服务于我们的核心目标，我们需要尽可能的将通信时间和访存时间放在计算时间内，使得GPU不存在运算时间的泡泡。&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;大模型分布式系统执行的核心组件是并行的GPU。提升GPU运算的最佳方法，是通过计算与通信的重叠来实现。这种重叠可以通过两种方式达成：算子分解或通信内核融合。虽然算子分解实现起来简单，但往往导致性能欠佳。而将通信内核与计算内核融合，则需要更多的思考和计算。&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;故此最原始的想法就是用计算时间掩盖通信时间，通过大批量的数据直接发送到GPU，减少kernel开启和关闭、通信的开销。&lt;/p&gt;&lt;p style="text-align: center"&gt;&lt;img src="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250917234340532.png" width="100%" height="100%" style="display: inline-block"&gt;&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;h1 style="" id="triton-distributed"&gt;Triton-distributed&lt;/h1&gt;&lt;p style="text-indent: 2em"&gt;Triton-distributed是字节seed团队开发的Triton 编译器的扩展。对于 LLM 来说，分布式优化的关键要求是计算通信重叠。以前，在小规模分布式训练/推理中，通信开销并不是一个关键的成本问题。然而，随着集群数量呈指数级增长，计算与通信重叠变得至关重要。&lt;/p&gt;</description></item><item><title>Triton is all you need —— Triton 源码、编译和调试</title><link>https://moyutianzun.cn/en/blog/triton-is-all-you-need------triton-yuan-ma-bian-yi-he-diao-shi/</link><pubDate>Thu, 18 Sep 2025 07:35:21 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/triton-is-all-you-need------triton-yuan-ma-bian-yi-he-diao-shi/</guid><description>&lt;p style="text-align: left"&gt;（施工ing）&lt;/p&gt;&lt;p style="text-align: left"&gt;&lt;/p&gt;&lt;p style="text-align: center"&gt;&lt;a href="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250918010843097.png"&gt;&lt;img src="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250918010843097.png" width="100%" height="100%" style="display: inline-block"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="text-align: left"&gt;&lt;/p&gt;&lt;p style="text-align: left"&gt;&lt;img src="https://moyutianzun.cn/upload/image.png" width="100%" height="100%" style="display: inline-block"&gt;&lt;/p&gt;&lt;p style="text-align: left"&gt;include日录主要存放了编译器核心功能的.h头文件，提供约定和规范&lt;/p&gt;&lt;p style="text-align: left"&gt;lib是.c和.cpp，主要是功能的实现，和include一一对应&lt;/p&gt;</description></item><item><title>triton is all you need 之 GEMM</title><link>https://moyutianzun.cn/en/blog/triton-is-all-you-need/</link><pubDate>Sun, 07 Sep 2025 08:10:38 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/triton-is-all-you-need/</guid><description>&lt;blockquote&gt;&lt;p style=""&gt;代码参考了傅哥，请b站关注我是傅傅猪喵，谢谢喵！&lt;/p&gt;&lt;/blockquote&gt;&lt;p style="text-indent: 2em"&gt;Triton DSL是以BLOCK tile为中心的Python DSL。与CUDA相比，Triton的使用者无法控制所有细节，因为某些优化是自动完成的，但是在Triton编译器的逐层编译优化之下也可以获得与Cuda相近甚至超过的性能。另外，Triton的编写和调试更加简单，而且学习成本更低。&lt;/p&gt;</description></item><item><title>【CUDA从入门到入土】四、矩阵乘法</title><link>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-si-ju-zhen-cheng-fa/</link><pubDate>Wed, 03 Sep 2025 14:50:37 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-si-ju-zhen-cheng-fa/</guid><description>&lt;p style="text-indent: 2em"&gt;矩阵乘法跟之前不同，之前一维可以直接写一个kernel，或者多个kernel线性的排布来并行计算，那么矩阵乘法就是由一维向二维转变的关键。这时候一维的kernel排布也变成了二维排布。&lt;/p&gt;</description></item><item><title>【CUDA从入门到入土】三、reduce算子及其优化</title><link>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-san-reducesuan-zi-ji-qi-you-hua/</link><pubDate>Sat, 23 Aug 2025 08:46:59 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-san-reducesuan-zi-ji-qi-you-hua/</guid><description>&lt;p style="text-indent: 2em"&gt;该项目代码参考&lt;a href="https://space.bilibili.com/1822828582" target="_self" rel=""&gt;傅哥的课程&lt;/a&gt;，很有用的课程，请多多支持他。&lt;/p&gt;&lt;hr&gt;&lt;p style="text-indent: 2em"&gt;&lt;/p&gt;&lt;p style="text-indent: 2em"&gt;&lt;/p&gt;&lt;p style="text-indent: 2em"&gt;reduce 规约求和是cuda中一个经典的问题，其本质是将输入的序列进行求和。&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style="text-indent: 2em"&gt;在CUDA的多线程中，我们清楚数据被分为一个一个的block中进行运行，每个block通过warp来并发32个线程进行运算。&lt;/p&gt;</description></item><item><title>【CUDA从入门到入土】二、CUDA调试和必知必会 &amp; Nsight Computer 入门</title><link>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-er-cudabi-zhi-bi-hui/</link><pubDate>Sun, 17 Aug 2025 17:09:02 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-er-cudabi-zhi-bi-hui/</guid><description>&lt;p style="text-indent: 2em"&gt;上文中，我们运行了一个简单的cuda函数，并且一次过的将其运行了起来，这次，我们需要补充一些基础的概念，通过概念和框架的建立，我们才能走的更远，高屋建瓴的认识更多。&lt;/p&gt;</description></item><item><title>【CUDA从入门到入土】五、cuda_kernel_和_cuda_attention详解</title><link>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-er-cuda_kernel_he_cuda_attentionxiang-jie/</link><pubDate>Sun, 17 Aug 2025 15:14:21 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-er-cuda_kernel_he_cuda_attentionxiang-jie/</guid><description>&lt;p style=""&gt;从cuda kernel出发，看懂人生第一个cuda attention&lt;/p&gt;</description></item><item><title>【CUDA从入门到入土】一、丝滑的CUDA入门</title><link>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-yi-si-hua-de-cudaru-men/</link><pubDate>Thu, 14 Aug 2025 16:27:54 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/cudacong-ru-men-dao-ru-tu-yi-si-hua-de-cudaru-men/</guid><description>&lt;p style=""&gt;&lt;strong&gt;CUDA是什么&lt;/strong&gt;&lt;/p&gt;&lt;p style=""&gt;cuda是一种gpu编程组件，是一种原生支持GPU软硬件的架构，使得开发者可以直接在 GPU 上编写和执行通用计算程序。&lt;/p&gt;&lt;h2 style="" id="gpu%E6%9E%B6%E6%9E%84"&gt;&lt;strong&gt;GPU架构&lt;/strong&gt;&lt;/h2&gt;&lt;p style=""&gt;&lt;img src="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250807194901764.png" alt="" width="100%" height="100%" style="display: inline-block"&gt;&lt;/p&gt;&lt;p style=""&gt;上图是H100白皮书中，H100 GPU带满了144个SM的架构图&lt;/p&gt;&lt;p style=""&gt;&lt;img src="https://moyublog-picture.oss-cn-guangzhou.aliyuncs.com/images/20250807195051758.png" alt="" width="991px" height="1379px" style="display: inline-block"&gt;&lt;/p&gt;&lt;p style=""&gt;上图是H100中，1个SM的架构图&lt;/p&gt;&lt;h3 style="" id="sm%E6%9E%B6%E6%9E%84%E8%AF%A6%E8%A7%A3"&gt;&lt;strong&gt;SM架构详解&lt;/strong&gt;&lt;/h3&gt;&lt;p style=""&gt;由此，我们可以高屋建瓴的看懂GPU，这里不雕琢细节，只直白的说明白GPU怎么工作的：&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=""&gt;PCIE 就是负责数据从显卡和主板之间进出的数据传输协议，理解为传递数据的就对了&lt;/p&gt;</description></item><item><title>WSL2搭建cuda-triton开发环境</title><link>https://moyutianzun.cn/en/blog/wsl2%E6%90%AD%E5%BB%BAcuda-triton%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/</link><pubDate>Thu, 14 Aug 2025 16:21:13 +0800</pubDate><guid>https://moyutianzun.cn/en/blog/wsl2%E6%90%AD%E5%BB%BAcuda-triton%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/</guid><description>&lt;p style=""&gt;先用了vmware + ubuntu的方法，结果发现现在gpu无法透传到vmware的虚拟机里&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;p style=""&gt;故此使用wsl2的开发环境，后续会更新许多更舒服丝滑的操作&lt;/p&gt;&lt;p style=""&gt;&lt;/p&gt;&lt;h1 style="" id="wsl2"&gt;&lt;strong&gt;WSL2&lt;/strong&gt;&lt;/h1&gt;&lt;p style=""&gt;查看所有发行版本&lt;/p&gt;&lt;pre&gt;&lt;code&gt;wsl --list --online
&lt;/code&gt;&lt;/pre&gt;&lt;p style=""&gt;安装指定版本&lt;/p&gt;&lt;pre&gt;&lt;code&gt;wsl --install Ubuntu-22.04
&lt;/code&gt;&lt;/pre&gt;&lt;p style=""&gt;安装成功之后设置一下账户密码就行&lt;/p&gt;&lt;h2 style="" id="%E5%AE%89%E8%A3%85miniconda"&gt;&lt;strong&gt;安装miniconda&lt;/strong&gt;&lt;/h2&gt;&lt;p style=""&gt;用脚本安装&lt;/p&gt;&lt;pre&gt;&lt;code&gt;wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
&lt;p&gt;chmod +x Miniconda3-latest-Linux-x86_64.sh&lt;/p&gt;
&lt;p&gt;./Miniconda3-latest-Linux-x86_64.sh&lt;/p&gt;
&lt;p&gt;source ~/.bashrc&lt;/p&gt;
&lt;p&gt;conda list
&lt;/code&gt;&lt;/pre&gt;&lt;p style=""&gt;用脚本安装就一直yes就行&lt;/p&gt;&lt;h2 style="" id="%E5%AE%89%E8%A3%85cuda-toolkit-%E5%92%8C-cudnn"&gt;&lt;strong&gt;安装CUDA Toolkit 和 cudnn&lt;/strong&gt;&lt;/h2&gt;&lt;p style=""&gt;使用 nvidia-smi查看cuda版本，在下面链接选择相应的版本&lt;/p&gt;</description></item></channel></rss>