Skip to content

[BUG] Default SIMT device::Gemm propagates NaNs when alpha is zero #3901

Open

Description

@LittlehamsterXu

Which component has the problem?

CUTLASS C++

Bug Report

Describe the bug

The default SIMT cutlass::gemm::device::Gemm<float, ...> path propagates NaNs from both input matrices when alpha=0, although the operation should reduce to D = beta * C. This is observable with valid allocated tensors and does not require a null pointer or an invalid launch.

CUTLASS's device-level GEMM documentation describes this API with an example implementing the functionality of cuBLAS SGEMM NN. The reference BLAS explicitly handles alpha=0 without allowing NaNs or Infs in A/B to affect the result.

Steps/Code to reproduce bug

From the CUTLASS repository root, build and run:

nvcc -std=c++17 -O2 -arch=sm_86 -I include -I tools/util/include repro.cu -lcublas -o repro
./repro

The following standalone source includes a finite-input control and a cuBLAS comparison:

#include <cutlass/gemm/device/gemm.h>
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <vector>
using Layout = cutlass::layout::RowMajor;
using Gemm = cutlass::gemm::device::Gemm<float, Layout, float, Layout, float, Layout,
                                         float, cutlass::arch::OpClassSimt, cutlass::arch::Sm80>;
constexpr int M=8, N=8, K=8;
#define CUDA_OK(x) do { auto e=(x); if(e!=cudaSuccess){std::cerr<<#x<<": "<<cudaGetErrorString(e)<<"\n"; return 2;} } while(0)
int main() {
  std::vector<float> a(M*K, std::numeric_limits<float>::quiet_NaN());
  std::vector<float> b(K*N, std::numeric_limits<float>::quiet_NaN());
  std::vector<float> c(M*N), got_cutlass(M*N), got_cublas(M*N);
  for (int i=0;i<M*N;++i) c[i]=float(i+1);
  float *da=nullptr,*db=nullptr,*dc=nullptr,*dd=nullptr,*dr=nullptr;
  CUDA_OK(cudaMalloc(&da,a.size()*sizeof(float)));
  CUDA_OK(cudaMalloc(&db,b.size()*sizeof(float)));
  CUDA_OK(cudaMalloc(&dc,c.size()*sizeof(float)));
  CUDA_OK(cudaMalloc(&dd,c.size()*sizeof(float)));
  CUDA_OK(cudaMalloc(&dr,c.size()*sizeof(float)));
  CUDA_OK(cudaMemcpy(da,a.data(),a.size()*sizeof(float),cudaMemcpyHostToDevice));
  CUDA_OK(cudaMemcpy(db,b.data(),b.size()*sizeof(float),cudaMemcpyHostToDevice));
  CUDA_OK(cudaMemcpy(dc,c.data(),c.size()*sizeof(float),cudaMemcpyHostToDevice));
  Gemm gemm;
  typename Gemm::Arguments args({M,N,K},{da,K},{db,N},{dc,N},{dd,N},{0.0f,1.0f});
  auto st=gemm.can_implement(args);
  std::cout<<"CUTLASS can_implement="<<cutlassGetStatusString(st)<<"\n";
  if(st!=cutlass::Status::kSuccess) return 3;
  size_t ws=gemm.get_workspace_size(args); void* workspace=nullptr;
  if(ws) CUDA_OK(cudaMalloc(&workspace,ws));
  st=gemm(args,workspace); std::cout<<"CUTLASS launch="<<cutlassGetStatusString(st)<<"\n";
  CUDA_OK(cudaDeviceSynchronize());
  CUDA_OK(cudaMemcpy(got_cutlass.data(),dd,got_cutlass.size()*sizeof(float),cudaMemcpyDeviceToHost));
  cublasHandle_t h; auto cs=cublasCreate(&h); std::cout<<"cuBLAS create="<<cs<<"\n";
  if (cs != CUBLAS_STATUS_SUCCESS) { std::cerr << "cublasCreate failed\n"; return 4; }
  CUDA_OK(cudaMemcpy(dr,c.data(),c.size()*sizeof(float),cudaMemcpyHostToDevice));
  const float alpha=0.0f,beta=1.0f;
  // Row-major CUTLASS GEMM mapped to column-major cuBLAS:
  // (A @ B)^T = B^T @ A^T, so dimensions are (N, M, K) and operands are swapped.
  cs=cublasSgemm(h,CUBLAS_OP_N,CUBLAS_OP_N,N,M,K,&alpha,db,N,da,K,&beta,dr,N);
  std::cout<<"cuBLAS alpha0 launch="<<cs<<"\n";
  if (cs != CUBLAS_STATUS_SUCCESS) { std::cerr << "cublasSgemm failed\n"; return 4; }
  CUDA_OK(cudaDeviceSynchronize());
  CUDA_OK(cudaMemcpy(got_cublas.data(),dr,got_cublas.size()*sizeof(float),cudaMemcpyDeviceToHost));
  std::vector<float> af(M*K,0.0f), bf(K*N), got_finite(M*N);
  for(int i=0;i<M;++i) af[i*K+i]=1.0f;
  for(int i=0;i<K*N;++i) bf[i]=float(i-20);
  CUDA_OK(cudaMemcpy(da,af.data(),af.size()*sizeof(float),cudaMemcpyHostToDevice));
  CUDA_OK(cudaMemcpy(db,bf.data(),bf.size()*sizeof(float),cudaMemcpyHostToDevice));
  typename Gemm::Arguments args_finite({M,N,K},{da,K},{db,N},{dc,N},{dd,N},{0.0f,1.0f});
  auto sf=gemm(args_finite,workspace);
  CUDA_OK(cudaDeviceSynchronize());
  CUDA_OK(cudaMemcpy(got_finite.data(),dd,got_finite.size()*sizeof(float),cudaMemcpyDeviceToHost));
  int finite_mismatch=0;
  for(int i=0;i<M*N;++i) if(got_finite[i]!=c[i]) ++finite_mismatch;
  std::cout<<"CUTLASS finite A/B alpha0 beta1 mismatch_vs_C="<<finite_mismatch
           <<" status="<<cutlassGetStatusString(sf)<<"\n";
  auto summarize=[&](char const* name,std::vector<float> const& out){
    int nonfinite=0, mismatch=0; float maxerr=0;
    for(int i=0;i<M*N;++i){ if(!std::isfinite(out[i])) { ++nonfinite; ++mismatch; } else {float e=std::fabs(out[i]-c[i]); maxerr=std::max(maxerr,e); if(e!=0) ++mismatch;} }
    std::cout<<name<<" nonfinite="<<nonfinite<<" mismatch_vs_betaC="<<mismatch<<" max_error="<<maxerr
             <<" first="<<out[0]<<" last="<<out.back()<<"\n";
  };
  summarize("CUTLASS",got_cutlass); summarize("cuBLAS",got_cublas);
  cublasDestroy(h); if(workspace) cudaFree(workspace);
  cudaFree(da);cudaFree(db);cudaFree(dc);cudaFree(dd);cudaFree(dr);
  return 0;
}

