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