File size: 14,979 Bytes
d19bd3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
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
166
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "msmv_sampling.h"

#define MAX_POINT 32

void ms_deformable_im2col_cuda_c2345(
    const float* feat_c2,
    const float* feat_c3,
    const float* feat_c4,
    const float* feat_c5,
    const int h_c2, const int w_c2,
    const int h_c3, const int w_c3,
    const int h_c4, const int w_c4,
    const int h_c5, const int w_c5,
    const float* data_sampling_loc,
    const float* data_attn_weight,
    const int batch_size,
    const int channels,
    const int num_views,
    const int num_query,
    const int num_point,
    float* data_col
);

void ms_deformable_im2col_cuda_c23456(
    const float* feat_c2,
    const float* feat_c3,
    const float* feat_c4,
    const float* feat_c5,
    const float* feat_c6,
    const int h_c2, const int w_c2,
    const int h_c3, const int w_c3,
    const int h_c4, const int w_c4,
    const int h_c5, const int w_c5,
    const int h_c6, const int w_c6,
    const float* data_sampling_loc,
    const float* data_attn_weight,
    const int batch_size,
    const int channels,
    const int num_views,
    const int num_query,
    const int num_point,
    float* data_col
);

void ms_deformable_col2im_cuda_c2345(
    const float* grad_col,
    const float* feat_c2,
    const float* feat_c3,
    const float* feat_c4,
    const float* feat_c5,
    const int h_c2, const int w_c2,
    const int h_c3, const int w_c3,
    const int h_c4, const int w_c4,
    const int h_c5, const int w_c5,
    const float* data_sampling_loc,
    const float* data_attn_weight,
    const int batch_size,
    const int channels,
    const int num_views,
    const int num_query,
    const int num_point,
    float* grad_value_c2,
    float* grad_value_c3,
    float* grad_value_c4,
    float* grad_value_c5,
    float* grad_sampling_loc,
    float* grad_attn_weight
);

void ms_deformable_col2im_cuda_c23456(
    const float *grad_col,
    const float *feat_c2,
    const float *feat_c3,
    const float *feat_c4,
    const float *feat_c5,
    const float *feat_c6,
    const int h_c2, const int w_c2,
    const int h_c3, const int w_c3,
    const int h_c4, const int w_c4,
    const int h_c5, const int w_c5,
    const int h_c6, const int w_c6,
    const float *data_sampling_loc,
    const float *data_attn_weight,
    const int batch_size,
    const int channels,
    const int num_views,
    const int num_query,
    const int num_point,
    float *grad_value_c2,
    float *grad_value_c3,
    float *grad_value_c4,
    float *grad_value_c5,
    float *grad_value_c6,
    float *grad_sampling_loc,
    float *grad_attn_weight
);

