Debian Codegraph @

clients/samples/09_hipblaslt_gemm_amax_ext/sample_hipblaslt_gemm_amax_ext.cpp

raw
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2022-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 simpleGemmAmaxExt(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 an example using hipblaslt extension API.
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, 1.f, 32 * 1024 * 1024);
60
61 runner.run([&runner] {
62 simpleGemmAmaxExt(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 simpleGemmAmaxExt(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 hipblaslt_ext::GemmPreference gemmPref;
101 gemmPref.setMaxWorkspaceBytes(max_workspace_size);
102 hipblaslt_ext::Gemm gemm(handle,
103 trans_a,
104 trans_b,
105 HIP_R_8F_E4M3_FNUZ,
106 HIP_R_8F_E4M3_FNUZ,
107 HIP_R_8F_E4M3_FNUZ,
108 HIP_R_8F_E4M3_FNUZ,
109 HIPBLAS_COMPUTE_32F);
110
111 void* amax;
112 CHECK_HIP_ERROR(hipMalloc(&amax, 1 * sizeof(float)));
113
114 hipblaslt_ext::GemmEpilogue
115 epilogue; // No action needed, default is HIPBLASLT_EPILOGUE_DEFAULT. (Gemm only)
116 hipblaslt_ext::GemmInputs inputs;
117 inputs.setA(d_a);
118 inputs.setB(d_b);
119 inputs.setC(d_c);
120 inputs.setD(d_d);
121 inputs.setAlpha(&alpha);
122 inputs.setBeta(&beta);
123 inputs.setAmaxD(amax);
124 gemm.setProblem(m, n, k, batch_count, epilogue, inputs);
125
126 const int request_solutions = 1;
127 std::vector<hipblasLtMatmulHeuristicResult_t> heuristicResult;
128 CHECK_HIPBLASLT_ERROR(gemm.algoGetHeuristic(request_solutions, gemmPref, heuristicResult));
129
130 if(heuristicResult.empty())
131 {
132 std::cerr << "No valid solution found!" << std::endl;
133 return;
134 }
135
136 // In this sample, the workspace is already allocated with max_workspace_size
137 // If not, calculate the needed workspace_size and allocate d_workspace here
138 // Then initialize gemm with calculated d_workspace and workspace_size
139 // uint64_t workspace_size = 0;
140 // for(int i = 0; i < returnedAlgoCount; i++)
141 // workspace_size = max(workspace_size, heuristicResult[i].workspaceSize);
142 // CHECK_HIP_ERRORhipMalloc(&d_workspace, workspace_size));
143
144 // Make sure to initialize every time when algo changes
145 gemm.setMaxWorkspaceBytes(max_workspace_size);
146 CHECK_HIPBLASLT_ERROR(gemm.initialize(heuristicResult[0].algo, d_workspace));
147 CHECK_HIPBLASLT_ERROR(gemm.run(stream));
148
149 float h_amax = 0.0f; // Host variable to store scaleA
150 CHECK_HIP_ERROR(hipMemcpy(&h_amax, amax, sizeof(float), hipMemcpyDeviceToHost));
151 CHECK_HIP_ERROR(hipStreamSynchronize(stream));
152 std::cout << "Amax value: " << h_amax << std::endl;
153
154 return;
155 }