Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ elseif(BUILD_HIP)
list(APPEND SRC_FILES ${GPU_FILES})

# 4-bit GEMM: build the SIMT kernel + C dispatch on ROCm.
set_source_files_properties(csrc/gemm_4bit_simt.cu csrc/gemm_4bit.cu PROPERTIES LANGUAGE HIP)
list(APPEND SRC_FILES csrc/gemm_4bit_simt.cu csrc/gemm_4bit.cu)
set_source_files_properties(csrc/gemm_4bit_simt.cu csrc/gemm_4bit_wmma.cu csrc/gemm_4bit.cu PROPERTIES LANGUAGE HIP)
list(APPEND SRC_FILES csrc/gemm_4bit_simt.cu csrc/gemm_4bit_wmma.cu csrc/gemm_4bit.cu)

string(APPEND BNB_OUTPUT_NAME "_rocm")

Expand Down
51 changes: 39 additions & 12 deletions bitsandbytes/backends/cuda/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,24 +811,52 @@ def _gemm_4bit_use_custom_cuda(device_index, dtype, M, N, K):
return M <= (16 if (tall_k_2xn or n_blocks < 48) else 8)


# WMMA kernel targets (csrc/gemm_4bit_wmma_rules.cuh).
_ROCM_WMMA_TARGETS = frozenset(
("gfx1100", "gfx1101", "gfx1102", "gfx1103", "gfx1150", "gfx1151", "gfx1152", "gfx1153", "gfx1200", "gfx1201")
)
# The target whose limits a target uses where it has none of its own (as wmma_fallback in the rules header).
_ROCM_WMMA_FALLBACK = {
"gfx1101": "gfx1100",
"gfx1150": "gfx1151",
"gfx1152": "gfx1103",
"gfx1153": "gfx1103",
"gfx1200": "gfx1201",
}
# Largest M on the fused path per WMMA target: (bf16/fp16, fp32).
_ROCM_WMMA_MAX_M = {
"gfx1100": (384, 128), # W7900
"gfx1101": (448, 512), # RX 7700 XT
"gfx1102": (512, 512), # RX 7600 XT
"gfx1103": (512, 512), # Radeon 760M
"gfx1151": (512, 512), # Radeon 8060S
"gfx1200": (512, 512), # RX 9060 XT
"gfx1201": (384, 512), # R9700
}


@functools.cache
def _gemm_4bit_use_custom_rocm(device_index, dtype, M, N, K):
"""
Fused SIMT kernel vs dequant+F.linear heuristic for ROCm.
Fused custom kernel vs dequant+F.linear heuristic for ROCm.

RDNA3/RDNA4 calibration keeps the SIMT kernel through ~M=8.
gfx125x (CDNA5) is Wave32 but lacks the v_dot2 SIMT
instructions, so its kernel takes the fp32-FMA CDNA math path. The crossover
below is left at the gfx12 default (M<=8) pending dedicated calibration.
CDNA/gfx9 is calibrated on MI308X (gfx942): bf16/fp16 win through M<=4
after the SIMT math-path tuning, while fp32 only has a broad win through M<=2.
RDNA3/RDNA3.5/RDNA4 with K % 64 == 0 have a WMMA kernel (the C dispatch picks SIMT or WMMA),
which stays ahead of dequant+F.linear up to the limits in _ROCM_WMMA_MAX_M; a chip without its own
entry uses its fallback's (_ROCM_WMMA_FALLBACK).
Otherwise (K % 64 != 0) gfx11/gfx12 keep SIMT through M=8. gfx125x (CDNA5) is Wave32 but lacks the v_dot2
SIMT instructions, so its kernel takes the fp32-FMA CDNA math path; it keeps the gfx12 default
(M<=8) pending calibration. CDNA/gfx9 is calibrated on MI308X (gfx942): bf16/fp16 win through
M<=4 after the SIMT math-path tuning, fp32 only through M<=2.

TODO: revisit once WMMA/MFMA kernels land; calibrate the gfx125x crossover.
TODO: add MFMA kernels for CDNA; calibrate the gfx125x crossover.
"""
if M <= _GEMM_4BIT_CUSTOM_FLOOR_M and dtype != torch.float32:
return True

