第 21 篇 案例:海光 DCU第 4 讲 / 共 6 讲

第158讲DCU 上的矩阵乘:hipBLASLt 与小 M kernel

第 150 讲用海光插件注释里的数字算过一笔账:decode 时 M ≤ 64,hipBLASLt 选 256 × 256 的宏块,lm_head 那个形状上实际带宽只有 0.93 TB/s,而只沿 N 分块的专用 kernel 能跑到 1.73 TB/s。这一讲把那个专用 kernel 打开,逐行看它是怎么写的。

这是全篇最硬的一讲,但它值得读完:一个真实的、为具体硬件写的矩阵乘 kernel,把前面讲过的所有硬件常数(波前 64、MMAC 的 16×16×16、片上存储、对齐要求、架构分支)全用上了。

这个 kernel 解决什么形状

third_party/vllm-plugin-das/vllm_hcu/utils/bf16_fp32_mmac.py 第 1–23 行 / 共 354 行
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright (c) 2026 Hygon Information Technology Co., Ltd.
"""Small-M BF16xBF16 -> FP32 NT GEMM on HCU MMAC.

Ported from sglang-das ``deepseek_v4/linear_bf16_fp32.cuh``. The DeepSeek V4
compressor projections run with M <= 64 tokens per rank during decode, where
hipBLASLt picks a 256x256 tile and takes ~135us regardless of N. These kernels
cover M in [1, 64], N in {256, 512, 1024, 2048} and K % 128 == 0.
"""

from __future__ import annotations

import functools
from typing import Any

import torch

SUPPORTED_N = frozenset((256, 512, 1024, 2048))
_MMAC_ARCHES = ("gfx928", "gfx936", "gfx938")
MAX_M = 64
_BF16_BYTES = 2
# Must match the byte range encoded in make_buffer_resource().
_BUFFER_RESOURCE_RANGE_BYTES = 0x80000000
  1. 注释写得像一份工单:这段代码从 SGLang 的 DCU 分支移植过来;DeepSeek V4 的压缩投影在 decode 时每卡 M ≤ 64;在这个区间 hipBLASLt 选 256×256 的分块,不管 N 多大都要约 135 μs;所以这些 kernel 覆盖 M ∈ [1, 64]、N ∈ {256, 512, 1024, 2048}、K 是 128 的倍数。

    “不管 N 多大”这半句是整件事的关键(第 150 讲算过):时间和工作量脱钩了,说明代价全在分块的固定开销上。一个为大矩阵调好的库,在小 M 上会退化成这样。

  2. 支持的 N 做成一个集合,支持的架构做成一个元组(gfx928、gfx936、gfx938),M 的上限 64,BF16 两字节,还有一个 0x80000000 —— 注释说明它必须和下面手工拼的那个“缓冲描述符”里编码的字节范围一致。

    这几行是第 150 讲说的“把假设写成检查”:不满足就不走这条路,而不是算错。

架构分支:同一个形状,三种写法

third_party/vllm-plugin-das/vllm_hcu/utils/bf16_fp32_mmac.py 第 31–80 行 / 共 354 行

typedef __hip_bfloat16 bf16_t;
typedef _Float16 hcu_half4_t __attribute__((__vector_size__(8)));
typedef short hcu_bf16_mfma_t __attribute__((__vector_size__(8)));
typedef float hcu_float4_t __attribute__((__vector_size__(16)));
typedef std::uint32_t hcu_uint32x4_t __attribute__((ext_vector_type(4)));

struct HcuBf16x8 {
  hcu_half4_t data[2];
};

__device__ __forceinline__ void hcu_mmac_bf16(
    const hcu_half4_t& reg_a, const hcu_half4_t& reg_b, hcu_float4_t& reg_c) {
#if defined(__gfx936__) || defined(__gfx928__)
  reg_c = __builtin_amdgcn_mmac_f32_16x16x16bf16(
      *reinterpret_cast<const hcu_bf16_mfma_t*>(&reg_a),
      *reinterpret_cast<const hcu_bf16_mfma_t*>(&reg_b), reg_c);
#elif defined(__gfx938__)
  reg_c = __builtin_hcu_mmac_f32_16x16x16_bf16_lit_lts(
      *reinterpret_cast<const hcu_bf16_mfma_t*>(&reg_a),
      *reinterpret_cast<const hcu_bf16_mfma_t*>(&reg_b), reg_c, false, false);
#elif defined(__HIP_DEVICE_COMPILE__)
  // Torch builds every arch in PYTORCH_ROCM_ARCH; supported() never routes
  // devices without MMAC here.
  __builtin_trap();
#endif
}

__device__ __forceinline__ hcu_uint32x4_t make_buffer_resource(
    const std::uint32_t* ptr) {
  hcu_uint32x4_t resource = {};
  const std::uint64_t address = reinterpret_cast<std::uint64_t>(ptr);
  resource[0] =
      __builtin_amdgcn_readfirstlane(static_cast<std::uint32_t>(address));
  resource[1] = __builtin_amdgcn_readfirstlane(
      static_cast<std::uint32_t>(address >> 32));
  resource[2] = 0x80000000u;
  resource[3] = 0x00020000u;
  return resource;
}

template <typename T>
__device__ __forceinline__ void async_load8(
    T* lds_base, std::int32_t lds_offset, hcu_uint32x4_t resource,
    std::int32_t gmem_offset) {
  auto* destination =
      (__attribute__((address_space(3))) int*)(lds_base + lds_offset);
  __builtin_hcu_raw_buffer_load_lds(
      resource, destination, 16, gmem_offset * sizeof(T), 0, 0, 0);
}
  1. 先定义几个向量类型:4 个半精度打包成一个 8 字节的向量、8 个 BF16 作为矩阵指令的输入、4 个 FP32 作为累加器、4 个 32 位整数作为缓冲描述符。

    为什么累加器正好是 4 个 FP32?第 142 讲算过:16×16 的输出有 256 个元素,摊到 64 条通道上,每条通道 4 个。这个数字不是选的,是硬件定的。

  2. 同一个 16×16×16 的 BF16 矩阵乘累加,三种情况:gfx936 和 gfx928 用一个内建函数,gfx938 用另一个(名字里多了两个布尔参数),其他架构直接 __builtin_trap()。

    最后那个分支的注释解释了它的必要性:PyTorch 会为 PYTORCH_ROCM_ARCH 里列出的每个架构都编一份代码,所以这段源码会被不支持 MMAC 的架构编译到;真正的保险是上层的 supported() 不会把这些设备路由到这里。“能编译”和“该执行”是两回事,这是多架构构建里很容易翻车的地方(第 156 讲)。

  3. 这是第 143 讲说的“必须重写”的那类代码:手工拼出一个四元组,描述“一段显存”——低 32 位地址、高 32 位地址、字节范围(0x80000000,就是上面那个常量)、以及一个控制字。

    __builtin_amdgcn_readfirstlane 的作用是告诉编译器“这个值在整个波前里是一样的”,于是它可以放进标量寄存器,省下向量寄存器。这类指令在 C++ 里没有对应物,只能用内建函数。

  4. 用一条内建指令把 16 字节从显存直接搬进共享内存(地址空间 3 就是共享内存),不经过寄存器。这就是第 142 讲说的“搬运和计算重叠”的硬件基础:发出这条指令后不用等,可以接着算上一块。

    注意这个函数在当前版本里只被那个被关掉的 split-K 变体用到(下面会讲为什么关掉)。

主 kernel:一块 16 行,波前沿 K 切

