multi_tensor_l2norm_kernel.cu 2.52 KB
Newer Older
Michael Carilli's avatar
Michael Carilli committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <ATen/ATen.h>
#include <ATen/AccumulateType.h>
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/Exceptions.h>
// Another possibility:
// #include <torch/all.h>

#include <assert.h>

#include "type_shim.h"
#include "multi_tensor_apply.cuh"

#define BLOCK_SIZE 512
#define ILP 4

template<typename x_t>
struct L2NormFunctor
{
   __device__ __forceinline__ void operator()(
    int chunk_size,
    volatile int* noop_gmem,
    TensorListMetadata<1>& tl,
    float* output)
  {
    // I'd like this kernel to propagate infs/nans.
    // if(*noop_gmem == 1)
    //   return;

    int tensor_loc = tl.block_to_tensor[blockIdx.x];
    int chunk_idx = tl.block_to_chunk[blockIdx.x];
    int n = tl.sizes[tensor_loc];

    x_t* x = (x_t*)tl.addresses[0][tensor_loc];
    x += chunk_idx*chunk_size;

    n -= chunk_idx*chunk_size;

    __shared__ float vals[512];

    // Non-divergent exit condition for __syncthreads, not necessary here
    float val = 0;
    for(int i = threadIdx.x; i < n && i < chunk_size; i += blockDim.x)
    {
      float next = static_cast<float>(x[i]);
      val += next*next;
    }

    float final = reduce_block_into_lanes(vals, val);

    if(threadIdx.x == 0)
    {
      if(!isfinite(final))
        *noop_gmem = 1; // Blindly fire off a write.  These will race but that's ok.
      output[blockIdx.x] += final;
    }
  }
};

59
60
61
62
63
64
65
66
67
68
69
70
71
72
__global__ void cleanup(float* x, float* ret)
{
  __shared__ float vals[512];

  float val = 0;
  if(threadIdx.x < 320)
    val = x[threadIdx.x];

  float final = reduce_block_into_lanes(vals, val);

  if(threadIdx.x == 0)
    *ret = sqrt(final);
}

Michael Carilli's avatar
Michael Carilli committed
73
74
75
76
77
at::Tensor multi_tensor_l2norm_cuda(
  int chunk_size,
  at::Tensor noop_flag,
  std::vector<std::vector<at::Tensor>> tensor_lists)
{
78
  auto output = at::zeros({320}, tensor_lists[0][0].options().dtype(at::kFloat));
Michael Carilli's avatar
Michael Carilli committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92

  DISPATCH_FLOAT_AND_HALF(tensor_lists[0][0].scalar_type(), 0, "multi_tensor_l2norm_cuda",
    multi_tensor_apply<1>(
      BLOCK_SIZE,
      chunk_size,
      noop_flag,
      tensor_lists,
      L2NormFunctor<scalar_t_0>(),
      output.data<float>());)

  AT_CUDA_CHECK(cudaGetLastError());

  // AT_CUDA_CHECK(cudaDeviceSynchronize());

93
  // This involves one more small kernel launches, but will be negligible end to end.
Michael Carilli's avatar
Michael Carilli committed
94
95
  // I could get rid of these by hacking the functor + multi tensor harness with persistence
  // logic, but keeping it simple for now
96
97
98
99
  auto ret = at::empty({1}, output.options());
  auto stream = at::cuda::getCurrentCUDAStream();
  cleanup<<<1, 512, 0, stream>>>(output.data<float>(), ret.data<float>());
  return ret;
Michael Carilli's avatar
Michael Carilli committed
100
}