/* * SPDX-FileCopyrightText: Copyright (c) 2020-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include "helpers.h" /* Structure to store information about different run trials */ struct customMatmulPerf_t { cublasLtMatmulAlgo_t algo; cublasStatus_t status; float time; size_t workspaceSize; // actual memory workspace needed cublasMath_t mathMode; cublasLtReductionScheme_t reductionScheme; int customOption; float wavesCount; }; // Utility function to print customMatmulPerf_t structure inline void printPerfStructure(const customMatmulPerf_t &perf) { cublasLtMatmulTile_t tile; int algoId, swizzle, customOption, numSplitsK, reductionScheme, stages; const cublasLtMatmulAlgo_t *matmulAlgo = &perf.algo; cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_ID, &algoId, sizeof(algoId), NULL); cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_TILE_ID, &tile, sizeof(tile), NULL); cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_SPLITK_NUM, &numSplitsK, sizeof(numSplitsK), NULL); cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_REDUCTION_SCHEME, &reductionScheme, sizeof(reductionScheme), NULL); cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_CTA_SWIZZLING, &swizzle, sizeof(swizzle), NULL); cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_CUSTOM_OPTION, &customOption, sizeof(customOption), NULL); cublasLtMatmulAlgoConfigGetAttribute(matmulAlgo, CUBLASLT_ALGO_CONFIG_STAGES_ID, &stages, sizeof(stages), NULL); printf("algo={ Id=%d, tileIdx=%d (%s) splitK=%d reduc=%d swizzle=%d custom=%d stages=%d} status %d " "time %f workspace=%d mathMode=%d waves=%f\n", algoId, tile, tileToString(tile), numSplitsK, reductionScheme, swizzle, customOption, stages, perf.status, perf.time, (int)perf.workspaceSize, (int)perf.mathMode, perf.wavesCount); } inline bool time_compare(const customMatmulPerf_t &perf_a, const customMatmulPerf_t &perf_b) { return ((perf_a.status == CUBLAS_STATUS_SUCCESS) && (perf_a.time < perf_b.time)); } inline cublasStatus_t customMatmulRun(cublasLtHandle_t ltHandle, // to get the capabilities (required a GPU) cublasLtMatmulDesc_t operationDesc, const void *alpha, /* host or device pointer */ const void *A, cublasLtMatrixLayout_t Adesc, const void *B, cublasLtMatrixLayout_t Bdesc, const void *beta, /* host or device pointer */ const void *C, cublasLtMatrixLayout_t Cdesc, void *D, cublasLtMatrixLayout_t Ddesc, const cublasLtMatmulAlgo_t &algo, int kernelRepeats, void *workSpace, size_t workSpaceSizeInBytes, customMatmulPerf_t &perfResults, cudaStream_t stream, cudaEvent_t &startEvent, cudaEvent_t &stopEvent) { cublasLtMatmulHeuristicResult_t heurResult; /* Looping over the Algo */ int repeats = kernelRepeats; cublasStatus_t algoStatus = cublasLtMatmulAlgoCheck(ltHandle, operationDesc, Adesc, Bdesc, Cdesc, Ddesc, &algo, &heurResult); if (algoStatus == CUBLAS_STATUS_SUCCESS) { if (heurResult.workspaceSize <= workSpaceSizeInBytes) { cudaError_t err, err1, err2, err3; err = cudaEventRecord(startEvent, stream); for (int loop = 0; loop < repeats; loop++) { cublasStatus_t oneRunStatus = cublasLtMatmul(ltHandle, operationDesc, alpha, A, Adesc, B, Bdesc, beta, C, Cdesc, D, Ddesc, &algo, workSpace, workSpaceSizeInBytes, stream); if (oneRunStatus != CUBLAS_STATUS_SUCCESS) { algoStatus = oneRunStatus; break; } } err1 = cudaEventRecord(stopEvent, stream); err2 = cudaEventSynchronize(stopEvent); float time; err3 = cudaEventElapsedTime(&time, startEvent, stopEvent); if ((err != cudaSuccess) || (err1 != cudaSuccess) || (err2 != cudaSuccess) || (err3 != cudaSuccess)) { algoStatus = CUBLAS_STATUS_INTERNAL_ERROR; } // For the moment only add successful findings if (algoStatus == CUBLAS_STATUS_SUCCESS) { perfResults.algo = algo; perfResults.time = time; perfResults.workspaceSize = heurResult.workspaceSize; perfResults.wavesCount = heurResult.wavesCount; } } else { algoStatus = CUBLAS_STATUS_NOT_SUPPORTED; // Not enough workspace } } return algoStatus; } /// Sample wrapper running through multiple algo and config attributes combination for single precision gemm using /// cublasLt low-level API inline void LtMatmulCustomFind(cublasLtHandle_t ltHandle, cublasOperation_t transa, cublasOperation_t transb, int m, int n, int k, cudaDataType_t scaleType, const void *alpha, /* host pointer */ cudaDataType_t Atype, const void *A, int lda, cudaDataType_t Btype, const void *B, int ldb, const void *beta, /* host pointer */ cudaDataType_t Ctype, const void *C, int ldc, cudaDataType_t Dtype, void *D, int ldd, void *workSpace, size_t workSpaceSize) { cublasStatus_t status = CUBLAS_STATUS_SUCCESS; cublasLtMatmulDesc_t operationDesc = NULL; cublasLtMatrixLayout_t Adesc = NULL, Bdesc = NULL, Cdesc = NULL, Ddesc = NULL; cublasLtMatmulPreference_t preference = NULL; cudaEvent_t startEvent = NULL, stopEvent = NULL; cudaStream_t stream = NULL; // SplitK values that we are going to try when SplitK is supported for a given algo const int splitKSequenceA[] = {2, 3, 4, 5, 6, 8, 12, 16, 32}; // Let try a fixed number of combinations #define ALGO_COMBINATIONS 100 int AlgoCombinations = ALGO_COMBINATIONS; int AlgoCount = 0; int kernelRepeats = 10; // number of time the CUDA kernels will be run back to back customMatmulPerf_t perfResults[ALGO_COMBINATIONS]; int nbAlgoIds = 0; #define ALGO_IDS 16 int algoIdA[ALGO_IDS]; cublasComputeType_t computeType = CUBLAS_COMPUTE_32F; // create operation desciriptor; see cublasLtMatmulDescAttributes_t for details about defaults; here we just need to // set the transforms for A and B checkCublasStatus(cublasLtMatmulDescCreate(&operationDesc, CUBLAS_COMPUTE_32F, CUDA_R_32F)); checkCublasStatus( cublasLtMatmulDescSetAttribute(operationDesc, CUBLASLT_MATMUL_DESC_TRANSA, &transa, sizeof(transa))); checkCublasStatus( cublasLtMatmulDescSetAttribute(operationDesc, CUBLASLT_MATMUL_DESC_TRANSB, &transb, sizeof(transb))); // create matrix descriptors, we are good with the details here so no need to set any extra attributes checkCublasStatus( cublasLtMatrixLayoutCreate(&Adesc, Atype, transa == CUBLAS_OP_N ? m : k, transa == CUBLAS_OP_N ? k : m, lda)); checkCublasStatus( cublasLtMatrixLayoutCreate(&Bdesc, Btype, transb == CUBLAS_OP_N ? k : n, transb == CUBLAS_OP_N ? n : k, ldb)); checkCublasStatus(cublasLtMatrixLayoutCreate(&Cdesc, Ctype, m, n, ldc)); checkCublasStatus(cublasLtMatrixLayoutCreate(&Ddesc, Dtype, m, n, ldd)); checkCublasStatus(cublasLtMatmulAlgoGetIds(ltHandle, computeType, scaleType, Atype, Btype, Ctype, Dtype, ALGO_IDS, algoIdA, &nbAlgoIds)); // Create CUDA event to time the execution time of each algo checkCudaStatus(cudaEventCreate(&startEvent, cudaEventBlockingSync)); checkCudaStatus(cudaEventCreate(&stopEvent, cudaEventBlockingSync)); int device; checkCudaStatus(cudaGetDevice(&device)); int clusterLaunchSupported; checkCudaStatus(cudaDeviceGetAttribute(&clusterLaunchSupported, cudaDevAttrClusterLaunch, device)); uint16_t cgaLastShape = clusterLaunchSupported ? CUBLASLT_CLUSTER_SHAPE_END : CUBLASLT_CLUSTER_SHAPE_AUTO + 1; // Loop over the Algo IDs for (int idx = 0; (idx < nbAlgoIds) && (AlgoCount < AlgoCombinations); idx++) { cublasLtMatmulAlgo_t algo; size_t sizeWritten = 0; // Initialize algo structure with given Algo ID status = cublasLtMatmulAlgoInit(ltHandle, computeType, scaleType, Atype, Btype, Ctype, Dtype, algoIdA[idx], &algo); if (status != CUBLAS_STATUS_SUCCESS) { continue; } // Query the tiles enums supported by that algo checkCublasStatus(cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_TILE_IDS, NULL, 0, &sizeWritten)); int nbTiles = int(sizeWritten / sizeof(int)); int *tileA = new int[nbTiles == 0 ? 1 : nbTiles]; if (nbTiles == 0) { tileA[0] = CUBLASLT_MATMUL_TILE_UNDEFINED; nbTiles = 1; } checkCublasStatus( cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_STAGES_IDS, NULL, 0, &sizeWritten)); int nbStages = int(sizeWritten / sizeof(int)); std::vector stagesA(nbStages == 0 ? 1 : nbStages); if (nbStages == 0) { stagesA[0] = CUBLASLT_MATMUL_STAGES_UNDEFINED; nbStages = 1; } else { checkCublasStatus(cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_STAGES_IDS, stagesA.data(), sizeof(int) * nbStages, &sizeWritten)); } int splitkSupport, redMask, swizzlingMax, customOptionMax; // Retrieve Algo Capabilities attributes to be able to setup loop over the different combinations cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_TILE_IDS, tileA, sizeof(int) * nbTiles, &sizeWritten); cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_SPLITK_SUPPORT, &splitkSupport, sizeof(splitkSupport), &sizeWritten); cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_REDUCTION_SCHEME_MASK, &redMask, sizeof(redMask), &sizeWritten); cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_CTA_SWIZZLING_SUPPORT, &swizzlingMax, sizeof(swizzlingMax), &sizeWritten); cublasLtMatmulAlgoCapGetAttribute(&algo, CUBLASLT_ALGO_CAP_CUSTOM_OPTION_MAX, &customOptionMax, sizeof(customOptionMax), &sizeWritten); // Loop over the different tiles for (int tileIdx = 0; tileIdx < nbTiles; tileIdx++) { checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute(&algo, CUBLASLT_ALGO_CONFIG_TILE_ID, &tileA[tileIdx], sizeof(tileA[tileIdx]))); // Loop over different stages count for (int stagesIdx = 0; stagesIdx < nbStages; stagesIdx++) { checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_STAGES_ID, &stagesA[stagesIdx], sizeof(stagesA[stagesIdx]))); // Loop over different cluster configurations on devices that support it for (uint16_t cgaIdx = 0; cgaIdx < cgaLastShape; cgaIdx++) { checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute(&algo, CUBLASLT_ALGO_CONFIG_CLUSTER_SHAPE_ID, &cgaIdx, sizeof(cgaIdx))); // Loop over the different custom option if any for (int customOption = 0; customOption <= customOptionMax; customOption++) { checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_CUSTOM_OPTION, &customOption, sizeof(customOption))); // loop over the CTAs swizzling support for (int k = 0; k <= swizzlingMax; k++) { int splitK_trial = 0; if (splitkSupport) { splitK_trial += sizeof(splitKSequenceA) / sizeof(splitKSequenceA[0]); } // Loop over the splitK value over a fixed sequence splitKSequenceA in addtion to the case // where splitK is not enabled for (int l = 0; (l < (1 + splitK_trial)) && (AlgoCount < AlgoCombinations); l++) { int splitK_val = 0; int redScheme = CUBLASLT_REDUCTION_SCHEME_NONE; checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_SPLITK_NUM, &splitK_val, sizeof(splitK_val))); checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_CTA_SWIZZLING, &k, sizeof(k))); checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_REDUCTION_SCHEME, &redScheme, sizeof(int))); if (l > 0) { // Split-K case splitK_val = splitKSequenceA[l - 1]; checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_SPLITK_NUM, &splitKSequenceA[l - 1], sizeof(splitKSequenceA[l - 1]))); // Going over all the reduction schemes for (redScheme = 1; redScheme < (int)CUBLASLT_REDUCTION_SCHEME_MASK && (AlgoCount < AlgoCombinations); redScheme = redScheme << 1) { if (redScheme & redMask) { checkCublasStatus(cublasLtMatmulAlgoConfigSetAttribute( &algo, CUBLASLT_ALGO_CONFIG_REDUCTION_SCHEME, &redScheme, sizeof(redScheme))); status = customMatmulRun(ltHandle, operationDesc, alpha, // host or device pointer A, Adesc, B, Bdesc, beta, // host or device pointer C, Cdesc, D, Ddesc, algo, kernelRepeats, workSpace, workSpaceSize, perfResults[AlgoCount], stream, startEvent, stopEvent); perfResults[AlgoCount].status = status; if (status == CUBLAS_STATUS_SUCCESS) AlgoCount++; } // end if } // end for } else { // Non-splitK case // if user preference is ok with workspace if (AlgoCount < AlgoCombinations) { status = customMatmulRun(ltHandle, operationDesc, alpha, // host or device pointer A, Adesc, B, Bdesc, beta, // host or device pointer C, Cdesc, D, Ddesc, algo, kernelRepeats, workSpace, workSpaceSize, perfResults[AlgoCount], stream, startEvent, stopEvent); perfResults[AlgoCount].status = status; if (status == CUBLAS_STATUS_SUCCESS) AlgoCount++; } } } // end l } // end k } // end customOption } // end cgaIdx } // end stagesIdx } // end tileIdx delete[] tileA; } // end idx // Sort the results per run duration std::sort(perfResults, perfResults + AlgoCount, time_compare); // Print timing and perf details for (int i = 0; i < AlgoCount; i++) { printf("result %03d : ", i); printPerfStructure(perfResults[i]); } // descriptors are no longer needed as all GPU work was already enqueued if (preference) checkCublasStatus(cublasLtMatmulPreferenceDestroy(preference)); if (Cdesc) checkCublasStatus(cublasLtMatrixLayoutDestroy(Cdesc)); if (Bdesc) checkCublasStatus(cublasLtMatrixLayoutDestroy(Bdesc)); if (Adesc) checkCublasStatus(cublasLtMatrixLayoutDestroy(Adesc)); if (operationDesc) checkCublasStatus(cublasLtMatmulDescDestroy(operationDesc)); if (startEvent) checkCudaStatus(cudaEventDestroy(startEvent)); if (stopEvent) checkCudaStatus(cudaEventDestroy(stopEvent)); }