[BUG] Half-precision gemm fails with CUBLAS_STATUS_NOT_SUPPORTED when A or B is not 16-byte aligned

Open
#3,234 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active

Research direction

Start in raft/linalg/detail/cublaslt_wrappers.hpp at the legacy_matmul call and inspect how the heuristic preferences and matmul_key_t cache key are built. Reproduce with the supplied half-input case using an offset of one element, then check that the revised heuristic selection handles misaligned pointers and that cache reuse respects their alignment. Done means the reported cases run and match the aligned results within rounding.

Written by the indexing model from the issue text.

Description

bug

Describe the bug

raft::linalg::gemm with half inputs and float output fails with CUBLAS_STATUS_NOT_SUPPORTED when the A or B pointer is less than 16-byte aligned, for example a view that starts a few elements into a larger allocation. The same call with the operands at the start of their allocations works.

RAFT queries the cuBLASLt heuristic with default preferences, so CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_{A,B,C,D}_BYTES stay at their default of 256. The heuristic is free to return an algorithm that needs aligned operands, and cublasLtMatmul then rejects the call for the actual pointers. The cached descriptor (matmul_key_t) doesn't depend on the pointers either, so an algorithm selected for aligned operands is reused for misaligned ones.

cuBLAS error encountered at: file=.../raft/linalg/detail/cublaslt_wrappers.hpp line=478
function=void raft::linalg::detail::legacy_matmul(...) [with bool DevicePointerMode = false; S = float; A = __half; B = __half; C = float; ...]:
call='cublasLtMatmul(resource::get_cublaslt_handle(res), mm_desc->desc, w.alpha, a_ptr, mm_desc->a, b_ptr, mm_desc->b, w.beta, c_ptr, mm_desc->c, c_ptr, mm_desc->c, &(mm_desc->heuristics.algo), nullptr, 0, stream)',
Reason=15:CUBLAS_STATUS_NOT_SUPPORTED

Steps/Code to reproduce bug

#include <raft/core/device_mdarray.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resources.hpp>
#include <raft/linalg/gemm.cuh>

#include <cuda_fp16.h>

// Column-major C[m, n] = A[m, k] * B[k, n], with A starting `a_offset` elements into its buffer.
void run(int m, int n, int k, int a_offset)
{
  raft::resources res;
  auto stream = raft::resource::get_cuda_stream(res).get();
  auto a      = raft::make_device_vector<half, int64_t>(res, int64_t(m) * k + a_offset);
  auto b      = raft::make_device_vector<half, int64_t>(res, int64_t(k) * n);
  auto c      = raft::make_device_vector<float, int64_t>(res, int64_t(m) * n);
  RAFT_CUDA_TRY(cudaMemsetAsync(a.data_handle(), 0, a.size() * sizeof(half), stream));
  RAFT_CUDA_TRY(cudaMemsetAsync(b.data_handle(), 0, b.size() * sizeof(half), stream));
  float alpha = 1.0f, beta = 0.0f;
  raft::linalg::gemm(res, false, false, m, n, k, &alpha,
                     static_cast<const half*>(a.data_handle() + a_offset), m,
                     static_cast<const half*>(b.data_handle()), k, &beta,
                     c.data_handle(), m, stream);
  raft::resource::sync_stream(res);
}

int main()
{
  run(1000, 100, 128, 0);  // ok
  run(1000, 100, 128, 1);  // throws: A is 2-byte aligned
}

Results on one H100 (each call on a fresh raft::resources; offsets in half elements):

m × n × k A or B offset 0 1 (2 bytes) 2 (4 bytes) 4 (8 bytes) 8 (16 bytes)
1000 × 100 × 128 ok throws throws throws ok
262144 × 512 × 768 ok throws throws throws ok
1001 × 100 × 128 ok ok ok ok ok
1000 × 100 × 127 ok ok ok ok ok
  • Offsetting B instead of A fails the same way.
  • The shapes with an odd m or an odd k don't fail at any offset; cuBLASLt presumably selects a different kernel for them.
  • With float inputs (A, B, C all float) the same offsets work.

Expected behavior

gemm works for any valid operand pointers, as it does for float inputs.

A change that fixed every failing case above in a local test: set CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_{A,B,C,D}_BYTES from the alignment of the call's pointers (the lowest set bit of each address, capped at 256) before cublasLtMatmulAlgoGetHeuristic, and add those alignments to matmul_key_t so that a cached algorithm is only reused for operands aligned the same way. Operands that are 256-byte aligned keep the current key and algorithm. With that change the misaligned calls run and match the aligned results to within rounding.

Environment details

  • RAFT main at d107c555 (headers only)
  • NVIDIA H100 80GB HBM3, driver 595.71.05
  • CUDA toolkit 13.0, cuBLAS 13.1.1 (cublasLtGetVersion() = 130101)
  • Linux, conda environment

Additional context

Found while reviewing NVIDIA/cuvs#3049. cuVS brute-force search passes index + j * d and search + i * d to this gemm, so a half index or query view that doesn't start on a 16-byte boundary can hit this.

Dominant language
Cuda
Stars
1.1k
Forks
260
Avg merge
1d 18h
Merged PRs (30d)
21

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/raft

All issues in NVIDIA/raft

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.