Home | History | Annotate | Download | only in lib
      1 /* Copyright 2018 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_CLIENT_LIB_PRNG_H_
     17 #define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_PRNG_H_
     18 
     19 #include <array>
     20 
     21 #include "tensorflow/compiler/xla/client/xla_builder.h"
     22 #include "tensorflow/compiler/xla/xla_data.pb.h"
     23 
     24 namespace xla {
     25 
     26 // Implements the ThreeFry counter-based PRNG algorithm.
     27 // Salmon et al. SC 2011. Parallel random numbers: as easy as 1, 2, 3.
     28 // http://www.thesalmons.org/john/random123/papers/random123sc11.pdf
     29 using ThreeFry2x32State = std::array<XlaOp, 2>;
     30 ThreeFry2x32State ThreeFry2x32(ThreeFry2x32State input, ThreeFry2x32State key);
     31 
     32 // Returns a tensor containing 'shape' random values uniformly distributed in
     33 // the range [minval, maxval). Requires 2 32-bit integer seeds.
     34 // Currently only 'shape's of type F32, S32 and S64 are implemented.
     35 XlaOp StatelessRngUniform(std::array<XlaOp, 2> seeds, const Shape& shape,
     36                           XlaOp minval, XlaOp maxval);
     37 
     38 // Converts a 32-bit (signed or unsigned) integer random number `bits` into a
     39 // float32 in the range [minval, maxval).
     40 XlaOp StatelessRngUniformF32(XlaOp bits, XlaOp minval, XlaOp maxval);
     41 
     42 // Converts an integer random number 'bits' of type 'type' to a random number
     43 // in the range [minval, maxval), of the same type. 'unsigned_type' is the
     44 // unsigned version of 'type' (could be the same) with the same bit width.
     45 // The algorithm is the same one that TF uses right now, but it's
     46 // uniform only when maxval - minval is a divisor of the range that bits is
     47 // generated from.
     48 // TODO(b/72573764): Generate real uniform integer distribution.
     49 XlaOp StatelessRngUniformInt(XlaOp bits, XlaOp minval, XlaOp maxval,
     50                              PrimitiveType type, PrimitiveType unsigned_type);
     51 
     52 // The following 2 functions, for converting between one uint64 and two uint32s,
     53 // use the contract "lower 32 bits for the first uint32, higher 32 bits for the
     54 // second".
     55 ThreeFry2x32State Uint64ToUint32s(XlaOp u64);
     56 XlaOp Uint32sToUint64(ThreeFry2x32State u32s);
     57 
     58 }  // namespace xla
     59 
     60 #endif  // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_PRNG_H_
     61