-
Notifications
You must be signed in to change notification settings - Fork 74k
/
conv_ops.cc
245 lines (208 loc) · 10.5 KB
/
conv_ops.cc
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
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// See docs in ../ops/nn_ops.cc.
#define USE_EIGEN_TENSOR
#define EIGEN_USE_THREADS
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#define EIGEN_USE_GPU
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#include "tensorflow/core/kernels/conv_ops.h"
#include <string.h>
#include "tensorflow/core/framework/bounds_check.h"
#include "tensorflow/core/framework/kernel_shape_util.h"
namespace tensorflow {
typedef Eigen::ThreadPoolDevice CPUDevice;
typedef Eigen::GpuDevice GPUDevice;
#define TF_REQUIRES(EXP, STATUS) \
do { \
if (!TF_PREDICT_TRUE(EXP)) return (STATUS); \
} while (false)
Status InitConv2DParameters(const OpKernelConstruction* context,
Conv2DParameters* params) {
TF_RETURN_IF_ERROR(context->GetAttr("dilations", ¶ms->dilations));
TF_RETURN_IF_ERROR(context->GetAttr("strides", ¶ms->strides));
TF_RETURN_IF_ERROR(context->GetAttr("padding", ¶ms->padding));
if (context->HasAttr("explicit_paddings")) {
TF_RETURN_IF_ERROR(
context->GetAttr("explicit_paddings", ¶ms->explicit_paddings));
}
string data_format_string;
TF_RETURN_IF_ERROR(context->GetAttr("data_format", &data_format_string));
TF_REQUIRES(FormatFromString(data_format_string, ¶ms->data_format),
errors::InvalidArgument("Invalid data format"));
const auto& strides = params->strides;
const auto& dilations = params->dilations;
const auto& data_format = params->data_format;
TF_REQUIRES(dilations.size() == 4,
errors::InvalidArgument("Sliding window dilations field must "
"specify 4 dimensions"));
TF_REQUIRES(strides.size() == 4,
errors::InvalidArgument("Sliding window strides field must "
"specify 4 dimensions"));
const int64_t stride_n = GetTensorDim(strides, data_format, 'N');
const int64_t stride_c = GetTensorDim(strides, data_format, 'C');
const int64_t stride_h = GetTensorDim(strides, data_format, 'H');
const int64_t stride_w = GetTensorDim(strides, data_format, 'W');
TF_REQUIRES(
stride_n == 1 && stride_c == 1,
errors::Unimplemented("Current implementation does not yet support "
"strides in the batch and depth dimensions."));
TF_REQUIRES(stride_h > 0 && stride_w > 0,
errors::InvalidArgument(
"Row and column strides should be larger than 0."));
const int64_t dilation_n = GetTensorDim(dilations, data_format, 'N');
const int64_t dilation_c = GetTensorDim(dilations, data_format, 'C');
const int64_t dilation_h = GetTensorDim(dilations, data_format, 'H');
const int64_t dilation_w = GetTensorDim(dilations, data_format, 'W');
TF_REQUIRES(
dilation_n == 1 && dilation_c == 1,
errors::Unimplemented("Current implementation does not yet support "
"dilations in the batch and depth dimensions."));
TF_REQUIRES(
dilation_h > 0 && dilation_w > 0,
errors::InvalidArgument("Dilated rates should be larger than 0."));
int num_dims = data_format == TensorFormat::FORMAT_NCHW_VECT_C ? 5 : 4;
TF_RETURN_IF_ERROR(CheckValidPadding(
params->padding, params->explicit_paddings, num_dims, data_format));
return OkStatus();
}
Status ComputeConv2DDimension(const Conv2DParameters& params,
const Tensor& input, const Tensor& filter,
Conv2DDimensions* dimensions) {
int required_dims =
params.data_format == TensorFormat::FORMAT_NCHW_VECT_C ? 5 : 4;
// Check that 2D convolution input and filter have exactly required_dims.
TF_REQUIRES(
input.dims() == required_dims,
errors::InvalidArgument("convolution input must be ", required_dims,
"-dimensional: ", input.shape().DebugString()));
TF_REQUIRES(
filter.dims() == required_dims,
errors::InvalidArgument("convolution filter must be ", required_dims,
"-dimensional: ", filter.shape().DebugString()));
for (int i = 0; i < required_dims - 1; i++) {
TF_REQUIRES(
FastBoundsCheck(filter.dim_size(i), std::numeric_limits<int>::max()),
errors::InvalidArgument("filter too large"));
}
FilterTensorFormat filter_format =
params.data_format == TensorFormat::FORMAT_NCHW_VECT_C
? FilterTensorFormat::FORMAT_OIHW_VECT_I
: FilterTensorFormat::FORMAT_HWIO;
// The last dimension for input is in_depth. Check that it is the same as the
// filter's in_depth or it is evenly divisible by filter's in_depth.
const int64_t in_depth_raw = GetTensorDim(input, params.data_format, 'C');
const int64_t patch_depth_raw = GetFilterDim(filter, filter_format, 'I');
TF_REQUIRES(FastBoundsCheck(in_depth_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("Input depth too large"));
TF_REQUIRES(FastBoundsCheck(patch_depth_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("Patch depth too large"));
const int in_depth = static_cast<int>(in_depth_raw);
const int patch_depth = static_cast<int>(patch_depth_raw);
TF_REQUIRES(patch_depth > 0,
errors::InvalidArgument(
"filter depth must be stricly positive, got ", patch_depth));
TF_REQUIRES(in_depth % patch_depth == 0,
errors::InvalidArgument(
"input depth must be evenly divisible by filter depth: ",
in_depth, " vs ", patch_depth));
// The last dimension for filter is out_depth.
const int out_depth =
static_cast<int>(GetFilterDim(filter, filter_format, 'O'));
// The second dimension for input is rows/height.
// The first dimension for filter is rows/height.
const int64_t input_rows_raw = GetTensorDim(input, params.data_format, 'H');
TF_REQUIRES(FastBoundsCheck(input_rows_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("Input rows too large"));
const int input_rows = static_cast<int>(input_rows_raw);
const int filter_rows =
static_cast<int>(GetFilterDim(filter, filter_format, 'H'));
// The third dimension for input is columns/width.
// The second dimension for filter is columns/width.
const int64_t input_cols_raw = GetTensorDim(input, params.data_format, 'W');
TF_REQUIRES(FastBoundsCheck(input_cols_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("Input cols too large"));
const int input_cols = static_cast<int>(input_cols_raw);
const int filter_cols =
static_cast<int>(GetFilterDim(filter, filter_format, 'W'));
// The first dimension for input is batch.
const int64_t batch_raw = GetTensorDim(input, params.data_format, 'N');
TF_REQUIRES(FastBoundsCheck(batch_raw, std::numeric_limits<int>::max()),
errors::InvalidArgument("batch is too large"));
const int batch = static_cast<int>(batch_raw);
// Take the stride and dilation from the second and third dimensions only (we
// do not support striding or dilation on the batch or depth dimension).
const int stride_rows = GetTensorDim(params.strides, params.data_format, 'H');
const int stride_cols = GetTensorDim(params.strides, params.data_format, 'W');
const int dilation_rows =
GetTensorDim(params.dilations, params.data_format, 'H');
const int dilation_cols =
GetTensorDim(params.dilations, params.data_format, 'W');
int64_t pad_rows_before, pad_rows_after, pad_cols_before, pad_cols_after;
if (params.padding == Padding::EXPLICIT) {
GetExplicitPaddingForDim(params.explicit_paddings, params.data_format, 'H',
&pad_rows_before, &pad_rows_after);
GetExplicitPaddingForDim(params.explicit_paddings, params.data_format, 'W',
&pad_cols_before, &pad_cols_after);
}
// Compute windowed output sizes for rows and columns.
int64_t out_rows = 0, out_cols = 0;
TF_RETURN_IF_ERROR(GetWindowedOutputSizeVerbose(
input_rows, filter_rows, dilation_rows, stride_rows, params.padding,
&out_rows, &pad_rows_before, &pad_rows_after));
TF_RETURN_IF_ERROR(GetWindowedOutputSizeVerbose(
input_cols, filter_cols, dilation_cols, stride_cols, params.padding,
&out_cols, &pad_cols_before, &pad_cols_after));
dimensions->batch = batch;
dimensions->input_rows = input_rows;
dimensions->input_cols = input_cols;
dimensions->in_depth = in_depth;
dimensions->filter_rows = filter_rows;
dimensions->filter_cols = filter_cols;
dimensions->patch_depth = patch_depth;
dimensions->out_depth = out_depth;
dimensions->stride_rows = stride_rows;
dimensions->stride_cols = stride_cols;
dimensions->dilation_rows = dilation_rows;
dimensions->dilation_cols = dilation_cols;
dimensions->out_rows = out_rows;
dimensions->out_cols = out_cols;
dimensions->pad_rows_before = pad_rows_before;
dimensions->pad_rows_after = pad_rows_after;
dimensions->pad_cols_before = pad_cols_before;
dimensions->pad_cols_after = pad_cols_after;
return OkStatus();
}
#undef TF_REQUIRES
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
int64_t GetDnnWorkspaceLimit(const string& envvar_in_mb,
int64_t default_value_in_bytes) {
const char* workspace_limit_in_mb_str = getenv(envvar_in_mb.c_str());
if (workspace_limit_in_mb_str != nullptr &&
strcmp(workspace_limit_in_mb_str, "") != 0) {
int64_t scratch_limit_in_mb = -1;
if (strings::safe_strto64(workspace_limit_in_mb_str,
&scratch_limit_in_mb)) {
return scratch_limit_in_mb * (1 << 20);
} else {
LOG(WARNING) << "Invalid value for env-var " << envvar_in_mb << ": "
<< workspace_limit_in_mb_str;
}
}
return default_value_in_bytes;
}
int64_t GetDnnWorkspaceLimitOrDefault() {
return GetDnnWorkspaceLimit("TF_CUDNN_WORKSPACE_LIMIT_IN_MB",
1LL << 33); // 8GB by default
}
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
} // namespace tensorflow