CUTLASS 2.x Tile Iterator 概念详解:从 Tensor 分块、概念族到 GEMM 实战
2026/9/16 19:51:52 网站建设 项目流程

CUTLASS 2.x Tile Iterator 概念详解:从 Tensor 分块、概念族到 GEMM 实战

【免费下载链接】cutlassCUDA Templates and Python DSLs for High-Performance Linear Algebra项目地址: https://gitcode.com/GitHub_Trending/cu/cutlass

导读

Tile Iterator(分块迭代器)是 CUTLASS 2.x 在固定尺寸矩阵/张量分块(tile)上实现通用算法(如 GEMM、卷积)的核心抽象:它将"对巨大张量的分块"抽象成一组带类型约束的迭代器概念,让同一套 GEMM 模板可以无缝适配全局内存、共享内存等不同数据布局。本文以 media/docs/cpp/tile_iterator_concept.md 为主体,结合仓库中PredicatedTileIteratorRegularTileIteratorMmaPipelined等真实实现与模板消费点,完整梳理 CUTLASS 2.x Tile Iterator 的概念族、常用组合、掩码机制与 GEMM 流水线中的实际用法,读完即可在自定义 kernel 中正确选用和实现自己的 Tile Iterator。需要说明的是,CUTLASS 3.0 已弃用这套迭代器并全面转向 CuTe 的cute::Tensor单一词汇类型,本文讨论的内容仅适用于 CUTLASS 2.x API。

下图展示了 CUTLASS GEMM 中 tile 迭代器沿逻辑坐标空间按固定步长遍历矩阵分块的示意,蓝色区域为被迭代的分块在张量中的位置,右侧网格中重复出现的同一 tile 标识(如 T9)表示迭代器在不同迭代步所指向的 tile。

背景与定位:为什么需要 Tile Iterator

CUTLASS 2.x 的算法(如gemm::threadblock::MmaPipelined、各种卷积与 epilogue)并不是直接操作整块巨大张量,而是基于尺寸恒定的 tile进行泛型编程。这些 tile 可以被视为"尺寸无穷大张量"的分区(partition),而Tile Iterator 就是访问这些分区序列的工具:它负责回答"下一个 tile 在内存中的哪里、如何把 tile 的数据搬进/搬出寄存器片段(Fragment)"这类问题。

文档 tile_iterator_concept.md 特别强调了一个设计动机:不同的数据结构对"随机访问"的支持能力不同。例如,链表式矩阵只能顺序遍历,而连续内存中的矩阵可以廉价地随机访问某个 tile。因此,以 tile 序列为单位实现的算法,只应要求 Tile Iterator 提供"最小必要算子集"——顺序算法只需要前向遍历,流水线算法可能还需要指针偏移与随机访问。这正是下述概念族存在的意义:用接口组合而非继承体系来刻画迭代器能力。

需要澄清的是,这里的 "Concept" 并非 C++20 引入的concept关键字,而是一组施加在类型上的成员与类型定义要求(requirement set)。一个 Tile Iterator 往往同时实现多个概念,其公开成员就是各个概念成员的并集。这套定义的灵感来自 Boost 的 "New style" iterator concepts。虽然全部概念组合的数量非常大,但 CUTLASS 中绝大多数 Tile Iterator 模板都能由少数几种常见组合描述,见后文"常用组合"一节。

基础概念族

以下每个概念都给出了成员签名,但注意这些是"需求规格"而非真实类定义——仓库中并不存在名为TileIteratorConcept的类,真实的迭代器(如PredicatedTileIterator)通过满足这些签名来兑现概念。

1. 基础概念:Base Tile Iterator Concept

所有 Tile Iterator 都必须描述两种类型:组成 tile 的Element类型,以及描述 tile 范围的Shape类型。

/// Base concept for all tile iterators struct TileIteratorConcept { using Element; ///< Element type composing tile (concept: numeric type or Array<>) using Shape; ///< Shape type describing extent of tile. The shape concept depends /// on iterator implementation. };
  • Element:可以是标量数值类型,也可以是cutlass::Array<>这样的向量类型;
  • Shape:描述 tile 的尺寸(行列等),其具体"形状概念"由迭代器实现决定(例如 2D tile 的ShapeTileCoord)。

