• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/util/guarded_philox_random.h"
17 #include "tensorflow/core/lib/random/random.h"
18 
19 namespace tensorflow {
20 
Init(OpKernelConstruction * context)21 Status GuardedPhiloxRandom::Init(OpKernelConstruction* context) {
22   // Grab seed Attrs.
23   int64 seed, seed2;
24   auto status = context->GetAttr("seed", &seed);
25   if (!status.ok()) return status;
26   status = context->GetAttr("seed2", &seed2);
27   if (!status.ok()) return status;
28 
29   // Initialize with the given seeds
30   Init(seed, seed2);
31   return Status::OK();
32 }
33 
Init(int64 seed,int64 seed2)34 void GuardedPhiloxRandom::Init(int64 seed, int64 seed2) {
35   CHECK(!initialized_);
36   if (seed == 0 && seed2 == 0) {
37     // If both seeds are unspecified, use completely random seeds.
38     seed = random::New64();
39     seed2 = random::New64();
40   }
41   mutex_lock lock(mu_);
42   generator_ = random::PhiloxRandom(seed, seed2);
43   initialized_ = true;
44 }
45 
Init(random::PhiloxRandom::ResultType counter,random::PhiloxRandom::Key key)46 void GuardedPhiloxRandom::Init(random::PhiloxRandom::ResultType counter,
47                                random::PhiloxRandom::Key key) {
48   CHECK(!initialized_);
49   mutex_lock lock(mu_);
50   generator_ = random::PhiloxRandom(counter, key);
51   initialized_ = true;
52 }
53 
ReserveSamples128(int64 samples)54 random::PhiloxRandom GuardedPhiloxRandom::ReserveSamples128(int64 samples) {
55   CHECK(initialized_);
56   mutex_lock lock(mu_);
57   auto local = generator_;
58   generator_.Skip(samples);
59   return local;
60 }
61 
62 }  // namespace tensorflow
63