| 40 | } |
| 41 | |
| 42 | void MatrixMulForwardImpl::AlgoCuBlas::exec(const ExecArgs& args) const { |
| 43 | auto&& handle = concrete_handle(args.opr->handle()); |
| 44 | auto&& cublas_handle = handle->cublas_handle(); |
| 45 | auto&& param = args.opr->param(); |
| 46 | size_t m = args.tensor_c.layout.shape[0], n = args.tensor_c.layout.shape[1], |
| 47 | k = args.tensor_a.layout.shape[param.transposeA ? 0 : 1]; |
| 48 | |
| 49 | auto sgemm = [&]() { |
| 50 | auto zero = handle->zero_device(); |
| 51 | auto one = handle->one_device(); |
| 52 | #if CUDART_VERSION >= 11000 |
| 53 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_TF32_TENSOR_OP_MATH)); |
| 54 | #endif |
| 55 | cublas_check(cublasSgemm( |
| 56 | cublas_handle, param.transposeB ? CUBLAS_OP_T : CUBLAS_OP_N, |
| 57 | param.transposeA ? CUBLAS_OP_T : CUBLAS_OP_N, n, m, k, one, |
| 58 | args.tensor_b.ptr<dt_float32>(), args.tensor_b.layout.stride[0], |
| 59 | args.tensor_a.ptr<dt_float32>(), args.tensor_a.layout.stride[0], zero, |
| 60 | args.tensor_c.ptr<dt_float32>(), args.tensor_c.layout.stride[0])); |
| 61 | #if CUDART_VERSION >= 11000 |
| 62 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_DEFAULT_MATH)); |
| 63 | #endif |
| 64 | }; |
| 65 | |
| 66 | auto sgemm_ex = [&]() { |
| 67 | auto zero = handle->zero_device(); |
| 68 | auto one = handle->one_device(); |
| 69 | #if CUDART_VERSION >= 11000 |
| 70 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_TF32_TENSOR_OP_MATH)); |
| 71 | #elif CUDART_VERSION >= 9000 |
| 72 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_TENSOR_OP_MATH)); |
| 73 | #endif |
| 74 | auto sgemm_ex_err = cublasSgemmEx( |
| 75 | cublas_handle, param.transposeB ? CUBLAS_OP_T : CUBLAS_OP_N, |
| 76 | param.transposeA ? CUBLAS_OP_T : CUBLAS_OP_N, n, m, k, one, |
| 77 | args.tensor_b.raw_ptr(), SE_CUDA_DATA_HALF, |
| 78 | args.tensor_b.layout.stride[0], args.tensor_a.raw_ptr(), |
| 79 | SE_CUDA_DATA_HALF, args.tensor_a.layout.stride[0], zero, |
| 80 | args.tensor_c.raw_ptr(), SE_CUDA_DATA_HALF, |
| 81 | args.tensor_c.layout.stride[0]); |
| 82 | cublas_check(sgemm_ex_err); |
| 83 | #if CUDART_VERSION >= 9000 |
| 84 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_DEFAULT_MATH)); |
| 85 | #endif |
| 86 | }; |
| 87 | |
| 88 | auto hgemm = [&]() { |
| 89 | #if CUDART_VERSION >= 11000 |
| 90 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_TF32_TENSOR_OP_MATH)); |
| 91 | #elif CUDART_VERSION >= 9000 |
| 92 | cublas_check(cublasSetMathMode(cublas_handle, CUBLAS_TENSOR_OP_MATH)); |
| 93 | #endif |
| 94 | auto one_half = handle->one_device_h(); |
| 95 | auto zero_half = handle->zero_device_h(); |
| 96 | auto hgemm_ex_err = cublasHgemm( |
| 97 | cublas_handle, param.transposeB ? CUBLAS_OP_T : CUBLAS_OP_N, |
| 98 | param.transposeA ? CUBLAS_OP_T : CUBLAS_OP_N, n, m, k, one_half, |
| 99 | static_cast<const __half*>(args.tensor_b.raw_ptr()), |
nothing calls this directly
no test coverage detected