"sgl-kernel/python/vscode:/vscode.git/clone" did not exist on "a2ffdd604e4b1e3fbb54d90c1ea555b597fa5ed0"
segment_csr.cpp 8.56 KB
Newer Older
1
#ifdef WITH_PYTHON
rusty1s's avatar
update  
rusty1s committed
2
#include <Python.h>
3
4
#endif

rusty1s's avatar
rusty1s committed
5
6
7
#include <torch/script.h>

#include "cpu/segment_csr_cpu.h"
8
#include "macros.h"
9
#include "utils.h"
rusty1s's avatar
rusty1s committed
10
11
12
13
14

#ifdef WITH_CUDA
#include "cuda/segment_csr_cuda.h"
#endif

rusty1s's avatar
update  
rusty1s committed
15
#ifdef _WIN32
16
#ifdef WITH_PYTHON
rusty1s's avatar
update  
rusty1s committed
17
18
19
20
21
#ifdef WITH_CUDA
PyMODINIT_FUNC PyInit__segment_csr_cuda(void) { return NULL; }
#else
PyMODINIT_FUNC PyInit__segment_csr_cpu(void) { return NULL; }
#endif
rusty1s's avatar
update  
rusty1s committed
22
#endif
23
#endif
rusty1s's avatar
update  
rusty1s committed
24

rusty1s's avatar
rusty1s committed
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
std::tuple<torch::Tensor, torch::optional<torch::Tensor>>
segment_csr_fw(torch::Tensor src, torch::Tensor indptr,
               torch::optional<torch::Tensor> optional_out,
               std::string reduce) {
  if (src.device().is_cuda()) {
#ifdef WITH_CUDA
    return segment_csr_cuda(src, indptr, optional_out, reduce);
#else
    AT_ERROR("Not compiled with CUDA support");
#endif
  } else {
    return segment_csr_cpu(src, indptr, optional_out, reduce);
  }
}

torch::Tensor gather_csr_fw(torch::Tensor src, torch::Tensor indptr,
                            torch::optional<torch::Tensor> optional_out) {
  if (src.device().is_cuda()) {
#ifdef WITH_CUDA
    return gather_csr_cuda(src, indptr, optional_out);
#else
    AT_ERROR("Not compiled with CUDA support");
#endif
  } else {
    return gather_csr_cpu(src, indptr, optional_out);
  }
}

using torch::autograd::AutogradContext;
using torch::autograd::Variable;
using torch::autograd::variable_list;

class SegmentSumCSR : public torch::autograd::Function<SegmentSumCSR> {
public:
  static variable_list forward(AutogradContext *ctx, Variable src,
                               Variable indptr,
                               torch::optional<Variable> optional_out) {
    ctx->saved_data["src_shape"] = src.sizes();
    auto out = std::get<0>(segment_csr_fw(src, indptr, optional_out, "sum"));
    ctx->save_for_backward({indptr});
    if (optional_out.has_value())
      ctx->mark_dirty({optional_out.value()});
    return {out};
  }

  static variable_list backward(AutogradContext *ctx, variable_list grad_outs) {
    auto grad_out = grad_outs[0];
    auto saved = ctx->get_saved_variables();
    auto indptr = saved[0];
74
    auto src_shape = list2vec(ctx->saved_data["src_shape"].toIntList());
rusty1s's avatar
rusty1s committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    auto grad_in = torch::empty(src_shape, grad_out.options());
    gather_csr_fw(grad_out, indptr, grad_in);
    return {grad_in, Variable(), Variable()};
  }
};

class SegmentMeanCSR : public torch::autograd::Function<SegmentMeanCSR> {
public:
  static variable_list forward(AutogradContext *ctx, Variable src,
                               Variable indptr,
                               torch::optional<Variable> optional_out) {
    ctx->saved_data["src_shape"] = src.sizes();
    auto out = std::get<0>(segment_csr_fw(src, indptr, optional_out, "mean"));
    ctx->save_for_backward({indptr});
    if (optional_out.has_value())
      ctx->mark_dirty({optional_out.value()});
    return {out};
  }

