跳到主内容
@wquguru
精选88PyTorch 博客(RSS)技巧与观点

PyTorch详解IBM Spyre加速器MLIR数据流后端

From KTIR to SpyreCode: An MLIR Dataflow Backend for IBM Spyre

原文
发到 X
推荐理由

针对AI基础设施工程师和编译器开发者的硬核技术分享,详细拆解了如何将PyTorch模型高效部署到IBM Spyre定制硬件上,提供了可复现的MLIR架构设计思路。

TL;DR

Torch-spyre’s Inductor decides how each kernel’s work is divided across Spyre’s cores and manages tensor residency between on-chip scratchpad and off-chip memory, and the backend compiler turns those decisions into something the hardware can actually run. The contract between them is represented using KTIR (Kernel Tile IR), an MLIR representation built on a data-parallel view of the accelerator, in which every compute tile can see every memory and a single logical tensor can live spread across several cores’ scratchpads. A new open-source Dataflow Scheduler consumes KTIR and turns each tile’s work into a dataflow pipeline: it splits the kernel into programs, starts from a simple load → compute → store pipeline, routes every transfer through the memory hierarchy given in an architecture description, and expands that pipeline into a nested, double-buffered, multi-instance schedule. The output of the scheduler is represented using DataflowIR, an MLIR intermediate representation that Spyre’s device-specific stages use to produce SpyreCode, a symbolic SpyreBinary plus a job plan that the Spyre runtime prepares once and launches many times, filling in tensor addresses and shapes at launch.

Torch-spyre 的 Inductor 负责决定每个内核的工作如何在 Spyre 的核心之间划分,并管理张量在片上暂存区与片外内存之间的驻留情况;后端编译器则将这些决策转化为硬件实际可执行的代码。它们之间的契约通过 KTIR(Kernel Tile IR)来表示,这是一种基于加速器数据并行视角构建的 MLIR 表示形式,在这种表示中,每个计算瓦片都能访问所有内存,且单个逻辑张量可以分布在多个核心的暂存区中。一个新的开源数据流调度器消费 KTIR,并将每个瓦片的工作转化为数据流流水线:它将内核拆分为程序,从简单的加载 → 计算 → 存储流水线开始,根据架构描述中给出的内存层次结构路由每一次传输,并将该流水线扩展为嵌套、双缓冲、多实例的调度方案。调度器的输出使用 DataflowIR 来表示,这是一种 MLIR 中间表示形式,Spyre 的设备特定阶段利用它来生成 SpyreCode,即一个符号化的 SpyreBinary 加上一个作业计划;Spyre 运行时只需准备一次该计划并在启动时多次执行,在启动时填入张量的地址和形状。

This post is the middle chapter of three. Extending Inductor for the IBM Spyre Accelerator described how torch-spyre’s Inductor extensions settle layout, tiling, work division and scratchpad placement, and Building Spyre as a Native PyTorch Device described how the runtime launches a compiled artifact whose symbolic values are only filled in at the last moment. This post covers how the decisions Inductor makes actually become a program the runtime can launch.

本文是三部曲中的中间一章。《为 IBM Spyre 加速器扩展 Inductor》描述了 torch-spyre 的 Inductor 扩展如何确定布局、分块、工作划分和暂存区放置;《将 Spyre 构建为原生 PyTorch 设备》描述了运行时如何启动一个编译产物,其符号值仅在最后一刻才被填充。本文介绍 Inductor 所做的决策实际上如何成为运行时可以启动的程序。

Where the backend compiler sits in the Spyre software stack

后端编译器在 Spyre 软件栈中的位置

Three ideas run through the design.

设计贯穿着三个理念。

  • The frontend decides the parallel decomposition, and the backend makes it run. KTIR is designed to carry the mapping Inductor has already chosen (which core computes what, over which data, out of which memory) so that the backend never has to rediscover it. The backend’s job is dataflow scheduling inside that mapping.
  • Scheduling means building a pipeline. Spyre is a dataflow machine with decoupled access and execution, so loads, stores and compute run on separate engines communicating via FIFOs. The scheduler starts from the simplest legal pipeline and refines it one transformation at a time, and every one of those steps is a readable MLIR rewrite.
  • The boundary is an IR, not a library call. KTIR, the scheduler’s ScheduleIR and DataflowIR are all ordinary MLIR built on upstream dialects (linalg, scf, arith, memref, tensor), and the scheduler and its dialects are open source under Apache-2.0. Device-specific lowering stages for code generation start at the DataflowIR boundary.
  • 前端决定并行分解方式,后端使其运行。KTIR 旨在承载 Inductor 已经选定的映射关系(哪个核心计算什么、处理哪些数据、来自哪些内存),以便后端无需重新发现这些映射。后端的工作是在该映射内部进行数据流调度。
  • 调度的意义在于构建流水线。Spyre 是一台具有解耦访问和执行的数据流机器,因此加载、存储和计算在不同的引擎上运行,并通过 FIFO 进行通信。调度器从最简单的合法流水线开始,每次通过一种变换进行细化,而这些步骤中的每一步都是可读的 MLIR 重写操作。
  • 边界是 IR,而非库调用。KTIR、调度器的 ScheduleIR 和 DataflowIR 都是构建在上游方言(linalg、scf、arith、memref、tensor)之上的普通 MLIR,且调度器及其方言在 Apache-2.0 许可下开源。代码生成的设备特定降低阶段从 DataflowIR 边界开始。

