"vscode:/vscode.git/clone" did not exist on "dc461f60226a9ee4074a9bdd5cda4893063b03dd"
reordering.cu 6.76 KB
Newer Older
traveller59's avatar
traveller59 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Copyright 2019 Yan Yan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <ATen/ATen.h>
#include <chrono>
#include <limits>
#include <spconv/reordering.cu.h>
19
20
21
22
#include <spconv/reordering.h>
#include <tensorview/cuda_utils.h>
#include <tensorview/kernel_utils.h>
#include <tensorview/mp_helper.h>
Yan Yan's avatar
Yan Yan committed
23
#include <tensorview/tensor.h>
traveller59's avatar
traveller59 committed
24
#include <tensorview/tensorview.h>
Yan Yan's avatar
Yan Yan committed
25
#include <tensorview/torch_utils.h>
traveller59's avatar
traveller59 committed
26
27
28
29
30
#include <type_traits>
#include <utility/timer.h>

namespace spconv {

31
32
33
using float_types_t = tv::mp_list<float, double, at::Half>;
using int_types_t = tv::mp_list<int32_t, int64_t>;

Yan Yan's avatar
Yan Yan committed
34
35
36
37
38
39
void sparse_gather_cuda(torch::Tensor buffer, torch::Tensor features,
                        torch::Tensor indices, int size) {
  if (size <= 0)
    return;
  int numPlanes = features.size(1);
  auto stream = at::cuda::getCurrentCUDAStream();
40
41
42
  auto dtype = features.scalar_type();
  auto inds_dtype = indices.scalar_type();
  tv::DispatchTorch<float_types_t>()(dtype, [&](auto TValue) {
Yan Yan's avatar
Yan Yan committed
43
44
45
46
    using T = decltype(TValue);
    using vecload_type_t =
        std::conditional_t<std::is_same<T, at::Half>::value, int2, int4>;
    using kernel_block_t = tv::mp_list_c<int, 64, 32, 16>;
47
48
49
50
51
52
    tv::DispatchTorch<int_types_t>()(inds_dtype, [&](auto IndexValue) {
      using Index = decltype(IndexValue);
      bool notFound = true;
      constexpr int vecloadFactor = sizeof(vecload_type_t) / sizeof(T);
      tv::mp_for_each<kernel_block_t>(
          [=, &buffer, &features, &indices, &notFound](auto NumTLP) {
Yan Yan's avatar
Yan Yan committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            constexpr int NumILP = NumTLP / 4;
            // constexpr int NumILP = NumTLP / (64 / (NumTLP / vecloadFactor));
            int nHotBlock = (size / NumTLP) * NumTLP;
            if (notFound) {
              if (numPlanes % NumTLP == 0) {
                if (nHotBlock >= NumTLP) {
                  gatherVecBlockKernel<T, Index, int(NumTLP), NumILP,
                                       vecload_type_t>
                      <<<dim3(numPlanes / NumTLP, size / NumTLP),
                         dim3(NumTLP / vecloadFactor, NumTLP / NumILP), 0,
                         stream>>>(buffer.data_ptr<T>(), features.data_ptr<T>(),
                                   indices.data_ptr<Index>(), nHotBlock,
                                   numPlanes / vecloadFactor);

                  TV_CHECK_CUDA_ERR();
                }
                if (size - nHotBlock > 0) {
                  gatherVecKernel<T, Index, int(NumTLP), NumILP, vecload_type_t>
                      <<<dim3(1, numPlanes / NumTLP),
                         dim3(NumTLP / NumILP, NumTLP / vecloadFactor), 0,
                         stream>>>(buffer.data_ptr<T>() + nHotBlock * numPlanes,
                                   features.data_ptr<T>(),
                                   indices.data_ptr<Index>() + nHotBlock,
                                   size - nHotBlock, numPlanes / vecloadFactor);
                  TV_CHECK_CUDA_ERR();
                }
                notFound = false;
              }
            }
          });

84
85
86
87
88
89
90
91
92
93
94
95
      if (notFound) {
        constexpr int NumTLP = 64;
        constexpr int NumILP = NumTLP / 4;
        gatherGenericKernel<T, Index, NumTLP, NumILP>
            <<<dim3(tv::cuda::DivUp(size, NumTLP),
                    tv::cuda::DivUp(numPlanes, NumTLP)),
               dim3(NumTLP / NumILP, NumTLP), 0, stream>>>(
                buffer.data_ptr<T>(), features.data_ptr<T>(),
                indices.data_ptr<Index>(), size, numPlanes);
        TV_CHECK_CUDA_ERR();
      }
    });
Yan Yan's avatar
Yan Yan committed
96
97
  });
}
traveller59's avatar
traveller59 committed
98

Yan Yan's avatar
Yan Yan committed
99
100
101
102
103
104
void sparse_scatter_add_cuda(torch::Tensor buffer, torch::Tensor outFeatures,
                             torch::Tensor indices, int size) {
  if (size <= 0)
    return;
  int numPlanes = outFeatures.size(1);
  auto stream = at::cuda::getCurrentCUDAStream();
105
106
  auto dtype = outFeatures.scalar_type();
  auto inds_dtype = indices.scalar_type();
traveller59's avatar
traveller59 committed
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  tv::DispatchTorch<float_types_t>()(dtype, [&](auto TValue) {
    using T = decltype(TValue);
    using vecload_type_t =
        std::conditional_t<std::is_same<T, at::Half>::value, int2, int4>;
    using kernel_block_t = tv::mp_list_c<int, 64, 32, 16>;
    tv::DispatchTorch<int_types_t>()(inds_dtype, [&](auto IndexValue) {
      using Index = decltype(IndexValue);
      bool notFound = true;
      constexpr int vecloadFactor =
          sizeof(vecload_type_t) / sizeof(T); // important for half.
      tv::mp_for_each<kernel_block_t>([=, &outFeatures, &buffer, &indices,
                                       &notFound](auto NumTLP) {
        // constexpr int NumILP = NumTLP / (64 / (NumTLP /
        // vecloadFactor));
        constexpr int NumILP = NumTLP / 4;
        int nHotBlock = (size / NumTLP) * NumTLP;
        if (notFound) {
          if (numPlanes % NumTLP == 0) {
            if (nHotBlock >= NumTLP) {
              scatterAddVecBlockKernel<T, Index, int(NumTLP), NumILP,
                                       vecload_type_t>
                  <<<dim3(numPlanes / NumTLP, size / NumTLP),
                     dim3(NumTLP / vecloadFactor, NumTLP / NumILP), 0,
                     stream>>>(outFeatures.data_ptr<T>(), buffer.data_ptr<T>(),
                               indices.data_ptr<Index>(), nHotBlock,
                               numPlanes / vecloadFactor);
              TV_CHECK_CUDA_ERR();
            }
            if (size - nHotBlock > 0) {
              scatterAddGenericKernel<T, Index, int(NumTLP), NumILP>
                  <<<dim3(1, numPlanes / NumTLP), dim3(NumTLP / NumILP, NumTLP),
                     0, stream>>>(outFeatures.data_ptr<T>(),
Yan Yan's avatar
Yan Yan committed
140
141
142
                                  buffer.data_ptr<T>() + nHotBlock * numPlanes,
                                  indices.data_ptr<Index>() + nHotBlock,
                                  size - nHotBlock, numPlanes);
143
144
145
146
147
              TV_CHECK_CUDA_ERR();
            }
            notFound = false;
          }
        }
Yan Yan's avatar
Yan Yan committed
148
      });
149
150
151
152
153
154
155
156
157
158
159
160
161
      if (notFound) {
        constexpr int NumTLP = 64;
        constexpr int NumILP = NumTLP / 4;
        scatterAddGenericKernel<T, Index, NumTLP, NumILP>
            <<<dim3(tv::cuda::DivUp(size, NumTLP),
                    tv::cuda::DivUp(numPlanes, NumTLP)),
               dim3(NumTLP / NumILP, NumTLP), 0, stream>>>(
                outFeatures.data_ptr<T>(), buffer.data_ptr<T>(),
                indices.data_ptr<Index>(), size, numPlanes);
        TV_CHECK_CUDA_ERR();
      }
    });
  });
Yan Yan's avatar
Yan Yan committed
162
}
traveller59's avatar
traveller59 committed
163
164

} // namespace spconv