cutlass icon indicating copy to clipboard operation
cutlass copied to clipboard

[QST] What is the UP-TO-DATE arguments to run a selected GEMM?

Open ghostplant opened this issue 2 years ago • 11 comments

What is your question?

I'm using cutlass interface V3 to launch a standard 4Kx4K GEMM kernel for H100 (sm90) using GemmUniversalAdapter. The following code failed to compile while I have no idea how to correct the argument setting of mainloop_args and epilogue_args below:

int main() {
  using namespace cute;
  using LayoutA = cutlass::layout::RowMajor;
  using LayoutB = cutlass::layout::RowMajor;
  using LayoutC = cutlass::layout::ColumnMajor;

  using CollectiveOp = typename cutlass::gemm::collective::CollectiveBuilder<
      cutlass::arch::Sm90, cutlass::arch::OpClassTensorOp,
      cutlass::half_t, LayoutA, 8,
      cutlass::half_t, LayoutB, 8,
      float,
      Shape<_128,_128,_64>, Shape<_1,_1,_1>,
      cutlass::gemm::collective::StageCountAuto,
      cutlass::gemm::KernelCpAsyncWarpSpecializedCooperative
    >::CollectiveOp;

  using CollectiveEpilogue = typename cutlass::epilogue::collective::CollectiveBuilder<
      cutlass::arch::Sm90, cutlass::arch::OpClassTensorOp,
      Shape<_128,_128,_64>, Shape<_1,_1,_1>,
      cutlass::epilogue::collective::EpilogueTileAuto,
      float, float,
      cutlass::half_t, LayoutC, 8,
      cutlass::half_t, LayoutC, 8,
      cutlass::epilogue::NoSmemWarpSpecialized
    >::CollectiveOp;

  using GemmKernel = cutlass::gemm::kernel::GemmUniversal<
      Shape<int,int,int,int>,
      CollectiveOp,
      CollectiveEpilogue
  >;

  int m = 4096, n = 4096, k = 4096;
  cutlass::half_t *a_ptr = nullptr, *b_ptr = nullptr, *c_ptr = nullptr;
  cudaMalloc(&a_ptr, n * k * sizeof(*a_ptr));
  cudaMalloc(&b_ptr, m * k * sizeof(*a_ptr));
  cudaMalloc(&c_ptr, n * m * sizeof(*a_ptr));

  typename GemmKernel::ProblemShape prob_shape{m, n, k, 1};

  // FIXME: Problematic mainloop_args and epilogue_args:
  typename GemmKernel::MainloopArguments mainloop_args{a_ptr, Stride<int64_t, Int<1>, int64_t>{k, Int<1>{}, 0}, b_ptr, Stride<Int<1>, int64_t, int64_t>{Int<1>{}, k, 0}};
  typename GemmKernel::EpilogueArguments epilogue_args{c_ptr, Stride<int64_t, Int<1>, int64_t>{k, Int<1>{}, 0}, c_ptr, Stride<int64_t, Int<1>, int64_t>{k, Int<1>{}, 0}};

  using GemmOp = cutlass::gemm::device::GemmUniversalAdapter<GemmKernel>;
  typename GemmKernel::Arguments args{cutlass::gemm::GemmUniversalMode::kGemm, prob_shape, mainloop_args, epilogue_args};

  GemmOp gemm_op;
  size_t workspace_size = gemm_op.get_workspace_size(args);
  void *workspace = nullptr;
  cudaMalloc(&workspace, workspace_size);

  return (int)gemm_op.run(args, workspace, nullptr);
}

ghostplant avatar Oct 28 '24 10:10 ghostplant

please see examples 48, 49, 50, 51 etc : https://github.com/NVIDIA/cutlass/tree/main/examples/48_hopper_warp_specialized_gemm#L348

thakkarV avatar Oct 28 '24 14:10 thakkarV

@thakkarV Thanks, it runs normally. However, since the template selects tile-size =128x128x64. When I change the GEMM input size to be a smaller shape (e.g. int m = 8, n = 32, k = 20;), the above cutlass template doesn't get the correct answer. Is this something as expected?

ghostplant avatar Oct 30 '24 06:10 ghostplant

That's because you've set your strides as K major and alignment to 8 but then trying to run a problem shape where K is 20. If you called can_implement on that problem shape it would return false.

thakkarV avatar Oct 30 '24 11:10 thakkarV

I don't think alignment solves the problem. For above case, if I change to alignment = 2 && k = 64, the result is still not correct and gemm_op.can_implement(args) & gemm_op.initialize(args) reports no problem. However, only alignment = 2 && k = 128 starts to produce correct result. What's wrong?

