Debian Codegraph @

clients/samples/04_hipblaslt_gemm_bias_ext/sample_hipblaslt_gemm_bias_ext.cpp

raw
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2024-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-ext.hpp>
29 #include <iostream>
30
31 #include "helper.h"
32
33 void simpleGemmEpilogueBiasVecExt(hipblasLtHandle_t handle,
34 hipblasOperation_t trans_a,
35 hipblasOperation_t trans_b,
36 hipblasLtEpilogue_t epilogue,
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 Runner<hipblasLtHalf, hipblasLtHalf, hipblasLtHalf, float, float> runner(
54 1024, 512, 1024, 1, 1.f, 1.f, 32 * 1024 * 1024);
55
56 runner.setBiasInfo(true, 'D');
57
58 runner.run([&runner] {
59 simpleGemmEpilogueBiasVecExt(runner.handle,
60 HIPBLAS_OP_N,
61 HIPBLAS_OP_T,
62 HIPBLASLT_EPILOGUE_BIAS,
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 return 0;
78 }
79
80 void simpleGemmEpilogueBiasVecExt(hipblasLtHandle_t handle,
81 hipblasOperation_t trans_a,
82 hipblasOperation_t trans_b,
83 hipblasLtEpilogue_t epilogue,
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 hipblaslt_ext::GemmPreference gemmPref;
99 gemmPref.setMaxWorkspaceBytes(max_workspace_size);
100 hipblaslt_ext::Gemm gemm(
101 handle, trans_a, trans_b, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIPBLAS_COMPUTE_32F);
102
103 hipblaslt_ext::GemmEpilogue gemmEpilogue;
104 gemmEpilogue.setMode(epilogue);
105 gemmEpilogue.setBiasDataType(HIP_R_16F);
106
107 std::vector<hipblasLtHalf> h_bias(m, 1.0f); // Example bias values, adjust as needed
108 void* d_bias;
109 CHECK_HIP_ERROR(hipMalloc(&d_bias, m * sizeof(hipblasLtHalf))); // Allocate memory for bias
110 CHECK_HIP_ERROR(hipMemcpy(d_bias,
111 h_bias.data(),
112 m * sizeof(hipblasLtHalf),
113 hipMemcpyHostToDevice)); // Copy bias to device
114
115 hipblaslt_ext::GemmInputs inputs;
116 inputs.setA(d_a);
117 inputs.setB(d_b);
118 inputs.setC(d_c);
119 inputs.setD(d_d);
120 inputs.setBias(d_bias);
121 inputs.setAlpha(&alpha);
122 inputs.setBeta(&beta);
123 gemm.setProblem(m, n, k, batch_count, gemmEpilogue, inputs);
124
125 const int request_solutions = 1;
126 std::vector<hipblasLtMatmulHeuristicResult_t> heuristicResult;
127 CHECK_HIPBLASLT_ERROR(gemm.algoGetHeuristic(request_solutions, gemmPref, heuristicResult));
128
129 if(heuristicResult.empty())
130 {
131 std::cerr << "No valid solution found!" << std::endl;
132 return;
133 }
134
135 // In this sample, the workspace is already allocated with max_workspace_size
136 // If not, calculate the needed workspace_size and allocate d_workspace here
137 // Then initialize gemm with calculated d_workspace and workspace_size
138 // uint64_t workspace_size = 0;
139 // for(int i = 0; i < returnedAlgoCount; i++)
140 // workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
141 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
142
143 // Make sure to initialize every time when algo changes
144 gemm.setMaxWorkspaceBytes(max_workspace_size);
145 CHECK_HIPBLASLT_ERROR(gemm.initialize(heuristicResult[0].algo, d_workspace));
146 CHECK_HIPBLASLT_ERROR(gemm.run(stream));
147 return;
148 }