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 // Utilities for working with permutations.
17
18 #ifndef TENSORFLOW_COMPILER_XLA_PERMUTATION_UTIL_H_
19 #define TENSORFLOW_COMPILER_XLA_PERMUTATION_UTIL_H_
20
21 #include <vector>
22
23 #include "absl/types/span.h"
24 #include "tensorflow/core/platform/logging.h"
25 #include "tensorflow/compiler/xla/types.h"
26
27 namespace xla {
28
29 // Returns true if permutation is a permutation of the integers
30 // [0, permutation.size()).
31 bool IsPermutation(absl::Span<const int64_t> permutation);
32
33 // Applies `permutation` on `input` and returns the permuted array.
34 // For each i, output[i] = input[permutation[i]].
35 //
36 // Precondition:
37 // 1. `permutation` is a permutation of 0..permutation.size()-1.
38 // 2. permutation.size() == input.size().
39 template <typename Container>
Permute(const Container & input,absl::Span<const int64_t> permutation)40 std::vector<typename Container::value_type> Permute(
41 const Container& input, absl::Span<const int64_t> permutation) {
42 using T = typename Container::value_type;
43 absl::Span<const T> data(input);
44 CHECK_EQ(permutation.size(), data.size());
45 CHECK(IsPermutation(permutation));
46 std::vector<T> output(data.size());
47 for (size_t i = 0; i < permutation.size(); ++i) {
48 output[i] = data[permutation[i]];
49 }
50 return output;
51 }
52 // Applies the inverse of `permutation` on `input` and returns the permuted
53 // array. For each i, output[permutation[i]] = input[i].
54 //
55 // Precondition:
56 // 1. `permutation` is a permutation of 0..permutation.size()-1.
57 // 2. permutation.size() == input.size().
58 template <typename Container>
PermuteInverse(const Container & input,absl::Span<const int64_t> permutation)59 std::vector<typename Container::value_type> PermuteInverse(
60 const Container& input, absl::Span<const int64_t> permutation) {
61 using T = typename Container::value_type;
62 absl::Span<const T> data(input);
63 CHECK_EQ(permutation.size(), data.size());
64 CHECK(IsPermutation(permutation));
65 std::vector<T> output(data.size());
66 for (size_t i = 0; i < permutation.size(); ++i) {
67 output[permutation[i]] = data[i];
68 }
69 return output;
70 }
71
72 // Inverts a permutation, i.e., output_permutation[input_permutation[i]] = i.
73 std::vector<int64_t> InversePermutation(
74 absl::Span<const int64_t> input_permutation);
75
76 // Composes two permutations: output[i] = p1[p2[i]].
77 std::vector<int64_t> ComposePermutations(absl::Span<const int64_t> p1,
78 absl::Span<const int64_t> p2);
79
80 // Returns true iff permutation == {0, 1, 2, ...}.
81 bool IsIdentityPermutation(absl::Span<const int64_t> permutation);
82
83 } // namespace xla
84
85 #endif // TENSORFLOW_COMPILER_XLA_PERMUTATION_UTIL_H_
86