-
Notifications
You must be signed in to change notification settings - Fork 2
/
cudnn_convolution.cpp
98 lines (89 loc) · 2.58 KB
/
cudnn_convolution.cpp
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
#include <torch/extension.h>
#include <vector>
#include <ATen/NativeFunctions.h>
#include <ATen/Config.h>
/*
PyTorch extension enabling direct access to the following cuDNN-accelerated C++ functions
that are included in PyTorch:
- cudnn_convolution
- cudnn_convolution_backward_weight
- cudnn_convolution_backward_input
The functions defined here can be called from Python in replacement of
torch.nn.conv2d, torch.nn.grad.conv2d_weight and torch.nn.grad.conv2d_input,
and run significantly faster. See 'example.py' for how these functions
are called.
Adapted from code posted by hanspinckaers:
https://discuss.pytorch.org/t/cuda-error-with-cudnn-convolution-backward-weight-function/41214
*/
at::Tensor convolution(
const at::Tensor& input,
const at::Tensor& weight,
const at::Tensor& bias,
c10::ArrayRef<int64_t> stride,
c10::ArrayRef<int64_t> padding,
c10::ArrayRef<int64_t> dilation,
int64_t groups,
bool benchmark,
bool deterministic) {
return at::cudnn_convolution(
input,
weight,
bias,
padding,
stride,
dilation,
groups,
benchmark,
deterministic);
}
at::Tensor convolution_backward_weight(
const at::Tensor& input,
c10::ArrayRef<int64_t> weight_size,
const at::Tensor& grad_output,
c10::ArrayRef<int64_t> stride,
c10::ArrayRef<int64_t> padding,
c10::ArrayRef<int64_t> dilation,
int64_t groups,
bool benchmark,
bool deterministic,
bool allow_tf32) {
return at::cudnn_convolution_backward_weight(
weight_size,
grad_output,
input,
padding,
stride,
dilation,
groups,
benchmark,
deterministic,
allow_tf32);
}
at::Tensor convolution_backward_input(
c10::ArrayRef<int64_t> input_size,
const at::Tensor& weight,
const at::Tensor& grad_output,
c10::ArrayRef<int64_t> stride,
c10::ArrayRef<int64_t> padding,
c10::ArrayRef<int64_t> dilation,
int64_t groups,
bool benchmark,
bool deterministic,
bool allow_tf32) {
return at::cudnn_convolution_backward_input(
input_size,
grad_output,
weight,
padding,
stride,
dilation,
groups,
benchmark,
deterministic,
allow_tf32);
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("convolution", &convolution, "convolution");
m.def("convolution_backward_weight", &convolution_backward_weight, "convolution backward weight");
m.def("convolution_backward_input", &convolution_backward_input, "convolution backward input");
}