| 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 |
|
| 27 |
#include <hip/hip_runtime.h> |
| 28 |
#include <hipblaslt/hipblaslt-ext.hpp> |
| 29 |
#include <iostream> |
| 30 |
|
| 31 |
#include "helper.h" |
| 32 |
|
| 33 |
void simplebatchGemmExt(hipblasLtHandle_t handle, |
| 34 |
hipblasOperation_t trans_a, |
| 35 |
hipblasOperation_t trans_b, |
| 36 |
int64_t m, |
| 37 |
int64_t n, |
| 38 |
int64_t k, |
| 39 |
int64_t batch_count, |
| 40 |
float& alpha, |
| 41 |
float& beta, |
| 42 |
void* d_a, |
| 43 |
void* d_b, |
| 44 |
void* d_c, |
| 45 |
void* d_d, |
| 46 |
void* d_workspace, |
| 47 |
int64_t max_workspace_size, |
| 48 |
hipStream_t stream); |
| 49 |
|
| 50 |
int main() |
| 51 |
{ |
| 52 |
/** This is an example using hipblaslt extension API. |
| 53 |
* This is a NN example with |
| 54 |
* a = (m, k). lda = m |
| 55 |
* b = (k, n). ldb = k |
| 56 |
* c = d = (m, n). ldc = ldd = m |
| 57 |
*/ |
| 58 |
Runner<hipblasLtHalf, hipblasLtHalf, hipblasLtHalf, float, float> runner( |
| 59 |
1024, 512, 1024, 10, 1.f, 1.f, 32 * 1024 * 1024); |
| 60 |
|
| 61 |
runner.run([&runner] { |
| 62 |
simplebatchGemmExt(runner.handle, |
| 63 |
HIPBLAS_OP_N, |
| 64 |
HIPBLAS_OP_N, |
| 65 |
runner.m, |
| 66 |
runner.n, |
| 67 |
runner.k, |
| 68 |
runner.batch_count, |
| 69 |
runner.alpha, |
| 70 |
runner.beta, |
| 71 |
runner.d_a, |
| 72 |
runner.d_b, |
| 73 |
runner.d_c, |
| 74 |
runner.d_d, |
| 75 |
runner.d_workspace, |
| 76 |
runner.max_workspace_size, |
| 77 |
runner.stream); |
| 78 |
}); |
| 79 |
|
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
void simplebatchGemmExt(hipblasLtHandle_t handle, |
| 84 |
hipblasOperation_t trans_a, |
| 85 |
hipblasOperation_t trans_b, |
| 86 |
int64_t m, |
| 87 |
int64_t n, |
| 88 |
int64_t k, |
| 89 |
int64_t batch_count, |
| 90 |
float& alpha, |
| 91 |
float& beta, |
| 92 |
void* d_a, |
| 93 |
void* d_b, |
| 94 |
void* d_c, |
| 95 |
void* d_d, |
| 96 |
void* d_workspace, |
| 97 |
int64_t max_workspace_size, |
| 98 |
hipStream_t stream) |
| 99 |
{ |
| 100 |
hipblaslt_ext::GemmPreference gemmPref; |
| 101 |
gemmPref.setMaxWorkspaceBytes(max_workspace_size); |
| 102 |
hipblaslt_ext::Gemm gemm( |
| 103 |
handle, trans_a, trans_b, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIPBLAS_COMPUTE_32F); |
| 104 |
|
| 105 |
hipblaslt_ext::GemmEpilogue |
| 106 |
epilogue; // No action needed, default is HIPBLASLT_EPILOGUE_DEFAULT. (Gemm only) |
| 107 |
hipblaslt_ext::GemmInputs inputs; |
| 108 |
inputs.setA(d_a); |
| 109 |
inputs.setB(d_b); |
| 110 |
inputs.setC(d_c); |
| 111 |
inputs.setD(d_d); |
| 112 |
inputs.setAlpha(&alpha); |
| 113 |
inputs.setBeta(&beta); |
| 114 |
gemm.setProblem(m, n, k, batch_count, epilogue, inputs); |
| 115 |
|
| 116 |
const int request_solutions = 1; |
| 117 |
std::vector<hipblasLtMatmulHeuristicResult_t> heuristicResult; |
| 118 |
CHECK_HIPBLASLT_ERROR(gemm.algoGetHeuristic(request_solutions, gemmPref, heuristicResult)); |
| 119 |
|
| 120 |
if(heuristicResult.empty()) |
| 121 |
{ |
| 122 |
std::cerr << "No valid solution found!" << std::endl; |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
// In this sample, the workspace is already allocated with max_workspace_size |
| 127 |
// If not, calculate the needed workspace_size and allocate d_workspace here |
| 128 |
// Then initialize gemm with calculated d_workspace and workspace_size |
| 129 |
// uint64_t workspace_size = 0; |
| 130 |
// for(int i = 0; i < returnedAlgoCount; i++) |
| 131 |
// workspace_size = max(workspace_size, heuristicResult[i].workspaceSize); |
| 132 |
// CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size)); |
| 133 |
|
| 134 |
// Make sure to initialize every time when algo changes |
| 135 |
gemm.setMaxWorkspaceBytes(max_workspace_size); |
| 136 |
CHECK_HIPBLASLT_ERROR(gemm.initialize(heuristicResult[0].algo, d_workspace)); |
| 137 |
CHECK_HIPBLASLT_ERROR(gemm.run(stream)); |
| 138 |
return; |
| 139 |
} |