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