Debian Codegraph @

clients/samples/07_hipblaslt_gemm_alphavec_ext/sample_hipblaslt_gemm_alphavec_ext.cpp

raw
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2023-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 simpleGemmAlphaVecExt(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_alphaVec,
47 void* d_workspace,
48 int64_t max_workspace_size,
49 hipStream_t stream);
50
51 int main()
52 {
53 /** This is an example using hipblaslt extension API.
54 * This is a NN example with
55 * a = (m, k). lda = m
56 * b = (k, n). ldb = k
57 * c = d = (m, n). ldc = ldd = m
58 */
59 Runner<hipblasLtHalf, hipblasLtHalf, hipblasLtHalf, float, float> runner(
60 1024, 512, 1024, 1, 1.f, 1.f, 32 * 1024 * 1024);
61
62 runner.run([&runner] {
63 simpleGemmAlphaVecExt(runner.handle,
64 HIPBLAS_OP_N,
65 HIPBLAS_OP_N,
66 runner.m,
67 runner.n,
68 runner.k,
69 runner.batch_count,
70 runner.alpha,
71 runner.beta,
72 runner.d_a,
73 runner.d_b,
74 runner.d_c,
75 runner.d_d,
76 runner.d_alphaVec,
77 runner.d_workspace,
78 runner.max_workspace_size,
79 runner.stream);
80 });
81
82 return 0;
83 }
84
85 void simpleGemmAlphaVecExt(hipblasLtHandle_t handle,
86 hipblasOperation_t trans_a,
87 hipblasOperation_t trans_b,
88 int64_t m,
89 int64_t n,
90 int64_t k,
91 int64_t batch_count,
92 float& alpha,
93 float& beta,
94 void* d_a,
95 void* d_b,
96 void* d_c,
97 void* d_d,
98 void* d_alphaVec,
99 void* d_workspace,
100 int64_t max_workspace_size,
101 hipStream_t stream)
102 {
103 hipblaslt_ext::GemmPreference gemmPref;
104 gemmPref.setMaxWorkspaceBytes(max_workspace_size);
105 hipblaslt_ext::Gemm gemm(
106 handle, trans_a, trans_b, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIP_R_16F, HIPBLAS_COMPUTE_32F);
107
108 hipblaslt_ext::GemmEpilogue
109 epilogue; // No action needed, default is HIPBLASLT_EPILOGUE_DEFAULT. (Gemm only)
110 hipblaslt_ext::GemmInputs inputs;
111 inputs.setA(d_a);
112 inputs.setB(d_b);
113 inputs.setC(d_c);
114 inputs.setD(d_d);
115 inputs.setAlpha(&alpha);
116 inputs.setBeta(&beta);
117 inputs.setScaleAlphaVec(d_alphaVec); // use AlphaVec mode
118 gemm.setProblem(m, n, k, batch_count, epilogue, inputs);
119
120 const int request_solutions = 1;
121 std::vector<hipblasLtMatmulHeuristicResult_t> heuristicResult;
122 CHECK_HIPBLASLT_ERROR(gemm.algoGetHeuristic(request_solutions, gemmPref, heuristicResult));
123
124 if(heuristicResult.empty())
125 {
126 std::cerr << "No valid solution found!" << std::endl;
127 return;
128 }
129
130 // In this sample, the workspace is already allocated with max_workspace_size
131 // If not, calculate the needed workspace_size and allocate d_workspace here
132 // Then initialize gemm with calculated d_workspace and workspace_size
133 // uint64_t workspace_size = 0;
134 // for(int i = 0; i < returnedAlgoCount; i++)
135 // workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
136 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
137 if(max_workspace_size < (int64_t)heuristicResult[0].workspaceSize)
138 {
139 std::cerr << "Allocated workspace size is too small!" << std::endl;
140 return;
141 }
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 }