  static variable_list backward(AutogradContext *ctx, variable_list grad_outs) {
    auto grad_out = grad_outs[0];
    auto saved = ctx->get_saved_variables();
    auto indptr = saved[0];
98
    auto src_shape = list2vec(ctx->saved_data["src_shape"].toIntList());
rusty1s's avatar
rusty1s committed
99
    auto grad_in = torch::empty(src_shape, grad_out.options());
rusty1s's avatar
rusty1s committed
100
101
102
103
104
105
106
107
    if (grad_in.numel() > 0) {
      gather_csr_fw(grad_out, indptr, grad_in);
      auto indptr1 = indptr.narrow(-1, 0, indptr.size(-1) - 1);
      auto indptr2 = indptr.narrow(-1, 1, indptr.size(-1) - 1);
      auto count = (indptr2 - indptr1).to(grad_in.options());
      count = gather_csr_fw(count, indptr, torch::nullopt);
      for (auto i = 0; i < grad_out.dim() - indptr.dim(); i++)
        count = count.unsqueeze(-1);
rusty1s's avatar
rusty1s committed
108
      grad_in.true_divide_(count);
rusty1s's avatar
rusty1s committed
109
    }
rusty1s's avatar
rusty1s committed
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
    return {grad_in, Variable(), Variable()};
  }
};

class SegmentMinCSR : public torch::autograd::Function<SegmentMinCSR> {
public:
  static variable_list forward(AutogradContext *ctx, Variable src,
                               Variable indptr,
                               torch::optional<Variable> optional_out) {
    ctx->saved_data["src_shape"] = src.sizes();
    auto result = segment_csr_fw(src, indptr, optional_out, "min");
    auto out = std::get<0>(result);
    auto arg_out = std::get<1>(result).value();
    ctx->save_for_backward({indptr, arg_out});
    ctx->mark_non_differentiable({arg_out});
    if (optional_out.has_value())
      ctx->mark_dirty({optional_out.value()});
    return {out, arg_out};
  }

  static variable_list backward(AutogradContext *ctx, variable_list grad_outs) {
    auto grad_out = grad_outs[0];
    auto saved = ctx->get_saved_variables();
    auto indptr = saved[0];
    auto arg_out = saved[1];
135
    auto src_shape = list2vec(ctx->saved_data["src_shape"].toIntList());
rusty1s's avatar
rusty1s committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    src_shape[indptr.dim() - 1] += 1;
    auto grad_in = torch::zeros(src_shape, grad_out.options());
    grad_in.scatter_(indptr.dim() - 1, arg_out, grad_out);
    grad_in =
        grad_in.narrow(indptr.dim() - 1, 0, src_shape[indptr.dim() - 1] - 1);
    return {grad_in, Variable(), Variable()};
  }
};

class SegmentMaxCSR : public torch::autograd::Function<SegmentMaxCSR> {
public:
  static variable_list forward(AutogradContext *ctx, Variable src,
                               Variable indptr,
                               torch::optional<Variable> optional_out) {
    ctx->saved_data["src_shape"] = src.sizes();
    auto result = segment_csr_fw(src, indptr, optional_out, "max");
    auto out = std::get<0>(result);
    auto arg_out = std::get<1>(result).value();
    ctx->save_for_backward({indptr, arg_out});
    ctx->mark_non_differentiable({arg_out});
    if (optional_out.has_value())
      ctx->mark_dirty({optional_out.value()});
    return {out, arg_out};
  }

