Home | History | Annotate | Download | only in kernels
      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/kernels/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 
     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   }
     34   return Eigen::PADDING_SAME;  // Prevent compiler warning about missing return
     35 }
     36 
     37 Status GetBroadcastSize(const int index, const int in_size, const int ksize,
     38                         const int stride, const int pad_size, int* bindex,
     39                         int* bsize) {
     40   // Cannot have index beyond the input size.
     41   if (index * stride > in_size) {
     42     return errors::InvalidArgument(
     43         "index * stride must be less than or equal to input size");
     44   }
     45   *bindex = index * stride;
     46   *bsize = ksize;
     47   if (*bindex < pad_size) {
     48     // If the current index is in the padding area, start broadcast  from index
     49     // 0 with broadcast size reduced by padding size.
     50     *bsize = ksize + *bindex - pad_size;
     51     *bindex = 0;
     52   } else {
     53     // Otherwise, start broadcast from current index reduced by padding size.
     54     *bindex -= pad_size;
     55   }
     56   if (*bindex + ksize > in_size) {
     57     *bsize = std::min((in_size - *bindex), ksize);
     58   }
     59   return Status::OK();
     60 }
     61 
     62 string SanitizeThreadSuffix(string suffix) {
     63   string clean;
     64   for (int i = 0; i < suffix.size(); ++i) {
     65     const char ch = suffix[i];
     66     if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
     67         (ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
     68       clean += ch;
     69     } else {
     70       clean += '_';
     71     }
     72   }
     73   return clean;
     74 }
     75 
     76 }  // namespace tensorflow
     77