How to?

336-2

Jun 24, 2026
技术学习336
4 Minutes
636 Words

Lec 5 GPUs

GPU 结构

硬件层:

  • GPU
  • 1 GPU = 多个 SM (streaming multiprocessor)
  • 1 SM = 4 warp schedulers
  • 1 SM = 多个 SP (streaming processor)

应用层(被动态分配到硬件层):

  • 1 cuda app
  • 1 app =4096 blocks (每个动态分配到一个 SM)
    • 1 block 有自己的 shared memory
    • 1 block = 8 warps
    • 1 warp = 32 threads
    • 1 thread 有自己的 register
  • 多个 blocks in 1 grid (这 grid 是硬件吗)
    • 1 grid 有自己的 global memory(CPU DRAM) 和 constant memory

default

优化技巧 1: 低精度运算

  • 前沿 : MXFP8
  • 关于量化:
  • 所有权重包括FFN 和 QKV 矩阵都支持 fp32 到 fp8 量化吗?
    • [gemini] yes。但部分最终层 head 可能保留
  • 大概多少数据共享一个 scailing factor? 相邻数据的数量级在实际模型中会比较接近吗(否则就没法相邻数据用 scailing factor吧)?
    • [gemini] (1x32) 或者 (32x1)
    • 在模型训练或存储时,编译器会进行重排,尽量让数值相关性强的权重放在同一个 Tile(块)里

优化技巧 2: Operator fusion 算子融合

  • Computing sin2𝑥 + cos2 𝑥 naively launches 5 CUDA kernels (back and forth)
  • ‘Easy’ fusions like this can be done automatically by compilers (torch.compile)

优化技巧3: recomputing 不要缓存

三个连续的 sigmoid,forward 进行 3 次 write(存梯度),backward 进行 3 次 read(取梯度),浪费带宽。不如直接不存梯度,反向传播的时候从 x 开始重新直接算梯度

优化技巧4 : Memory coalescing with DRAM

  • ? For row-major matrices – threads that move along rows are not coalesced. Note how the second diagram reads the entire vector at each step!

Trick 5 (the big one): tiling 矩阵分块

一种提升缓存效率的方法.

default

Matrix mystery

Part 1: tiling: 矩阵形状最好是 2^n,比如64比4更好

Part 2: wave quantization: 矩阵从1792x1792到 1793x1793, tile 数量增加到刚好超出A100的SM数量,导致A100无法同时执行所有的 tiles.

Flash attn

  • Tiling part 1: tiling for the KQV matrix multiply
  • Tiling part 2: incremental computation of the softmax (telescoping sum trick)

SM 的 shm 和 L1 的区别:shm 由程序员手动控制,而 L1 不可见.

Lec6 Tritons:

上节课补充:

  • All threads within a warp must execute same instructions in lockstep on an SM. if different threads in a warp need to execute different instructions (if A, else B), must be done sequentially (bad)
  • SM 的 warp 调度器支持异步(例如一个 warp 被 IO 阻塞了)
  • SM occupancy 指的是 实际调度的 warp 数(随着可变的 num_registers_per_thread 变化,受限于出厂固定的 num_threads_per_block 和 max_registers_per_sm) / SM 支持的 warp 调度器数量. 这个 occupancy 低不一定坏

Lec9 S C A L I N G L A W S

  • skip most of this part

HW3 4 5

    1. 简单 scailing law
    1. 数据清洗
    1. PPO/RL (GRPO)

Lec 10

Article title:336-2
Article author:Julyfun
Release time:Jun 24, 2026
Copyright 2026
Sitemap