KTIR: the contract between Inductor and the backend

KTIR:Inductor 与后端之间的契约

Why a new interface

为何需要新接口

One of the key requirements for efficient backend scheduling is a contract between torch-spyre’s Inductor and the backend compiler with the capability and flexibility to capture the complete data-parallel view of the accelerator, in which every compute tile can see every memory and a single logical tensor can live spread across several cores’ scratchpads.

高效后端调度的关键要求之一是 torch-spyre 的 Inductor 与后端编译器之间建立契约,具备捕捉加速器完整数据并行视图的能力与灵活性,其中每个计算瓦片都能看到所有内存,且单个逻辑张量可以跨越多个核心的暂存区分布。

KTIR (RFC 0682, implemented in torch-spyre/ktir-mlir-frontend) is a tile-based, block-structured IR built on open-source MLIR. Our goal was to make it expressive enough to capture data-parallel mappings from two quite different sources: Inductor on one side, and hand-written Spyre kernels in languages like Triton or Helion on the other.

KTIR(RFC 0682,在 torch-spyre/ktir-mlir-frontend 中实现)是一个基于开源 MLIR 构建的基于瓦片、块结构的 IR。我们的目标是使其具有足够的表现力,以捕捉来自两个截然不同来源的数据并行映射:一侧是 Inductor,另一侧是用 Triton 或 Helion 等语言编写的手写 Spyre 内核。

A data-parallel abstraction of the accelerator

加速器的数据并行抽象

KTIR is written against a deliberately simple picture of the hardware: a set of cores, each with a compute engine and its own on-chip scratchpad, joined by an on-chip interconnect that also reaches one or more off-chip memories. The property that matters most is that every compute tile has a global view of every memory, which means its own scratchpad, every other core’s scratchpad, and device memory.

KTIR 针对故意简化的硬件图景进行编写:一组核心,每个核心拥有计算引擎及其各自的片上暂存区,并通过也连接到一个或多个片外存储器的片上互连网络相连。最重要的属性是每个计算瓦片对所有内存具有全局视图,这意味着它能看到自己的暂存区、其他所有核心的暂存区以及设备内存。

Hardware abstraction of a multi-core accelerator embodied in KTIR (from RFC 0682)

KTIR 中体现的多核加速器硬件抽象(源自 RFC 0682)

That picture matches Spyre’s shape closely: 32 cores on a ring, each with 2 MB of scratchpad, backed by off-chip device memory. Data moves between device memory and the scratchpads in sticks, fixed 128-byte chunks that hold 64 fp16 values. Intentionally, the picture leaves out details such as FIFOs, load/store units and decoupled execution engines. KTIR describes what each compute tile does and where its data lives, not how that data gets there, because working out how it gets there is exactly the scheduler’s job.

该图景与 Spyre 的形状高度吻合:环形排列的 32 个核心,每个核心配备 2 MB 暂存区,并由片外设备存储器支持。数据在设备存储器与暂存区之间以“条”(sticks)形式移动,即固定的 128 字节块,包含 64 个 fp16 值。有意地,该图景省略了 FIFO、加载/存储单元和解耦执行引擎等细节。KTIR 描述每个计算瓦片做什么以及其数据位于何处,而不是数据如何到达那里,因为确定数据如何到达正是调度器的职责。

KTIR is also compositional. Arithmetic, reductions and control flow come straight from upstream MLIR dialects (arith, math, linalg, scf, tensor, memref), and a dedicated dialect, ktdp, adds the two things those dialects can’t express: explicit memories and explicit accesses. A second, much smaller dialect, spyreop, covers Spyre’s own intrinsics. The core ktdp operations are:

KTIR 也是组合式的。算术运算、归约和控制流直接来自上游 MLIR 方言(arith、math、linalg、scf、tensor、memref),而一个专用的方言 ktdp 补充了这些方言无法表达的两种内容:显式内存和显式访问。第二个规模小得多的方言 spyreop 涵盖了 Spyre 自身的内在函数。核心的 ktdp 操作如下:

ktdp opWhat it expresses
get_compute_tile_idWhich compute tile this is; 1-D or multi-dimensional, over the grid attribute on the kernel
construct_memory_viewInterpret a region at some (possibly symbolic) address in a named memory space as a strided tensor; no allocation, no data movement
construct_distributed_memory_viewCompose several memory views, each covering part of a global coordinate space, into one logical tensor
construct_access_tileA structured set of coordinates into a view: an integer set for the domain and an affine map for the traversal order, so tiles can be rectangular, strided, skewed or triangular
construct_indirect_access_tileThe same, with some dimensions indexed through another tensor, e.g. paged KV-cache gathers
load / storeThe only data movement: consume an access tile and produce or write a data tile
inter_tile_produce / inter_tile_reduceCommunication between compute tiles, e.g. an all-reduce of partial sums
ktdp op表达内容
get_compute_tile_id这是哪个计算瓦片;一维或多维,基于内核上的 grid 属性
construct_memory_view将命名内存空间中某个(可能是符号化的)地址处的区域解释为带步长的张量;无分配,无数据移动
construct_distributed_memory_view将多个内存视图组合成一个逻辑张量,每个视图覆盖全局坐标空间的一部分
construct_access_tile视图中一组结构化的坐标:域使用整数集,遍历顺序使用仿射映射,因此瓦片可以是矩形、带步长、倾斜或三角形的
construct_indirect_access_tile同上,但某些维度通过另一个张量进行索引,例如分页 KV-cache 的 gather 操作
load / store唯一的数据移动操作:消费一个访问瓦片并生成或写入数据瓦片
inter_tile_produce / inter_tile_reduce计算瓦片之间的通信,例如部分和的全归约 (all-reduce)

Here is a complete kernel, an elementwise add over 96×64 fp16 tensors divided among 32 compute tiles of three rows each.

这是一个完整的内核,对分布在 32 个计算瓦片上的 96×64 fp16 张量执行逐元素加法,每个瓦片有三行。

代码 · 25 行
#tile = affine_set<(d0, d1) : (d0 >= 0, -d0 + 2 >= 0, d1 >= 0, -d1 + 63 >= 0)>   // 3x64
#full = affine_set<(d0, d1) : (d0 >= 0, -d0 + 95 >= 0, d1 >= 0, -d1 + 63 >= 0)>
#id   = affine_map<(d0, d1) -> (d0, d1)>
// The tensors' start addresses arrive as arguments: PyTorch's allocator owns them.
func.func @add(%A: index, %B: index, %C: index) attributes {grid = [32]} {
  %c0   = arith.constant 0 : index
  %c3   = arith.constant 3 : index
  %id   = ktdp.get_compute_tile_id : index
  %row0 = arith.muli %id, %c3 : index                 // this tile's 3-row slab
  %A_view = ktdp.construct_memory_view %A, sizes: [96, 64], strides: [64, 1] {
      coordinate_set = #full, memory_space = #ktdp.memory_space<global>
  } : memref<96x64xf16>
  // ... %B_view and %C_view alike
  %A_t = ktdp.construct_access_tile %A_view[%row0, %c0] {
      access_tile_set = #tile, access_tile_order = #id
  } : memref<96x64xf16> -> !ktdp.access_tile<3x64xindex>
  // ... %B_t and %C_t alike
  %a = ktdp.load %A_t : !ktdp.access_tile<3x64xindex> -> tensor<3x64xf16>
  %b = ktdp.load %B_t : !ktdp.access_tile<3x64xindex> -> tensor<3x64xf16>
  %e = tensor.empty() : tensor<3x64xf16>
  %c = linalg.add ins(%a, %b : tensor<3x64xf16>, tensor<3x64xf16>)
                  outs(%e : tensor<3x64xf16>) -> tensor<3x64xf16>
  ktdp.store %c, %C_t : tensor<3x64xf16>, !ktdp.access_tile<3x64xindex>
  return
}

The kernel keeps four concerns apart. Views say how memory should be interpreted, access tiles say which coordinates a given tile touches, loads and stores are the only operations that move data, and linalg does the arithmetic. Notice also that the work division is already settled: grid = [32], together with the arithmetic on %id, is the partitioning, and the backend is expected to honor it rather than second-guess it.

该内核将四个关注点分开。视图说明如何解释内存,访问瓦片说明给定瓦片触及哪些坐标,加载和存储是唯一移动数据的操作,而 linalg 负责算术运算。还要注意,工作划分已经确定:grid = [32] 以及 %id 上的算术运算是分区方式,后端预期会尊重这一划分,而不是对其重新揣测。

Spyre intrinsics: the spyreop dialect

Spyre 内在函数:spyreop 方言

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件