  static variable_list backward(AutogradContext *ctx, variable_list grad_outs) {
    auto grad_out = grad_outs[0];
    auto saved = ctx->get_saved_variables();
    auto indptr = saved[0];
    auto arg_out = saved[1];
166
    auto src_shape = list2vec(ctx->saved_data["src_shape"].toIntList());
rusty1s's avatar
rusty1s committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    src_shape[indptr.dim() - 1] += 1;
    auto grad_in = torch::zeros(src_shape, grad_out.options());
    grad_in.scatter_(indptr.dim() - 1, arg_out, grad_out);
    grad_in =
        grad_in.narrow(indptr.dim() - 1, 0, src_shape[indptr.dim() - 1] - 1);
    return {grad_in, Variable(), Variable()};
  }
};

class GatherCSR : public torch::autograd::Function<GatherCSR> {
public:
  static variable_list forward(AutogradContext *ctx, Variable src,
                               Variable indptr,
                               torch::optional<Variable> optional_out) {
    ctx->saved_data["src_shape"] = src.sizes();
    auto out = gather_csr_fw(src, indptr, optional_out);
    ctx->save_for_backward({indptr});
    if (optional_out.has_value())
      ctx->mark_dirty({optional_out.value()});
    return {out};
  }

  static variable_list backward(AutogradContext *ctx, variable_list grad_outs) {
    auto grad_out = grad_outs[0];
    auto saved = ctx->get_saved_variables();
    auto indptr = saved[0];
193
    auto src_shape = list2vec(ctx->saved_data["src_shape"].toIntList());
rusty1s's avatar
rusty1s committed
194
195
196
197
198
199
200

    auto grad_in = torch::empty(src_shape, grad_out.options());
    segment_csr_fw(grad_out, indptr, grad_in, "sum");
    return {grad_in, Variable(), Variable()};
  }
};

201
202
203
SCATTER_API torch::Tensor
segment_sum_csr(torch::Tensor src, torch::Tensor indptr,
                torch::optional<torch::Tensor> optional_out) {
rusty1s's avatar
rusty1s committed
204
205
206
  return SegmentSumCSR::apply(src, indptr, optional_out)[0];
}

207
208
209
SCATTER_API torch::Tensor
segment_mean_csr(torch::Tensor src, torch::Tensor indptr,
                 torch::optional<torch::Tensor> optional_out) {
rusty1s's avatar
rusty1s committed
210
211
212
  return SegmentMeanCSR::apply(src, indptr, optional_out)[0];
}

213
SCATTER_API std::tuple<torch::Tensor, torch::Tensor>
rusty1s's avatar
rusty1s committed
214
215
216
217
218
219
segment_min_csr(torch::Tensor src, torch::Tensor indptr,
                torch::optional<torch::Tensor> optional_out) {
  auto result = SegmentMinCSR::apply(src, indptr, optional_out);
  return std::make_tuple(result[0], result[1]);
}

220
SCATTER_API std::tuple<torch::Tensor, torch::Tensor>
rusty1s's avatar
rusty1s committed
221
222
223
224
225
226
segment_max_csr(torch::Tensor src, torch::Tensor indptr,
                torch::optional<torch::Tensor> optional_out) {
  auto result = SegmentMaxCSR::apply(src, indptr, optional_out);
  return std::make_tuple(result[0], result[1]);
}

227
228
229
SCATTER_API torch::Tensor
gather_csr(torch::Tensor src, torch::Tensor indptr,
           torch::optional<torch::Tensor> optional_out) {
rusty1s's avatar
rusty1s committed
230
231
232
233
234
235
236
237
238
239
  return GatherCSR::apply(src, indptr, optional_out)[0];
}

static auto registry =
    torch::RegisterOperators()
        .op("torch_scatter::segment_sum_csr", &segment_sum_csr)
        .op("torch_scatter::segment_mean_csr", &segment_mean_csr)
        .op("torch_scatter::segment_min_csr", &segment_min_csr)
        .op("torch_scatter::segment_max_csr", &segment_max_csr)
        .op("torch_scatter::gather_csr", &gather_csr);