1 /* Copyright 2019 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 #include "tensorflow/core/kernels/data/random_seed_ops.h"
16
17 #include "tensorflow/core/framework/dataset.h"
18 #include "tensorflow/core/framework/partial_tensor_shape.h"
19 #include "tensorflow/core/framework/resource_mgr.h"
20 #include "tensorflow/core/framework/tensor.h"
21 #include "tensorflow/core/lib/random/philox_random.h"
22 #include "tensorflow/core/lib/random/random.h"
23 #include "tensorflow/core/lib/random/random_distributions.h"
24 #include "tensorflow/core/platform/errors.h"
25
26 namespace tensorflow {
27 namespace data {
28 namespace {
29
30 const char kSeedGenerator[] = "SeedGenerator";
31 const char kSeed[] = "seed";
32 const char kSeed2[] = "seed2";
33 const char kReshuffle[] = "reshuffle";
34
35 } // namespace
36
DebugString() const37 string SeedGeneratorManager::DebugString() const { return kSeedGenerator; }
38
GenerateSeeds(int64_t * seed1,int64_t * seed2)39 void FixedSeedGenerator::GenerateSeeds(int64_t* seed1, int64_t* seed2) {
40 mutex_lock l(mu_);
41 num_random_samples_++;
42 *seed1 = seeds_.seed();
43 *seed2 = seeds_.seed2();
44 }
45
GenerateSeeds(int64_t * seed1,int64_t * seed2)46 void RandomSeedGenerator::GenerateSeeds(int64_t* seed1, int64_t* seed2) {
47 mutex_lock l(mu_);
48 num_random_samples_++;
49 *seed1 = generator_();
50 num_random_samples_++;
51 *seed2 = generator_();
52 }
53
Reset()54 void RandomSeedGenerator::Reset() {
55 mutex_lock l(mu_);
56 // Reset the generators based on the current seeds.
57 parent_generator_ = random::PhiloxRandom(seeds_.seed(), seeds_.seed2());
58 generator_ =
59 random::SingleSampleAdapter<random::PhiloxRandom>(&parent_generator_);
60 generator_.Skip(num_random_samples_);
61 }
62
AnonymousSeedGeneratorHandleOp(OpKernelConstruction * ctx)63 AnonymousSeedGeneratorHandleOp::AnonymousSeedGeneratorHandleOp(
64 OpKernelConstruction* ctx)
65 : AnonymousResourceOp<SeedGeneratorManager>(ctx,
66 /* ref_counting */ true,
67 /* return_deleter */ true) {}
68
Compute(OpKernelContext * ctx)69 void AnonymousSeedGeneratorHandleOp::Compute(OpKernelContext* ctx) {
70 int64_t seed;
71 OP_REQUIRES_OK(ctx, ParseScalarArgument<int64_t>(ctx, kSeed, &seed));
72 int64_t seed2;
73 OP_REQUIRES_OK(ctx, ParseScalarArgument<int64_t>(ctx, kSeed2, &seed2));
74 // Seeds will be consumed by `CreateResource`, which is called via `Compute`.
75 mutex_lock l(mu_);
76 seeds_ = std::make_unique<RandomSeeds>(seed, seed2);
77 OP_REQUIRES_OK(ctx, ParseScalarArgument<bool>(ctx, kReshuffle, &reshuffle_));
78 AnonymousResourceOp<SeedGeneratorManager>::Compute(ctx);
79 }
80
name()81 std::string AnonymousSeedGeneratorHandleOp::name() { return kSeedGenerator; }
82
CreateResource(OpKernelContext * ctx,std::unique_ptr<FunctionLibraryDefinition> flib_def,std::unique_ptr<ProcessFunctionLibraryRuntime> pflr,FunctionLibraryRuntime * lib,SeedGeneratorManager ** manager)83 Status AnonymousSeedGeneratorHandleOp::CreateResource(
84 OpKernelContext* ctx, std::unique_ptr<FunctionLibraryDefinition> flib_def,
85 std::unique_ptr<ProcessFunctionLibraryRuntime> pflr,
86 FunctionLibraryRuntime* lib, SeedGeneratorManager** manager)
87 TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
88 if (reshuffle_) {
89 *manager = new SeedGeneratorManager(new RandomSeedGenerator(*seeds_));
90 } else {
91 *manager = new SeedGeneratorManager(new FixedSeedGenerator(*seeds_));
92 }
93 seeds_ = nullptr;
94 return OkStatus();
95 }
96
Compute(OpKernelContext * ctx)97 void DeleteSeedGeneratorOp::Compute(OpKernelContext* ctx) {
98 ResourceHandle handle = ctx->input(0).flat<ResourceHandle>()(0);
99 // The resource is guaranteed to exist because the variant tensor wrapping the
100 // deleter is provided as an unused input to this op, which guarantees that it
101 // has not run yet.
102 OP_REQUIRES_OK(ctx, ctx->resource_manager()->Delete(handle));
103 }
104
105 namespace {
106 REGISTER_KERNEL_BUILDER(Name("AnonymousSeedGenerator").Device(DEVICE_CPU),
107 AnonymousSeedGeneratorHandleOp);
108
109 REGISTER_KERNEL_BUILDER(Name("DeleteSeedGenerator").Device(DEVICE_CPU),
110 DeleteSeedGeneratorOp);
111
112 REGISTER_KERNEL_BUILDER(Name("AnonymousRandomSeedGenerator").Device(DEVICE_CPU),
113 AnonymousSeedGeneratorHandleOp);
114
115 REGISTER_KERNEL_BUILDER(Name("DeleteRandomSeedGenerator").Device(DEVICE_CPU),
116 DeleteSeedGeneratorOp);
117
118 REGISTER_KERNEL_BUILDER(Name("DummySeedGenerator").Device(DEVICE_CPU),
119 DummyResourceOp<SeedGenerator>);
120
121 } // namespace
122 } // namespace data
123 } // namespace tensorflow
124