third_party/vllm-plugin-das/vllm_hcu/utils/bf16_fp32_mmac.py 第 82–141 行 / 共 354 行
// One 16-row x kNPerBlock-col tile per block; waves split K and reduce in LDS.
template <int kWarps, int kNPerBlock>
__global__ void gemm_nt_bf16_fp32(
    const bf16_t* __restrict__ a, const bf16_t* __restrict__ b,
    float* __restrict__ out, std::int32_t m, std::int32_t n, std::int32_t k) {
  constexpr std::int32_t kWaveSize = 64;
  const std::int32_t block_x = blockIdx.x;
  const std::int32_t block_y = blockIdx.y;
  const std::int32_t thread = threadIdx.x;
  const std::int32_t wave = __builtin_amdgcn_readfirstlane(thread / kWaveSize);
  const std::int32_t lane = thread % kWaveSize;
  const std::int32_t row_in_tile = lane % 16;
  const std::int32_t row_group = lane / 16;
  const std::int32_t row = block_y * 16 + row_in_tile;
  const std::int32_t col = block_x * kNPerBlock + row_in_tile;
  const std::int32_t k_offset = wave * 16 * 2 + row_group * 4 * 2;

  HcuBf16x8 a_vec = {};
  HcuBf16x8 b_vec;
  hcu_float4_t accumulator = {0, 0, 0, 0};
  const bf16_t* a_row = row < m ? a + row * k + k_offset : a;
  const bf16_t* b_row = b + col * k + k_offset;

  for (std::int32_t i = 0; i + 16 * 2 * wave < k; i += 16 * 2 * kWarps) {
    if (row < m) {
      a_vec = *reinterpret_cast<const HcuBf16x8*>(a_row + i);
    }
    b_vec = *reinterpret_cast<const HcuBf16x8*>(b_row + i);
    hcu_mmac_bf16(a_vec.data[0], b_vec.data[0], accumulator);
    hcu_mmac_bf16(a_vec.data[1], b_vec.data[1], accumulator);
  }

  extern __shared__ hcu_float4_t reduction[];
#pragma unroll
  for (std::int32_t groups = kWarps; groups > 1; groups /= 2) {
    const std::int32_t midpoint = groups / 2;
    if (row < m && wave >= midpoint && wave < groups) {
      reduction[(wave - midpoint) * 4 * 16 + row_in_tile * 4 + row_group] =
          accumulator;
    }
    __syncthreads();
    if (row < m && wave < midpoint) {
      const hcu_float4_t partial =
          reduction[wave * 4 * 16 + row_in_tile * 4 + row_group];
#pragma unroll
      for (std::int32_t i = 0; i < 4; ++i) {
        accumulator[i] += partial[i];
      }
    }
    __syncthreads();
  }

  if (row < m && wave == 0) {
    float* output = out + block_x * kNPerBlock + row * n + row_group;
#pragma unroll
    for (std::int32_t i = 0; i < 4; ++i) {
      output[i * 4] = accumulator[i];
    }
  }
}
  1. 注释把策略说完了:一个线程块负责 16 行 × kNPerBlock 列的一块,块里的几个波前沿 K 方向分工,最后在共享内存里归约。

    对照第 150 讲的结论:M 方向不补零(16 行正好是矩阵指令的行数),并行度靠 N 方向的块数和 K 方向的波前数凑出来。这正是“只沿 N 分块 + 沿 K 切波前”的实现。

  2. 线程号拆成波前号和通道号(kWaveSize = 64,第 155 讲引过这一行)。通道号再拆成两段:lane % 16 是这条通道在 16×16 块里的行,lane / 16 是四个“行组”之一。

    最后一行 k_offset 把两件事叠在一起:第几个波前(沿 K 偏移 32 个元素)和第几个行组(再偏移 8 个)。一条通道一次读 8 个 BF16(16 字节,正好一次向量加载)。

  3. A 的一行和 B 的一列各读 8 个 BF16,拆成两个 4 元素向量,连发两条 MMAC 指令累加到同一个累加器上。循环的步长是 16 × 2 × kWarps——几个波前把 K 分段,各自跳着走。

    第 102 行有一处细节:row < m 不成立时让指针退回到 a 的开头。越界的行不参与计算,但指针必须是合法的,否则向量加载会越界读显存。小 M 的 kernel 里这类边界处理很多,因为 M 几乎总是不能整除 16。

  4. 各波前算的是 K 的不同段,所以要把它们的部分和加起来。做法是在共享内存里折半归约:一半的波前把自己的累加器写进去,同步,另一半读出来累加,再同步;每轮波前数减半。

    第 142、143 讲说的“归约步数由线程组宽度决定”在这里是另一种形式:这里归约的是波前之间,步数由波前个数(kWarps)决定,所以写成了循环而不是写死的次数。

  5. 只有 0 号波前写结果,每条通道按行组的偏移写 4 个 FP32,间隔 4 列——这个布局正是 MMAC 指令把输出摊在通道里的方式。

    输出是 FP32 而不是 BF16:这个 kernel 的用途是压缩投影,后面还要接别的运算,保留 FP32 可以少一次精度损失(文件名里的 bf16_fp32 就是这个意思)。

线程组宽度和片上缓冲怎么限制分块
线程组宽度 w:
矩阵指令:
图 158-1 把演示的参数调成这个 kernel 的设定:线程组宽度 64、矩阵指令 16×16×16、M 拖到 16 或 64、输出块选 16 × 64,再对比 256 × 256 时的补零比例(第 142、150 讲)。

一个被关掉的优化,和它的注释

