Debian Codegraph @

clients/samples/19_hipblaslt_gemm_mix_precision/sample_hipblaslt_gemm_mix_precision.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.h>
29 #include <iostream>
30
31 #include "helper.h"
32
33 void simpleGemmMixPrecision(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 mixed-precision NN example with
53 * a = (m, k) in FP16, lda = m
54 * b = (k, n) in FP16, ldb = k
55 * c = d = (m, n) in FP16, ldc = ldd = m
56 * Compute type is FP32.
57 */
58 Runner<hipblaslt_f8_fnuz, hipblasLtHalf, float, float, float> runner(
59 1024, 512, 1024, 1, 1.f, 1.f, 32 * 1024 * 1024);
60
61 runner.run([&runner] {
62 simpleGemmMixPrecision(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 simpleGemmMixPrecision(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 // Use half precision for input matrices
101 hipblasLtMatrixLayout_t matA, matB, matC, matD;
102 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matA, HIP_R_8F_E4M3_FNUZ, m, k, m));
103 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matB, HIP_R_16F, k, n, k));
104 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matC, HIP_R_32F, m, n, m));
105 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matD, HIP_R_32F, m, n, m));
106
107 if(batch_count > 1)
108 {
109 int64_t stride_a = m * k;
110 int64_t stride_b = k * n;
111 int64_t stride_c = m * n;
112 int64_t stride_d = m * n;
113 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
114 matA, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
115 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
116 matA, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_a, sizeof(stride_a)));
117 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
118 matB, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
119 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
120 matB, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_b, sizeof(stride_b)));
121 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
122 matC, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
123 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
124 matC, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_c, sizeof(stride_c)));
125 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
126 matD, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
127 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
128 matD, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_d, sizeof(stride_d)));
129 }
130
131 hipblasLtMatmulDesc_t matmul;
132 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescCreate(&matmul, HIPBLAS_COMPUTE_32F_FAST_16F, HIP_R_32F));
133 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
134 matmul, HIPBLASLT_MATMUL_DESC_TRANSA, &trans_a, sizeof(int32_t)));
135 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
136 matmul, HIPBLASLT_MATMUL_DESC_TRANSB, &trans_b, sizeof(int32_t)));
137
138 float h_scale_a = 2.f;
139 float* d_scale_a;
140 CHECK_HIP_ERROR(hipMalloc(&d_scale_a, sizeof(float)));
141 CHECK_HIP_ERROR(hipMemcpyAsync(d_scale_a, &h_scale_a, sizeof(float), hipMemcpyHostToDevice, stream));
142 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
143 matmul, HIPBLASLT_MATMUL_DESC_A_SCALE_POINTER, &d_scale_a, sizeof(float*)));
144
145 hipblasLtEpilogue_t epilogue = HIPBLASLT_EPILOGUE_DEFAULT;
146 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
147 matmul, HIPBLASLT_MATMUL_DESC_EPILOGUE, &epilogue, sizeof(epilogue)));
148
149 // Set User Preference attributes
150 hipblasLtMatmulPreference_t pref;
151 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceCreate(&pref));
152 CHECK_HIPBLASLT_ERROR(
153 hipblasLtMatmulPreferenceSetAttribute(pref,
154 HIPBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
155 &max_workspace_size,
156 sizeof(max_workspace_size)));
157
158 const int request_solutions = 1;
159 hipblasLtMatmulHeuristicResult_t heuristicResult[request_solutions];
160 int returnedAlgoCount = 0;
161 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulAlgoGetHeuristic(handle,
162 matmul,
163 matA,
164 matB,
165 matC,
166 matD,
167 pref,
168 request_solutions,
169 heuristicResult,
170 &returnedAlgoCount));
171
172 if(returnedAlgoCount == 0)
173 {
174 std::cerr << "No valid solution found!" << std::endl;
175 return;
176 }
177
178 uint64_t workspace_size = 0;
179 for(int i = 0; i < returnedAlgoCount; i++)
180 workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
181 // In this sample, the workspace is already allocated with max_workspace_size
182 // If not, allocate d_workspace here
183 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
184
185 // Perform the mixed-precision GEMM operation
186 CHECK_HIPBLASLT_ERROR(hipblasLtMatmul(handle,
187 matmul,
188 &alpha,
189 d_a,
190 matA,
191 d_b,
192 matB,
193 &beta,
194 d_c,
195 matC,
196 d_d,
197 matD,
198 &heuristicResult[0].algo,
199 d_workspace,
200 workspace_size,
201 stream));
202
203 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matA));
204 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matB));
205 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matC));
206 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matD));
207 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescDestroy(matmul));
208 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceDestroy(pref));
209 return;
210 }