Debian Codegraph @

clients/samples/09_hipblaslt_gemm_amax/sample_hipblaslt_gemm_amax.cpp

raw
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-ext-op.h>
29 #include <hipblaslt/hipblaslt.h>
30 #include <iostream>
31
32 #include "helper.h"
33
34 void simpleGemmAmaxWithScale(hipblasLtHandle_t handle,
35 hipblasOperation_t trans_a,
36 hipblasOperation_t trans_b,
37 int64_t m,
38 int64_t n,
39 int64_t k,
40 int64_t batch_count,
41 float& alpha,
42 float& beta,
43 void* d_a,
44 void* d_b,
45 void* d_c,
46 void* d_d,
47 void* d_workspace,
48 int64_t max_workspace_size,
49 hipStream_t stream);
50
51 int main()
52 {
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<hipblaslt_f8_fnuz, hipblaslt_f8_fnuz, hipblaslt_f8_fnuz, float, float> runner(
59 1024, 512, 1024, 1, 1.f, 0.f, 32 * 1024 * 1024);
60
61 runner.run([&runner] {
62 simpleGemmAmaxWithScale(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 simpleGemmAmaxWithScale(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 // allocate data for amax
101 void *out_tmp, *in_scale, *out_amax; // host
102 void *d_out_tmp, *d_in_scale, *d_out_amax; // device
103
104 CHECK_HIP_ERROR(hipMalloc(&d_in_scale, 1 * sizeof(float)));
105 CHECK_HIP_ERROR(hipMalloc(&d_out_amax, 1 * sizeof(float)));
106
107 CHECK_HIP_ERROR(hipHostMalloc(&in_scale, 1 * sizeof(float)));
108 CHECK_HIP_ERROR(hipHostMalloc(&out_amax, 1 * sizeof(float)));
109
110 // copy amax data to device
111 *(float*)in_scale = (float)1.0;
112 CHECK_HIP_ERROR(
113 hipMemcpyAsync(d_in_scale, in_scale, 1 * sizeof(float), hipMemcpyHostToDevice, stream));
114
115 // set matrix layout for gemm
116 hipblasLtMatrixLayout_t matA, matB, matC, matD;
117 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matA, HIP_R_8F_E4M3_FNUZ, m, k, m));
118 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matB, HIP_R_8F_E4M3_FNUZ, k, n, k));
119 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matC, HIP_R_8F_E4M3_FNUZ, m, n, m));
120 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matD, HIP_R_8F_E4M3_FNUZ, m, n, m));
121
122 hipblasLtMatmulDesc_t matmul;
123 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescCreate(&matmul, HIPBLAS_COMPUTE_32F, HIP_R_32F));
124 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
125 matmul, HIPBLASLT_MATMUL_DESC_TRANSA, &trans_a, sizeof(int32_t)));
126 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
127 matmul, HIPBLASLT_MATMUL_DESC_TRANSB, &trans_b, sizeof(int32_t)));
128
129 hipblasLtEpilogue_t epilogue = HIPBLASLT_EPILOGUE_DEFAULT;
130 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
131 matmul, HIPBLASLT_MATMUL_DESC_EPILOGUE, &epilogue, sizeof(epilogue)));
132 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
133 matmul, HIPBLASLT_MATMUL_DESC_AMAX_D_POINTER, &d_out_amax, sizeof(void*)));
134 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
135 matmul, HIPBLASLT_MATMUL_DESC_D_SCALE_POINTER, &d_in_scale, sizeof(void*)));
136
137 // Set User Preference attributes
138 hipblasLtMatmulPreference_t pref;
139 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceCreate(&pref));
140 CHECK_HIPBLASLT_ERROR(
141 hipblasLtMatmulPreferenceSetAttribute(pref,
142 HIPBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
143 &max_workspace_size,
144 sizeof(max_workspace_size)));
145
146 const int request_solutions = 1;
147 hipblasLtMatmulHeuristicResult_t heuristicResult[request_solutions];
148 int returnedAlgoCount = 0;
149 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulAlgoGetHeuristic(handle,
150 matmul,
151 matA,
152 matB,
153 matC,
154 matD,
155 pref,
156 request_solutions,
157 heuristicResult,
158 &returnedAlgoCount));
159
160 if(returnedAlgoCount == 0)
161 {
162 std::cerr << "No valid solution found!" << std::endl;
163 return;
164 }
165
166 uint64_t workspace_size = max_workspace_size;
167 for(int i = 0; i < returnedAlgoCount; i++)
168 workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
169 // In this sample, the workspace is already allocated with max_workspace_size
170 // If not, allocate d_workspace here
171 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
172
173 CHECK_HIPBLASLT_ERROR(hipblasLtMatmul(handle,
174 matmul,
175 &alpha,
176 d_a,
177 matA,
178 d_b,
179 matB,
180 &beta,
181 d_c,
182 matC,
183 d_d,
184 matD,
185 &heuristicResult[0].algo,
186 d_workspace,
187 workspace_size,
188 stream));
189
190 // deallocate memory space of amax
191 CHECK_HIP_ERROR(hipFree(d_in_scale));
192 CHECK_HIP_ERROR(hipFree(d_out_amax));
193
194 CHECK_HIP_ERROR(hipFree(in_scale));
195 CHECK_HIP_ERROR(hipFree(out_amax));
196
197 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matA));
198 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matB));
199 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matC));
200 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matD));
201 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescDestroy(matmul));
202 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceDestroy(pref));
203
204 return;
205 }