源码里还有一个 split-K 的变体(第 87 讲的 split-K:沿 K 切开凑并行度),用双缓冲、异步搬运、原子加归约。但它被一个宏关掉了,注释写明了原因:

// The current DTK toolchain emits an empty gfx938 code object when the split-K raw
// LDS load path is present in this JIT translation unit. Keep it out of device
// compilation until it can be built as an independent extension.
#define VLLM_HCU_ENABLE_MMAC_SPLITK 0

翻译过来:当前的 DTK 工具链在这个即时编译单元里遇到 split-K 那条“直接搬进共享内存”的路径时,会给 gfx938 生成一个空的代码对象;所以先把它从设备编译里排除,等能作为独立扩展构建时再说。

这条注释值得单独说,因为它展示了适配工作里最真实的一面:一个写好的优化,因为工具链的一个问题而暂时关掉。处理方式有三点可取——用宏整段关闭而不是删掉代码;注释里写清症状(空的代码对象)、条件(在这个即时编译单元里)和重新启用的条件(作为独立扩展构建);以及关掉之后主路径仍然完整可用。第 149 讲说过“把判断过程写下来”,这就是一个范例。

入口:检查、路由、即时编译

third_party/vllm-plugin-das/vllm_hcu/utils/bf16_fp32_mmac.py 第 242–300 行 / 共 354 行
torch::Tensor linear_bf16_fp32_mmac(torch::Tensor x, torch::Tensor w) {
  TORCH_CHECK(x.dim() == 2 && w.dim() == 2, "x and w must be 2-D");
  TORCH_CHECK(x.is_cuda() && w.is_cuda(), "x and w must be CUDA tensors");
  TORCH_CHECK(x.device() == w.device(), "x and w must be on the same device");
  TORCH_CHECK(x.scalar_type() == torch::kBFloat16 &&
                  w.scalar_type() == torch::kBFloat16,
              "x and w must be BF16");
  TORCH_CHECK(x.is_contiguous() && w.is_contiguous(),
              "x and w must be contiguous");
  const int64_t m = x.size(0), k = x.size(1), n = w.size(0);
  TORCH_CHECK(w.size(1) == k, "x and w have incompatible K dimensions");
  TORCH_CHECK(m > 0 && m <= 64, "M must be in [1, 64], got ", m);
  TORCH_CHECK(n == 256 || n == 512 || n == 1024 || n == 2048,
              "unsupported N ", n);
  TORCH_CHECK(k > 0 && k % 128 == 0, "K must be a multiple of 128, got ", k);

  const at::cuda::OptionalCUDAGuard device_guard(device_of(x));
#if VLLM_HCU_ENABLE_MMAC_SPLITK
  const bool splitk = uses_splitk(m, n, k);
#else
  constexpr bool splitk = false;
#endif
  auto out = splitk
      ? torch::zeros({m, n}, x.options().dtype(torch::kFloat32))
      : torch::empty({m, n}, x.options().dtype(torch::kFloat32));
  auto stream = at::cuda::getCurrentCUDAStream().stream();
  const auto* a = reinterpret_cast<const bf16_t*>(x.data_ptr());
  const auto* b = reinterpret_cast<const bf16_t*>(w.data_ptr());
  auto* o = out.data_ptr<float>();
  const int mi = static_cast<int>(m), ni = static_cast<int>(n),
            ki = static_cast<int>(k);
  const unsigned blocks_y = static_cast<unsigned>((m + 15) / 16);
#if VLLM_HCU_ENABLE_MMAC_SPLITK
  if (splitk) {
    const dim3 grid(static_cast<unsigned>(n / 64), blocks_y, n == 1024 ? 8 : 4);
    if (n == 1024) {
      hipLaunchKernelGGL((gemm_nt_bf16_fp32_splitk<4, 64, 64, 8>), grid,
                         dim3(256), 0, stream, a, b, o, mi, ni, ki);
    } else {
      hipLaunchKernelGGL((gemm_nt_bf16_fp32_splitk<4, 64, 64, 4>), grid,
                         dim3(256), 0, stream, a, b, o, mi, ni, ki);
    }
  } else
#endif
  {
    const dim3 grid(static_cast<unsigned>(n / 16), blocks_y);
    hipLaunchKernelGGL((gemm_nt_bf16_fp32<4, 16>), grid, dim3(256),
                       2 * 16 * 16 * sizeof(float), stream, a, b, o, mi, ni,
                       ki);
  }
  C10_HIP_CHECK(hipGetLastError());
  return out;
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("linear_bf16_fp32_mmac", &linear_bf16_fp32_mmac,
        "HCU MMAC small-M BF16xBF16 -> FP32 NT GEMM");
}
"""
  1. 进 kernel 之前把所有假设检查一遍:两个张量都是二维、都在设备上、同一个设备、都是 BF16、都连续、K 维对得上、M 在 [1, 64]、N 是那四个值之一、K 是 128 的倍数。

    这些检查不是防御性编程的样板,而是契约:下面的 kernel 完全按这些前提写的(一次读 8 个元素、按 16 行分块、不处理非连续布局)。任何一条不满足,结果就是错的而不是慢的。

  2. 设备守卫(保证后续操作在正确的设备上)、输出张量(split-K 关掉时用 empty,开启时要用 zeros,因为那条路径用原子加累加部分和)、拿当前流、算网格的 Y 方向块数((m + 15) / 16,就是 M 补齐到 16 的倍数)。

  3. 网格的 X 方向是 n / 16,每块 256 个线程(4 个波前),动态共享内存 2 × 16 × 16 × sizeof(float) —— 正好是折半归约第一轮要用的空间。模板参数 <4, 16> 就是“4 个波前、每块 16 列”。

    发射用的是 hipLaunchKernelGGL 宏(第 143 讲的对照表里有它),后面紧跟一次错误检查。

Python 侧的两层把它接进引擎:

third_party/vllm-plugin-das/vllm_hcu/utils/bf16_fp32_mmac.py 第 303–354 行 / 共 354 行
@functools.lru_cache(maxsize=1)
def _jit_module() -> Any:
    import torch.utils.cpp_extension

    return torch.utils.cpp_extension.load_inline(
        name="vllm_hcu_linear_bf16_fp32_mmac",
        cpp_sources="",
        cuda_sources=_SOURCE,
        extra_cflags=["-O3"],
        extra_cuda_cflags=["-O3"],
        verbose=False,
    )


@functools.lru_cache(maxsize=None)
def _device_has_mmac(device_index: int) -> bool:
    arch = torch.cuda.get_device_properties(device_index).gcnArchName
    return arch.split(":")[0] in _MMAC_ARCHES


def supported(x: torch.Tensor, weight: torch.Tensor) -> bool:
    """Return whether ``x @ weight.T`` fits the MMAC kernel contract."""
    if x.dim() != 2 or weight.dim() != 2 or not x.is_cuda:
        return False
    if not _device_has_mmac(x.device.index or 0):
        return False
    m, k = x.shape
    n, weight_k = weight.shape
    return (
        0 < m <= MAX_M
        and n in SUPPORTED_N
        and k == weight_k
        and k > 0
        and k % 128 == 0
        and m * k * _BF16_BYTES <= _BUFFER_RESOURCE_RANGE_BYTES
        and n * k * _BF16_BYTES <= _BUFFER_RESOURCE_RANGE_BYTES
        and x.dtype == torch.bfloat16
        and weight.dtype == torch.bfloat16
        and x.is_cuda
        and x.device == weight.device
        and x.is_contiguous()
        and weight.is_contiguous()
        and x.data_ptr() % 16 == 0
        and weight.data_ptr() % 16 == 0
    )


def linear_bf16_fp32_mmac(x: torch.Tensor, weight: torch.Tensor) -> torch.Tensor:
    """Compute ``x @ weight.T`` for BF16 [M, K] x [N, K] -> FP32 [M, N]."""
    if not supported(x, weight):
        raise ValueError("MMAC BF16-to-FP32 GEMM does not support these tensors")
    return _jit_module().linear_bf16_fp32_mmac(x, weight)
  1. 用 PyTorch 的 load_inline 在运行时编译这段源码,外面套一个只缓存一个结果的装饰器,所以一个进程里只编一次。

    第 143 讲提过这种做法的取舍:不用预编译所有架构,代价是第一次调用要等几十秒——在服务里必须放进预热阶段(第 160 讲的清单里有这一项)。

  2. 从设备属性里读 gcnArchName,取冒号前面的部分,看在不在支持的三个代号里。注意这里用的是 torch.cuda.get_device_properties——第 156 讲说的“torch.cuda 照样能用”在这里又出现了一次。

  3. 和 C++ 侧那组检查重复了一遍,但多了三条只有 Python 侧方便查的:张量总字节数不超过缓冲描述符里编码的范围(那个 0x80000000)、两个张量的数据指针都是 16 字节对齐(向量加载的要求)、以及设备确实有 MMAC。

    为什么要两层检查?因为上层的调用方用它来决定走哪条路(不满足就回退到库),而 C++ 侧那组是最后的保险。第 150 讲说的“把对齐要求写成入口检查”就���这个意思。

  4. 十行不到:不满足契约就抛错,满足就调编译好的扩展。真正的路由逻辑在它的调用方——下面讲。

调用方:三层回退

这个 kernel 不是直接被模型调用的,而是放在一个三层回退的链条里(vllm_hcu/utils/bf16_fp32_gemm.py)。顺序是:

  1. 布局不对就走原生 torch。如果权重按 [in_features, out_features] 存(插件的一个选项),直接用 torch.mm 并要求 FP32 输出。
  2. 一长串前置条件:环境变量没关、torch.version.hip 不是 None、两个张量都是 BF16、都在设备上、都是二维。任何一条不满足,记下原因、退回 torch.mm。
  3. 小 M 走 MMAC kernel:开关打开且 supported() 通过时调上面那个 kernel。注释里就写着 Decode-sized M: hipBLASLt picks a 256x256 tile here (~135us/call)。调用失败就把这条路永久关掉(一个进程内的标志位),记一条警告,退到 hipBLASLt。
  4. 再失败退到 torch.mm,同样是永久关掉加一条警告。

最后,整个函数通过 direct_register_custom_op 注册成一个自定义算子(hcu_linear_bf16_fp32),还配了一个只按形状返回空张量的假实现——第 144 讲讲过这个假实现是给图编译用的。

另一个专用 kernel:lm_head

同一个思路还用在 lm_head 上(vllm_hcu/model_executor/layers/skinny_lm_head.py),但实现换成了 Triton。文件开头的注释给了第 150 讲算过的那组数:M ≤ 64 时这个投影是纯粹的权重流;hipBLASLt 在这里选 256×256 的宏块、在 gfx938 上达到约 0.93 TB/s;这个 kernel 只沿 N 分块、M 方向一个块吃完,达到 1.4–1.7 TB/s(Step5 的 lm_head 分片 32224 × 4096:M=16 时 285 → 153 μs,M=64 时 190 μs)。

两个 kernel 的对比正好印证第 151 讲的选择逻辑:

压缩投影的 kernellm_head 的 kernel
语言HIP C++(手写,带架构分支和内建指令)Triton
形状约束M ≤ 64,N 取 256 / 512 / 1024 / 2048,K % 128 = 0M ≤ 64,K % 128 = 0,N 任意
分块16 行 × 16 列,波前沿 K 切按 M 分三档选 BM、BN 和 warp 数
开关环境变量 + supported()环境变量 STEP5_SKINNY_LM_HEAD
为什么这样选要控制矩阵指令、搬运和归约的每一步逻辑简单(纯流式读权重),Triton 够用且好维护

同一个问题(小 M 矩阵乘),两种语言,按复杂度分工——这比“全部手写”或“全部 Triton”都更实际。

回顾

这个 kernel 是前面所有硬件知识的落点。它解决的形状是 decode 的小 M(M ≤ 64),而库在这个区间退化成“不管 N 多大都要约 135 μs”。它的策略是一个块吃 16 行(不补零)、沿 N 分块、块里的波前沿 K 分工最后在共享内存里折半归约。实现里用到的每一个常数都来自硬件:波前 64、MMAC 的 16×16×16、每条通道 4 个 FP32 累加器、一次读 8 个 BF16(16 字节对齐)、按 gfx928 / gfx936 / gfx938 分支选内建函数、不支持的架构编译出一条陷阱指令。工程上有三处可以直接抄:把假设写成两层检查(Python 侧用来路由、C++ 侧作为保险)、三层回退且失败后永久降级只警告一次、以及用宏关掉一个因工具链问题暂时不能用的优化并把症状和重启条件写进注释。另一个专用 kernel(lm_head)用 Triton 写,说明语言选择要按算子的复杂度分工。

矩阵乘讲完了。下一讲把注意力、量化和通信这三块在 DCU 上的选择一起看:后端按什么顺序挑、支持哪些量化方法、自定义 All-Reduce 在什么条件下才用,以及 PD 分离怎么搭。