aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/kernels/stateless_random_ops.cc
blob: 79d0c07acdee4f3e9e979c00bf8ccbf2853a77cc (plain)
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
/* Copyright 2015 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.
==============================================================================*/

#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/bounds_check.h"
#include "tensorflow/core/kernels/random_op.h"
#include "tensorflow/core/lib/random/random_distributions.h"
#include "tensorflow/core/platform/logging.h"

namespace tensorflow {

using CPUDevice = Eigen::ThreadPoolDevice;
using GPUDevice = Eigen::GpuDevice;

namespace {

class StatelessRandomOpBase : public OpKernel {
 public:
  explicit StatelessRandomOpBase(OpKernelConstruction* context)
      : OpKernel(context) {}

  void Compute(OpKernelContext* context) override {
    // Sanitize input
    const Tensor& shape_t = context->input(0);
    const Tensor& seed_t = context->input(1);
    TensorShape shape;
    OP_REQUIRES_OK(context, MakeShape(shape_t, &shape));
    OP_REQUIRES(context, seed_t.dims() == 1 && seed_t.dim_size(0) == 2,
                errors::InvalidArgument("seed must have shape [2], not ",
                                        seed_t.shape().DebugString()));

    // Allocate output
    Tensor* output;
    OP_REQUIRES_OK(context, context->allocate_output(0, shape, &output));
    if (shape.num_elements() == 0) return;

    // Grab the two seeds
    const auto seed = seed_t.flat<int64>();
    const uint64 seed0 = internal::SubtleMustCopy(seed(0));
    const uint64 seed1 = internal::SubtleMustCopy(seed(1));

    // Scramble the seeds so that the user doesn't need to worry about which
    // part of the seed needs to be strong.
    random::PhiloxRandom::Key key;
    random::PhiloxRandom::ResultType counter;
    key[0] = 0x3ec8f720;
    key[1] = 0x02461e29;
    counter[0] = static_cast<uint32>(seed0);
    counter[1] = static_cast<uint32>(seed0 >> 32);
    counter[2] = static_cast<uint32>(seed1);
    counter[3] = static_cast<uint32>(seed1 >> 32);
    const auto mix = random::PhiloxRandom(counter, key)();
    key[0] = mix[0];
    key[1] = mix[1];
    counter[0] = counter[1] = 0;
    counter[2] = mix[2];
    counter[3] = mix[3];

    // Fill in the random numbers
    Fill(context, random::PhiloxRandom(counter, key), output);
  }

  // The part of Compute that depends on device, type, and distribution
  virtual void Fill(OpKernelContext* context, random::PhiloxRandom random,
                    Tensor* output) = 0;
};

template <typename Device, class Distribution>
class StatelessRandomOp : public StatelessRandomOpBase {
 public:
  using StatelessRandomOpBase::StatelessRandomOpBase;

  void Fill(OpKernelContext* context, random::PhiloxRandom random,
            Tensor* output) override {
    typedef typename Distribution::ResultElementType T;
    auto flat = output->flat<T>();
    // Reuse the compute kernels from the stateful random ops
    functor::FillPhiloxRandom<Device, Distribution>()(
        context, context->eigen_device<Device>(), random, flat.data(),
        flat.size(), Distribution());
  }
};

}  // namespace

#define REGISTER(TYPE)                                                 \
  REGISTER_KERNEL_BUILDER(                                             \
      Name("StatelessRandomUniform")                                   \
          .Device(DEVICE_CPU)                                          \
          .HostMemory("shape")                                         \
          .TypeConstraint<TYPE>("dtype"),                              \
      StatelessRandomOp<CPUDevice, random::UniformDistribution<        \
                                       random::PhiloxRandom, TYPE> >); \
  REGISTER_KERNEL_BUILDER(                                             \
      Name("StatelessRandomNormal")                                    \
          .Device(DEVICE_CPU)                                          \
          .HostMemory("shape")                                         \
          .TypeConstraint<TYPE>("dtype"),                              \
      StatelessRandomOp<CPUDevice, random::NormalDistribution<         \
                                       random::PhiloxRandom, TYPE> >); \
  REGISTER_KERNEL_BUILDER(                                             \
      Name("StatelessTruncatedNormal")                                 \
          .Device(DEVICE_CPU)                                          \
          .HostMemory("shape")                                         \
          .TypeConstraint<TYPE>("dtype"),                              \
      StatelessRandomOp<                                               \
          CPUDevice,                                                   \
          random::TruncatedNormalDistribution<                         \
              random::SingleSampleAdapter<random::PhiloxRandom>, TYPE> >);

TF_CALL_half(REGISTER);
TF_CALL_float(REGISTER);
TF_CALL_double(REGISTER);

#undef REGISTER

#if GOOGLE_CUDA

#define REGISTER(TYPE)                                                 \
  REGISTER_KERNEL_BUILDER(                                             \
      Name("StatelessRandomUniform")                                   \
          .Device(DEVICE_GPU)                                          \
          .HostMemory("shape")                                         \
          .HostMemory("seed")                                          \
          .TypeConstraint<int32>("T")                                  \
          .TypeConstraint<TYPE>("dtype"),                              \
      StatelessRandomOp<GPUDevice, random::UniformDistribution<        \
                                       random::PhiloxRandom, TYPE> >); \
  REGISTER_KERNEL_BUILDER(                                             \
      Name("StatelessRandomNormal")                                    \
          .Device(DEVICE_GPU)                                          \
          .HostMemory("shape")                                         \
          .HostMemory("seed")                                          \
          .TypeConstraint<int32>("T")                                  \
          .TypeConstraint<TYPE>("dtype"),                              \
      StatelessRandomOp<GPUDevice, random::NormalDistribution<         \
                                       random::PhiloxRandom, TYPE> >); \
  REGISTER_KERNEL_BUILDER(                                             \
      Name("StatelessTruncatedNormal")                                 \
          .Device(DEVICE_GPU)                                          \
          .HostMemory("shape")                                         \
          .HostMemory("seed")                                          \
          .TypeConstraint<int32>("T")                                  \
          .TypeConstraint<TYPE>("dtype"),                              \
      StatelessRandomOp<                                               \
          GPUDevice,                                                   \
          random::TruncatedNormalDistribution<                         \
              random::SingleSampleAdapter<random::PhiloxRandom>, TYPE> >);

TF_CALL_half(REGISTER);
TF_CALL_float(REGISTER);
TF_CALL_double(REGISTER);

#undef REGISTER

#endif  // GOOGLE_CUDA

}  // namespace tensorflow