CUDA基础(四)杂。GEMM优化思想、内存事务。

文章探讨了GEMM操作在深度学习模型优化中的关键作用,介绍了从全局内存、共享内存到寄存器层面的数据分块和搬运策略,以减少存储墙的影响。同时,讲解了CUDA的SIMT架构和内存事务概念,强调了高效访问模式对减少内存交易次数和提高带宽利用率的重要性。此外,还涉及了避免bank冲突和预取(prefetching)技术来进一步优化访存延迟。
摘要由CSDN通过智能技术生成

GEMM优化

优化思路和分析

这些年涌现了一系列的深度学习模型。模型里面最耗时的东西,包括卷积、全连接层、attention,都可以转换成GEMM操作。所以说,GEMM优化的重要性,怎么突出都不过分。
介绍GEMM中的数据分块和如何在多级存储进行数据搬运。这也是HPC优化的核心思想,怎么样让数据放在更近的存储上来掩盖计算的延时,从而减少存储墙的影响
文章分为四个方面进行叙述,首先介绍在global memory层面如何进行分块以及数据搬运,随后介绍在shared memory层面如何进行分块以及数据搬运,而后介绍在register层面如何进行分块以及避免bank冲突,最后介绍如何进行prefetch以更好地掩盖访存时延。

CUDA SASS指令集合科普
CUDA微架构与指令集(3)-SASS指令集分类
首先,最简单的指令形式如FFMA R2, R4, R2, R5 ;。
其中FFMA表示这个指令进行的是32bit float fused multiply-add 操作,也就是R2=R4*R2+R5
通常称FFMA为操作码(Opcode,当然它本质上还是指的FFMA对应的编码), 后面的R2, R4, R2, R5表示参与操作的通用寄存器(General Purpose Register,简称GPR,有时也直接叫Register),称为操作数(Operand)。SASS中的习惯是第一个是目的操作数(dst),后面是源操作数(src,可以依次叫src0,src1,src2…或者srcA,srcB,srcC…都有见过)

内存事务

When a warp executes an instruction that accesses memory, it is important to consider the access pattern created by the threads in that warp.
For example, when loading data through the L1 cache, an entire 128-byte cache line is fetched, regardless of whether one thread is reading one value (the least efficient pattern), or if all 32 threads are reading consecutive 4-byte values (the most efficient pattern).
A memory “request” is an instruction which accesses memory, and a “transaction” is the movement of a unit of data between two regions of memory.内存事务是transaction,是一个movement,内存请求是一条指令。
Efficient access patterns minimize the number of transactions incurred by a request. Inefficient patterns make large numbers of transactions, using only a small amount of data from each transaction, wasting bandwidth in the connections between regions of the Memory Hierarchy.

每一个内存事务,简单说就是一个邮递员,他每次可以携带一封信从A到B,信最大可以携带128字节的信息量,通勤一次时间200天,所以你是愿意写一封128字节的信,还是写32封4字节的信,方案不言而喻。

S I M T A r c h i t e c t u r e SIMT Architecture SIMTArchitecture

The multiprocessor creates, manages, schedules, and executes threads in groups of 32 parallel threads called warps. Individual threads composing a warp /start together at the same program address, but they have their own instruction address counter and register state and are therefore free to branch and execute independently.(组成 warp 的各个线程在同一个程序地址一起开始,但它们有自己的指令地址计数器和寄存器状态,因此可以自由分支和独立执行。) The term warp originates from weaving, the first parallel thread technology. A half-warp is either the first or second half of a warp. (既…也)A quarter-warp is either the first, second, third, or fourth quarter of a warp.

When a multiprocessor is given one or more thread blocks to execute, it partitions them into warps and each warp gets scheduled by a warp scheduler for execution.
The way a block is partitioned into warps is always the same; each warp contains threads of consecutive, increasing thread IDs with the first warp containing thread 0. ** the thread ID of a thread of index$ (x, y, z) $ is ( x + y D x + z D x D y ) (x + y Dx + z Dx Dy) (x+yDx+zDxDy).

在同一线程束中的线程执行不同的指令,被称为线程束分化
A warp executes one common instruction at a time, so full efficiency is realized when all 32 threads of a warp agree on their execution path. If threads of a warp diverge via a data-dependent conditional branch, the warp executes each branch path taken, disabling threads that are not on that path. Branch divergence occurs only within a warp; different warps execute independently regardless of whether they are executing common or disjoint code paths.

Linux创始人LinusTorvalds有一句名言:Talk is cheap, Show me the code.(冗谈不够,放码过来!)。 代码阅读是从入门到提高的必由之路。尤其对深度学习,许多框架隐藏了神经网络底层的实现,只能在上层调包使用,对其内部原理很难认识清晰,不利于进一步优化和创新。  YOLOv3是一种基于深度学习的端到端实时目标检测方法,以速度快见长。YOLOv3的实现Darknet是使用C语言开发的轻型开源深度学习框架,依赖少,可移植性好,可以作为很好的代码阅读案例,让我们深入探究其实现原理。  本课程将解析YOLOv3的实现原理和源码,具体内容包括: YOLO目标检测原理  神经网络及Darknet的C语言实现,尤其是反向传播的梯度求解和误差计算 代码阅读工具及方法 深度学习计算的利器:BLAS和GEMM GPU的CUDA编程方法及在Darknet的应用 YOLOv3的程序流程及各层的源码解析本课程将提供注释后的Darknet的源码程序文件。  除本课程《YOLOv3目标检测:原理与源码解析》外,本人推出了有关YOLOv3目标检测的系列课程,包括:   《YOLOv3目标检测实战:训练自己的数据集》  《YOLOv3目标检测实战:交通标志识别》  《YOLOv3目标检测:原理与源码解析》  《YOLOv3目标检测:网络模型改进方法》 建议先学习课程《YOLOv3目标检测实战:训练自己的数据集》或课程《YOLOv3目标检测实战:交通标志识别》,对YOLOv3的使用方法了解以后再学习本课程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值