Observed on current main: CUTLASS reports nonfinite=64 mismatch_vs_betaC=64; the finite A/B control reports zero mismatches, and cuBLAS reports zero nonfinite values and zero mismatches. Both CUTLASS can_implement and launch return success, and device synchronization succeeds.

Expected behavior

For alpha=0, beta=1, the result should equal C for all elements, including when A or B contains NaN. Reference LAPACK SGEMM explicitly special-cases alpha=0 so that NaNs or Infs in A or B do not affect the result.

Environment details (please complete the following information):

  • Environment location: Bare-metal Linux
  • CUTLASS source: current main at b2f249acd20392beeb16f5e5a31f4f1a4e38b3bd
  • GPU: NVIDIA GeForce RTX 3080 Ti (SM 8.6)
  • Driver: 575.57.08
  • CUDA Toolkit: 12.9
  • Host compiler: GCC 13.1

Additional context

The same probe also reproduces on CUTLASS v4.8.1. The relevant default GEMM and SIMT epilogue source was checked in the current-main archive. The current LinearCombination implementation applies alpha to the computed accumulator without a zero-alpha special case.

Sources:

  • CUTLASS GEMM API example:
    Example of a CUTLASS GEMM operator implementing the functionality of cuBLAS's SGEMM NN
    is as follows:
  • CUTLASS linear-combination epilogue:
    /// Computes linear scaling with source: D = alpha * accumulator + beta * source
    CUTLASS_HOST_DEVICE
    FragmentOutput operator()(
    FragmentAccumulator const &accumulator,
    FragmentSource const &source) const {
    // Convert source to internal compute numeric type
    NumericArrayConverter<ElementCompute, ElementSource, kCount, Round> source_converter;
    NumericArrayConverter<ElementCompute, ElementAccumulator, kCount, Round> accumulator_converter;
    // Convert to destination numeric type
    NumericArrayConverter<ElementOutput, ElementCompute, kCount, Round> destination_converter;
    FragmentCompute converted_source = source_converter(source);
    FragmentCompute converted_accumulator = accumulator_converter(accumulator);
    if (Scale == ScaleType::Nothing)
    return destination_converter(converted_accumulator);
    // Perform binary operations
    FragmentCompute intermediate;
    multiplies<FragmentCompute> mul_add_source;
    multiply_add<FragmentCompute> mul_add_accumulator;
    if (Scale == ScaleType::NoBetaScaling)
    intermediate = converted_source;
    else
    intermediate = mul_add_source(beta_, converted_source); // X = beta * C + uniform
    intermediate = mul_add_accumulator(alpha_, converted_accumulator, intermediate); // D = alpha * Accum + X
  • Reference LAPACK SGEMM: https://github.com/Reference-LAPACK/lapack/blob/master/BLAS/SRC/sgemm.f

Activity

  1. Miya555orz commented on Nov 24, 2026

    @Miya555orz

    I have reproduced this on an NVIDIA RTX 500 Ada (sm_89, CUDA 13.2) and submitted a fix with thread-level and device-level SIMT SGEMM regression tests in #3930.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ? - Needs TriageCUTLASS C++bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions