1 | /* Copyright 2020 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_RNG_ALG_H_ |
17 | #define TENSORFLOW_CORE_FRAMEWORK_RNG_ALG_H_ |
18 | |
19 | #include "tensorflow/core/platform/logging.h" |
20 | |
21 | namespace tensorflow { |
22 | |
23 | enum Algorithm { |
24 | // The Philox algorithm, as described in paper |
25 | // ['Parallel Random Numbers: As Easy as 1, 2, 3'] |
26 | // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf) |
27 | RNG_ALG_PHILOX = 1, |
28 | // The ThreeFry algorithm, as described in paper |
29 | // ['Parallel Random Numbers: As Easy as 1, 2, 3'] |
30 | // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf) |
31 | RNG_ALG_THREEFRY = 2, |
32 | // An algorithm auto-selected by the system according to device type. |
33 | RNG_ALG_AUTO_SELECT = 3 |
34 | }; |
35 | |
36 | // Same as `Algorithm`, but without AUTO_SELECT. We use C++ compiler's -Wswitch |
37 | // and -Werror to check that `switch` covers all cases. When the algorithm |
38 | // auto-selection has been resolved, we use this type so that |
39 | // we don't need to (unnecessarily) handle the AUTO_SELECT case. |
40 | enum class ConcreteRngAlgorithm { |
41 | RNG_ALG_PHILOX = 1, |
42 | RNG_ALG_THREEFRY = 2, |
43 | }; |
44 | |
45 | // Gets the counter size (in unit of uint64) for a counter-based RNG |
46 | // algorithm `alg`. Callers of this function must ensure that `alg` doesn't have |
47 | // non-enumerator values. |
48 | inline int GetCounterSize(ConcreteRngAlgorithm alg) { |
49 | switch (alg) { |
50 | case ConcreteRngAlgorithm::RNG_ALG_PHILOX: |
51 | return 2; |
52 | case ConcreteRngAlgorithm::RNG_ALG_THREEFRY: |
53 | return 1; |
54 | } |
55 | LOG(ERROR) << "This point shouldn't have been reached." ; |
56 | } |
57 | static constexpr int RNG_MAX_COUNTER_SIZE = 2; |
58 | |
59 | static constexpr int RNG_KEY_SIZE = 1; |
60 | |
61 | } // end namespace tensorflow |
62 | |
63 | #endif // TENSORFLOW_CORE_FRAMEWORK_RNG_ALG_H_ |
64 | |