1 /* Copyright 2015 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_STREAM_EXECUTOR_RNG_H_ 17 #define TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_RNG_H_ 18 19 #include <limits.h> 20 21 #include <complex> 22 23 #include "tensorflow/compiler/xla/stream_executor/platform/logging.h" 24 #include "tensorflow/compiler/xla/stream_executor/platform/port.h" 25 26 namespace stream_executor { 27 28 class Stream; 29 template <typename ElemT> 30 class DeviceMemory; 31 32 namespace rng { 33 34 // Random-number-generation support interface -- this can be derived from a GPU 35 // executor when the underlying platform has an RNG library implementation 36 // available. See StreamExecutor::AsRng(). 37 // When a seed is not specified, the backing RNG will be initialized with the 38 // default seed for that implementation. 39 // 40 // Thread-hostile: see StreamExecutor class comment for details on 41 // thread-hostility. 42 class RngSupport { 43 public: 44 static constexpr int kMinSeedBytes = 16; 45 static constexpr int kMaxSeedBytes = INT_MAX; 46 47 // Releases any random-number-generation resources associated with this 48 // support object in the underlying platform implementation. ~RngSupport()49 virtual ~RngSupport() {} 50 51 // Populates a GPU memory allocation with random values appropriate for the 52 // DeviceMemory element type; i.e. populates DeviceMemory<float> with random 53 // float values. 54 virtual bool DoPopulateRandUniform(Stream *stream, 55 DeviceMemory<float> *v) = 0; 56 virtual bool DoPopulateRandUniform(Stream *stream, 57 DeviceMemory<double> *v) = 0; 58 virtual bool DoPopulateRandUniform(Stream *stream, 59 DeviceMemory<std::complex<float>> *v) = 0; 60 virtual bool DoPopulateRandUniform(Stream *stream, 61 DeviceMemory<std::complex<double>> *v) = 0; 62 63 // Populates a GPU memory allocation with random values sampled from a 64 // Gaussian distribution with the given mean and standard deviation. DoPopulateRandGaussian(Stream * stream,float mean,float stddev,DeviceMemory<float> * v)65 virtual bool DoPopulateRandGaussian(Stream *stream, float mean, float stddev, 66 DeviceMemory<float> *v) { 67 LOG(ERROR) 68 << "platform's random number generator does not support gaussian"; 69 return false; 70 } DoPopulateRandGaussian(Stream * stream,double mean,double stddev,DeviceMemory<double> * v)71 virtual bool DoPopulateRandGaussian(Stream *stream, double mean, 72 double stddev, DeviceMemory<double> *v) { 73 LOG(ERROR) 74 << "platform's random number generator does not support gaussian"; 75 return false; 76 } 77 78 // Specifies the seed used to initialize the RNG. 79 // This call does not transfer ownership of the buffer seed; its data should 80 // not be altered for the lifetime of this call. At least 16 bytes of seed 81 // data must be provided, but not all seed data will necessarily be used. 82 // seed: Pointer to seed data. Must not be null. 83 // seed_bytes: Size of seed buffer in bytes. Must be >= 16. 84 virtual bool SetSeed(Stream *stream, const uint8 *seed, 85 uint64_t seed_bytes) = 0; 86 87 protected: 88 static bool CheckSeed(const uint8 *seed, uint64_t seed_bytes); 89 }; 90 91 } // namespace rng 92 } // namespace stream_executor 93 94 #endif // TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_RNG_H_ 95