2. 连续内存:Contiguous Memory Tile Iterator Concept

针对存储在连续内存块中、可任意排布的 tile。其要点是:可以向内部持有的指针加上以Element为单位的线性偏移来"移动"迭代器。因此该概念要求一个Index类型与一个add_pointer_offset()方法:

/// Tile iterator over partitions of a tensor in contiguous memory which may be referenced via a /// TensorRef object. struct ContiguousMemoryTileIterator : public TileIteratorConcept { using Index; ///< index type used to add pointer offsets /// Adds a linear offset in units of Element to internal pointer(s) into tensor CUTLASS_DEVICE void add_pointer_offset(Index pointer_offset); };

3. 可读:Readable Tile Iterator Concept

可读迭代器定义一个Fragment类型,用来存放每个线程各自负责的那部分数据load()方法把 tile 从内存读入Fragment

/// Tile iterator capable of loading tiles from memory into fragments struct ReadableTileIteratorConcept { using Fragment; ///< fragment object derived from cutlass::Array<Element, N> CUTLASS_DEVICE void load(Fragment &frag); ///< loads a fragment from memory };

4. 可读 + 连续内存:Readable Contiguous Tile Iterator Concept

在从连续内存读取时,支持一个可选指针偏移,加载前叠加到内部指针上,便于"加载与偏移折叠"成一次操作:

/// Union of the following tile iterator concepts: /// /// - ReadableTileIteratorConcept /// - ContiguousMemoryTileIterator /// struct ReadableContiguousTileIteratorConcept : public ReadableTileIteratorConcept, public ContiguousMemoryTileIterator { /// Loads a fragment from memory with additional logical offset CUTLASS_DEVICE void load_with_pointer_offset( Fragment &frag, ///< fragment to load from the tensor Index pointer_offset); ///< loads a tile with a linear offset };

5. 可写:Writeable Tile Iterator Concept

与可读概念对称:Fragment持有每个线程要写出的数据,store()把 tile 写入内存:

/// Tile iterator capable of storing tiles from memory struct WriteableTileIteratorConcept { using Fragment; ///< fragment object derived from cutlass::Array<Element, N> /// Stores a fragment to memory CUTLASS_DEVICE void store(Fragment const &frag); ///< stores a fragment to memory };

6. 可写 + 连续内存:Writeable Contiguous Tile Iterator Concept

写路径同样支持"折叠偏移"的store_with_pointer_offset()

/// Union of the following tile iterator concepts: /// /// - WriteableTileIteratorConcept /// - ContiguousMemoryTileIterator /// struct WriteableContiguousTileIteratorConcept : public WriteableTileIteratorConcept, public ContiguousMemoryTileIterator { /// Loads a fragment from memory with additional logical offset CUTLASS_DEVICE void store_with_pointer_offset( Fragment const &frag, ///< fragment to store to the tensor Index pointer_offset); ///< stores a tile with a linear offset };

7. 前向遍历:Forward Tile Iterator Concept

提供沿预定序列向前推进一个 tile的能力。序列通常与迭代器被定义的上下文相关,例如沿 GEMM 的 K 维推进。相等/不等运算符用于判断两个迭代器是否指向同一个 tile:

/// Tile iterator that may be incremented along a traversal sequence. struct ForwardTileIteratorConcept { CUTLASS_DEVICE bool operator==(TileIterator const &it); ///< true if iterators point to same tile, false if otherwise CUTLASS_DEVICE bool operator!=(TileIterator const &it); ///< false if iterators point to same tile, true if otherwise CUTLASS_DEVICE ForwardTileIteratorConcept & operator++(); ///< pre-increment - advance to next tile in sequence CUTLASS_DEVICE ForwardTileIteratorConcept operator++(int); ///< post-increment - advance to next tile in sequence };

8. 双向遍历:Bidirectional Tile Iterator Concept

ForwardTileIteratorConcept基础上增加--递减,支持前后两个方向:

/// Tile iterator which may be traverse in both directions along a defined sequence. struct BidirectionalTileIteratorConcept : public ForwardTileIteratorConcept { CUTLASS_DEVICE BidirectionalTileIteratorConcept & operator--(); ///< pre-decrement - traverse to previous tile in sequence CUTLASS_DEVICE BidirectionalTileIteratorConcept operator--(int); ///< post-decrement - traverse to previous tile in sequence };

9. 随机访问:Random Access Tile Iterator Concept

随机访问发生在底层张量的逻辑坐标系中,因此要求底层张量具有明确的Layout、描述逻辑位置的TensorCoord以及TensorRef引用类型。偏移以"整 tile"为单位,沿每个维度推进:

/// Tile iterator offering random access to tiles in contiguous memory. struct RandomAccessTileIteratorConcept : public BidirectionalTileIteratorConcept, public ContiguousMemoryTileIterator { using Layout; ///< Layout object mapping using TensorRef; ///< Tensor Reference object using TensorCoord; ///< Logical coordinate in referenced tensor ///< advances in units of whole tiles along the logical coordinate space of the tensor CUTLASS_DEVICE RandomAccessTileIteratorConcept & add_tile_offset(TensorCoord const &tile_offset); ///< advances in units of whole tiles along the logical coordinate space of the tensor CUTLASS_DEVICE RandomAccessTileIteratorConcept & operator+=(TensorCoord const &tile_offset); ///< advances in units of whole tiles along the logical coordinate space of the tensor CUTLASS_DEVICE RandomAccessTileIteratorConcept & operator-=(TensorCoord const &tile_offset); };

10. 可读随机访问:Readable Random Access Tile Iterator Concept

在加载 Fragment 时额外接受一个逻辑坐标系下的 tile 偏移:

/// Loads a fragment with a logical coordinate offset in units of whole tiles. struct ReadableRandomAccessTileIteratorConcept : public RandomAccessTileIteratorConcept, public ReadableTileIteratorConcept { /// Loads a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void load( Fragment &frag, ///< fragment to load from the tensor TensorCoord const &tile_offset); ///< loads a tile with a logical offset in units of whole tiles };

11. 可读随机访问 + 连续内存:Readable Random Access Contiguous Tile Iterator Concept

load()同时接受逻辑 tile 偏移与线性指针偏移,是"逻辑 + 物理"双偏移的完整形态:

/// Loads a fragment with a logical coordinate offset in units of whole tiles. struct ReadableRandomAccessContiguousTileIteratorConcept : public ReadableRandomAccessTileIteratorConcept, ReadableContiguousTileIteratorConcept { /// Loads a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void load( Fragment &frag, ///< fragment to load from the tensor TensorCoord const &tile_offset, ///< loads a tile with a logical offset in units of whole tiles Index pointer_offset); ///< loads a tile with a logical offset AND a pointer offset };

12. 可写随机访问:Writeable Random Access Tile Iterator Concept

写路径的对称形式,store()接受逻辑 tile 偏移:

/// Stores a fragment with a logical coordinate offset in units of whole tiles. struct WriteableRandomAccessTileIteratorConcept : public RandomAccessTileIteratorConcept, public WriteableContiguousTileIteratorConcept { /// Stores a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void store( Fragment const &frag, ///< fragment to store to the location pointed to by the tensor TensorCoord const &tile_offset); ///< stores a tile with a given offset from the current iterator };

13. 可写随机访问 + 连续内存:Writeable Random Access Contiguous Tile Iterator Concept

/// Stores a fragment with a logical coordinate offset in units of whole tiles. struct WriteableRandomAccessContiguousTileIteratorConcept : public WriteableRandomAccessTileIteratorConcept, public WriteableContiguousTileIteratorConcept { /// Stores a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void store( Fragment const &frag, ///< fragment to store to the location pointed to by the tensor TensorCoord const &tile_offset, ///< stores a tile with a logical offset in units of whole tiles Index pointer_offset); ///< stores a tile witha logical offset AND a pointer offset };

14. 掩码:Masked Tile Iterator Concept

矩阵/张量的尺寸未必是整 tile 的整数倍,此时需要用掩码(Mask)守卫内存访问,避免越界。掩码的具体语义与接口由每个迭代器自行定义,但概念规定了几个通用便捷方法,用于高效地"清空/使能"全部守卫的访问:

/// Supports iterating over tiles that are not 'whole' in memory. Iterator maintains a mask object /// which guards against out-of-bounds access. /// /// Note, this concept definition does not formally define operations on the mask or methods it /// supports. These remain implementation-dependent details of iterators implementing this concept. struct MaskedTileIteratorConcept { using Mask; ///< mask object used to guard against acceses. CUTLASS_DEVICE void clear_mask(); ///< efficiently disables all accesses guarded by mask CUTLASS_DEVICE void enable_mask(); ///< efficiently enables all accesses guarded by mask CUTLASS_DEVICE void get_mask(Mask &mask); ///< gets the mask CUTLASS_DEVICE void set_mask(Mask const &mask); ///< sets the mask };

常用组合:Frequently Used Tile Iterator Concepts

单独的概念是"积木",实际迭代器由若干概念组合而成。文档将两个最常见的组合完整展开为类型声明,方便读者对照。它们在 CUTLASS 中被大量复用,比如PredicatedTileIterator的注释明确标注其满足ForwardTileIteratorConcept | ReadableContiguousTileIteratorConcept | WriteableContiguousTileIteratorConcept | MaskedTileIteratorConcept(见 include/cutlass/transform/threadblock/predicated_tile_iterator.h)。

组合 A:Writeable, Readable, Forward, Contiguous Memory

能够加载/存储 tile 并沿遍历序列向前推进的迭代器,是流水线式全局内存加载的基础形态。它等于以下三个概念的并集:

  • ForwardTileIteratorConcept
  • ReadableContiguousTileIteratorConcept
  • WriteableContiguousTileIteratorConcept
/// This tile iterator embodies several of the above: /// /// - ForwardTileIteratorConcept /// - ReadableContiguousTileIteratorConcept /// - WriteableContiguousTileIteratorConcept /// /// It is restated explicitly for convenience of the reader. /// struct WriteableReadableForwardContiguousTileIteratorConcept { // // Data types // using Element; ///< Element type composing tile. using Shape; ///< Shape type describing extent of tile. The shape concept depends /// on iterator implementation using Index; ///< index type used as base for TensorCoord using Fragment; ///< fragment object derived from cutlass::Array<Element, N> // // Methods // /// Adds a linear offset in units of Element to internal pointer(s) into tensor CUTLASS_DEVICE void add_pointer_offset(Index offset); /// true if iterators point to same tile, false if otherwise CUTLASS_DEVICE bool operator==(WriteableReadableForwardContiguousTileIteratorConcept const &it); ///< false if iterators point to same tile, true if otherwise CUTLASS_DEVICE bool operator!=(WriteableReadableForwardContiguousTileIteratorConcept const &it); /// pre-increment - traverse to next tile in sequence CUTLASS_DEVICE WriteableReadableForwardContiguousTileIteratorConcept & operator++(); ///< post-increment - traverse to next tile in sequence CUTLASS_DEVICE WriteableReadableForwardContiguousTileIteratorConcept operator++(int); /// Loads a fragment from memory CUTLASS_DEVICE void load(Fragment &frag); ///< fragment to be loaded from memory /// Loads a fragment from memory with additional logical offset CUTLASS_DEVICE void load_with_pointer_offset( Fragment &frag, ///< fragment to be loaded from memory Index pointer_offset); ///< linear offset (in units of Element) when loading /// Stores a fragment to memory CUTLASS_DEVICE void store(Fragment const &frag); ///< fragment to store to memory /// Stores a fragment from memory with additional logical offset CUTLASS_DEVICE void store_with_pointer_offset( Fragment const &frag, ///< fragment to store to memory Index pointer_offset); ///< linear offset (in units of Element) when storing };

组合 B:Writeable, Readable, Random Access, Contiguous Memory

适合加载 GEMM 矩阵操作数的随机访问迭代器,等价于以下两个概念的并集:

  • ReadableRandomAccessContiguousTileIteratorConcept
  • WriteableRandomAccessContiguousTileIteratorConcept
/// This tile iterator embodies several of the above: /// /// - ReadableRandomAccessContiguousTileIteratorConcept /// - WriteableRandomAccessContiguousTileIteratorConcept /// /// It is restated explicitly for convenience of the reader. /// struct WriteableReadableRandomAccessContiguousTileIteratorConcept { // // Data types // using Element; ///< Element type composing tile. using Shape; ///< Shape type describing extent of tile. The shape concept depends /// on iterator implementation using Layout; ///< Layout object mapping using TensorRef; ///< Tensor Reference object using TensorCoord; ///< Logical coordinate in referenced tensor using Index; ///< index type used as base for TensorCoord using Fragment; ///< fragment object derived from cutlass::Array<Element, N> // // Methods // /// Adds a linear offset in units of Element to internal pointer(s) into tensor CUTLASS_DEVICE void add_pointer_offset(Index pointer_offset); /// true if iterators point to same tile, false if otherwise CUTLASS_DEVICE bool operator==(WriteableReadableRandomAccessContiguousTileIteratorConcept const &it); ///< false if iterators point to same tile, true if otherwise CUTLASS_DEVICE bool operator!=(WriteableReadableRandomAccessContiguousTileIteratorConcept const &it); /// pre-increment - traverse to next tile in sequence CUTLASS_DEVICE WriteableReadableRandomAccessContiguousTileIteratorConcept & operator++(); ///< post-increment - traverse to next tile in sequence CUTLASS_DEVICE WriteableReadableRandomAccessContiguousTileIteratorConcept operator++(int); /// pre-decrement - traverse to previous tile in sequence CUTLASS_DEVICE WriteableReadableRandomAccessContiguousTileIteratorConcept & operator--(); ///< post-decrement - traverse to previous tile in sequence CUTLASS_DEVICE WriteableReadableRandomAccessContiguousTileIteratorConcept operator--(int); ///< advances in units of whole tiles along the logical coordinate space of the tensor CUTLASS_DEVICE WriteableReadableRandomAccessContiguousTileIteratorConcept & operator+=(TensorCoord const &tile_offset); ///< advances in units of whole tiles along the logical coordinate space of the tensor CUTLASS_DEVICE WriteableReadableRandomAccessContiguousTileIteratorConcept & operator-=(TensorCoord const &tile_offset); /// Loads a fragment from memory CUTLASS_DEVICE void load(Fragment &frag); ///< fragment to be loaded from memory /// Loads a fragment from memory with additional logical offset CUTLASS_DEVICE void load_with_pointer_offset( Fragment &frag, ///< fragment to be loaded from memory Index pointer_offset); ///< linear offset (in units of Element) when loading /// Loads a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void load( Fragment &frag, ///< fragment to be loaded from memory TensorCoord const &tile_offset); ///< loads a tile with a logical offset in units of whole tiles /// Loads a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void load( Fragment &frag, ///< fragment to be loaded from memory TensorCoord const &tile_offset, ///< loads a tile with a logical offset in units of whole tiles Index pointer_offset); ///< loads a tile with a logical offset AND a pointer offset /// Stores a fragment to memory CUTLASS_DEVICE void store(Fragment const &frag); ///< fragment to store to memory /// Loads a fragment from memory with additional logical offset CUTLASS_DEVICE void store_with_pointer_offset( Fragment const &frag, ///< fragment to store to memory Index pointer_offset); ///< linear offset (in units of Element) when loading /// Stores a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void store( Fragment const &frag, ///< fragment to store to memory TensorCoord const &tile_offset); ///< stores with logical offset in units of whole tiles /// Stores a fragment from memory with logical offset in units of whole tiles. CUTLASS_DEVICE void store( Fragment const &frag, ///< fragment to store to memory TensorCoord const &tile_offset, ///< stores with logical offset in units of whole tiles Index pointer_offset); };

源码实现:这些概念在仓库里长什么样

概念是规格,实现才是血肉。下面以 CUTLASS 2.x 中最具代表性的几个迭代器类为例,看它们如何兑现上述概念。全部位于 include/cutlass/transform/threadblock/。

PredicatedTileIterator:掩码 + 连续内存 + 前向的典型代表

PredicatedTileIterator(include/cutlass/transform/threadblock/predicated_tile_iterator.h)专门负责从pitch-linear 的 rank=2 张量加载/存储 tile,其头注释明确给出概念归属:

Satisfies: ForwardTileIteratorConcept | ReadableContiguousTileIteratorConcept | WriteableContiguousTileIteratorConcept | MaskedTileIteratorConcept

也就是说它是"组合 A + 掩码"的实现。其模板签名如下(源码 L133-L143):

template < typename Shape, typename Element, typename Layout, int AdvanceRank, typename ThreadMap, int AccessSize = ThreadMap::kElementsPerAccess, bool Gather = false, typename PermuteLayout = layout::NoPermute > class PredicatedTileIterator;

关键设计要点(来自源码头注释与实现):

  • 预计算 Params 对象Params(源码 L198-L219)由宿主端基于 layout 构造,把大量常量计算提前完成,从而把迭代器保存在寄存器中的运行时状态降到最低;迭代过程中用整数加法推进指针。
  • 残差 tile + 稳态 tile 两段式遍历:迭代序列被设计为先访问一个可能"部分满"的残差 tile(residual tile),再进入稳态(steady state)。operator++()(源码 L280-L301)第一次调用时更新谓词并把内部指针回退到第一个稳态 tile,后续调用只需更新指针,非常轻量。沿哪个维度推进由AdvanceRank决定:kAdvanceRank==1add_tile_offset({0,1}),否则add_tile_offset({1,0})
  • 掩码接口clear_mask()enable_mask()set_mask()get_mask()全部委托给内部地址迭代器(源码 L303-L317),对应MaskedTileIteratorConcept。只要在解引用前调用clear_mask(),越界访问就是安全的(表现为 NO-OP)。
  • 加载与偏移折叠load_with_pointer_offset()把指针偏移换算成字节偏移后由load_with_byte_offset()实现(源码 L319-L348),内部按ThreadMap::IterationskAccessesPerVector展开循环,并通过address_iterator_.valid()得到谓词后调用cutlass::arch::global_load<>执行带掩码的全局加载。

源码头注释还给出了一个高效流水线使用范式(源码 L88-L130):先加载残差 tile、++iter进入稳态,然后在#pragma unroll循环中反复fragment = *iter; ++iter;,在最后一轮迭代前调用iter.clear_mask()让后续 load 变为 NO-OP,从而把整数运算压到最低。

RegularTileIterator:无掩码的规则迭代器

PredicatedTileIterator相对,RegularTileIterator(include/cutlass/transform/threadblock/regular_tile_iterator.h)用于尺寸恰好对齐、无需谓词守卫的场景,头注释定位为"存储 pitch-linear rank=2 张量的 tile"的模板。其签名同样携带Shape / Element / Layout / AdvanceRank / ThreadMap / Alignment模板参数,其中Alignment默认由sizeof_bits<Element>::value * ThreadMap::kElementsPerAccess / 8推导,保证访问按向量宽度对齐。同一目录下还有面向 TensorOp、sm70、2D thread tile 等专门化的变体:

  • regular_tile_iterator_tensor_op.h:适配 Tensor Core 操作数布局
  • regular_tile_iterator_tensor_op_sm70.h
  • regular_tile_iterator_pitch_linear.h 与 regular_tile_iterator_pitch_linear_2dthreadtile.h

更多特化形态

同一概念族在仓库中还有大量针对具体数据通路/算子的特化,例如:

  • ell_predicated_tile_iterator.h:面向 ELL 稀疏格式的掩码迭代器;
  • predicated_tile_iterator_2dthreadtile.h 与 predicated_tile_iterator_triangular_matrix.h:2D 线程块 tile、三角矩阵特化;
  • epilogue 侧则在 include/cutlass/epilogue/threadblock/ 下提供了PredicatedTileIterator的卷积(predicated_tile_iterator_conv.h)、直接卷积(predicated_tile_iterator_direct_conv.h)、dgrad(predicated_tile_iterator_strided_dgrad.h)、仿射(predicated_tile_iterator_affine.h)、BLAS3(predicated_tile_iterator_blas3.h)等变体,其头注释分别标注ReadableTileIterator | PredicatedTileIterator | ForwardTileIterator等满足关系。

这些实现分布印证了文档的判断:概念组合种类虽多,但实际迭代器都收敛在少数几种"常见组合"上。

GEMM 流水线中的消费方式

Tile Iterator 的最终价值体现在被高层算法模板当作模板参数使用。以gemm::threadblock::MmaPipelined(include/cutlass/gemm/threadblock/mma_pipelined.h)为例,其模板参数包含IteratorASmemIteratorAIteratorBSmemIteratorB等(源码 L62-L88):

  • IteratorA/IteratorB:遍历全局内存中 A/B 操作数 tile 的迭代器(通常即上述PredicatedTileIteratorRegularTileIterator系列);
  • SmemIteratorA/SmemIteratorB:遍历共享内存 tile 的迭代器。

在类内部(源码 L99-L119)可以清楚看到消费方式:

using IteratorA = IteratorA_; ///< Iterates over tiles of A operand in global memory using IteratorB = IteratorB_; ///< Iterates over tiles of B operand in global memory using SmemIteratorA = SmemIteratorA_; using SmemIteratorB = SmemIteratorB_; using FragmentA = typename IteratorA::Fragment; using FragmentB = typename IteratorB::Fragment;

FragmentA/FragmentB直接取自已满足ReadableTileIteratorConcept的迭代器——这就是概念中using Fragment在真实模板中的用法:算法只依赖迭代器暴露的Fragmentload/store方法,而不关心具体迭代器实现。交换不同布局的迭代器(pitch-linear、TensorOp 专用布局等),同一套MmaPipelined模板即可生成不同数据通路上的 kernel。K 维主循环中"全局内存迭代器加载到 Fragment → 存入共享内存 → 共享内存迭代器喂给 MMA"的流水线,正是文档所说"沿 K 维推进"的前向序列语义的实际体现。

与 CUTLASS 3.0 的关系:为何被弃用

文档开头的 Note 是理解整个概念体系历史地位的关键:

CUTLASS 3.0 deprecates all tile access iterators in favour of CuTe's single vocabulary typecute::Tensor, which is parameterized oncute::Layout.cute::Tensors can therefore be manipulated with the same layout algebra as all CuTe layouts. This removes the need for bespoke types that encapsulate iterator properties.

翻译过来即是:CUTLASS 3.0 弃用了全部 tile access iterator,转而使用 CuTe 的单一词汇类型cute::Tensor(以cute::Layout为参数)。由于cute::Tensor可以直接套用 CuTe 整套布局代数(layout algebra)进行操作,就不再需要封装迭代器属性的专用类型了。因此本文描述的概念族仅适用于 legacy CUTLASS 2.x API。仓库中对应 CuTe 的文档位于 media/docs/cpp/cute/(如 00_quickstart.md、03_tensor.md、04_algorithms.md),同一概念的 CuTe 侧表述可以参考cutlass_compiler下的 cute_concepts 相关教程。迁移到 3.x 时,原来"迭代器 + Fragment + load/store"的写法被"cute::Tensor+ CuTe 算法(如cute::copycute::gemm)"取代。

小结

CUTLASS 2.x Tile Iterator 概念体系可以用一句话概括:用一组最小化的类型约束(Element/Shape/Index/TensorCoord/Fragment)与方法约束(add_pointer_offset/load/store/++/--/+=/-=/掩码管理)描述"能干什么",用组合而非继承表达"复合能力",让算法模板只依赖接口而保持实现可替换。理解这套概念,是读懂 CUTLASS 2.x 的MmaPipelined、epilogue 迭代器以及各类卷积/GEMM kernel 的关键前置知识;而PredicatedTileIterator的残差-稳态两段式遍历与掩码折叠技巧,即使在今天编写自定义 CUDA kernel 时依然有直接借鉴价值。

版权说明:本文涉及的概念定义与源码片段均来自 CUTLASS 仓库,版权归 NVIDIA CORPORATION & AFFILIATES 所有,许可证为 BSD-3-Clause(见 LICENSE.txt)。

【免费下载链接】cutlassCUDA Templates and Python DSLs for High-Performance Linear Algebra项目地址: https://gitcode.com/GitHub_Trending/cu/cutlass

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询