arch = _rocm_gfx_arch(device_index)
if arch in _ROCM_WMMA_TARGETS and K % 64 == 0: # WMMA kernel
max_m = _ROCM_WMMA_MAX_M.get(arch) or _ROCM_WMMA_MAX_M[_ROCM_WMMA_FALLBACK[arch]]
return M <= max_m[dtype == torch.float32]
if arch.startswith("gfx11") or arch.startswith("gfx12"): # RDNA3 / RDNA4 / CDNA5 (gfx125x)
return M <= 8
if arch.startswith("gfx9"): # CDNA1-4 / MI-series (Wave64)
Expand Down Expand Up @@ -917,7 +945,7 @@ def _dequant_linear_fallback(


# Unified CUDA/ROCm dispatch for bitsandbytes::gemm_4bit. The choice *among* custom
# kernels (CUDA SIMT vs MMA; ROCm SIMT) is made in the C dispatch (csrc/gemm_4bit.cu).
# kernels (CUDA SIMT vs MMA; ROCm SIMT vs WMMA) is made in the C dispatch (csrc/gemm_4bit.cu).
_GEMM_4BIT_CUSTOM_FLOOR_M = 4
if torch.version.hip is None:
_gemm_4bit_use_custom_fn = _gemm_4bit_use_custom_cuda
Expand All @@ -926,9 +954,8 @@ def _dequant_linear_fallback(
_gemm_4bit_custom_max_m = 1536
else:
_gemm_4bit_use_custom_fn = _gemm_4bit_use_custom_rocm
# ROCm: the custom path is SIMT-only today; the per-arch heuristic above owns
# RDNA/CDNA thresholds. Keep a hard upper cap while WMMA/MFMA paths are absent.
_gemm_4bit_custom_max_m = 256
# ROCm: the per-arch heuristic above owns RDNA/CDNA thresholds; this is the largest of them.
_gemm_4bit_custom_max_m = 512


@register_kernel("bitsandbytes::gemm_4bit", "cuda")
Expand Down
25 changes: 21 additions & 4 deletions csrc/gemm_4bit.cu
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// C API for the custom 4-bit GEMM.
// Dispatches to SIMT or MMA kernels based on GPU architecture and shape.
// Dispatches to SIMT, MMA (CUDA) or WMMA (ROCm RDNA3/RDNA4) kernels based on GPU architecture and shape.
// Computes out[M, N] = A[M, K] @ B[N, K]^T. All pointers are device memory.

#include <cstdint>
#include <type_traits>

#include "compat.cuh"
#include "gemm_4bit_simt.cuh"
#if BNB_HIP
#include "gemm_4bit_wmma.cuh"
#include "gemm_4bit_wmma_rules.cuh"
#endif
#if defined(BNB_HAS_GEMM4BIT_SM75) || defined(BNB_HAS_GEMM4BIT_SM80)
// Included only when an MMA-capable arch (sm75/sm80+) is in the build.
#include "gemm_4bit_common.cuh" // GpuProps, used only by the MMA dispatch below
Expand Down Expand Up @@ -38,8 +42,9 @@ GpuProps get_gpu_props() {

/// @brief Fused 4-bit dequantize + GEMM. Computes out[M,N] = A[M,K] @ B[N,K]^T + bias.
///
/// Dispatches to SIMT (sm60+) or MMA (sm75 fp16, sm80+ bf16/fp16) based on GPU arch and shape.
/// fp32 always uses SIMT. Supports single-level and double-quantized (nested) absmax.
/// Dispatches to SIMT (sm60+, all ROCm), MMA (sm75 fp16, sm80+ bf16/fp16) or WMMA (RDNA3/RDNA4
/// bf16/fp16/fp32) based on GPU arch and shape. fp32 uses SIMT except on RDNA3/RDNA4. Supports
/// single-level and double-quantized (nested) absmax.
///
/// @tparam T Input/output dtype (`bnb_bfloat16`, `half`, or `float`)
template <typename T>
Expand All @@ -61,7 +66,19 @@ static void gemm_4bit(
) {
constexpr bool is_fp32 = std::is_same_v<T, float>;

// fp32 and M<=3 are always SIMT regardless of GPU -- skip the props lookup.
#if BNB_HIP
// WMMA needs K % 64 == 0 and at least 32-element quantization blocks; wmma_preferred()
// (gemm_4bit_wmma_rules.cuh) picks WMMA or SIMT by target, dtype, M and N.
const int wmma_arch = K % 64 == 0 && blocksize >= 32 ? gemm_4bit_wmma_arch() : 0;
const bool use_wmma = wmma_arch != 0 && wmma_preferred(wmma_arch, is_fp32, M, N);
if (use_wmma && launch_gemm_4bit_wmma<T>(
wmma_arch, A, B, absmax, absmax_8bit, absmax_code, absmax_offset, out, bias, M, N, K, blocksize,
quant_type, stream
))
return;
#endif

// Otherwise fp32 and M<=3 are always SIMT -- skip the props lookup.
if (is_fp32 || M <= 3) {
launch_gemm_4bit_simt<T>(
A, B, absmax, absmax_8bit, absmax_code, absmax_offset, out, bias, M, N, K, blocksize, quant_type, stream
Expand Down
Loading
Loading