| 1 |
/******************************************************************************* |
| 2 |
* |
| 3 |
* MIT License |
| 4 |
* |
| 5 |
* Copyright (C) 2022-2024 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.h> |
| 29 |
#include <iostream> |
| 30 |
|
| 31 |
#include "helper.h" |
| 32 |
|
| 33 |
void simpleGemm(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 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 |
Runner<hipblasLtHalf, hipblasLtHalf, hipblasLtHalf, float, float> runner( |
| 58 |
1024, 512, 1024, 1, 1.f, 1.f, 32 * 1024 * 1024); |
| 59 |
|
| 60 |
runner.run([&runner] { |
| 61 |
simpleGemm(runner.handle, |
| 62 |
HIPBLAS_OP_N, |
| 63 |
HIPBLAS_OP_N, |
| 64 |
runner.m, |
| 65 |
runner.n, |
| 66 |
runner.k, |
| 67 |
runner.batch_count, |
| 68 |
runner.alpha, |
| 69 |
runner.beta, |
| 70 |
runner.d_a, |
| 71 |
runner.d_b, |
| 72 |
runner.d_c, |
| 73 |
runner.d_d, |
| 74 |
runner.d_workspace, |
| 75 |
runner.max_workspace_size, |
| 76 |
runner.stream); |
| 77 |
}); |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
void simpleGemm(hipblasLtHandle_t handle, |
| 83 |
hipblasOperation_t trans_a, |
| 84 |
hipblasOperation_t trans_b, |
| 85 |
int64_t m, |
| 86 |
int64_t n, |
| 87 |
int64_t k, |
| 88 |
int64_t batch_count, |
| 89 |
float& alpha, |
| 90 |
float& beta, |
| 91 |
void* d_a, |
| 92 |
void* d_b, |
| 93 |
void* d_c, |
| 94 |
void* d_d, |
| 95 |
void* d_workspace, |
| 96 |
int64_t max_workspace_size, |
| 97 |
hipStream_t stream) |
| 98 |
{ |
| 99 |
hipblasLtMatrixLayout_t matA, matB, matC, matD; |
| 100 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matA, HIP_R_16F, m, k, m)); |
| 101 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matB, HIP_R_16F, k, n, k)); |
| 102 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matC, HIP_R_16F, m, n, m)); |
| 103 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matD, HIP_R_16F, m, n, m)); |
| 104 |
|
| 105 |
if(batch_count > 1) |
| 106 |
{ |
| 107 |
int64_t stride_a = m * k; |
| 108 |
int64_t stride_b = k * n; |
| 109 |
int64_t stride_c = m * n; |
| 110 |
int64_t stride_d = m * n; |
| 111 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 112 |
matA, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count))); |
| 113 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 114 |
matA, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_a, sizeof(stride_a))); |
| 115 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 116 |
matB, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count))); |
| 117 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 118 |
matB, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_b, sizeof(stride_b))); |
| 119 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 120 |
matC, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count))); |
| 121 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 122 |
matC, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_c, sizeof(stride_c))); |
| 123 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 124 |
matD, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count))); |
| 125 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute( |
| 126 |
matD, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_d, sizeof(stride_d))); |
| 127 |
} |
| 128 |
|
| 129 |
hipblasLtMatmulDesc_t matmul; |
| 130 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescCreate(&matmul, HIPBLAS_COMPUTE_32F, HIP_R_32F)); |
| 131 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute( |
| 132 |
matmul, HIPBLASLT_MATMUL_DESC_TRANSA, &trans_a, sizeof(int32_t))); |
| 133 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute( |
| 134 |
matmul, HIPBLASLT_MATMUL_DESC_TRANSB, &trans_b, sizeof(int32_t))); |
| 135 |
|
| 136 |
hipblasLtEpilogue_t epilogue = HIPBLASLT_EPILOGUE_DEFAULT; |
| 137 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute( |
| 138 |
matmul, HIPBLASLT_MATMUL_DESC_EPILOGUE, &epilogue, sizeof(epilogue))); |
| 139 |
|
| 140 |
// Set User Preference attributes |
| 141 |
hipblasLtMatmulPreference_t pref; |
| 142 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceCreate(&pref)); |
| 143 |
CHECK_HIPBLASLT_ERROR( |
| 144 |
hipblasLtMatmulPreferenceSetAttribute(pref, |
| 145 |
HIPBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, |
| 146 |
&max_workspace_size, |
| 147 |
sizeof(max_workspace_size))); |
| 148 |
|
| 149 |
const int request_solutions = 1; |
| 150 |
hipblasLtMatmulHeuristicResult_t heuristicResult[request_solutions]; |
| 151 |
int returnedAlgoCount = 0; |
| 152 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulAlgoGetHeuristic(handle, |
| 153 |
matmul, |
| 154 |
matA, |
| 155 |
matB, |
| 156 |
matC, |
| 157 |
matD, |
| 158 |
pref, |
| 159 |
request_solutions, |
| 160 |
heuristicResult, |
| 161 |
&returnedAlgoCount)); |
| 162 |
|
| 163 |
if(returnedAlgoCount == 0) |
| 164 |
{ |
| 165 |
std::cerr << "No valid solution found!" << std::endl; |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
uint64_t workspace_size = 0; |
| 170 |
for(int i = 0; i < returnedAlgoCount; i++) |
| 171 |
workspace_size = max(workspace_size, heuristicResult[i].workspaceSize); |
| 172 |
// In this sample, the workspace is already allocated with max_workspace_size |
| 173 |
// If not, allocate d_workspace here |
| 174 |
// CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size)); |
| 175 |
|
| 176 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmul(handle, |
| 177 |
matmul, |
| 178 |
&alpha, |
| 179 |
d_a, |
| 180 |
matA, |
| 181 |
d_b, |
| 182 |
matB, |
| 183 |
&beta, |
| 184 |
d_c, |
| 185 |
matC, |
| 186 |
d_d, |
| 187 |
matD, |
| 188 |
&heuristicResult[0].algo, |
| 189 |
d_workspace, |
| 190 |
workspace_size, |
| 191 |
stream)); |
| 192 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matA)); |
| 193 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matB)); |
| 194 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matC)); |
| 195 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matD)); |
| 196 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescDestroy(matmul)); |
| 197 |
CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceDestroy(pref)); |
| 198 |
return; |
| 199 |
} |