一文搞明白 hipBLAS:ROCm 里的 BLAS 加速核心

在 GPU 计算世界里,只要你碰到“矩阵乘法、向量加法、Transformer、CNN”,几乎都绕不开一个关键词:BLAS(Basic Linear Algebra Subprograms)

在 NVIDIA CUDA 生态里是 cuBLAS,而在 AMD ROCm 生态里,对应的就是本文主角:

🚀 hipBLAS:AMD GPU 上的高性能 BLAS 实现


1. hipBLAS 是什么?

hipBLAS = AMD GPU 上的 BLAS 加速库(类似 cuBLAS)

它提供:

  • 向量运算(Level 1 BLAS)
  • 矩阵-向量(Level 2 BLAS)
  • 矩阵-矩阵(Level 3 BLAS)

核心目标:

用 GPU 的并行能力,加速线性代数计算


🌰 典型函数

类别 函数 作用
Level 1 hipblasSaxpy y = a*x + y
Level 1 hipblasSdot 向量点积
Level 3 hipblasSgemm 矩阵乘法(最重要)

2. hipBLAS 在 ROCm 体系中的位置

ROCm 软件栈大致是:

PyTorch / TensorFlow / vLLM
        ↓
    ATen / Inductor
        ↓
   HIP runtime (类似 CUDA runtime)
        ↓
   hipBLAS / hipFFT / rocBLAS / rocPRIM
        ↓
        GPU

关键点:

hipBLAS 本质上是 BLAS API + HIP backend 封装


3. hipBLAS vs cuBLAS

特性 cuBLAS hipBLAS
厂商 NVIDIA AMD
API风格 CUDA HIP
功能 BLAS BLAS
对应关系 CUDA ecosystem ROCm ecosystem

👉 可以理解为:

hipBLAS = cuBLAS 的 AMD 对应实现


4. hipBLAS 最重要的函数:GEMM

💡 GEMM 是什么?

C = αAB + βC

这是:

深度学习里最核心的计算:矩阵乘法


🌰 PyTorch 背后的真实调用

例如:

import torch

a = torch.randn(1024, 4096, device="cuda")
b = torch.randn(4096, 4096, device="cuda")

c = a @ b

在 ROCm 下执行流程:

torch.matmul
   ↓
ATen operator
   ↓
HIP kernel dispatch
   ↓
hipBLAS::hipblasGemmEx
   ↓
GPU kernel(wavefront并行)

5. 一个 hipBLAS C++ 示例(最经典)

#include <hipblas.h>

int main() {
    hipblasHandle_t handle;
    hipblasCreate(&handle);

    float *A, *B, *C;

    // C = A * B
    hipblasSgemm(
        handle,
        HIPBLAS_OP_N,
        HIPBLAS_OP_N,
        1024, 1024, 1024,
        &alpha,
        A, 1024,
        B, 1024,
        &beta,
        C, 1024
    );

    hipblasDestroy(handle);
}

6. hipBLAS 在 AI / LLM 中的作用(非常关键)

你可以把 hipBLAS 理解为:

🔥 LLM 推理/训练的“发动机之一”

在 Transformer 中:

模块 是否依赖 BLAS
QKV projection ✔ GEMM
FFN(MLP) ✔ GEMM
Attention score ✔ GEMM
Softmax ❌(非BLAS)

👉 结论:

Transformer 80% 计算都靠 GEMM(hipBLAS)


7. 为什么你在 profiling 里会看到 hipBLAS?

你之前用 rocprofv2 看到:

KernelExecution
hipblas / gemm kernel

原因:

  • PyTorch 调用 matmul
  • 自动 fallback 到 hipBLAS
  • rocprofiler hook 到 kernel launch

所以你看到的“kernel trace”本质是:

hipBLAS dispatch 的 GPU kernel


8. hipBLAS vs rocBLAS(容易混淆)

很多人会问:

hipBLAS 和 rocBLAS 是什么关系?

答案:

角色
rocBLAS 底层高性能实现
hipBLAS HIP API wrapper(更兼容 CUDA风格)

👉 可以理解为:

hipBLAS = API 层
rocBLAS = 真正干活的底层实现


9. 性能优化关键点

hipBLAS 性能主要受:

1️⃣ Tile 分块策略

GPU wavefront 并行执行 GEMM

2️⃣ Memory coalescing

连续访问 global memory

3️⃣ Tensor Core / Matrix Core(部分 GPU)

4️⃣ 数据布局

  • row-major vs column-major
  • stride

10. 常见问题(工程坑)

❌ 1. 找不到 libhipblas.so

/usr/bin/ld: cannot find -lhipblas

解决:

export LD_LIBRARY_PATH=/opt/dtk/hipblas/lib:$LD_LIBRARY_PATH

❌ 2. 性能不如预期

可能原因:

  • 未使用 GEMM kernel
  • dtype 不匹配(fp32 vs fp16)
  • layout 不对(stride问题)

11. 总结一句话

hipBLAS 是 ROCm 生态中负责高性能线性代数计算的核心库,是 PyTorch / vLLM / Transformer 计算性能的底层关键组件。


在这里插入图片描述

Logo

免费领 200 小时云算力,进群参与显卡、AI PC 幸运抽奖

更多推荐