Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\torch\include\ATen\native\cuda\block_reduce.cuh with huggingface_hub
Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//torch//include//ATen//native//cuda//block_reduce.cuh
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <thrust/tuple.h>
|
| 4 |
+
|
| 5 |
+
#include <ATen/native/SharedReduceOps.h>
|
| 6 |
+
#include <ATen/cuda/DeviceUtils.cuh>
|
| 7 |
+
|
| 8 |
+
namespace at::native::cuda_utils {
|
| 9 |
+
|
| 10 |
+
constexpr int kCUDABlockReduceNumThreads = 512;
|
| 11 |
+
// Algorithmic limitation: BlockReduce does two WarpReduce calls, each
|
| 12 |
+
// of which reduces C10_WARP_SIZE elements. So, at most
|
| 13 |
+
// C10_WARP_SIZE**2 elements can be reduced at a time.
|
| 14 |
+
// NOTE: This is >= the max block size on current hardware anyway (1024).
|
| 15 |
+
constexpr int kCUDABlockReduceMaxThreads = C10_WARP_SIZE * C10_WARP_SIZE;
|
| 16 |
+
|
| 17 |
+
// Sums `val` across all threads in a warp.
|
| 18 |
+
//
|
| 19 |
+
// Assumptions:
|
| 20 |
+
// - The size of each block should be a multiple of `C10_WARP_SIZE`
|
| 21 |
+
template <typename T>
|
| 22 |
+
__inline__ __device__ T WarpReduceSum(T val) {
|
| 23 |
+
#pragma unroll
|
| 24 |
+
for (int offset = (C10_WARP_SIZE >> 1); offset > 0; offset >>= 1) {
|
| 25 |
+
val += WARP_SHFL_DOWN(val, offset);
|
| 26 |
+
}
|
| 27 |
+
return val;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Picks the maximum `val` across all threads in a warp.
|
| 31 |
+
//
|
| 32 |
+
// Assumptions:
|
| 33 |
+
// - The size of each block should be a multiple of `C10_WARP_SIZE`
|
| 34 |
+
template <typename T>
|
| 35 |
+
__inline__ __device__ T WarpReduceMax(T val) {
|
| 36 |
+
#pragma unroll
|
| 37 |
+
for (int offset = (C10_WARP_SIZE >> 1); offset > 0; offset >>= 1) {
|
| 38 |
+
val = max_propagate_nan(val, WARP_SHFL_DOWN(val, offset));
|
| 39 |
+
}
|
| 40 |
+
return val;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
struct Block1D {
|
| 44 |
+
static __forceinline__ __device__ int Tid() { return threadIdx.x; }
|
| 45 |
+
|
| 46 |
+
static __forceinline__ __device__ int Warps() {
|
| 47 |
+
return blockDim.x / C10_WARP_SIZE;
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
struct Block2D {
|
| 52 |
+
static __forceinline__ __device__ int Tid() {
|
| 53 |
+
return threadIdx.x + threadIdx.y * blockDim.x;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
static __forceinline__ __device__ int Warps() {
|
| 57 |
+
return blockDim.x * blockDim.y / C10_WARP_SIZE;
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
// Sums `val` across all threads in a block.
|
| 62 |
+
//
|
| 63 |
+
// Warning: the return value is only valid for thread 0.
|
| 64 |
+
// Assumptions:
|
| 65 |
+
// - The size of each block should be a multiple of `C10_WARP_SIZE`
|
| 66 |
+
// - `shared` should be a pointer to shared memory with size of, at least,
|
| 67 |
+
// `sizeof(T) * number_of_warps`
|
| 68 |
+
template <typename T, typename B = Block1D>
|
| 69 |
+
__inline__ __device__ T BlockReduceSum(T val, T* shared) {
|
| 70 |
+
const int tid = B::Tid();
|
| 71 |
+
const int lid = tid % C10_WARP_SIZE;
|
| 72 |
+
const int wid = tid / C10_WARP_SIZE;
|
| 73 |
+
val = WarpReduceSum(val);
|
| 74 |
+
__syncthreads(); // prevent races when BlockReduces are called in a row.
|
| 75 |
+
if (lid == 0) {
|
| 76 |
+
shared[wid] = val;
|
| 77 |
+
}
|
| 78 |
+
__syncthreads();
|
| 79 |
+
val = (tid < B::Warps()) ? shared[lid] : T(0);
|
| 80 |
+
if (wid == 0) {
|
| 81 |
+
val = WarpReduceSum(val);
|
| 82 |
+
}
|
| 83 |
+
return val;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Picks out the maximum `val` across all threads in a block.
|
| 87 |
+
//
|
| 88 |
+
// Warning: the return value is only valid for thread 0.
|
| 89 |
+
// Assumptions:
|
| 90 |
+
// - The size of each block should be a multiple of `C10_WARP_SIZE`
|
| 91 |
+
// - `shared` should be a pointer to shared memory with size of, at least,
|
| 92 |
+
// `sizeof(T) * number_of_warps`
|
| 93 |
+
template <typename T, typename B = Block1D>
|
| 94 |
+
__inline__ __device__ T BlockReduceMax(T val, T* shared) {
|
| 95 |
+
const int tid = B::Tid();
|
| 96 |
+
const int lid = tid % C10_WARP_SIZE;
|
| 97 |
+
const int wid = tid / C10_WARP_SIZE;
|
| 98 |
+
val = WarpReduceMax(val);
|
| 99 |
+
__syncthreads(); // prevent races when BlockReduces are called in a row.
|
| 100 |
+
if (lid == 0) {
|
| 101 |
+
shared[wid] = val;
|
| 102 |
+
}
|
| 103 |
+
__syncthreads();
|
| 104 |
+
val = (tid < B::Warps()) ? shared[lid] : T(std::numeric_limits<T>::lowest());
|
| 105 |
+
if (wid == 0) {
|
| 106 |
+
val = WarpReduceMax(val);
|
| 107 |
+
}
|
| 108 |
+
return val;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
template <typename T, class ReduceOp>
|
| 112 |
+
__inline__ __device__ T WarpReduce(T val, const ReduceOp& op) {
|
| 113 |
+
#pragma unroll
|
| 114 |
+
for (int offset = (C10_WARP_SIZE >> 1); offset > 0; offset >>= 1) {
|
| 115 |
+
val = op.combine(val, op.warp_shfl_down(val, offset));
|
| 116 |
+
}
|
| 117 |
+
return val;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
template <typename T, class ReduceOp, typename B = Block1D>
|
| 121 |
+
__inline__ __device__ T
|
| 122 |
+
BlockReduce(T val, const ReduceOp& op, const T& identity_element, T* shared) {
|
| 123 |
+
const int tid = B::Tid();
|
| 124 |
+
const int lid = tid % C10_WARP_SIZE;
|
| 125 |
+
const int wid = tid / C10_WARP_SIZE;
|
| 126 |
+
val = WarpReduce(val, op);
|
| 127 |
+
__syncthreads(); // prevent races when BlockReduces are called in a row.
|
| 128 |
+
if (lid == 0) {
|
| 129 |
+
shared[wid] = val;
|
| 130 |
+
}
|
| 131 |
+
__syncthreads();
|
| 132 |
+
val = (tid < B::Warps()) ? shared[lid] : identity_element;
|
| 133 |
+
if (wid == 0) {
|
| 134 |
+
val = WarpReduce(val, op);
|
| 135 |
+
}
|
| 136 |
+
return val;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
} // namespace at::native::cuda_utils
|