Prompt48 commited on
Commit
87232ca
·
verified ·
1 Parent(s): 9c7ad76

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\torch\include\ATen\native\cuda\Loops.cuh with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//torch//include//ATen//native//cuda//Loops.cuh ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <ATen/detail/FunctionTraits.h>
4
+ #include <ATen/native/TensorIterator.h>
5
+ #include <ATen/native/TensorIteratorDynamicCasting.h>
6
+ #include <ATen/cuda/detail/OffsetCalculator.cuh>
7
+ #include <ATen/OpMathType.h>
8
+ #include <ATen/native/cuda/thread_constants.h>
9
+
10
+ #include <thrust/tuple.h>
11
+
12
+ #include <ATen/native/cuda/MemoryAccess.cuh>
13
+
14
+ #include <tuple>
15
+
16
+ namespace at::native {
17
+
18
+ template<int N>
19
+ static OffsetCalculator<N> make_input_offset_calculator(const TensorIteratorBase& iter) {
20
+ // array size can not be 0, this happens when N == 0
21
+ constexpr int array_size = std::max<int>(N, 1);
22
+ TORCH_INTERNAL_ASSERT(N == iter.ntensors() - iter.noutputs());
23
+ std::array<const int64_t*, array_size> strides;
24
+ int64_t element_sizes[array_size];
25
+ for (int i = 0; i < N; i++) {
26
+ strides[i] = iter.strides(i + iter.noutputs()).data();
27
+ element_sizes[i] = iter.element_size(i + iter.noutputs());
28
+ }
29
+ return OffsetCalculator<N>(iter.ndim(), iter.shape().data(), strides.data(), element_sizes);
30
+ }
31
+
32
+ template <int num_outputs = 1>
33
+ static OffsetCalculator<num_outputs> make_output_offset_calculator(const TensorIteratorBase& iter) {
34
+ TORCH_INTERNAL_ASSERT(num_outputs == iter.noutputs());
35
+ std::array<const int64_t*, num_outputs> strides;
36
+ int64_t element_sizes[num_outputs];
37
+ for (int i = 0; i < num_outputs; i++) {
38
+ strides[i] = iter.strides(i).data();
39
+ element_sizes[i] = iter.element_size(i);
40
+ }
41
+ return OffsetCalculator<num_outputs>(iter.ndim(), iter.shape().data(), strides.data(), element_sizes);
42
+ }
43
+
44
+ template<typename func_t, typename policy_t>
45
+ __device__ inline void elementwise_kernel_helper(func_t f, policy_t policy) {
46
+ using traits = function_traits<func_t>;
47
+ using return_t = typename traits::result_type;
48
+ using args_t = typename traits::ArgsTuple;
49
+ constexpr int elems_per_thread = policy_t::tws;
50
+
51
+ int idx = blockIdx.x;
52
+
53
+ return_t results[elems_per_thread];
54
+ args_t args[elems_per_thread];
55
+
56
+ // load
57
+ policy.load(args, idx);
58
+
59
+ // compute
60
+ #pragma unroll
61
+ for (int i = 0; i < elems_per_thread; i++) {
62
+ if (policy.check_inbounds(i)) {
63
+ results[i] = c10::guts::apply(f, args[i]);
64
+ }
65
+ }
66
+
67
+ // store
68
+ policy.store(results, idx);
69
+ }
70
+
71
+ } // namespace at::native
72
+
73
+ #include <ATen/native/cuda/CUDALoops.cuh>
74
+
75
+ namespace at:: native {
76
+
77
+ template <typename func_t>
78
+ void gpu_kernel_nocast(TensorIteratorBase& iter, const func_t& f) {
79
+
80
+ for (int arg = 0; arg < iter.ntensors(); arg++) {
81
+ TORCH_INTERNAL_ASSERT(
82
+ iter.device(arg).is_cuda(),
83
+ "argument ", arg, ": expected a CUDA device but found ", iter.device(arg));
84
+ }
85
+
86
+ if (iter.numel() == 0) {
87
+ return;
88
+ }
89
+
90
+ if (!iter.can_use_32bit_indexing()) {
91
+ for (auto& sub_iter : iter.with_32bit_indexing()) {
92
+ gpu_kernel_nocast(sub_iter, f);
93
+ }
94
+ return;
95
+ }
96
+
97
+ gpu_kernel_impl_nocast(iter, f);
98
+ }
99
+
100
+ template <typename func_t>
101
+ void gpu_kernel(TensorIteratorBase& iter, const func_t& f) {
102
+
103
+ for (int arg = 0; arg < iter.ntensors(); arg++) {
104
+ TORCH_INTERNAL_ASSERT(
105
+ iter.device(arg).is_cuda(),
106
+ "argument ", arg, ": expected a CUDA device but found ", iter.device(arg));
107
+ }
108
+
109
+ if (iter.numel() == 0) {
110
+ return;
111
+ }
112
+
113
+ if (!iter.can_use_32bit_indexing()) {
114
+ for (auto& sub_iter : iter.with_32bit_indexing()) {
115
+ gpu_kernel(sub_iter, f);
116
+ }
117
+ return;
118
+ }
119
+
120
+ gpu_kernel_impl(iter, f);
121
+ }
122
+
123
+ template<typename arg1_t, typename arg2_t, typename return_t, typename func_t>
124
+ struct AUnaryFunctor {
125
+ using traits = function_traits<func_t>;
126
+ using opmath_arg1_t = typename traits::template arg<0>::type;
127
+ __device__ return_t operator()(arg2_t b) const {
128
+ return f(a, b);
129
+ }
130
+ // NB: scalar is stored in higher precision!
131
+ AUnaryFunctor(func_t f_, opmath_arg1_t a_): f(f_), a(a_) {}
132
+ private:
133
+ func_t f;
134
+ opmath_arg1_t a;
135
+ };
136
+
137
+ template<typename arg1_t, typename arg2_t, typename return_t, typename func_t>
138
+ struct BUnaryFunctor {
139
+ using traits = function_traits<func_t>;
140
+ using opmath_arg2_t = typename traits::template arg<1>::type;
141
+ __device__ return_t operator()(arg1_t a) const {
142
+ return f(a, b);
143
+ }
144
+ // NB: scalar is stored in higher precision!
145
+ BUnaryFunctor(func_t f_, opmath_arg2_t b_): f(f_), b(b_) {}
146
+ private:
147
+ func_t f;
148
+ opmath_arg2_t b;
149
+ };
150
+
151
+ // Though seemingly noop, this inserts casts from arg1_t to func_t's type
152
+ // (which may be higher precision), as well as casts to return_t
153
+ template <typename arg1_t, typename arg2_t, typename return_t, typename func_t>
154
+ struct BinaryFunctor {
155
+ __device__ return_t operator()(arg1_t a, arg2_t b) const {
156
+ return f(a, b);
157
+ }
158
+ BinaryFunctor(func_t f_): f(f_) {}
159
+ private:
160
+ func_t f;
161
+ };
162
+
163
+ // Unlike gpu_kernel_with_scalars, this allows you to pass a func_t which
164
+ // accepts inputs at higher precision (typically opmath_t), but then
165
+ // ensure that we load from memory at the correct precision (scalar_t)
166
+ // to avoid expensive loads. For the whole sordid story see
167
+ // https://dev-discuss.pytorch.org/t/cuda-loops-case-study-code-generation-vs-templates/302
168
+ template <typename arg1_t, typename arg2_t = arg1_t, typename return_t = arg1_t, typename func_t>
169
+ void opmath_gpu_kernel_with_scalars(TensorIteratorBase& iter, const func_t& f) {
170
+ TORCH_INTERNAL_ASSERT(iter.ntensors() == 3);
171
+
172
+ using traits = function_traits<func_t>;
173
+ using opmath_arg1_t = typename traits::template arg<0>::type;
174
+ using opmath_arg2_t = typename traits::template arg<1>::type;
175
+ static_assert(
176
+ traits::arity == 2,
177
+ "gpu_kernel_with_scalars only supports two input arguments");
178
+
179
+ if (iter.is_cpu_scalar(1)) {
180
+ AUnaryFunctor<arg1_t, arg2_t, return_t, func_t> af(f, iter.scalar_value<opmath_arg1_t>(1));
181
+ iter.remove_operand(1);
182
+ // TODO: When all kernels that use gpu_kernel_with_scalars are
183
+ // ported to structured, this device guard can be deleted. This
184
+ // works around incorrect device guard generation for pre-structured
185
+ // kernels device guards, but structured kernels do it right and
186
+ // we can assume the device is already set correctly
187
+ const OptionalDeviceGuard device_guard(iter.device(1));
188
+ gpu_kernel(iter, af);
189
+ } else if (iter.is_cpu_scalar(2)) {
190
+ BUnaryFunctor<arg1_t, arg2_t, return_t, func_t> bf(f, iter.scalar_value<opmath_arg2_t>(2));
191
+ iter.remove_operand(2);
192
+ gpu_kernel(iter, bf);
193
+ } else {
194
+ gpu_kernel(iter, BinaryFunctor<arg1_t, arg2_t, return_t, func_t>(f));
195
+ }
196
+ }
197
+
198
+ template <typename scalar_t, typename return_t = scalar_t, typename func_t>
199
+ void opmath_symmetric_gpu_kernel_with_scalars(TensorIteratorBase& iter, const func_t& f) {
200
+ // Use symmetric property of the functor to reduce number of kernels,
201
+ // requires f(a, b) == f(b, a)
202
+ TORCH_INTERNAL_ASSERT(iter.ntensors() == 3);
203
+
204
+ using traits = function_traits<func_t>;
205
+ using opmath_arg_t = typename traits::template arg<0>::type;
206
+ static_assert(
207
+ traits::arity == 2,
208
+ "gpu_kernel_with_scalars only supports two input arguments");
209
+ static_assert(std::is_same_v<opmath_arg_t, typename traits::template arg<1>::type>,
210
+ "f is not symmetric");
211
+
212
+ OptionalDeviceGuard device_guard;
213
+ opmath_arg_t scalar_val{};
214
+
215
+ if (iter.is_cpu_scalar(1)) {
216
+ scalar_val = iter.scalar_value<opmath_arg_t>(1);
217
+ iter.remove_operand(1);
218
+
219
+ // TODO: When all kernels that use gpu_kernel_with_scalars are
220
+ // ported to structured, this device guard can be deleted. This
221
+ // works around incorrect device guard generation for pre-structured
222
+ // kernels device guards, but structured kernels do it right and
223
+ // we can assume the device is already set correctly
224
+ device_guard.reset_device(iter.device(1));
225
+ } else if (iter.is_cpu_scalar(2)) {
226
+ scalar_val = iter.scalar_value<opmath_arg_t>(2);
227
+ iter.remove_operand(2);
228
+ }
229
+
230
+ if (iter.ninputs() == 2) {
231
+ gpu_kernel(iter, BinaryFunctor<scalar_t, scalar_t, return_t, func_t>(f));
232
+ } else {
233
+ AUnaryFunctor<scalar_t, scalar_t, return_t, func_t> unary_f(f, scalar_val);
234
+ gpu_kernel(iter, unary_f);
235
+ }
236
+ }
237
+
238
+ // Legacy variant that assumes that func_t has the correct types
239
+ // that we expect to load from memory
240
+ template <typename func_t>
241
+ void gpu_kernel_with_scalars(TensorIteratorBase& iter, const func_t& f) {
242
+ using traits = function_traits<func_t>;
243
+ static_assert(
244
+ traits::arity == 2,
245
+ "gpu_kernel_with_scalars only supports two input arguments");
246
+ using arg1_t = typename traits::template arg<0>::type;
247
+ using arg2_t = typename traits::template arg<1>::type;
248
+ using return_t = typename traits::result_type;
249
+ opmath_gpu_kernel_with_scalars<arg1_t, arg2_t, return_t, func_t>(iter, f);
250
+ }
251
+
252
+ namespace { // functions for `gpu_kernel_multiple_outputs`.
253
+
254
+ // check the return type is `thrust::tuple`, not `std::tuple`.
255
+ template <typename T> struct is_tuple: std::false_type {};
256
+
257
+ template <typename ...T> struct is_tuple<thrust::tuple<T...>>: std::true_type {};
258
+
259
+ template <int num_outputs, typename func_t, typename array_t, typename inp_calc_t, typename out_calc_t>
260
+ C10_LAUNCH_BOUNDS_1(num_threads())
261
+ __global__ void unrolled_elementwise_kernel_for_multi_outputs(int N, func_t f, array_t data, inp_calc_t ic, out_calc_t oc) {
262
+ int remaining = N - block_work_size() * blockIdx.x;
263
+ elementwise_kernel_helper(f, memory::policies::multi_outputs_unroll<array_t, inp_calc_t, out_calc_t, num_outputs>(data, remaining, ic, oc));
264
+ }
265
+
266
+ template <int num_outputs, typename func_t, typename array_t, typename inp_calc_t, typename out_calc_t>
267
+ static inline void launch_unrolled_kernel_for_multi_outputs(int64_t N, const func_t& f, array_t data, inp_calc_t ic, out_calc_t oc) {
268
+ TORCH_INTERNAL_ASSERT(N > 0 && N <= std::numeric_limits<int32_t>::max());
269
+ int64_t grid = (N + block_work_size() - 1) / block_work_size();
270
+ auto stream = at::cuda::getCurrentCUDAStream();
271
+ unrolled_elementwise_kernel_for_multi_outputs<num_outputs, func_t, array_t><<<grid, num_threads(), 0, stream>>>(N, f, data, ic, oc);
272
+ C10_CUDA_KERNEL_LAUNCH_CHECK();
273
+ }
274
+
275
+ template <typename func_t>
276
+ void gpu_kernel_multiple_outputs_impl(TensorIteratorBase& iter, const func_t& f) {
277
+ using traits = function_traits<func_t>;
278
+ using output_t = typename traits::result_type;
279
+ static_assert(is_tuple<output_t>::value, "f's return type must be `thrust::tuple`");
280
+ constexpr int num_outputs = thrust::tuple_size<output_t>::value;
281
+ constexpr int num_inputs = traits::arity;
282
+ constexpr int ntensors = num_outputs + num_inputs;
283
+
284
+ TORCH_INTERNAL_ASSERT(iter.can_use_32bit_indexing());
285
+ TORCH_INTERNAL_ASSERT(iter.ntensors() == ntensors);
286
+
287
+ std::array<char*, ntensors> data;
288
+ for (int i = 0; i < ntensors; i++) {
289
+ data[i] = (char*)iter.data_ptr(i);
290
+ }
291
+
292
+ int64_t numel = iter.numel();
293
+
294
+ if (iter.is_contiguous()) {
295
+ auto input_calc = TrivialOffsetCalculator<num_inputs>();
296
+ auto output_calc = TrivialOffsetCalculator<num_outputs>();
297
+ launch_unrolled_kernel_for_multi_outputs<num_outputs>(numel, f, data, input_calc, output_calc);
298
+ } else {
299
+ auto input_calc = make_input_offset_calculator<num_inputs>(iter);
300
+ auto output_calc = make_output_offset_calculator<num_outputs>(iter);
301
+ launch_unrolled_kernel_for_multi_outputs<num_outputs>(numel, f, data, input_calc, output_calc);
302
+ }
303
+ }
304
+ } // namespace
305
+
306
+ template <typename func_t>
307
+ void gpu_kernel_multiple_outputs(TensorIteratorBase& iter, const func_t& f) {
308
+ ASSERT_HOST_DEVICE_LAMBDA(func_t);
309
+
310
+ for (int arg = 0; arg < iter.ntensors(); arg++) {
311
+ TORCH_INTERNAL_ASSERT(iter.device(arg).is_cuda());
312
+ }
313
+
314
+ if (iter.numel() == 0) {
315
+ return;
316
+ }
317
+
318
+ if (!iter.can_use_32bit_indexing()) {
319
+ for (auto& sub_iter : iter.with_32bit_indexing()) {
320
+ gpu_kernel_multiple_outputs(sub_iter, f);
321
+ }
322
+ return;
323
+ }
324
+
325
+ gpu_kernel_multiple_outputs_impl(iter, f);
326
+ }
327
+
328
+ } //namespace at::native