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 namespace tensorflow {
20
21 enum Algorithm {
22 // The Philox algorithm, as described in paper
23 // ['Parallel Random Numbers: As Easy as 1, 2, 3']
24 // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf)
25 RNG_ALG_PHILOX = 1,
26 // The ThreeFry algorithm, as described in paper
27 // ['Parallel Random Numbers: As Easy as 1, 2, 3']
28 // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf)
29 RNG_ALG_THREEFRY = 2,
30 // An algorithm auto-selected by the system according to device type.
31 RNG_ALG_AUTO_SELECT = 3
32 };
33
34 static constexpr int RNG_KEY_SIZE = 1;
35 static constexpr int RNG_MAX_COUNTER_SIZE = 2;
36 // Gets the counter size (in unit of uint64) for a counter-based RNG
37 // algorithm `alg`. In the case of RNG_ALG_AUTO_SELECT, gets the minimal
38 // counter size among all algorithms.
GetCounterSize(Algorithm alg)39 inline int GetCounterSize(Algorithm alg) {
40 if (alg == RNG_ALG_PHILOX) {
41 return 2;
42 } else if (alg == RNG_ALG_THREEFRY) {
43 return 1;
44 }
45 return 1;
46 }
47
48 } // end namespace tensorflow
49
50 #endif // TENSORFLOW_CORE_FRAMEWORK_RNG_ALG_H_
51