1 | /* Copyright 2022 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_KERNELS_RANDOM_INDEX_SHUFFLE_H_ |
17 | #define TENSORFLOW_CORE_KERNELS_RANDOM_INDEX_SHUFFLE_H_ |
18 | |
19 | #include <array> |
20 | |
21 | #include "tensorflow/core/platform/types.h" |
22 | |
23 | namespace tensorflow { |
24 | namespace random { |
25 | |
26 | // Returns the position of `index` in a permutation of [0, ..., max_index]. |
27 | // |
28 | // Index must be number in [0, ..., max_index]. |
29 | // Key is the random key for the permutation. |
30 | // The returned index will also be in [0, ..., max_index]. For a fixed `key` |
31 | // and `max_index` the all possible `index` values and the returned values |
32 | // form a bijection. |
33 | // Rounds must be a positive even integer >= 4. Larger values increase improve |
34 | // 'randomness' of permutations for small `max_index` values. The time to |
35 | // compute the result scales linear with the number of rounds. We recommend 8 |
36 | // rounds for a good treat off. |
37 | // |
38 | // For more details on the algorithm see the top of the cc file. |
39 | uint64_t index_shuffle(const uint64_t index, const std::array<uint32_t, 3>& key, |
40 | const uint64_t max_index, const int32_t rounds); |
41 | |
42 | } // namespace random |
43 | } // namespace tensorflow |
44 | |
45 | #endif // TENSORFLOW_CORE_KERNELS_RANDOM_INDEX_SHUFFLE_H_ |
46 | |