ghostplant avatar Oct 31 '24 21:10 ghostplant

@IonThruster CC

thakkarV avatar Oct 31 '24 21:10 thakkarV

can you please include a dump of the input matrices and the incorrect output you observe? as well as the type of the kernel (can dump this by trying to set a bogus alias with using bogus = typename gemmkernel::bogus

thakkarV avatar Oct 31 '24 21:10 thakkarV

@thakkarV @IonThruster Can you try this example on Hopper? test_bed.cu.txt

The code is to calculate m,n,k = 128,128,64. but it doesn't produce correct results. Only if I change int k = 128, the result becomes correct.

ghostplant avatar Nov 02 '24 02:11 ghostplant

@sklevtsov-nvidia has tried to reproduce your issue but we can validate with example 48's settings when the builder alignment is set to 2 and the problem shape is set to K = 2.

Can you please include a link to a branch with

  • full repro instruction including cmake build command
  • environment details about NVCC, host compiler, OS version etc.
  • Values of the input matrices and any random seeds you are using

thakkarV avatar Nov 04 '24 18:11 thakkarV

Hi @ghostplant, in attached example you are incorrectly providing transfer sizes to cudaMemcpy:

std::vector<cutlass::half_t> hData(n * m, cutlass::half_t(1));
cudaMemcpy(a_ptr, hData.data(), hData.size() * sizeof(hData[0]), cudaMemcpyHostToDevice);
cudaMemcpy(b_ptr, hData.data(), hData.size() * sizeof(hData[0]), cudaMemcpyHostToDevice);

Your A and B device buffers are m*k and n*k elements (128*64 = 8192) but you are trying to copy m*n = 128*128 = 16384 elements. This is causing cudaMemcpy to fail (you can verify by checking its return value, which is always recommended) and leaves A/B initialized to zero. This also explains why it worked for you with k=128. Once fixed, you'll find the output tensor computed correctly for any k that is multiple of 2.

sklevtsov-nvidia avatar Nov 04 '24 21:11 sklevtsov-nvidia

Thank you, the correctness is solved. Then I get the following warning during compiling the exact code above, how to fix the performance loss?

ptxas info : (C7510) Potential Performance Loss: wgmma.mma_async instructions are serialized due to wgmma pipeline crossing function boundary at a function call in the function '_ZN7cutlass13device_kernelINS_4gemm6kernel13GemmUniversalIN4cute5tupleIJiiiiEEENS1_10collective13CollectiveMmaINS1_38MainloopSm90CpAsyncGmmaWarpSpecializedILi4ENS5_IJNS4_1CILi1EEESB_SB_EEENS1_39KernelCpAsyncWarpSpecializedCooperativeEEENS5_IJNSA_ILi256EEENSA_ILi128EEENSA_ILi64EEEEEENS_6half_tENS5_IJlSB_lEEESJ_SK_NS4_8TiledMMAINS4_8MMA_AtomIJNS4_4SM904GMMA26MMA_64x128x16_F32F16F16_SSILNSO_5MajorE0ELSQ_0ELNSO_7ScaleInE1ELSR_1EEEEEENS4_6LayoutINS5_IJNSA_ILi2EEESB_SB_EEENS5_IJSB_NSA_ILi0EEESX_EEEEENS5_IJNS4_10UnderscoreES10_S10_EEEEENS4_9TiledCopyINS4_9Copy_AtomIJNS4_25SM80_CP_ASYNC_CACHEALWAYSINS_9uint128_tES16_EESJ_EEENSU_INS5_IJNS5_IJNSA_ILi8EEENSA_ILi16EEEEEES19_EEENS5_IJNS5_IJSG_SB_EEES1A_EEEEENS5_IJS1A_SH_EEEEENS4_14ComposedLayoutINS4_7SwizzleILi3ELi4ELi3EEENS4_18smem_ptr_flag_bitsILi16EEENSU_INS5_IJS19_SH_EEENS5_IJSH_SB_EEEEEEEvNS4_8identityES1H_S1Q_vS1R_EENS_8epilogue10collective6detail29Sm90TmaWarpSpecializedAdapterINS1U_15DefaultEpilogueISK_SK_NS1T_6thread17LinearCombinationISJ_Li1EffLNS1Y_9ScaleType4KindE0ELNS_15FloatRoundStyleE2ESJ_EENS1_15EpilogueDefaultEEEEEvvEEEEvNT_6ParamsE'

ghostplant avatar Nov 05 '24 02:11 ghostplant

Performance Loss are further fixed by adding -DNDEBUG. Thank you!

ghostplant avatar Nov 05 '24 04:11 ghostplant