Home | History | Annotate | Download | only in xla
      1 /* Copyright 2017 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_COMPILER_XLA_WINDOW_UTIL_H_
     17 #define TENSORFLOW_COMPILER_XLA_WINDOW_UTIL_H_
     18 
     19 #include "absl/types/span.h"
     20 #include "tensorflow/compiler/xla/types.h"
     21 #include "tensorflow/compiler/xla/xla_data.pb.h"
     22 
     23 namespace xla {
     24 namespace window_util {
     25 
     26 // Creates a window with the given sizes in the dimensions and all strides set
     27 // to 1.
     28 Window MakeWindow(absl::Span<const int64> sizes);
     29 
     30 // Creates a padding config with symmetrical padding in each dimension, of value
     31 // given by sizes; e.g. {0, 1, 2} would create a R3 padding config that had zero
     32 // pixels of padding in dimension 0, one pixel of padding symmetrically, on each
     33 // side of dimension 1, and two pixels of padding symmetrically on dimension 2.
     34 PaddingConfig MakeSymmetricPadding(absl::Span<const int64> sizes);
     35 
     36 string ToString(const WindowDimension& dim);
     37 string ToString(const Window& window);
     38 
     39 // The below functions return true if the given field is set to have a
     40 // non-trivial effect, e.g. having a stride means that the stride of some
     41 // dimension is not one. Whether the proto field is populated is not a
     42 // consideration.
     43 
     44 bool HasStride(const Window& window);
     45 bool HasPadding(const Window& window);
     46 bool HasSymmetricPadding(const Window& window);
     47 bool HasNegativePadding(const Window& window);
     48 
     49 // As with HasSymmetricPadding(Window) above, returns whether the "padding low"
     50 // is equivalent to the "padding high" for all dimensions, but works on a
     51 // padding configuration.
     52 bool HasSymmetricPadding(const PaddingConfig& padding_config);
     53 
     54 bool HasBaseDilation(const Window& window);
     55 bool HasWindowDilation(const Window& window);
     56 bool HasDilation(const Window& window);
     57 
     58 bool HasWindowReversal(const Window& window);
     59 bool AllOrNoneReversed(const Window& window);
     60 
     61 // Returns true if the given logical dimension is inactive in the sense that it
     62 // has window bound 1, no striding and no padding.
     63 bool IsInactiveWindowDimension(const Window& window, int64 logical_dim);
     64 
     65 // Returns true if the provided window dimension is trivial (inactive and has no
     66 // dilation)
     67 bool IsTrivialWindowDimension(const WindowDimension& window_dimension);
     68 
     69 // Returns the new bound after dilation.
     70 //
     71 // If a window with the given bound in some dimension is dilated with the given
     72 // dilation factor in that dimension, then the value returned is the bound for
     73 // the array in that dimension after dilation.
     74 //
     75 // For a 1D array with 3 entries 1, 2, 3, a dilation factor of 2 yields a new
     76 // window with values 1, x, 2, x, 3, where x indicates holes left by the
     77 // dilation. So DilatedBound(3, 2) == 5.
     78 int64 DilatedBound(int64 bound, int64 dilation);
     79 
     80 // Returns the number of valid positions of a window with the given size and
     81 // stride within an array with the given bound. This is the bound of an output
     82 // array with one element per valid position of the window.
     83 //
     84 // For example, for arguments of (bound=5, window_size=2, stride=2), the
     85 // returned value is 2. There are valid positions at offset 0 and offset 2,
     86 // while offset 4 is not valid since the window's last entry would be at 5,
     87 // which is beyond the bound of 5.
     88 int64 StridedBound(int64 bound, int64 window_size, int64 stride);
     89 
     90 }  // namespace window_util
     91 }  // namespace xla
     92 
     93 #endif  // TENSORFLOW_COMPILER_XLA_WINDOW_UTIL_H_
     94