Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

deform_conv2d_kernel.cpp 6.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. #include "../deform_conv2d.h"
  2. #include <torch/autograd.h>
  3. #include <torch/types.h>
  4. namespace vision {
  5. namespace ops {
  6. namespace {
  7. class DeformConv2dFunction
  8. : public torch::autograd::Function<DeformConv2dFunction> {
  9. public:
  10. static torch::autograd::variable_list forward(
  11. torch::autograd::AutogradContext* ctx,
  12. const torch::autograd::Variable& input,
  13. const torch::autograd::Variable& weight,
  14. const torch::autograd::Variable& offset,
  15. const torch::autograd::Variable& mask,
  16. const torch::autograd::Variable& bias,
  17. c10::SymInt stride_h,
  18. c10::SymInt stride_w,
  19. c10::SymInt pad_h,
  20. c10::SymInt pad_w,
  21. c10::SymInt dilation_h,
  22. c10::SymInt dilation_w,
  23. c10::SymInt groups,
  24. c10::SymInt offset_groups,
  25. bool use_mask) {
  26. at::AutoDispatchBelowADInplaceOrView g;
  27. auto output = deform_conv2d_symint(
  28. input,
  29. weight,
  30. offset,
  31. mask,
  32. bias,
  33. stride_h,
  34. stride_w,
  35. pad_h,
  36. pad_w,
  37. dilation_h,
  38. dilation_w,
  39. groups,
  40. offset_groups,
  41. use_mask);
  42. ctx->save_for_backward({input, weight, offset, mask, bias});
  43. ctx->saved_data["stride_h"] = stride_h;
  44. ctx->saved_data["stride_w"] = stride_w;
  45. ctx->saved_data["pad_h"] = pad_h;
  46. ctx->saved_data["pad_w"] = pad_w;
  47. ctx->saved_data["dilation_h"] = dilation_h;
  48. ctx->saved_data["dilation_w"] = dilation_w;
  49. ctx->saved_data["groups"] = groups;
  50. ctx->saved_data["offset_groups"] = offset_groups;
  51. ctx->saved_data["use_mask"] = use_mask;
  52. return {
  53. output,
  54. };
  55. }
  56. static torch::autograd::variable_list backward(
  57. torch::autograd::AutogradContext* ctx,
  58. const torch::autograd::variable_list& grad_output) {
  59. auto saved = ctx->get_saved_variables();
  60. auto input = saved[0];
  61. auto weight = saved[1];
  62. auto offset = saved[2];
  63. auto mask = saved[3];
  64. auto bias = saved[4];
  65. auto stride_h = ctx->saved_data["stride_h"].toSymInt();
  66. auto stride_w = ctx->saved_data["stride_w"].toSymInt();
  67. auto pad_h = ctx->saved_data["pad_h"].toSymInt();
  68. auto pad_w = ctx->saved_data["pad_w"].toSymInt();
  69. auto dilation_h = ctx->saved_data["dilation_h"].toSymInt();
  70. auto dilation_w = ctx->saved_data["dilation_w"].toSymInt();
  71. auto groups = ctx->saved_data["groups"].toSymInt();
  72. auto offset_groups = ctx->saved_data["offset_groups"].toSymInt();
  73. auto use_mask = ctx->saved_data["use_mask"].toBool();
  74. auto grads = detail::_deform_conv2d_backward_symint(
  75. grad_output[0],
  76. input,
  77. weight,
  78. offset,
  79. mask,
  80. bias,
  81. stride_h,
  82. stride_w,
  83. pad_h,
  84. pad_w,
  85. dilation_h,
  86. dilation_w,
  87. groups,
  88. offset_groups,
  89. use_mask);
  90. auto grad_input = std::get<0>(grads);
  91. auto grad_weight = std::get<1>(grads);
  92. auto grad_offset = std::get<2>(grads);
  93. auto grad_mask = std::get<3>(grads);
  94. auto grad_bias = std::get<4>(grads);
  95. return {
  96. grad_input,
  97. grad_weight,
  98. grad_offset,
  99. grad_mask,
  100. grad_bias,
  101. torch::autograd::Variable(),
  102. torch::autograd::Variable(),
  103. torch::autograd::Variable(),
  104. torch::autograd::Variable(),
  105. torch::autograd::Variable(),
  106. torch::autograd::Variable(),
  107. torch::autograd::Variable(),
  108. torch::autograd::Variable(),
  109. torch::autograd::Variable(),
  110. };
  111. }
  112. };
  113. // TODO: There should be an easier way to do this
  114. class DeformConv2dBackwardFunction
  115. : public torch::autograd::Function<DeformConv2dBackwardFunction> {
  116. public:
  117. static torch::autograd::variable_list forward(
  118. torch::autograd::AutogradContext* ctx,
  119. const torch::autograd::Variable& grad,
  120. const torch::autograd::Variable& input,
  121. const torch::autograd::Variable& weight,
  122. const torch::autograd::Variable& offset,
  123. const torch::autograd::Variable& mask,
  124. const torch::autograd::Variable& bias,
  125. c10::SymInt stride_h,
  126. c10::SymInt stride_w,
  127. c10::SymInt pad_h,
  128. c10::SymInt pad_w,
  129. c10::SymInt dilation_h,
  130. c10::SymInt dilation_w,
  131. c10::SymInt groups,
  132. c10::SymInt offset_groups,
  133. bool use_mask) {
  134. at::AutoDispatchBelowADInplaceOrView g;
  135. auto result = detail::_deform_conv2d_backward_symint(
  136. grad,
  137. input,
  138. weight,
  139. offset,
  140. mask,
  141. bias,
  142. stride_h,
  143. stride_w,
  144. pad_h,
  145. pad_w,
  146. dilation_h,
  147. dilation_w,
  148. groups,
  149. offset_groups,
  150. use_mask);
  151. auto grad_input = std::get<0>(result);
  152. auto grad_weight = std::get<1>(result);
  153. auto grad_offset = std::get<2>(result);
  154. auto grad_mask = std::get<3>(result);
  155. auto grad_bias = std::get<4>(result);
  156. return {
  157. grad_input,
  158. grad_weight,
  159. grad_offset,
  160. grad_mask,
  161. grad_bias,
  162. };
  163. }
  164. static torch::autograd::variable_list backward(
  165. torch::autograd::AutogradContext* ctx,
  166. const torch::autograd::variable_list& grad_output) {
  167. TORCH_CHECK(0, "double backwards on deform_conv2d not supported");
  168. }
  169. };
  170. at::Tensor deform_conv2d_autograd(
  171. const at::Tensor& input,
  172. const at::Tensor& weight,
  173. const at::Tensor& offset,
  174. const at::Tensor& mask,
  175. const at::Tensor& bias,
  176. c10::SymInt stride_h,
  177. c10::SymInt stride_w,
  178. c10::SymInt pad_h,
  179. c10::SymInt pad_w,
  180. c10::SymInt dilation_h,
  181. c10::SymInt dilation_w,
  182. c10::SymInt groups,
  183. c10::SymInt offset_groups,
  184. bool use_mask) {
  185. return DeformConv2dFunction::apply(
  186. input,
  187. weight,
  188. offset,
  189. mask,
  190. bias,
  191. stride_h,
  192. stride_w,
  193. pad_h,
  194. pad_w,
  195. dilation_h,
  196. dilation_w,
  197. groups,
  198. offset_groups,
  199. use_mask)[0];
  200. }
  201. std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor, at::Tensor>
  202. deform_conv2d_backward_autograd(
  203. const at::Tensor& grad,
  204. const at::Tensor& input,
  205. const at::Tensor& weight,
  206. const at::Tensor& offset,
  207. const at::Tensor& mask,
  208. const at::Tensor& bias,
  209. c10::SymInt stride_h,
  210. c10::SymInt stride_w,
  211. c10::SymInt pad_h,
  212. c10::SymInt pad_w,
  213. c10::SymInt dilation_h,
  214. c10::SymInt dilation_w,
  215. c10::SymInt groups,
  216. c10::SymInt offset_groups,
  217. bool use_mask) {
  218. auto result = DeformConv2dBackwardFunction::apply(
  219. grad,
  220. input,
  221. weight,
  222. offset,
  223. mask,
  224. bias,
  225. stride_h,
  226. stride_w,
  227. pad_h,
  228. pad_w,
  229. dilation_h,
  230. dilation_w,
  231. groups,
  232. offset_groups,
  233. use_mask);
  234. return std::make_tuple(result[0], result[1], result[2], result[3], result[4]);
  235. }
  236. } // namespace
  237. TORCH_LIBRARY_IMPL(torchvision, Autograd, m) {
  238. m.impl(
  239. TORCH_SELECTIVE_NAME("torchvision::deform_conv2d"),
  240. TORCH_FN(deform_conv2d_autograd));
  241. m.impl(
  242. TORCH_SELECTIVE_NAME("torchvision::_deform_conv2d_backward"),
  243. TORCH_FN(deform_conv2d_backward_autograd));
  244. }
  245. } // namespace ops
  246. } // namespace vision
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...