Debian Codegraph @

clients/samples/24_hipblaslt_gemm_with_TF32/sample_hipblaslt_gemm_with_TF32.cpp

raw
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.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<float, float, float, float, float> runner(128, 128, 128, 1, 1.f, 1.f, 32 * 128 * 128);
58
59 runner.run([&runner] {
60 simpleGemm(runner.handle,
61 HIPBLAS_OP_N,
62 HIPBLAS_OP_N,
63 runner.m,
64 runner.n,
65 runner.k,
66 runner.batch_count,
67 runner.alpha,
68 runner.beta,
69 runner.d_a,
70 runner.d_b,
71 runner.d_c,
72 runner.d_d,
73 runner.d_workspace,
74 runner.max_workspace_size,
75 runner.stream);
76 });
77
78 return 0;
79 }
80
81 void simpleGemm(hipblasLtHandle_t handle,
82 hipblasOperation_t trans_a,
83 hipblasOperation_t trans_b,
84 int64_t m,
85 int64_t n,
86 int64_t k,
87 int64_t batch_count,
88 float& alpha,
89 float& beta,
90 void* d_a,
91 void* d_b,
92 void* d_c,
93 void* d_d,
94 void* d_workspace,
95 int64_t max_workspace_size,
96 hipStream_t stream)
97 {
98 hipblasLtMatrixLayout_t matA, matB, matC, matD;
99 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matA, HIP_R_32F, m, k, m));
100 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matB, HIP_R_32F, k, n, k));
101 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matC, HIP_R_32F, m, n, m));
102 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matD, HIP_R_32F, m, n, m));
103
104 if(batch_count > 1)
105 {
106 int64_t stride_a = m * k;
107 int64_t stride_b = k * n;
108 int64_t stride_c = m * n;
109 int64_t stride_d = m * n;
110 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
111 matA, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
112 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
113 matA, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_a, sizeof(stride_a)));
114 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
115 matB, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
116 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
117 matB, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_b, sizeof(stride_b)));
118 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
119 matC, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
120 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
121 matC, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_c, sizeof(stride_c)));
122 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
123 matD, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
124 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
125 matD, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_d, sizeof(stride_d)));
126 }
127
128 hipblasLtMatmulDesc_t matmul;
129 CHECK_HIPBLASLT_ERROR(
130 hipblasLtMatmulDescCreate(&matmul, HIPBLAS_COMPUTE_32F_FAST_TF32, 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 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matA));
167 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matB));
168 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matC));
169 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matD));
170 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescDestroy(matmul));
171 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceDestroy(pref));
172 return;
173 }
174
175 uint64_t workspace_size = 0;
176 for(int i = 0; i < returnedAlgoCount; i++)
177 workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
178 // In this sample, the workspace is already allocated with max_workspace_size
179 // If not, allocate d_workspace here
180 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
181
182 CHECK_HIPBLASLT_ERROR(hipblasLtMatmul(handle,
183 matmul,
184 &alpha,
185 d_a,
186 matA,
187 d_b,
188 matB,
189 &beta,
190 d_c,
191 matC,
192 d_d,
193 matD,
194 &heuristicResult[0].algo,
195 d_workspace,
196 workspace_size,
197 stream));
198 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matA));
199 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matB));
200 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matC));
201 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matD));
202 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescDestroy(matmul));
203 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceDestroy(pref));
204 return;
205 }