Prompt48 commited on
Commit
9d70d45
·
verified ·
1 Parent(s): 71bec01

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\torch\include\ATen\cuda\tunable\GemmRocblas.h with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//torch//include//ATen//cuda//tunable//GemmRocblas.h ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ #pragma once
5
+
6
+ #include <ATen/cuda/CUDAContext.h>
7
+ #include <ATen/cuda/tunable/TunableOp.h>
8
+ #include <ATen/cuda/tunable/GemmCommon.h>
9
+ #include <c10/util/StringUtil.h>
10
+ #include <fmt/printf.h>
11
+
12
+ #define ROCBLAS_BETA_FEATURES_API
13
+ #include <rocblas/rocblas.h>
14
+
15
+ #define TORCH_ROCBLAS_CHECK(EXPR) \
16
+ do { \
17
+ rocblas_status __err = EXPR; \
18
+ TORCH_CHECK(__err == rocblas_status_success, \
19
+ "rocblas error: ", \
20
+ rocblas_status_to_string(__err), \
21
+ " when calling `" #EXPR "`"); \
22
+ } while (0)
23
+
24
+ namespace at::cuda::tunable {
25
+
26
+ template <typename T>
27
+ constexpr rocblas_datatype RocBlasDataTypeFor();
28
+
29
+ template <>
30
+ constexpr rocblas_datatype RocBlasDataTypeFor<float>() {
31
+ return rocblas_datatype_f32_r;
32
+ }
33
+
34
+ template <>
35
+ constexpr rocblas_datatype RocBlasDataTypeFor<double>() {
36
+ return rocblas_datatype_f64_r;
37
+ }
38
+
39
+ template <>
40
+ constexpr rocblas_datatype RocBlasDataTypeFor<Half>() {
41
+ return rocblas_datatype_f16_r;
42
+ }
43
+
44
+ template <>
45
+ constexpr rocblas_datatype RocBlasDataTypeFor<BFloat16>() {
46
+ return rocblas_datatype_bf16_r;
47
+ }
48
+
49
+ template <>
50
+ constexpr rocblas_datatype RocBlasDataTypeFor<c10::complex<float>>() {
51
+ return rocblas_datatype_f32_c;
52
+ }
53
+
54
+ template <>
55
+ constexpr rocblas_datatype RocBlasDataTypeFor<c10::complex<double>>() {
56
+ return rocblas_datatype_f64_c;
57
+ }
58
+
59
+ template <typename T>
60
+ constexpr rocblas_datatype RocBlasComputeTypeFor();
61
+
62
+ template <>
63
+ constexpr rocblas_datatype RocBlasComputeTypeFor<float>() {
64
+ return rocblas_datatype_f32_r;
65
+ }
66
+
67
+ template <>
68
+ constexpr rocblas_datatype RocBlasComputeTypeFor<double>() {
69
+ return rocblas_datatype_f64_r;
70
+ }
71
+
72
+ template <>
73
+ constexpr rocblas_datatype RocBlasComputeTypeFor<Half>() {
74
+ // Note that we're returning the _compute_ type for a given datatype.
75
+ // As of 12/2022, using compute type FP16 for 16-bit floats was much
76
+ // slower than using compute type FP32. So we use FP32 compute even for
77
+ // FP16 datatypes. This is how GEMM is implemented even in the function
78
+ // rocblasGemmHelper (see fpgeneric.h)
79
+ return rocblas_datatype_f32_r;
80
+ }
81
+
82
+ template <>
83
+ constexpr rocblas_datatype RocBlasComputeTypeFor<BFloat16>() {
84
+ // Note that we're returning the _compute_ type for a given datatype.
85
+ // As of 12/2022, using compute type FP16 for 16-bit floats was much
86
+ // slower than using compute type FP32. So we use FP32 compute even for
87
+ // BF16 datatypes. This is how GEMM is implemented even in the function
88
+ // rocblasGemmHelper (see fpgeneric.h)
89
+ return rocblas_datatype_f32_r;
90
+ }
91
+
92
+ template <>
93
+ constexpr rocblas_datatype RocBlasComputeTypeFor<c10::complex<float>>() {
94
+ return rocblas_datatype_f32_c;
95
+ }
96
+
97
+ template <>
98
+ constexpr rocblas_datatype RocBlasComputeTypeFor<c10::complex<double>>() {
99
+ return rocblas_datatype_f64_c;
100
+ }
101
+
102
+ template <typename T>
103
+ auto DoCastForHalfOrBfloat16(const T fp) {
104
+ return fp;
105
+ }
106
+
107
+ template <>
108
+ inline auto DoCastForHalfOrBfloat16<Half>(const Half fp) {
109
+ // alpha and beta should be the same as compute_type, in Half case it is float.
110
+ float h = fp;
111
+ return h;
112
+ }
113
+
114
+ template <>
115
+ inline auto DoCastForHalfOrBfloat16<BFloat16>(const BFloat16 fp) {
116
+ // alpha and beta should be the same as compute_type, in bfloat16 case it is float.
117
+ float h = fp;
118
+ return h;
119
+ }
120
+
121
+ static rocblas_operation _rocblasOpFromChar(char op) {
122
+ switch (op) {
123
+ case 'n':
124
+ case 'N':
125
+ return rocblas_operation_none;
126
+ case 't':
127
+ case 'T':
128
+ return rocblas_operation_transpose;
129
+ case 'c':
130
+ case 'C':
131
+ return rocblas_operation_conjugate_transpose;
132
+ }
133
+ TORCH_CHECK(false,
134
+ "_rocblasOpFromChar input should be 't', 'n' or 'c' but got `", op, "`");
135
+ }
136
+
137
+ template <typename T>
138
+ class RocblasGemmOp : public Callable<GemmParams<T>> {
139
+ public:
140
+ RocblasGemmOp(int solution) : solution_{solution} {}
141
+
142
+ TuningStatus Call(const GemmParams<T>* params) override {
143
+ auto input_output_type = RocBlasDataTypeFor<T>();
144
+ auto compute_type = RocBlasComputeTypeFor<T>();
145
+ auto h_a = DoCastForHalfOrBfloat16(params->alpha);
146
+ auto h_b = DoCastForHalfOrBfloat16(params->beta);
147
+ auto status = rocblas_gemm_ex(
148
+ (rocblas_handle)at::cuda::getCurrentCUDABlasHandle(),
149
+ _rocblasOpFromChar(params->transa),
150
+ _rocblasOpFromChar(params->transb),
151
+ params->m, params->n, params->k,
152
+ &h_a,
153
+ params->a, input_output_type, params->lda,
154
+ params->b, input_output_type, params->ldb,
155
+ &h_b,
156
+ params->c, input_output_type, params->ldc,
157
+ params->c, input_output_type, params->ldc,
158
+ compute_type,
159
+ rocblas_gemm_algo_solution_index,
160
+ solution_,
161
+ rocblas_gemm_flags_none);
162
+ if (status != rocblas_status_success) {
163
+ return FAIL;
164
+ }
165
+ return OK;
166
+ }
167
+
168
+ private:
169
+ int solution_;
170
+ };
171
+
172
+ template <typename T>
173
+ auto GetRocBlasGemmTypeStringAndOps() {
174
+ rocblas_handle handle = (rocblas_handle)at::cuda::getCurrentCUDABlasHandle();
175
+ int solution_size;
176
+ auto input_output_type = RocBlasDataTypeFor<T>();
177
+ auto compute_type = RocBlasComputeTypeFor<T>();
178
+ // Get the number of available solutions
179
+ TORCH_ROCBLAS_CHECK(rocblas_gemm_ex_get_solutions_by_type(handle,
180
+ input_output_type,
181
+ input_output_type,
182
+ compute_type,
183
+ rocblas_gemm_flags_none,
184
+ nullptr,
185
+ &solution_size));
186
+ std::vector<int> solutions(solution_size);
187
+ // Get the list of available solutions
188
+ TORCH_ROCBLAS_CHECK(rocblas_gemm_ex_get_solutions_by_type(handle,
189
+ input_output_type,
190
+ input_output_type,
191
+ compute_type,
192
+ rocblas_gemm_flags_none,
193
+ solutions.data(),
194
+ &solution_size));
195
+ // Sort the solutions in ascending order to make the solution vector deterministic across runs
196
+ std::sort(solutions.begin(), solutions.end());
197
+
198
+ std::vector<std::pair<std::string, std::unique_ptr<Callable<GemmParams<T>>>>> ret;
199
+ for (size_t i = 0; i < solutions.size(); ++i) {
200
+ auto callable = std::make_unique<RocblasGemmOp<T>>(solutions[i]);
201
+ ret.emplace_back(std::make_pair(fmt::sprintf("Gemm_Rocblas_%d", solutions[i]), std::move(callable)));
202
+ }
203
+ return ret;
204
+ }
205
+
206
+ template <typename T>
207
+ class RocblasGemmStridedBatchedOp : public Callable<GemmStridedBatchedParams<T>> {
208
+ public:
209
+ RocblasGemmStridedBatchedOp(int solution) : solution_{solution} {}
210
+
211
+ TuningStatus Call(const GemmStridedBatchedParams<T>* params) override {
212
+ auto input_output_type = RocBlasDataTypeFor<T>();
213
+ auto compute_type = RocBlasComputeTypeFor<T>();
214
+ auto h_a = DoCastForHalfOrBfloat16(params->alpha);
215
+ auto h_b = DoCastForHalfOrBfloat16(params->beta);
216
+ auto status = rocblas_gemm_strided_batched_ex(
217
+ (rocblas_handle)at::cuda::getCurrentCUDABlasHandle(),
218
+ _rocblasOpFromChar(params->transa),
219
+ _rocblasOpFromChar(params->transb),
220
+ params->m, params->n, params->k,
221
+ &h_a,
222
+ params->a, input_output_type, params->lda, params->stride_a,
223
+ params->b, input_output_type, params->ldb, params->stride_b,
224
+ &h_b,
225
+ params->c, input_output_type, params->ldc, params->stride_c,
226
+ params->c, input_output_type, params->ldc, params->stride_c,
227
+ params->batch,
228
+ compute_type,
229
+ rocblas_gemm_algo_solution_index,
230
+ solution_,
231
+ rocblas_gemm_flags_none);
232
+ if (status != rocblas_status_success) {
233
+ return FAIL;
234
+ }
235
+ return OK;
236
+ }
237
+
238
+ private:
239
+ int solution_;
240
+ };
241
+
242
+ template <typename T>
243
+ auto GetRocBlasGemmStridedBatchedTypeStringAndOps() {
244
+ rocblas_handle handle = (rocblas_handle)at::cuda::getCurrentCUDABlasHandle();
245
+ int solution_size;
246
+ auto input_output_type = RocBlasDataTypeFor<T>();
247
+ auto compute_type = RocBlasComputeTypeFor<T>();
248
+ // Get the number of available solutions
249
+ TORCH_ROCBLAS_CHECK(rocblas_gemm_ex_get_solutions_by_type(handle,
250
+ input_output_type,
251
+ input_output_type,
252
+ compute_type,
253
+ rocblas_gemm_flags_none,
254
+ nullptr,
255
+ &solution_size));
256
+ std::vector<int> solutions(solution_size);
257
+ // Get the list of available solutions
258
+ TORCH_ROCBLAS_CHECK(rocblas_gemm_ex_get_solutions_by_type(handle,
259
+ input_output_type,
260
+ input_output_type,
261
+ compute_type,
262
+ rocblas_gemm_flags_none,
263
+ solutions.data(),
264
+ &solution_size));
265
+ // Sort the solutions in ascending order to make the solution vector deterministic across runs
266
+ std::sort(solutions.begin(), solutions.end());
267
+
268
+ std::vector<std::pair<std::string, std::unique_ptr<Callable<GemmStridedBatchedParams<T>>>>> ret;
269
+ for (size_t i = 0; i < solutions.size(); ++i) {
270
+ auto callable = std::make_unique<RocblasGemmStridedBatchedOp<T>>(solutions[i]);
271
+ ret.emplace_back(std::make_pair(c10::str("Gemm_Rocblas_", solutions[i]), std::move(callable)));
272
+ }
273
+ return ret;
274
+ }
275
+
276
+ } // namespace at::cuda::tunable