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 | // See docs in ../ops/random_ops.cc. |
17 | |
18 | #include "tensorflow/core/framework/register_types.h" |
19 | #include "tensorflow/core/kernels/shuffle_common.h" |
20 | #include "tensorflow/core/util/guarded_philox_random.h" |
21 | |
22 | namespace tensorflow { |
23 | |
24 | template <typename T> |
25 | class RandomShuffleOp : public OpKernel { |
26 | public: |
27 | explicit RandomShuffleOp(OpKernelConstruction* context) : OpKernel(context) { |
28 | OP_REQUIRES_OK(context, generator_.Init(context)); |
29 | } |
30 | |
31 | void Compute(OpKernelContext* ctx) override { |
32 | const Tensor& input = ctx->input(0); |
33 | auto get_rng = [this](int64_t num_samples) { |
34 | return generator_.ReserveSamples32(num_samples); |
35 | }; |
36 | OP_REQUIRES_OK(ctx, |
37 | RandomShuffle<T>(ctx, input, /*output_idx=*/0, get_rng)); |
38 | } |
39 | |
40 | private: |
41 | GuardedPhiloxRandom generator_; |
42 | }; |
43 | |
44 | #define REGISTER(T) \ |
45 | REGISTER_KERNEL_BUILDER( \ |
46 | Name("RandomShuffle").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ |
47 | RandomShuffleOp<T>); |
48 | |
49 | TF_CALL_ALL_TYPES(REGISTER) |
50 | |
51 | #undef REGISTER |
52 | |
53 | } // namespace tensorflow |
54 | |