forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conv_v8.cpp
177 lines (148 loc) · 6.03 KB
/
Conv_v8.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
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
#include <ATen/cuda/CUDAConfig.h> // for the definition of AT_CUDNN_ENABLED
#if AT_CUDNN_ENABLED()
#include <ATen/native/cudnn/Macros.h>
#if HAS_CUDNN_V8()
#include <ATen/cudnn/cudnn-wrapper.h>
#include <cudnn_frontend.h>
#include <ATen/ATen.h>
#include <ATen/TensorUtils.h>
#include <ATen/cuda/Exceptions.h>
#include <ATen/native/ConvUtils.h>
#include <ATen/native/cudnn/ConvShared.h>
#include <ATen/native/utils/ParamsHash.h>
#include <ATen/cudnn/Handle.h>
#include <ATen/TensorUtils.h>
#include <unordered_map>
namespace at { namespace native{
namespace {
uint8_t getAlignment(const Tensor &t) {
// alignment are in bytes
uint8_t alignment = 1;
uint64_t address = reinterpret_cast<uint64_t>(t.data_ptr());
while (address % alignment == 0 && alignment < 16) alignment *= 2;
return alignment;
}
cudnn_frontend::Tensor getTensorDescriptor(const Tensor &t, int64_t id, uint8_t alignment) {
auto shape = t.sizes();
auto strides = t.strides();
return cudnn_frontend::TensorBuilder()
.setDim(shape.size(), shape.data())
.setStrides(strides.size(), strides.data())
.setId(id)
.setAlignment(alignment)
.setDataType(getCudnnDataType(t))
.build();
}
cudnn_frontend::ConvDesc_v8 getConvDescriptor(cudnnDataType_t dataType, IntArrayRef padding, IntArrayRef stride, IntArrayRef dilation) {
uint64_t convDim = stride.size();
return cudnn_frontend::ConvDescBuilder()
.setDataType(dataType)
.setMathMode(CUDNN_CROSS_CORRELATION)
.setNDims(convDim)
.setStrides(convDim, stride.data())
.setPrePadding(convDim, padding.data())
.setPostPadding(convDim, padding.data())
.setDilation(convDim, dilation.data())
.build();
}
void filterEngineConfigs(
cudnn_frontend::EngineConfigList &from,
cudnn_frontend::EngineConfigList &to,
bool deterministic, bool allow_tf32, c10::ScalarType scalar_type)
{
auto filter = [=](cudnnBackendDescriptor_t c) {
if (deterministic) {
if (cudnn_frontend::hasNumericalNote<CUDNN_NUMERICAL_NOTE_NONDETERMINISTIC>(c)) return true;
}
if (scalar_type == kFloat || !allow_tf32) {
if (cudnn_frontend::hasNumericalNote<CUDNN_NUMERICAL_NOTE_DOWN_CONVERT_INPUTS>(c)) return true;
if (cudnn_frontend::hasNumericalNote<CUDNN_NUMERICAL_NOTE_TENSOR_CORE>(c)) return true;
}
return false;
};
cudnn_frontend::filter(from, to, filter);
}
struct CacheKey {
ConvolutionParams params;
uint8_t input_alignment;
uint8_t weight_alignment;
uint8_t output_alignment;
};
// FIXME: make this thread-safe by reusing the benchmark cache in Conv_v7.cpp
std::unordered_map<CacheKey, cudnn_frontend::ManagedOpaqueDescriptor, ParamsHash<CacheKey>, ParamsEqual<CacheKey>> engine_cache;
}
void raw_cudnn_convolution_forward_out(
const Tensor& output, const Tensor& input, const Tensor& weight,
IntArrayRef padding, IntArrayRef stride, IntArrayRef dilation, int64_t groups,
bool benchmark, bool deterministic, bool allow_tf32)
{
TORCH_CHECK(!benchmark, "not supported yet");
if (output.numel() == 0) {
return;
}
cudnnHandle_t handle = getCudnnHandle();
CacheKey key;
setConvolutionParams(&key.params, input, weight, padding, stride, dilation, groups, deterministic, allow_tf32);
key.input_alignment = getAlignment(input);
key.output_alignment = getAlignment(output);
key.weight_alignment = getAlignment(weight);
auto run = [&](cudnn_frontend::ManagedOpaqueDescriptor cfg) {
auto plan = cudnn_frontend::ExecutionPlanBuilder()
.setHandle(handle)
.setEngineConfig(cfg)
.build();
auto workspace_size = plan.getWorkspaceSize();
auto workspace = at::empty({workspace_size}, input.options().dtype(kByte));
void *data_ptrs[] = {input.data_ptr(), output.data_ptr(), weight.data_ptr()};
// std::cout << plan.describe() << " requires workspace " << workspace_size << std::endl;
int64_t uids[] = {'x', 'y', 'w'};
auto variantPack = cudnn_frontend::VariantPackBuilder()
.setWorkspacePointer(workspace.data_ptr())
.setDataPointers(3, data_ptrs)
.setUids(3, uids)
.build();
AT_CUDNN_CHECK(cudnnBackendExecute(handle, plan.get_raw_desc(), variantPack.get_raw_desc()));
};
auto search = engine_cache.find(key);
if (search != engine_cache.end()) {
run(search->second);
return;
}
auto op = cudnn_frontend::OperationBuilder(CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR)
.setxDesc(getTensorDescriptor(input, 'x', key.input_alignment))
.setyDesc(getTensorDescriptor(output, 'y', key.output_alignment))
.setwDesc(getTensorDescriptor(weight, 'w', key.weight_alignment))
.setcDesc(getConvDescriptor(key.params.dataType, padding, stride, dilation))
.build();
// std::cout << op.describe() << std::endl;
std::array<cudnn_frontend::Operation const *, 1> ops = {&op};
auto opGraph = cudnn_frontend::OperationGraphBuilder()
.setHandle(handle)
.setOperationGraph(1, ops.data())
.build();
// std::cout << opGraph.describe() << std::endl;
auto heuristics = cudnn_frontend::EngineHeuristicsBuilder()
.setOperationGraph(opGraph)
.setHeurMode(CUDNN_HEUR_MODE_INSTANT)
.build();
auto fallback = cudnn_frontend::EngineFallbackListBuilder()
.setOperationGraph(opGraph)
.setOperation(CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR)
.build();
auto& engine_configs = heuristics.getEngineConfig(heuristics.getEngineConfigCount());
auto& fallback_list = fallback.getFallbackList();
cudnn_frontend::EngineConfigList filtered_configs;
filterEngineConfigs(engine_configs, filtered_configs, deterministic, allow_tf32, input.scalar_type());
filterEngineConfigs(fallback_list, filtered_configs, deterministic, allow_tf32, input.scalar_type());
for (auto &cfg : filtered_configs) {
try {
run(cfg);
engine_cache[key] = cfg;
return;
} catch (cudnn_frontend::cudnnException &e) {} catch(CuDNNError &e) {}
}
TORCH_CHECK(false, "Unable to find an engine to execute this computation");
}
}} // at::native
#endif // HAS_CUDNN_V8
#endif // AT_CUDNN_ENABLED