Debian Codegraph @

clients/samples/04_hipblaslt_gemm_bias/sample_hipblaslt_gemm_bias.cpp

raw
1 /*******************************************************************************
2 *
3 * Copyright © Advanced Micro Devices, Inc., or its affiliates.
4 * SPDX-License-Identifier: MIT
5 *
6 *******************************************************************************/
7
8 #include <hip/hip_runtime.h>
9 #include <hipblaslt/hipblaslt.h>
10 #include <iostream>
11
12 #include "helper.h"
13
14 void simpleGemmBias(hipblasLtHandle_t handle,
15 hipblasOperation_t trans_a,
16 hipblasOperation_t trans_b,
17 int64_t m,
18 int64_t n,
19 int64_t k,
20 int64_t batch_count,
21 float& alpha,
22 float& beta,
23 void* d_a,
24 void* d_b,
25 void* d_c,
26 void* d_d,
27 void* d_workspace,
28 int64_t max_workspace_size,
29 hipStream_t stream);
30
31 int main()
32 {
33 /** This is a NN example with
34 * a = (m, k). lda = m
35 * b = (k, n). ldb = k
36 * c = d = (m, n). ldc = ldd = m
37 */
38 Runner<hipblasLtHalf, hipblasLtHalf, hipblasLtHalf, float, float> runner(
39 1024, 512, 1024, 1, 1.f, 1.f, 32 * 1024 * 1024);
40
41 runner.run([&runner] {
42 simpleGemmBias(runner.handle,
43 HIPBLAS_OP_N,
44 HIPBLAS_OP_N,
45 runner.m,
46 runner.n,
47 runner.k,
48 runner.batch_count,
49 runner.alpha,
50 runner.beta,
51 runner.d_a,
52 runner.d_b,
53 runner.d_c,
54 runner.d_d,
55 runner.d_workspace,
56 runner.max_workspace_size,
57 runner.stream);
58 });
59
60 return 0;
61 }
62
63 void simpleGemmBias(hipblasLtHandle_t handle,
64 hipblasOperation_t trans_a,
65 hipblasOperation_t trans_b,
66 int64_t m,
67 int64_t n,
68 int64_t k,
69 int64_t batch_count,
70 float& alpha,
71 float& beta,
72 void* d_a,
73 void* d_b,
74 void* d_c,
75 void* d_d,
76 void* d_workspace,
77 int64_t max_workspace_size,
78 hipStream_t stream)
79 {
80 hipblasLtMatrixLayout_t matA, matB, matC, matD;
81 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matA, HIP_R_16F, m, k, m));
82 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matB, HIP_R_16F, k, n, k));
83 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matC, HIP_R_16F, m, n, m));
84 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutCreate(&matD, HIP_R_16F, m, n, m));
85
86 if(batch_count > 1)
87 {
88 int64_t stride_a = m * k;
89 int64_t stride_b = k * n;
90 int64_t stride_c = m * n;
91 int64_t stride_d = m * n;
92 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
93 matA, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
94 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
95 matA, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_a, sizeof(stride_a)));
96 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
97 matB, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
98 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
99 matB, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_b, sizeof(stride_b)));
100 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
101 matC, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
102 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
103 matC, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_c, sizeof(stride_c)));
104 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
105 matD, HIPBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batch_count, sizeof(batch_count)));
106 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutSetAttribute(
107 matD, HIPBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride_d, sizeof(stride_d)));
108 }
109
110 hipblasLtMatmulDesc_t matmul;
111 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescCreate(&matmul, HIPBLAS_COMPUTE_32F, HIP_R_32F));
112 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
113 matmul, HIPBLASLT_MATMUL_DESC_TRANSA, &trans_a, sizeof(int32_t)));
114 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
115 matmul, HIPBLASLT_MATMUL_DESC_TRANSB, &trans_b, sizeof(int32_t)));
116
117 hipblasLtEpilogue_t epilogue = HIPBLASLT_EPILOGUE_BIAS;
118 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
119 matmul, HIPBLASLT_MATMUL_DESC_EPILOGUE, &epilogue, sizeof(epilogue)));
120
121 hipDataType bias_data_type = HIP_R_16F;
122 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(matmul, HIPBLASLT_MATMUL_DESC_BIAS_DATA_TYPE, &bias_data_type, sizeof(hipDataType)));
123
124 // Allocate and set the bias tensor
125 std::vector<hipblasLtHalf> h_bias(m, 1.0f); // Example bias values, adjust as needed
126 void* d_bias;
127 CHECK_HIP_ERROR(hipMalloc(&d_bias, m * sizeof(hipblasLtHalf))); // Allocate memory for bias
128 CHECK_HIP_ERROR(hipMemcpy(d_bias, h_bias.data(), m * sizeof(hipblasLtHalf), hipMemcpyHostToDevice)); // Copy bias to device
129
130 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescSetAttribute(
131 matmul, HIPBLASLT_MATMUL_DESC_BIAS_POINTER, &d_bias, sizeof(void*)));
132
133 // Set User Preference attributes
134 hipblasLtMatmulPreference_t pref;
135 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceCreate(&pref));
136 CHECK_HIPBLASLT_ERROR(
137 hipblasLtMatmulPreferenceSetAttribute(pref,
138 HIPBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
139 &max_workspace_size,
140 sizeof(max_workspace_size)));
141
142 const int request_solutions = 1;
143 hipblasLtMatmulHeuristicResult_t heuristicResult[request_solutions];
144 int returnedAlgoCount = 0;
145 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulAlgoGetHeuristic(handle,
146 matmul,
147 matA,
148 matB,
149 matC,
150 matD,
151 pref,
152 request_solutions,
153 heuristicResult,
154 &returnedAlgoCount));
155
156 if(returnedAlgoCount == 0)
157 {
158 std::cerr << "No valid solution found!" << std::endl;
159 return;
160 }
161
162 uint64_t workspace_size = 0;
163 for(int i = 0; i < returnedAlgoCount; i++)
164 workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
165 // In this sample, the workspace is already allocated with max_workspace_size
166 // If not, allocate d_workspace here
167 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
168 CHECK_HIPBLASLT_ERROR(hipblasLtMatmul(handle,
169 matmul,
170 &alpha,
171 d_a,
172 matA,
173 d_b,
174 matB,
175 &beta,
176 d_c,
177 matC,
178 d_d,
179 matD,
180 &heuristicResult[0].algo,
181 d_workspace,
182 workspace_size,
183 stream));
184 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matA));
185 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matB));
186 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matC));
187 CHECK_HIPBLASLT_ERROR(hipblasLtMatrixLayoutDestroy(matD));
188 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulDescDestroy(matmul));
189 CHECK_HIPBLASLT_ERROR(hipblasLtMatmulPreferenceDestroy(pref));
190 return;
191 }