• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/framework/node_def_util.h"
20 #include "tensorflow/core/lib/core/errors.h"
21 
22 namespace tensorflow {
23 
GetNodeAttr(const NodeDef & node_def,StringPiece attr_name,Padding * value)24 Status GetNodeAttr(const NodeDef& node_def, StringPiece attr_name,
25                    Padding* value) {
26   string str_value;
27   TF_RETURN_IF_ERROR(GetNodeAttr(node_def, attr_name, &str_value));
28   if (str_value == "SAME") {
29     *value = SAME;
30   } else if (str_value == "VALID") {
31     *value = VALID;
32   } else if (str_value == "EXPLICIT") {
33     *value = EXPLICIT;
34   } else {
35     return errors::NotFound(str_value, " is not an allowed padding type");
36   }
37   return Status::OK();
38 }
39 
CheckValidPadding(Padding padding_type,const std::vector<int64> & explicit_paddings,int num_dims,TensorFormat data_format)40 Status CheckValidPadding(Padding padding_type,
41                          const std::vector<int64>& explicit_paddings,
42                          int num_dims, TensorFormat data_format) {
43   if (padding_type == Padding::EXPLICIT) {
44     if (explicit_paddings.size() != 2 * num_dims) {
45       return errors::InvalidArgument(
46           "explicit_paddings attribute must contain ", 2 * num_dims,
47           " values, but got: ", explicit_paddings.size());
48     }
49     for (int64 padding_value : explicit_paddings) {
50       if (padding_value < 0) {
51         return errors::InvalidArgument(
52             "All elements of explicit_paddings must be nonnegative");
53       }
54     }
55     const int32 batch_index = GetTensorBatchDimIndex(num_dims, data_format);
56     const int32 depth_index = GetTensorFeatureDimIndex(num_dims, data_format);
57     if (explicit_paddings[2 * batch_index] != 0 ||
58         explicit_paddings[2 * batch_index + 1] != 0 ||
59         explicit_paddings[2 * depth_index] != 0 ||
60         explicit_paddings[2 * depth_index + 1] != 0) {
61       return errors::InvalidArgument(
62           "Nonzero explicit padding in the batch or depth dimensions is not "
63           "supported");
64     }
65   } else if (!explicit_paddings.empty()) {
66     return errors::InvalidArgument(
67         "explicit_paddings attribute must be empty if the padding attribute is "
68         "not EXPLICIT");
69   }
70   return Status::OK();
71 }
72 
GetPaddingAttrString()73 string GetPaddingAttrString() { return "padding: {'SAME', 'VALID'}"; }
74 
GetPaddingAttrStringWithExplicit()75 string GetPaddingAttrStringWithExplicit() {
76   return "padding: {'SAME', 'VALID', 'EXPLICIT'}";
77 }
78 
GetExplicitPaddingsAttrString()79 string GetExplicitPaddingsAttrString() {
80   return "explicit_paddings: list(int) = []";
81 }
82 
83 }  // end namespace tensorflow
84