| 1 |
/******************************************************************************* |
| 2 |
* |
| 3 |
* MIT License |
| 4 |
* |
| 5 |
* Copyright (C) 2022-2025 Advanced Micro Devices, Inc. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
* SOFTWARE. |
| 24 |
* |
| 25 |
*******************************************************************************/ |
| 26 |
#include <hip/hip_runtime.h> |
| 27 |
#include <hipblaslt/hipblaslt-ext.hpp> |
| 28 |
#include <iostream> |
| 29 |
|
| 30 |
#include "helper.h" |
| 31 |
|
| 32 |
void simpleGroupedGemmExt(hipblasLtHandle_t handle, |
| 33 |
hipblasOperation_t trans_a, |
| 34 |
hipblasOperation_t trans_b, |
| 35 |
std::vector<int64_t>& m, |
| 36 |
std::vector<int64_t>& n, |
| 37 |
std::vector<int64_t>& k, |
| 38 |
std::vector<int64_t>& batch_count, |
| 39 |
std::vector<float>& alpha, |
| 40 |
std::vector<float>& beta, |
| 41 |
std::vector<void*>& d_a, |
| 42 |
std::vector<void*>& d_b, |
| 43 |
std::vector<void*>& d_c, |
| 44 |
std::vector<void*>& d_d, |
| 45 |
void* d_workspace, |
| 46 |
int64_t max_workspace_size, |
| 47 |
hipStream_t stream); |
| 48 |
|
| 49 |
int main() |
| 50 |
{ |
| 51 |
/** This is an example using hipblaslt extension API. |
| 52 |
* This is a NN example with |
| 53 |
* a = (m, k). lda = m |
| 54 |
* b = (k, n). ldb = k |
| 55 |
* c = d = (m, n). ldc = ldd = m |
| 56 |
*/ |
| 57 |
std::vector<int64_t> m = {1024, 512}; |
| 58 |
std::vector<int64_t> n = {512, 512}; |
| 59 |
std::vector<int64_t> k = {1920, 128}; |
| 60 |
std::vector<int64_t> batch_count = {1, 1}; |
| 61 |
std::vector<float> alpha = {1.0f, 1.0f}; |
| 62 |
std::vector<float> beta = {1.0f, 1.0f}; |
| 63 |
RunnerVec<hipblasLtHalf, hipblasLtHalf, hipblasLtHalf, float, float> runner( |
| 64 |
m, n, k, batch_count, alpha, beta, 32 * 1024 * 1024); |
| 65 |
|
| 66 |
runner.run([&runner] { |
| 67 |
simpleGroupedGemmExt(runner.handle, |
| 68 |
HIPBLAS_OP_N, |
| 69 |
HIPBLAS_OP_N, |
| 70 |
runner.m, |
| 71 |
runner.n, |
| 72 |
runner.k, |
| 73 |
runner.batch_count, |
| 74 |
runner.alpha, |
| 75 |
runner.beta, |
| 76 |
runner.d_a, |
| 77 |
runner.d_b, |
| 78 |
runner.d_c, |
| 79 |
runner.d_d, |
| 80 |
runner.d_workspace, |
| 81 |
runner.max_workspace_size, |
| 82 |
runner.stream); |
| 83 |
}); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
void simpleGroupedGemmExt(hipblasLtHandle_t handle, |
| 89 |
hipblasOperation_t trans_a, |
| 90 |
hipblasOperation_t trans_b, |
| 91 |
std::vector<int64_t>& m, |
| 92 |
std::vector<int64_t>& n, |
| 93 |
std::vector<int64_t>& k, |
| 94 |
std::vector<int64_t>& batch_count, |
| 95 |
std::vector<float>& alpha, |
| 96 |
std::vector<float>& beta, |
| 97 |
std::vector<void*>& d_a, |
| 98 |
std::vector<void*>& d_b, |
| 99 |
std::vector<void*>& d_c, |
| 100 |
std::vector<void*>& d_d, |
| 101 |
void* d_workspace, |
| 102 |
int64_t max_workspace_size, |
| 103 |
hipStream_t stream) |
| 104 |
{ |
| 105 |
hipblaslt_ext::GemmPreference gemmPref; |
| 106 |
gemmPref.setMaxWorkspaceBytes(max_workspace_size); |
| 107 |
hipblaslt_ext::GroupedGemm groupedgemm( |
| 108 |
handle, trans_a, trans_b, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIPBLAS_COMPUTE_32F); |
| 109 |
|
| 110 |
std::vector<hipblaslt_ext::GemmEpilogue> epilogue{ |
| 111 |
hipblaslt_ext:: |
| 112 |
GemmEpilogue()}; // No action needed, default is HIPBLASLT_EPILOGUE_DEFAULT. (Gemm only) |
| 113 |
std::vector<hipblaslt_ext::GemmInputs> inputs(m.size()); |
| 114 |
for(int i = 0; i < m.size(); i++) |
| 115 |
{ |
| 116 |
inputs[i].setA(d_a[i]); |
| 117 |
inputs[i].setB(d_b[i]); |
| 118 |
inputs[i].setC(d_c[i]); |
| 119 |
inputs[i].setD(d_d[i]); |
| 120 |
inputs[i].setAlpha(&alpha[i]); |
| 121 |
inputs[i].setBeta(&beta[i]); |
| 122 |
} |
| 123 |
// hipblaslt_ext::GemmEpilogue supports broadcasting |
| 124 |
groupedgemm.setProblem(m, n, k, batch_count, epilogue, inputs); |
| 125 |
|
| 126 |
const int request_solutions = 1; |
| 127 |
std::vector<hipblasLtMatmulHeuristicResult_t> heuristicResult; |
| 128 |
CHECK_HIPBLASLT_ERROR( |
| 129 |
groupedgemm.algoGetHeuristic(request_solutions, gemmPref, heuristicResult)); |
| 130 |
|
| 131 |
if(heuristicResult.empty()) |
| 132 |
{ |
| 133 |
std::cerr << "No valid solution found! Please try get_all_algos instead" << std::endl; |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// In this sample, the workspace is already allocated with max_workspace_size |
| 138 |
// If not, calculate the needed workspace_size and allocate d_workspace here |
| 139 |
// Then initialize gemm with calculated d_workspace and workspace_size |
| 140 |
// uint64_t workspace_size = 0; |
| 141 |
// for(int i = 0; i < returnedAlgoCount; i++) |
| 142 |
// workspace_size = max(workspace_size, heuristicResult[i].workspaceSize); |
| 143 |
// CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size)); |
| 144 |
|
| 145 |
// Get the default values from the grouepdgemm object |
| 146 |
hipblaslt_ext::UserArguments* userArgs; |
| 147 |
CHECK_HIP_ERROR(hipHostMalloc(&userArgs, m.size() * sizeof(hipblaslt_ext::UserArguments))); |
| 148 |
groupedgemm.getDefaultValueForDeviceUserArguments(userArgs); |
| 149 |
// Copy them to device memory |
| 150 |
hipblaslt_ext::UserArguments* d_userArgs; |
| 151 |
CHECK_HIP_ERROR(hipMalloc(&d_userArgs, m.size() * sizeof(hipblaslt_ext::UserArguments))); |
| 152 |
CHECK_HIP_ERROR(hipMemcpy(d_userArgs, |
| 153 |
userArgs, |
| 154 |
m.size() * sizeof(hipblaslt_ext::UserArguments), |
| 155 |
hipMemcpyHostToDevice)); |
| 156 |
|
| 157 |
// Make sure to initialize every time when algo changes |
| 158 |
groupedgemm.setMaxWorkspaceBytes(max_workspace_size); |
| 159 |
CHECK_HIPBLASLT_ERROR(groupedgemm.initialize(heuristicResult[0].algo, d_workspace)); |
| 160 |
CHECK_HIPBLASLT_ERROR(groupedgemm.run(d_userArgs, stream)); |
| 161 |
|
| 162 |
CHECK_HIP_ERROR(hipFree(userArgs)); |
| 163 |
CHECK_HIP_ERROR(hipFree(d_userArgs)); |
| 164 |
return; |
| 165 |
} |