1 | /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | #include "tensorflow/core/util/padding.h" |
17 | |
18 | #include "tensorflow/core/framework/attr_value.pb.h" |
19 | #include "tensorflow/core/lib/core/errors.h" |
20 | |
21 | namespace tensorflow { |
22 | |
23 | Status GetPaddingFromString(StringPiece str_value, Padding* value) { |
24 | if (str_value == "SAME" ) { |
25 | *value = SAME; |
26 | } else if (str_value == "VALID" ) { |
27 | *value = VALID; |
28 | } else if (str_value == "EXPLICIT" ) { |
29 | *value = EXPLICIT; |
30 | } else { |
31 | return errors::NotFound(str_value, " is not an allowed padding type" ); |
32 | } |
33 | return OkStatus(); |
34 | } |
35 | |
36 | Status CheckValidPadding(Padding padding_type, |
37 | const std::vector<int64_t>& explicit_paddings, |
38 | int num_dims, TensorFormat data_format) { |
39 | if (padding_type == Padding::EXPLICIT) { |
40 | const int num_paddings = explicit_paddings.size(); |
41 | if (num_paddings != 2 * num_dims) { |
42 | return errors::InvalidArgument( |
43 | "explicit_paddings attribute must contain " , 2 * num_dims, |
44 | " values, but got: " , explicit_paddings.size()); |
45 | } |
46 | for (int64_t padding_value : explicit_paddings) { |
47 | if (padding_value < 0) { |
48 | return errors::InvalidArgument( |
49 | "All elements of explicit_paddings must be nonnegative" ); |
50 | } |
51 | } |
52 | const int32_t batch_index = GetTensorBatchDimIndex(num_dims, data_format); |
53 | const int32_t depth_index = GetTensorFeatureDimIndex(num_dims, data_format); |
54 | if (explicit_paddings[2 * batch_index] != 0 || |
55 | explicit_paddings[2 * batch_index + 1] != 0 || |
56 | explicit_paddings[2 * depth_index] != 0 || |
57 | explicit_paddings[2 * depth_index + 1] != 0) { |
58 | return errors::InvalidArgument( |
59 | "Nonzero explicit padding in the batch or depth dimensions is not " |
60 | "supported" ); |
61 | } |
62 | } else if (!explicit_paddings.empty()) { |
63 | return errors::InvalidArgument( |
64 | "explicit_paddings attribute must be empty if the padding attribute is " |
65 | "not EXPLICIT" ); |
66 | } |
67 | return OkStatus(); |
68 | } |
69 | |
70 | string GetPaddingAttrString() { return "padding: {'SAME', 'VALID'}" ; } |
71 | |
72 | string GetPaddingAttrStringWithExplicit() { |
73 | return "padding: {'SAME', 'VALID', 'EXPLICIT'}" ; |
74 | } |
75 | |
76 | string GetExplicitPaddingsAttrString() { |
77 | return "explicit_paddings: list(int) = []" ; |
78 | } |
79 | |
80 | } // end namespace tensorflow |
81 | |