-
Notifications
You must be signed in to change notification settings - Fork 74k
/
random_op_cpu.h
193 lines (158 loc) · 7.38 KB
/
random_op_cpu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_CORE_KERNELS_RANDOM_OP_CPU_H_
#define TENSORFLOW_CORE_KERNELS_RANDOM_OP_CPU_H_
#define EIGEN_USE_THREADS
#include <algorithm>
#include <cmath>
#include <memory>
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/kernels/random_op.h"
#include "tensorflow/core/kernels/random_ops_util.h"
#include "tensorflow/core/lib/hash/crc32c.h"
#include "tensorflow/core/lib/random/random_distributions.h"
#include "tensorflow/core/lib/random/simple_philox.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/util/guarded_philox_random.h"
#include "tensorflow/core/util/work_sharder.h"
#if EIGEN_COMP_GNUC && __cplusplus > 199711L
#define DISABLE_FLOAT_EQUALITY_WARNING \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wfloat-equal\"")
#define ENABLE_FLOAT_EQUALITY_WARNING _Pragma("GCC diagnostic pop")
#else
#define DISABLE_FLOAT_EQUALITY_WARNING
#define ENABLE_FLOAT_EQUALITY_WARNING
#endif
namespace tensorflow {
typedef Eigen::ThreadPoolDevice CPUDevice;
typedef Eigen::GpuDevice GPUDevice;
namespace functor {
using random::PhiloxRandom;
using random::SingleSampleAdapter;
// The default implementation of the functor, which should never be invoked
// But we still need to provide implementation for now for the linker to work,
// since we do not support all the distributions yet.
template <typename Device, class Distribution>
struct FillPhiloxRandom {
typedef typename Distribution::ResultElementType T;
void operator()(OpKernelContext* ctx, const Device&, const uint64* key,
const uint64* counter, random::PhiloxRandom gen, T* data,
int64_t size, Distribution dist) {
OP_REQUIRES(
ctx, false,
errors::Internal(
"Default `FillPhiloxRandom` implementation should not be executed. "
"The cause of this error is probably that `FillPhiloxRandom` does "
"not support this device or random distribution yet."));
}
};
// A class to fill a specified range of random groups
template <class Distribution, bool VariableSamplesPerOutput>
struct FillPhiloxRandomTask;
// Specialization for distribution that takes a fixed number of samples for
// each output.
template <class Distribution>
struct FillPhiloxRandomTask<Distribution, false> {
typedef typename Distribution::ResultElementType T;
static void Run(random::PhiloxRandom gen, T* data, int64_t size,
int64_t start_group, int64_t limit_group, Distribution dist) {
const int kGroupSize = Distribution::kResultElementCount;
gen.Skip(start_group);
int64_t offset = start_group * kGroupSize;
// First fill all the full-size groups
int64_t limit_group_full = std::min(limit_group, size / kGroupSize);
for (int64_t index = start_group; index < limit_group_full; ++index) {
auto samples = dist(&gen);
std::copy(&samples[0], &samples[0] + kGroupSize, data + offset);
offset += kGroupSize;
}
// If there are any remaining elements that need to be filled, process them
if (limit_group_full < limit_group) {
int64_t remaining_size = size - limit_group_full * kGroupSize;
auto samples = dist(&gen);
std::copy(&samples[0], &samples[0] + remaining_size, data + offset);
}
}
};
// Specialization for distribution that takes a variable number of samples for
// each output. This will be slower due to the generality.
template <class Distribution>
struct FillPhiloxRandomTask<Distribution, true> {
typedef typename Distribution::ResultElementType T;
static constexpr int64_t kReservedSamplesPerOutput = 256;
static void Run(random::PhiloxRandom base_gen, T* data, int64_t size,
int64_t start_group, int64_t limit_group, Distribution dist) {
const int kGroupSize = Distribution::kResultElementCount;
static const int kGeneratorSkipPerOutputGroup =
kGroupSize * kReservedSamplesPerOutput /
PhiloxRandom::kResultElementCount;
int64_t offset = start_group * kGroupSize;
// First fill all the full-size groups
int64_t limit_group_full = std::min(limit_group, size / kGroupSize);
int64_t group_index;
for (group_index = start_group; group_index < limit_group_full;
++group_index) {
// Reset the generator to the beginning of the output group region
// This is necessary if we want the results to be independent of order
// of work
PhiloxRandom gen = base_gen;
gen.Skip(group_index * kGeneratorSkipPerOutputGroup);
SingleSampleAdapter<PhiloxRandom> single_samples(&gen);
auto samples = dist(&single_samples);
std::copy(&samples[0], &samples[0] + kGroupSize, data + offset);
offset += kGroupSize;
}
// If there are any remaining elements that need to be filled, process them
if (limit_group_full < limit_group) {
PhiloxRandom gen = base_gen;
gen.Skip(group_index * kGeneratorSkipPerOutputGroup);
SingleSampleAdapter<PhiloxRandom> single_samples(&gen);
int64_t remaining_size = size - limit_group_full * kGroupSize;
auto samples = dist(&single_samples);
std::copy(&samples[0], &samples[0] + remaining_size, data + offset);
}
}
};
// Partial specialization for CPU to fill the entire region with randoms
// It splits the work into several tasks and run them in parallel
template <class Distribution>
void FillPhiloxRandom<CPUDevice, Distribution>::operator()(
OpKernelContext* ctx, const CPUDevice&, const uint64* key,
const uint64* counter, random::PhiloxRandom gen,
typename Distribution::ResultElementType* data, int64_t size,
Distribution dist) {
if (key != nullptr && counter != nullptr) {
gen = GetPhiloxRandomFromCounterKeyMem(counter, key);
}
const int kGroupSize = Distribution::kResultElementCount;
auto worker_threads = *(ctx->device()->tensorflow_cpu_worker_threads());
int64_t total_group_count = (size + kGroupSize - 1) / kGroupSize;
const int kGroupCost =
random::PhiloxRandom::kResultElementCount *
(random::PhiloxRandom::kElementCost + Distribution::kElementCost);
Shard(worker_threads.num_threads, worker_threads.workers, total_group_count,
kGroupCost,
[&gen, data, size, dist](int64_t start_group, int64_t limit_group) {
FillPhiloxRandomTask<
Distribution,
Distribution::kVariableSamplesPerOutput>::Run(gen, data, size,
start_group,
limit_group, dist);
});
}
} // namespace functor
} // end namespace tensorflow
#endif // TENSORFLOW_CORE_KERNELS_RANDOM_OP_CPU_H_