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 #ifndef TENSORFLOW_CORE_FRAMEWORK_OPS_UTIL_H_
17 #define TENSORFLOW_CORE_FRAMEWORK_OPS_UTIL_H_
18 
19 // This file contains utilities for various operations.
20 
21 #include <array>
22 
23 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
24 #include "tensorflow/core/framework/tensor_shape.h"
25 #include "tensorflow/core/framework/tensor_types.h"
26 #include "tensorflow/core/lib/core/status.h"
27 #include "tensorflow/core/util/padding.h"
28 
29 namespace tensorflow {
30 
31 // Calculates broadcast starting index and size.  For SAME padding, addition
32 // padding could be applied to right, left, top and bottom.  Depending on the
33 // current index, input size, kernel size, stride, padding size, the starting
34 // index and size for broadcast for that dimension are different from the
35 // current index and kernel size.
36 // This is mainly used by gradient algorithms for pooling operations.
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 
41 // Converts Brain's Padding to Eigen's PaddingType.
42 Eigen::PaddingType BrainPadding2EigenPadding(Padding padding);
43 
44 // Given a shape 's' of a tensor of type T. Returns true iff the
45 // number of bytes occupied by each dim 0 (i.e., &tensor(i + 1, ...) -
46 // &tensor(i, ...)) is multiple of EIGEN_MAX_ALIGN_BYTES.
47 template <typename T>
IsInnerDimsSizeAligned(const TensorShape & s)48 bool IsInnerDimsSizeAligned(const TensorShape& s) {
49   if (s.dims() == 0) return false;
50   const int64 dim0_size = s.dim_size(0);
51   if (dim0_size == 0) return false;
52 #if EIGEN_MAX_ALIGN_BYTES == 0
53   return true;
54 #else
55   const int64 bytes_per_dim0 = (s.num_elements() / dim0_size) * sizeof(T);
56   return bytes_per_dim0 % EIGEN_MAX_ALIGN_BYTES == 0;
57 #endif
58 }
59 
60 // Given a shape 's' of a tensor of type T and the `start` and `end` index of a
61 // dim 0 slice, returns true iff slice is aligned with respect to original
62 // tensor. Here aligned implies the address is a multiple of
63 // EIGEN_MAX_ALIGN_BYTES.
64 template <typename T>
IsDim0SliceAligned(const TensorShape & s,int64 start,int64 end_or_size)65 bool IsDim0SliceAligned(const TensorShape& s, int64 start, int64 end_or_size) {
66   if (s.dims() == 1) {
67 #if EIGEN_MAX_ALIGN_BYTES == 0
68     return true;
69 #else
70     bool start_aligned = (start * sizeof(T)) % EIGEN_MAX_ALIGN_BYTES == 0;
71     // End is aligned if either the explicit end index is passed and is a
72     // a multiple of EIGEN_MAX_ALIGN_BYTES, or the start index is aligned and
73     // the size is aligned. So for convenience we can either pass start and
74     // index, or start and size.
75     bool end_aligned = (end_or_size * sizeof(T)) % EIGEN_MAX_ALIGN_BYTES == 0;
76     return start_aligned && end_aligned;
77 #endif
78   } else {
79     return IsInnerDimsSizeAligned<T>(s);
80   }
81 }
82 
83 // Returns <suffix> sanitized to have only [a-zA-Z0-9-_].
84 std::string SanitizeThreadSuffix(std::string suffix);
85 
86 // Helper to compute 'strides' given a tensor 'shape'. I.e.,
87 // strides[i] = prod(shape.dim_size[(i+1):])
88 template <typename T>
ComputeStride(const TensorShape & shape)89 gtl::InlinedVector<T, 8> ComputeStride(const TensorShape& shape) {
90   const int ndims = shape.dims();
91   gtl::InlinedVector<T, 8> strides(ndims);
92   T stride = 1;
93   for (int i = ndims - 1; i >= 0; --i) {
94     strides[i] = stride;
95     stride *= static_cast<T>(shape.dim_size(i));
96   }
97   return strides;
98 }
99 
100 // Helper to compute 'strides' given an Eigen TensorDimensions
101 template <typename T, typename EigenDimensions>
ComputeEigenStrides(const EigenDimensions & shape)102 gtl::InlinedVector<T, 8> ComputeEigenStrides(const EigenDimensions& shape) {
103   const int ndims = shape.rank();
104   gtl::InlinedVector<T, 8> strides(ndims);
105   T stride = 1;
106   for (int i = ndims - 1; i >= 0; --i) {
107     strides[i] = stride;
108     stride *= static_cast<T>(shape[i]);
109   }
110   return strides;
111 }
112 
113 }  // namespace tensorflow
114 
115 #endif  // TENSORFLOW_CORE_FRAMEWORK_OPS_UTIL_H_
116