at::Tensor ms_deform_attn_cuda_c2345_forward(
    const at::Tensor& feat_c2,  // [B, N, H, W, C]
    const at::Tensor& feat_c3,  // [B, N, H, W, C]
    const at::Tensor& feat_c4,  // [B, N, H, W, C]
    const at::Tensor& feat_c5,  // [B, N, H, W, C]
    const at::Tensor& sampling_loc,  // [B, Q, P, 3]
    const at::Tensor& attn_weight  // [B, Q, P, 4]
    ) {
    AT_ASSERTM(feat_c2.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c3.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c4.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c5.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(sampling_loc.is_contiguous(), "sampling_loc tensor has to be contiguous");
    AT_ASSERTM(attn_weight.is_contiguous(), "attn_weight tensor has to be contiguous");

    AT_ASSERTM(feat_c2.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c3.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c4.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c5.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(sampling_loc.is_cuda(), "sampling_loc must be a CUDA tensor");
    AT_ASSERTM(attn_weight.is_cuda(), "attn_weight must be a CUDA tensor");

    const int batch_size = feat_c2.size(0);
    const int num_views = feat_c2.size(1);
    const int channels = feat_c2.size(4);
    const int num_query = sampling_loc.size(1);
    const int num_point = sampling_loc.size(2);
    AT_ASSERTM(num_point <= MAX_POINT, "num_point exceed limits");

    const int h_c2 = feat_c2.size(2);
    const int w_c2 = feat_c2.size(3);
    const int h_c3 = feat_c3.size(2);
    const int w_c3 = feat_c3.size(3);
    const int h_c4 = feat_c4.size(2);
    const int w_c4 = feat_c4.size(3);
    const int h_c5 = feat_c5.size(2);
    const int w_c5 = feat_c5.size(3);

    auto output = at::zeros({ batch_size, num_query, channels, num_point }, feat_c2.options());
    ms_deformable_im2col_cuda_c2345(
        feat_c2.data_ptr<float>(),
        feat_c3.data_ptr<float>(),
        feat_c4.data_ptr<float>(),
        feat_c5.data_ptr<float>(),
        h_c2, w_c2, h_c3, w_c3, h_c4, w_c4, h_c5, w_c5,
        sampling_loc.data_ptr<float>(),
        attn_weight.data_ptr<float>(),
        batch_size, channels, num_views, num_query, num_point,
        output.data_ptr<float>()
    );

    return output;
}

at::Tensor ms_deform_attn_cuda_c23456_forward(
    const at::Tensor& feat_c2,  // [B, N, H, W, C]
    const at::Tensor& feat_c3,  // [B, N, H, W, C]
    const at::Tensor& feat_c4,  // [B, N, H, W, C]
    const at::Tensor& feat_c5,  // [B, N, H, W, C]
    const at::Tensor& feat_c6,  // [B, N, H, W, C]
    const at::Tensor& sampling_loc,  // [B, Q, P, 3]
    const at::Tensor& attn_weight  // [B, Q, P, 4]
    ) {
    AT_ASSERTM(feat_c2.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c3.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c4.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c5.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c6.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(sampling_loc.is_contiguous(), "sampling_loc tensor has to be contiguous");
    AT_ASSERTM(attn_weight.is_contiguous(), "attn_weight tensor has to be contiguous");

    AT_ASSERTM(feat_c2.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c3.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c4.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c5.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c6.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(sampling_loc.is_cuda(), "sampling_loc must be a CUDA tensor");
    AT_ASSERTM(attn_weight.is_cuda(), "attn_weight must be a CUDA tensor");

    const int batch_size = feat_c2.size(0);
    const int num_views = feat_c2.size(1);
    const int channels = feat_c2.size(4);
    const int num_query = sampling_loc.size(1);
    const int num_point = sampling_loc.size(2);
    AT_ASSERTM(num_point <= MAX_POINT, "num_point exceed limits");

    const int h_c2 = feat_c2.size(2);
    const int w_c2 = feat_c2.size(3);
    const int h_c3 = feat_c3.size(2);
    const int w_c3 = feat_c3.size(3);
    const int h_c4 = feat_c4.size(2);
    const int w_c4 = feat_c4.size(3);
    const int h_c5 = feat_c5.size(2);
    const int w_c5 = feat_c5.size(3);
    const int h_c6 = feat_c6.size(2);
    const int w_c6 = feat_c6.size(3);

    auto output = at::zeros({ batch_size, num_query, channels, num_point }, feat_c2.options());
    ms_deformable_im2col_cuda_c23456(
        feat_c2.data_ptr<float>(),
        feat_c3.data_ptr<float>(),
        feat_c4.data_ptr<float>(),
        feat_c5.data_ptr<float>(),
        feat_c6.data_ptr<float>(),
        h_c2, w_c2, h_c3, w_c3, h_c4, w_c4, h_c5, w_c5, h_c6, w_c6,
        sampling_loc.data_ptr<float>(),
        attn_weight.data_ptr<float>(),
        batch_size, channels, num_views, num_query, num_point,
        output.data_ptr<float>()
    );

    return output;
}

std::vector<at::Tensor> ms_deform_attn_cuda_c2345_backward(
    const at::Tensor& grad_output,
    const at::Tensor& feat_c2,  // [B, N, H, W, C]
    const at::Tensor& feat_c3,  // [B, N, H, W, C]
    const at::Tensor& feat_c4,  // [B, N, H, W, C]
    const at::Tensor& feat_c5,  // [B, N, H, W, C]
    const at::Tensor& sampling_loc,  // [B, Q, P, 3]
    const at::Tensor& attn_weight  // [B, Q, P, 4]
    ) {
    AT_ASSERTM(feat_c2.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c3.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c4.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c5.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(sampling_loc.is_contiguous(), "sampling_loc tensor has to be contiguous");
    AT_ASSERTM(attn_weight.is_contiguous(), "attn_weight tensor has to be contiguous");
    AT_ASSERTM(grad_output.is_contiguous(), "grad_output tensor has to be contiguous");

    AT_ASSERTM(feat_c2.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c3.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c4.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c5.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(sampling_loc.is_cuda(), "sampling_loc must be a CUDA tensor");
    AT_ASSERTM(attn_weight.is_cuda(), "attn_weight must be a CUDA tensor");
    AT_ASSERTM(grad_output.is_cuda(), "grad_output must be a CUDA tensor");

    const int batch_size = feat_c2.size(0);
    const int num_views = feat_c2.size(1);
    const int channels = feat_c2.size(4);
    const int num_query = sampling_loc.size(1);
    const int num_point = sampling_loc.size(2);
    AT_ASSERTM(num_point <= MAX_POINT, "num_point exceed limits");

    auto grad_value_c2 = at::zeros_like(feat_c2);
    auto grad_value_c3 = at::zeros_like(feat_c3);
    auto grad_value_c4 = at::zeros_like(feat_c4);
    auto grad_value_c5 = at::zeros_like(feat_c5);
    auto grad_sampling_loc = at::zeros_like(sampling_loc);
    auto grad_attn_weight = at::zeros_like(attn_weight);

    const int h_c2 = feat_c2.size(2);
    const int w_c2 = feat_c2.size(3);
    const int h_c3 = feat_c3.size(2);
    const int w_c3 = feat_c3.size(3);
    const int h_c4 = feat_c4.size(2);
    const int w_c4 = feat_c4.size(3);
    const int h_c5 = feat_c5.size(2);
    const int w_c5 = feat_c5.size(3);

    ms_deformable_col2im_cuda_c2345(
        grad_output.data_ptr<float>(),
        feat_c2.data_ptr<float>(),
        feat_c3.data_ptr<float>(),
        feat_c4.data_ptr<float>(),
        feat_c5.data_ptr<float>(),
        h_c2, w_c2, h_c3, w_c3, h_c4, w_c4, h_c5, w_c5,
        sampling_loc.data_ptr<float>(),
        attn_weight.data_ptr<float>(),
        batch_size, channels, num_views, num_query, num_point,
        grad_value_c2.data_ptr<float>(),
        grad_value_c3.data_ptr<float>(),
        grad_value_c4.data_ptr<float>(),
        grad_value_c5.data_ptr<float>(),
        grad_sampling_loc.data_ptr<float>(),
        grad_attn_weight.data_ptr<float>()
    );

    return {
        grad_value_c2, grad_value_c3, grad_value_c4, grad_value_c5, grad_sampling_loc, grad_attn_weight
    };
}

std::vector<at::Tensor> ms_deform_attn_cuda_c23456_backward(
    const at::Tensor& grad_output,
    const at::Tensor& feat_c2,  // [B, N, H, W, C]
    const at::Tensor& feat_c3,  // [B, N, H, W, C]
    const at::Tensor& feat_c4,  // [B, N, H, W, C]
    const at::Tensor& feat_c5,  // [B, N, H, W, C]
    const at::Tensor& feat_c6,  // [B, N, H, W, C]
    const at::Tensor& sampling_loc,  // [B, Q, P, 3]
    const at::Tensor& attn_weight  // [B, Q, P, 4]
    ) {
    AT_ASSERTM(feat_c2.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c3.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c4.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c5.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(feat_c6.is_contiguous(), "value tensor has to be contiguous");
    AT_ASSERTM(sampling_loc.is_contiguous(), "sampling_loc tensor has to be contiguous");
    AT_ASSERTM(attn_weight.is_contiguous(), "attn_weight tensor has to be contiguous");
    AT_ASSERTM(grad_output.is_contiguous(), "grad_output tensor has to be contiguous");

    AT_ASSERTM(feat_c2.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c3.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c4.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c5.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(feat_c6.is_cuda(), "value must be a CUDA tensor");
    AT_ASSERTM(sampling_loc.is_cuda(), "sampling_loc must be a CUDA tensor");
    AT_ASSERTM(attn_weight.is_cuda(), "attn_weight must be a CUDA tensor");
    AT_ASSERTM(grad_output.is_cuda(), "grad_output must be a CUDA tensor");

    const int batch_size = feat_c2.size(0);
    const int num_views = feat_c2.size(1);
    const int channels = feat_c2.size(4);
    const int num_query = sampling_loc.size(1);
    const int num_point = sampling_loc.size(2);
    AT_ASSERTM(num_point <= MAX_POINT, "num_point exceed limits");

    auto grad_value_c2 = at::zeros_like(feat_c2);
    auto grad_value_c3 = at::zeros_like(feat_c3);
    auto grad_value_c4 = at::zeros_like(feat_c4);
    auto grad_value_c5 = at::zeros_like(feat_c5);
    auto grad_value_c6 = at::zeros_like(feat_c6);
    auto grad_sampling_loc = at::zeros_like(sampling_loc);
    auto grad_attn_weight = at::zeros_like(attn_weight);

    const int h_c2 = feat_c2.size(2);
    const int w_c2 = feat_c2.size(3);
    const int h_c3 = feat_c3.size(2);
    const int w_c3 = feat_c3.size(3);
    const int h_c4 = feat_c4.size(2);
    const int w_c4 = feat_c4.size(3);
    const int h_c5 = feat_c5.size(2);
    const int w_c5 = feat_c5.size(3);
    const int h_c6 = feat_c6.size(2);
    const int w_c6 = feat_c6.size(3);

    ms_deformable_col2im_cuda_c23456(
        grad_output.data_ptr<float>(),
        feat_c2.data_ptr<float>(),
        feat_c3.data_ptr<float>(),
        feat_c4.data_ptr<float>(),
        feat_c5.data_ptr<float>(),
        feat_c6.data_ptr<float>(),
        h_c2, w_c2, h_c3, w_c3, h_c4, w_c4, h_c5, w_c5, h_c6, w_c6,
        sampling_loc.data_ptr<float>(),
        attn_weight.data_ptr<float>(),
        batch_size, channels, num_views, num_query, num_point,
        grad_value_c2.data_ptr<float>(),
        grad_value_c3.data_ptr<float>(),
        grad_value_c4.data_ptr<float>(),
        grad_value_c5.data_ptr<float>(),
        grad_value_c6.data_ptr<float>(),
        grad_sampling_loc.data_ptr<float>(),
        grad_attn_weight.data_ptr<float>()
    );

    return {
        grad_value_c2, grad_value_c3, grad_value_c4, grad_value_c5, grad_value_c6, grad_sampling_loc, grad_attn_weight
    };
}

#ifdef TORCH_EXTENSION_NAME
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
    m.def("_ms_deform_attn_cuda_c2345_forward", &ms_deform_attn_cuda_c2345_forward, "pass");
    m.def("_ms_deform_attn_cuda_c2345_backward", &ms_deform_attn_cuda_c2345_backward, "pass");
    m.def("_ms_deform_attn_cuda_c23456_forward", &ms_deform_attn_cuda_c23456_forward, "pass");
    m.def("_ms_deform_attn_cuda_c23456_backward", &ms_deform_attn_cuda_c23456_backward, "pass");
}
#endif