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 <algorithm>
17 #include <cmath>
18
19 #include "tensorflow/core/framework/attr_value.pb.h"
20 #include "tensorflow/core/framework/ops_util.h"
21 #include "tensorflow/core/lib/core/errors.h"
22 #include "tensorflow/core/lib/strings/str_util.h"
23 #include "tensorflow/core/util/padding.h"
24
25 namespace tensorflow {
26
BrainPadding2EigenPadding(Padding padding)27 Eigen::PaddingType BrainPadding2EigenPadding(Padding padding) {
28 switch (padding) {
29 case Padding::VALID:
30 return Eigen::PADDING_VALID;
31 case Padding::SAME:
32 return Eigen::PADDING_SAME;
33 case Padding::EXPLICIT:
34 LOG(FATAL) << "Eigen does not have explicit padding enum " // Crash OK
35 "value";
36 }
37 return Eigen::PADDING_SAME; // Prevent compiler warning about missing return
38 }
39
GetBroadcastSize(const int index,const int in_size,const int ksize,const int stride,const int pad_size,int * bindex,int * bsize)40 Status GetBroadcastSize(const int index, const int in_size, const int ksize,
41 const int stride, const int pad_size, int* bindex,
42 int* bsize) {
43 // Cannot have index beyond the input size.
44 if (index * stride > in_size) {
45 return errors::InvalidArgument(
46 "index * stride must be less than or equal to input size");
47 }
48 *bindex = index * stride;
49 *bsize = ksize;
50 if (*bindex < pad_size) {
51 // If the current index is in the padding area, start broadcast from index
52 // 0 with broadcast size reduced by padding size.
53 *bsize = ksize + *bindex - pad_size;
54 *bindex = 0;
55 } else {
56 // Otherwise, start broadcast from current index reduced by padding size.
57 *bindex -= pad_size;
58 }
59 if (*bindex + ksize > in_size) {
60 *bsize = std::min((in_size - *bindex), ksize);
61 }
62 return Status::OK();
63 }
64
SanitizeThreadSuffix(string suffix)65 string SanitizeThreadSuffix(string suffix) {
66 string clean;
67 for (int i = 0; i < suffix.size(); ++i) {
68 const char ch = suffix[i];
69 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
70 (ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
71 clean += ch;
72 } else {
73 clean += '_';
74 }
75 }
76 return clean;
77 }
78
79 } // namespace tensorflow
80