返回资讯中心
外部精选
软件工程
#开发者实时流

GPT-2 on the GPU

Part 8 built the GPU path by hand: kernels in the gpu dialect, NVVM lowering, PTX, and the driver calls that allocate memory and launch a grid. Parts 9 through 13 built a tensor compiler whose output ran on the CPU. Thi…

Hacker News 最新ibobev2026/10/14 · 2026/10/1430 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

Reading Mode

Wider

ai

python

llms

compilers

mlir

Part 8 built the GPU path by hand: kernels in the gpu dialect, NVVM lowering, PTX, and the driver calls that allocate memory and launch a grid. Parts 9 through 13 built a tensor compiler whose output ran on the CPU. This chapter joins the two. The same traced tensor programs, with the same formulas and the same reference checks, run on an NVIDIA GPU.

There are three pieces of work. The parallel loops that remain after bufferization have to become kernels. Matrix multiplication needs a schedule that uses shared memory, since a naive thread-per-output mapping wastes the hardware. And the boundary between host arrays and device memory has to be decided. All of it lives in tinymlir/gpu.py, a module of about two hundred and fifty lines that the CPU path never calls into.

The chapter ends with a second way to compile the model. Rather than one function per sublayer, the whole NumPy reference forward pass is traced into a single module, and that module is handed to either target.

You need Linux on x86_64, an NVIDIA driver and a card of compute capability sm_75 or newer. A Tesla T4 on Google Colab is enough; the repository includes a notebook that installs the CUDA build of the bindings and runs everything below. Nothing else in the series requires a GPU.

Recall the shape of the pipeline from Part 12. Fusion runs on tensors. Bufferization assigns storage and inserts deallocations. The result is a module of linalg operations over memrefs, and that module is what each target receives:

PassManager.parse(BUFFERIZATION_PIPELINE).run(lowered.operation) if target == GPU: gpu.lower(lowered) else: blas.rewrite(lowered) PassManager.parse(LOWERING_PIPELINE).run(lowered.operation)

The GPU side has the same two-step structure as the CPU side, a substitution followed by a pipeline:

def outline(module): tiled_matmuls(module) PassManager.parse(KERNEL_PIPELINE).run(module.operation) managed_allocations(module)

def lower(module): outline(module) PassManager.parse(llvm_pipeline()).run(module.operation)

outline is the part that is independent of hardware. It runs in the test suite on every platform, including machines with no GPU, and asserts that every loop in the module became a kernel launch. lower continues to PTX and needs the CUDA build of MLIR, which exists only for Linux.

The order within outline matters. The matmul substitution has to come before the loop passes, otherwise the matmuls would be converted to parallel loops and nothing would be left to substitute. And both have to come after bufferization and buffer deallocation. The hand-written kernel contains gpu.barrier, whose memory effects the deallocation analysis can't see through. By the time the kernel is inserted, storage has already been assigned and released.

The second step is a pipeline of existing MLIR passes:

KERNEL_PIPELINE = ( "builtin.module(" "convert-linalg-to-parallel-loops," "func.func(scf-parallel-loop-tiling{parallel-loop-tile-sizes=16,16 " "no-min-max-bounds=true})," "func.func(gpu-map-parallel-loops)," "convert-parallel-loops-to-gpu," "gpu-kernel-outlining,canonicalize)" )

Each linalg.generic becomes an scf.parallel loop nest over its parallel iterators, with any reduction iterators as ordinary sequential scf.for loops inside. A softmax row reduction is therefore a parallel loop over rows containing a serial loop along each row. One thread handles one row. That is the right mapping for GPT-2's shapes, where the reduced dimension is at most a few thousand elements, and it is the only reduction strategy this backend has.

Tiling splits each parallel loop into an outer loop over tiles of sixteen and an inner loop within the tile. The mapping pass annotates the outer loops as block dimensions and the inner loops as thread dimensions. Conversion turns the annotated nest into a gpu.launch, and outlining moves its body into a gpu.func inside a gpu.module, replacing it with gpu.launch_func. For the GELU of a (64, 256) matrix, the host function after this pipeline is:

%memref = gpu.alloc host_shared () : memref<64x256xf64> gpu.launch_func @kernel_kernel::@kernel_kernel blocks in (%c4, %c16, %c1) threads in (%c16, %c16, %c1) args(%c16 : index, %c0 : index, %c16 : index, %c1 : index, %c0 : index, %arg0 : memref<64x256xf32>, %cst_2 : f32, %cst_1 : f32, %cst_0 : f64, %cst : f64, %cst_3 : f32, %memref : memref<64x256xf64>) memref.copy %memref, %arg1 : memref<64x256xf64> to memref<64x256xf64>

A grid of four by sixteen blocks of sixteen by sixteen threads covers the matrix. The kernel's body is the fused GELU region from Part 10 with gpu.block_id and gpu.thread_id in place of loop induction variables:

gpu.func @kernel_kernel(..., %arg5: memref<64x256xf32>, ..., %arg11: memref<64x256xf64>) kernel { %block_id_x = gpu.block_id x %thread_id_x = gpu.thread_id x ... %6 = memref.load %arg5[%4, %5] : memref<64x256xf32> %7 = math.powf %6, %arg6 : f32 ... %12 = math.tanh %11 : f64 ... memref.store %16, %arg11[%4, %5] : memref<64x256xf64> gpu.return }

The no-min-max-bounds option tells the tiling pass not to clamp the inner loop with min expressions when a dimension is not a multiple of sixteen, and to use a conditional in the body instead. The clamped form produces loop bounds that the GPU conversion can't map to a fixed block size.

The test suite checks the outcome structurally. For GELU, softmax, layer normalization, matmul, gather and attention, the host function after outline must contain gpu.launch_func and no scf, linalg or memref.alloc operations. Everything that iterated now runs on the device.

Running linalg.matmul through the same pipeline would work, and would be slow. It becomes a parallel loop over the output with a serial loop over $K$ inside, so each thread reads a full row of $A$ and a full column of $B$ from global memory. Adjacent threads in a block read overlapping data and nothing is shared. Part 8 introduced shared memory precisely for this case.

The backend therefore replaces every floating-point linalg.matmul and linalg.batch_matmul with a call to a kernel written by hand. The kernel is a textual MLIR template, formatted for the operand rank and element type, parsed with ir.Module.parse and moved into the module. Omitting the batch handling and some of the bounds checks, its structure is:

func.func private @tiled_matmul_2d_f32(%a: memref<?x?xf32, ...>, %b: ..., %out: ...) { %m = memref.dim %a, %i0 : ... %k = memref.dim %a, %i1 : ... %n = memref.dim %b, %i1 : ... %gx = arith.ceildivui %n, %tile : index %gy = arith.ceildivui %m, %tile : index gpu.launch blocks(%bx, %by, %bz) in (%sx = %gx, %sy = %gy, %sz = %i1) threads(%tx, %ty, %tz) in (%ux = %tile, %uy = %tile, %uz = %i1) workgroup(%sa : memref<16x16xf32, #gpu.address_space<workgroup>>, %sb : memref<16x16xf32, #gpu.address_space<workgroup>>) { %row = arith.addi (arith.muli %by, %tile), %ty %col = arith.addi (arith.muli %bx, %tile), %tx %sum = scf.for %k0 = %i0 to %k step %tile iter_args(%acc = %zero) -> (f32) { // each thread stages one element of the A tile and one of the B tile memref.store (load %a[%row, %k0 + %tx] or zero), %sa[%ty, %tx] memref.store (load %b[%k0 + %ty, %col] or zero), %sb[%ty, %tx] gpu.barrier %part = scf.for %kk = %i0 to %tile step %i1 iter_args(%s = %acc) -> (f32) { %x = memref.load %sa[%ty, %kk] %y = memref.load %sb[%kk, %tx] scf.yield (%s + %x * %y) } gpu.barrier scf.yield %part : f32 } scf.if (%row < %m and %col < %n) { %old = memref.load %out[%row, %col] memref.store (%old + %sum), %out[%row, %col] } gpu.terminator } return }

Each block owns a sixteen by sixteen tile of the output. It walks the reduction dimension in steps of sixteen. At every step each of the block's two hundred and fifty-six threads loads one element of the current $A$ tile and one of the current $B$ tile into the two workgroup buffers, out-of-range positions reading as zero. A barrier makes the tile visible to every thread. Each thread then accumulates sixteen products from shared memory, and a second barrier keeps the next step's loads from overwriting a tile still being read. Every global element is loaded once per block instead of once per thread, a factor of sixteen fewer global loads.

Two details connect the kernel to the surrounding program. The final store adds to the output rather than overwriting it, because linalg.matmul accumulates into its destination; the builder's linalg.fill with zero runs first as its own small kernel. And the kernel takes memrefs with fully dynamic sizes, strides and offset. The substitution inserts a memref.cast for each operand, so a single kernel per rank and element type serves every shape in the model, and strided views from slicing and transposition work without copies:

%cast = memref.cast %arg0 : memref<64x256xf32> to memref<?x?xf32, strided<[?, ?], offset: ?>> %cast_0 = memref.cast %arg1 : memref<256x32xf32> to memref<?x?xf32, strided<[?, ?], offset: ?>> %cast_1 = memref.cast %memref : memref<64x32xf32> to memref<?x?xf32, strided<[?, ?], offset: ?>> call @tiled_matmul_2d_f32(%cast, %cast_0, %cast_1)

The batched variant adds a third grid dimension indexed by the batch, which attention's per-head products use.

This is the classic shared-memory matmul from the CUDA programming guide, no more. There is no register tiling, no vectorized or asynchronous loads, no tensor cores, and the tile size is fixed. cuBLAS would be several times faster on these shapes. The point is that the kernel is sixty lines of readable IR, inserted at the one place in the pipeline where a matrix multiplication is still a matrix multiplication, and it is enough to run the model.

Part 8 moved data with explicit copies to and from the device. This backend avoids that bookkeeping with CUDA managed memory, which both the host and the device address through the same pointer. The compiler-side change is small. Every memref.alloc left by bufferization becomes a gpu.alloc with the host_shared attribute, and every memref.dealloc becomes the asynchronous gpu.dealloc bracketed by gpu.wait, which is the form the runtime library implements:

%memref = gpu.alloc host_shared () : memref<64x32xf32> ... %0 = gpu.wait async %1 = gpu.dealloc async [%0] %memref : memref<64x32xf32> gpu.wait [%1]

The runtime side is a Python class wrapping the same allocator. It calls mgpuMemAlloc from MLIR's CUDA runtime library with the host-shared flag and exposes the pointer through the NumPy array interface:

class Managed: def init(self, shape, dtype): self.pointer = runtime().mgpuMemAlloc(size, None, True) self.array_interface = { "shape": tuple(shape), "typestr": dtype.str, "data": (self.pointer, False), "version": 3, }

np.asarray on such an object is an ordinary NumPy array backed by managed memory. The JIT's call path places each input in managed memory, skipping arrays that already live there, and allocates the output there too. Since the output of one compiled sublayer is the input of the next, the residual stream never leaves the device during a forward pass. The model places its weights in managed memory once at load time.

The cost is that the driver, not the program, decides when pages migrate. A first touch from the host after a kernel writes faults the page back. For this model, where the host reads one row of logits per step, that is the right trade. A latency-sensitive backend would want explicit transfers and streams, and the gpu dialect's async tokens are how it would express them.

The execution engine loads the CUDA runtime library alongside the usual runner utilities. The PTX for each gpu.module is embedded in the host module as a gpu.binary, and a global constructor loads it into the driver when the engine initializes. Launching a kernel from the host function is then a call into the same runtime library that Part 8 called by hand.

The last pipeline carries the outlined module to LL

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →