multi_tensor_l2norm_kernel_mp.cu 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <ATen/ATen.h>
  2. #include <ATen/AccumulateType.h>
  3. #include <ATen/cuda/CUDAContext.h>
  4. #include <ATen/cuda/Exceptions.h>
  5. #include <c10/cuda/CUDAGuard.h>
  6. // Another possibility:
  7. // #include <torch/all.h>
  8. #include <assert.h>
  9. #include "type_shim.h"
  10. #include "multi_tensor_apply.cuh"
  11. #define BLOCK_SIZE 512
  12. #define ILP 4
  13. template<typename T>
  14. __device__ __forceinline__ bool is_aligned(T* p){
  15. return ((uint64_t)p) % (ILP*sizeof(T)) == 0;
  16. }
  17. template<typename T>
  18. __device__ __forceinline__ void load_store(T* dst, T* src, int dst_offset, int src_offset){
  19. typedef typename std::aligned_storage<ILP*sizeof(T), ILP*alignof(T)>::type LT;
  20. ((LT*)dst)[dst_offset] = ((LT*)src)[src_offset];
  21. }
  22. template<typename x_t>
  23. struct L2NormFunctor
  24. {
  25. __device__ __forceinline__ void operator()(
  26. int chunk_size,
  27. volatile int* noop_gmem,
  28. TensorListMetadata<1>& tl,
  29. float* output,
  30. float* output_per_tensor,
  31. bool per_tensor,
  32. int max_chunks_per_tensor)
  33. {
  34. if (*noop_gmem) {
  35. return;
  36. }
  37. int tensor_loc = tl.block_to_tensor[blockIdx.x];
  38. int chunk_idx = tl.block_to_chunk[blockIdx.x];
  39. int n = tl.sizes[tensor_loc];
  40. x_t* x = (x_t*)tl.addresses[0][tensor_loc];
  41. x += chunk_idx*chunk_size;
  42. n -= chunk_idx*chunk_size;
  43. __shared__ float s_vals[512];
  44. float vals[ILP]; // = {0}; // this probably works too but I want to be sure...
  45. x_t r_x[ILP];
  46. for(int i = 0; i < ILP; i++)
  47. {
  48. vals[i] = 0.f;
  49. r_x[i] = 0;
  50. }
  51. // to make things simple, we put aligned case in a different code path
  52. if(n % ILP == 0 && chunk_size % ILP == 0 && is_aligned(x))
  53. {
  54. for(int i_start = threadIdx.x; i_start*ILP < n && i_start*ILP < chunk_size; i_start += blockDim.x)
  55. {
  56. // load
  57. load_store(r_x, x, 0 , i_start);
  58. #pragma unroll
  59. for(int ii = 0; ii < ILP; ii++)
  60. {
  61. float next = static_cast<float>(r_x[ii]);
  62. vals[ii] += next*next;
  63. }
  64. }
  65. }
  66. else
  67. {
  68. for(int i_start = 0; i_start < n && i_start < chunk_size; i_start += blockDim.x*ILP)
  69. {
  70. #pragma unroll
  71. for(int ii = 0; ii < ILP; ii++)
  72. {
  73. int i = i_start + threadIdx.x + ii*blockDim.x;
  74. if(i < n && i < chunk_size)
  75. {
  76. float next = static_cast<float>(x[i]);
  77. vals[ii] += next*next;
  78. }
  79. }
  80. }
  81. }
  82. float val = 0.f;
  83. for(int i = 0; i < ILP; i++)
  84. val += vals[i];
  85. float final = reduce_block_into_lanes(s_vals, val);
  86. if(threadIdx.x == 0)
  87. {
  88. if(!isfinite(final))
  89. *noop_gmem = 1; // Blindly fire off a write. These will race but that's ok.
  90. output[blockIdx.x] += final;
  91. if(per_tensor)
  92. output_per_tensor[(tl.start_tensor_this_launch + tensor_loc)*max_chunks_per_tensor + chunk_idx] = final;
  93. }
  94. }
  95. };
  96. __global__ void cleanup(
  97. float* output,
  98. float* output_per_tensor,
  99. float* ret,
  100. float* ret_per_tensor,
  101. bool per_tensor,
  102. int max_chunks_per_tensor,
  103. volatile int* noop_gmem)
  104. {
  105. if (*noop_gmem) {
  106. return;
  107. }
  108. __shared__ float vals[512];
  109. if(blockIdx.x == 0)
  110. {
  111. float val = 0;
  112. if(threadIdx.x < 320)
  113. val = output[threadIdx.x];
  114. float final = reduce_block_into_lanes(vals, val);
  115. if(threadIdx.x == 0)
  116. *ret = sqrt(final);
  117. }
  118. if(per_tensor)
  119. {
  120. float* output_this_tensor = output_per_tensor + blockIdx.x*max_chunks_per_tensor;
  121. float val = 0;
  122. for(int i = threadIdx.x; i < max_chunks_per_tensor; i += blockDim.x)
  123. val += output_this_tensor[i];
  124. float final = reduce_block_into_lanes(vals, val);
  125. if(threadIdx.x == 0)
  126. ret_per_tensor[blockIdx.x] = sqrt(final);
  127. }
  128. }
  129. std::tuple<at::Tensor, at::Tensor> multi_tensor_l2norm_mp_cuda(
  130. int chunk_size,
  131. at::Tensor noop_flag,
  132. std::vector<std::vector<at::Tensor>> tensor_lists,
  133. at::optional<bool> per_tensor_python)
  134. {
  135. bool per_tensor = per_tensor_python.has_value() ? per_tensor_python.value() : false;
  136. auto float_options = tensor_lists[0][0].options().dtype(at::kFloat);
  137. auto output = at::zeros({320}, float_options);
  138. at::Tensor output_per_tensor;
  139. at::Tensor ret_per_tensor;
  140. int ntensors = tensor_lists[0].size();
  141. int max_chunks_per_tensor = -1;
  142. if(per_tensor)
  143. {
  144. for(int t = 0; t < ntensors; t++)
  145. {
  146. int max_chunks_this_tensor = (tensor_lists[0][t].numel() + chunk_size - 1)/chunk_size;
  147. if(max_chunks_this_tensor > max_chunks_per_tensor)
  148. max_chunks_per_tensor = max_chunks_this_tensor;
  149. }
  150. output_per_tensor = at::zeros({ntensors*max_chunks_per_tensor}, float_options);
  151. ret_per_tensor = at::empty({ntensors}, float_options);
  152. }
  153. else
  154. {
  155. ret_per_tensor = at::empty({0}, float_options);
  156. }
  157. DISPATCH_FLOAT_HALF_AND_BFLOAT(tensor_lists[0][0].scalar_type(), 0, "multi_tensor_l2norm_mp_cuda",
  158. multi_tensor_apply<1>(
  159. BLOCK_SIZE,
  160. chunk_size,
  161. noop_flag,
  162. tensor_lists,
  163. L2NormFunctor<scalar_t_0>(),
  164. output.data_ptr<float>(),
  165. per_tensor ? output_per_tensor.data_ptr<float>() : nullptr,
  166. per_tensor,
  167. max_chunks_per_tensor);)
  168. AT_CUDA_CHECK(cudaGetLastError());
  169. // AT_CUDA_CHECK(cudaDeviceSynchronize());
  170. // This involves one more small kernel launches, but will be negligible end to end.
  171. // I could get rid of these by hacking the functor + multi tensor harness with persistence
  172. // logic, but keeping it simple for now
  173. auto ret = at::empty({1}, output.options());
  174. const at::cuda::OptionalCUDAGuard device_guard(device_of(output));
  175. auto stream = at::cuda::getCurrentCUDAStream();
  176. cleanup<<<per_tensor ? ntensors : 1, 512, 0, stream>>>(
  177. output.data_ptr<float>(),
  178. per_tensor ? output_per_tensor.data_ptr<float>() : nullptr,
  179. ret.data_ptr<float>(),
  180. per_tensor ? ret_per_tensor.data_ptr<float>() : nullptr,
  181. per_tensor,
  182. max_chunks_per_tensor, noop_flag.data_ptr<int>());
  183. return std::tuple<at::Tensor, at::Tensor>(ret, ret_per_tensor);
  184. }