xref: /aosp_15_r20/external/pytorch/test/cpp_extensions/cuda_extension_kernel2.cu (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #include <cuda.h>
2 #include <cuda_runtime.h>
3 #include <c10/cuda/CUDAException.h>
4 
5 #include <ATen/ATen.h>
6 
tanh_add_kernel(const float * __restrict__ x,const float * __restrict__ y,float * __restrict__ output,const int size)7 __global__ void tanh_add_kernel(
8     const float* __restrict__ x,
9     const float* __restrict__ y,
10     float* __restrict__ output,
11     const int size) {
12   const int index = blockIdx.x * blockDim.x + threadIdx.x;
13   if (index < size) {
14     const float tanh_x = 2.0f / (1.0f + __expf(-2.0f * x[index])) - 1;
15     const float tanh_y = 2.0f / (1.0f + __expf(-2.0f * y[index])) - 1;
16     output[index] = tanh_x + tanh_y;
17   }
18 }
19 
tanh_add_cuda(const float * x,const float * y,float * output,int size)20 void tanh_add_cuda(const float* x, const float* y, float* output, int size) {
21   const int threads = 1024;
22   const int blocks = (size + threads - 1) / threads;
23   tanh_add_kernel<<<blocks, threads>>>(x, y, output, size);
24   C10_CUDA_KERNEL_LAUNCH_CHECK();
25 }
26