multi_tensor_l2norm_scale_kernel.cu 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 in_t, typename out_t>
  23. struct L2NormScaleFunctor
  24. {
  25. __device__ __forceinline__ void operator()(
  26. int chunk_size,
  27. volatile int* noop_gmem,
  28. TensorListMetadata<2>& tl,
  29. float* output,
  30. float* output_per_tensor,
  31. float scale,
  32. bool per_tensor,
  33. int max_chunks_per_tensor)
  34. {
  35. // I'd like this kernel to propagate infs/nans.
  36. // if(*noop_gmem == 1)
  37. // return;
  38. int tensor_loc = tl.block_to_tensor[blockIdx.x];
  39. int chunk_idx = tl.block_to_chunk[blockIdx.x];
  40. int n = tl.sizes[tensor_loc];
  41. in_t* in = (in_t*)tl.addresses[0][tensor_loc];
  42. in += chunk_idx*chunk_size;
  43. out_t* out = (out_t*)tl.addresses[1][tensor_loc];
  44. out += chunk_idx*chunk_size;
  45. n -= chunk_idx*chunk_size;
  46. __shared__ float s_vals[512];
  47. float vals[ILP]; // = {0}; // this probably works too but I want to be sure...
  48. in_t r_in[ILP];
  49. for(int i = 0; i < ILP; i++)
  50. {
  51. vals[i] = 0.f;
  52. r_in[i] = 0;
  53. }
  54. //bool finite = true;
  55. out_t r_out[ILP];
  56. // to make things simple, we put aligned case in a different code path
  57. if(n % ILP == 0 && chunk_size % ILP == 0 && is_aligned(in) && is_aligned(out))
  58. {
  59. for(int i_start = threadIdx.x; i_start*ILP < n && i_start*ILP < chunk_size; i_start += blockDim.x)
  60. {
  61. // load
  62. load_store(r_in, in, 0 , i_start);
  63. #pragma unroll
  64. for(int ii = 0; ii < ILP; ii++)
  65. {
  66. float next = static_cast<float>(r_in[ii]);
  67. r_out[ii] = next*scale;
  68. vals[ii] += next*next;
  69. //finite = finite && isfinite(r_in[ii]);
  70. }
  71. load_store(out, r_out, i_start, 0);
  72. }
  73. }
  74. else
  75. {
  76. for(int i_start = 0; i_start < n && i_start < chunk_size; i_start += blockDim.x*ILP)
  77. {
  78. #pragma unroll
  79. for(int ii = 0; ii < ILP; ii++)
  80. {
  81. r_in[ii] = 0;
  82. int i = i_start + threadIdx.x + ii*blockDim.x;
  83. if(i < n && i < chunk_size)
  84. {
  85. r_in[ii] = in[i];
  86. float next = static_cast<float>(in[i]);
  87. vals[ii] += next*next;
  88. }
  89. }
  90. #pragma unroll
  91. for(int ii = 0; ii < ILP; ii++)
  92. {
  93. r_out[ii] = static_cast<float>(r_in[ii]) * scale;
  94. // finite = finite && isfinite(r_in[ii]);
  95. }
  96. #pragma unroll
  97. for(int ii = 0; ii < ILP; ii++)
  98. {
  99. int i = i_start + threadIdx.x + ii*blockDim.x;
  100. if(i < n && i < chunk_size)
  101. out[i] = r_out[ii];
  102. }
  103. }
  104. }
  105. float val = 0.f;
  106. for(int i = 0; i < ILP; i++)
  107. val += vals[i];
  108. float final = reduce_block_into_lanes(s_vals, val);
  109. if(threadIdx.x == 0)
  110. {
  111. if(!isfinite(final))
  112. *noop_gmem = 1; // Blindly fire off a write. These will race but that's ok.
  113. output[blockIdx.x] += final;
  114. if(per_tensor)
  115. output_per_tensor[(tl.start_tensor_this_launch + tensor_loc)*max_chunks_per_tensor + chunk_idx] = final;
  116. }
  117. }
  118. };
  119. // Probably better to template, but since we are not likely to support other norm
  120. template<typename x_t>
  121. struct MaxNormFunctor
  122. {
  123. __device__ __forceinline__ void operator()(
  124. int chunk_size,
  125. volatile int* noop_gmem,
  126. TensorListMetadata<1>& tl,
  127. float* output,
  128. float* output_per_tensor,
  129. bool per_tensor,
  130. int max_chunks_per_tensor)
  131. {
  132. // I'd like this kernel to propagate infs/nans.
  133. // if(*noop_gmem == 1)
  134. // return;
  135. int tensor_loc = tl.block_to_tensor[blockIdx.x];
  136. int chunk_idx = tl.block_to_chunk[blockIdx.x];
  137. int n = tl.sizes[tensor_loc];
  138. x_t* x = (x_t*)tl.addresses[0][tensor_loc];
  139. x += chunk_idx*chunk_size;
  140. n -= chunk_idx*chunk_size;
  141. __shared__ float s_vals[512];
  142. float vals[ILP]; // = {0}; // this probably works too but I want to be sure...
  143. x_t r_x[ILP];
  144. for(int i = 0; i < ILP; i++)
  145. {
  146. vals[i] = 0.f;
  147. r_x[i] = 0;
  148. }
  149. // to make things simple, we put aligned case in a different code path
  150. if(n % ILP == 0 && chunk_size % ILP == 0 && is_aligned(x))
  151. {
  152. for(int i_start = threadIdx.x; i_start*ILP < n && i_start*ILP < chunk_size; i_start += blockDim.x)
  153. {
  154. // load
  155. load_store(r_x, x, 0 , i_start);
  156. #pragma unroll
  157. for(int ii = 0; ii < ILP; ii++)
  158. {
  159. float next = static_cast<float>(r_x[ii]);
  160. vals[ii] = fmaxf(fabsf(vals[ii]), fabsf(next));
  161. }
  162. }
  163. }
  164. else
  165. {
  166. for(int i_start = 0; i_start < n && i_start < chunk_size; i_start += blockDim.x*ILP)
  167. {
  168. #pragma unroll
  169. for(int ii = 0; ii < ILP; ii++)
  170. {
  171. int i = i_start + threadIdx.x + ii*blockDim.x;
  172. if(i < n && i < chunk_size)
  173. {
  174. float next = static_cast<float>(x[i]);
  175. vals[ii] = fmaxf(fabsf(vals[ii]), fabsf(next));
  176. }
  177. }
  178. }
  179. }
  180. float val = 0.f;
  181. for(int i = 0; i < ILP; i++)
  182. val = fmaxf(fabsf(val), fabsf(vals[i]));
  183. float final = reduce_block_into_lanes_max_op(s_vals, val);
  184. if(threadIdx.x == 0)
  185. {
  186. if(!isfinite(final))
  187. *noop_gmem = 1; // Blindly fire off a write. These will race but that's ok.
  188. output[blockIdx.x] = fmaxf(fabsf(output[blockIdx.x]), fabsf(final));
  189. if(per_tensor)
  190. output_per_tensor[(tl.start_tensor_this_launch + tensor_loc)*max_chunks_per_tensor + chunk_idx] = final;
  191. }
  192. }
  193. };
  194. __global__ void cleanup_v3(
  195. float* output,
  196. float* output_per_tensor,
  197. float* ret,
  198. float* ret_per_tensor,
  199. bool per_tensor,
  200. int max_chunks_per_tensor)
  201. {
  202. __shared__ float vals[512];
  203. if(blockIdx.x == 0)
  204. {
  205. float val = 0;
  206. if(threadIdx.x < 320)
  207. val = output[threadIdx.x];
  208. float final = reduce_block_into_lanes(vals, val);
  209. if(threadIdx.x == 0)
  210. *ret = sqrt(final);
  211. }
  212. if(per_tensor)
  213. {
  214. float* output_this_tensor = output_per_tensor + blockIdx.x*max_chunks_per_tensor;
  215. float val = 0;
  216. for(int i = threadIdx.x; i < max_chunks_per_tensor; i += blockDim.x)
  217. val += output_this_tensor[i];
  218. float final = reduce_block_into_lanes(vals, val);
  219. if(threadIdx.x == 0)
  220. ret_per_tensor[blockIdx.x] = sqrt(final);
  221. }
  222. }
  223. std::tuple<at::Tensor, at::Tensor> multi_tensor_l2norm_scale_cuda(
  224. int chunk_size,
  225. at::Tensor noop_flag,
  226. std::vector<std::vector<at::Tensor>> tensor_lists,
  227. float scale,
  228. at::optional<bool> per_tensor_python)
  229. {
  230. bool per_tensor = per_tensor_python.has_value() ? per_tensor_python.value() : false;
  231. auto float_options = tensor_lists[0][0].options().dtype(at::kFloat);
  232. auto output = at::zeros({320}, float_options);
  233. at::Tensor output_per_tensor;
  234. at::Tensor ret_per_tensor;
  235. int ntensors = tensor_lists[0].size();
  236. int max_chunks_per_tensor = -1;
  237. if(per_tensor)
  238. {
  239. for(int t = 0; t < ntensors; t++)
  240. {
  241. int max_chunks_this_tensor = (tensor_lists[0][t].numel() + chunk_size - 1)/chunk_size;
  242. if(max_chunks_this_tensor > max_chunks_per_tensor)
  243. max_chunks_per_tensor = max_chunks_this_tensor;
  244. }
  245. output_per_tensor = at::zeros({ntensors*max_chunks_per_tensor}, float_options);
  246. ret_per_tensor = at::empty({ntensors}, float_options);
  247. }
  248. else
  249. {
  250. ret_per_tensor = at::empty({0}, float_options);
  251. }
  252. DISPATCH_FLOAT_AND_HALF(tensor_lists[0][0].scalar_type(), 0, "multi_tensor_l2norm_scale_cuda",
  253. DISPATCH_FLOAT_AND_HALF(tensor_lists[1][0].scalar_type(), 1, "multi_tensor_l2norm_scale_cuda",
  254. multi_tensor_apply<2>(
  255. BLOCK_SIZE,
  256. chunk_size,
  257. noop_flag,
  258. tensor_lists,
  259. L2NormScaleFunctor<scalar_t_0, scalar_t_1>(),
  260. output.DATA_PTR<float>(),
  261. per_tensor ? output_per_tensor.DATA_PTR<float>() : nullptr,
  262. scale,
  263. per_tensor,
  264. max_chunks_per_tensor);))
  265. AT_CUDA_CHECK(cudaGetLastError());
  266. // AT_CUDA_CHECK(cudaDeviceSynchronize());
  267. // This involves one more small kernel launches, but will be negligible end to end.
  268. // I could get rid of these by hacking the functor + multi tensor harness with persistence
  269. // logic, but keeping it simple for now
  270. auto ret = at::empty({1}, output.options());
  271. const at::cuda::OptionalCUDAGuard device_guard(device_of(output));
  272. auto stream = at::cuda::getCurrentCUDAStream();
  273. cleanup_v3<<<per_tensor ? ntensors : 1, 512, 0, stream>>>(
  274. output.DATA_PTR<float>(),
  275. per_tensor ? output_per_tensor.DATA_PTR<float>() : nullptr,
  276. ret.DATA_PTR<float>(),
  277. per_tensor ? ret_per_tensor.DATA_PTR<float>() : nullptr,
  278. per_tensor,
  279. max_chunks_per_tensor);
  280. return std::tuple<at::Tensor, at::Tensor>(ret, ret_per_tensor);
  281. }