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 #ifndef TENSORFLOW_CORE_KERNELS_RANDOM_OP_H_ 17 #define TENSORFLOW_CORE_KERNELS_RANDOM_OP_H_ 18 19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 20 #include "tensorflow/core/lib/random/random_distributions.h" 21 22 namespace tensorflow { 23 24 class OpKernelContext; 25 26 namespace functor { 27 28 template <typename Device, class Distribution> 29 struct FillPhiloxRandom; 30 31 typedef Eigen::ThreadPoolDevice CPUDevice; 32 // Declares the partially CPU-specialized functor struct. 33 // 34 // NOTE: Due to inlining done by the compiler, you may need to add 35 // explicit instantiation of the functor in random_op.cc. See example 36 // functor::FillPhiloxRandom<CPUDevice, random::UniformDistribution>. 37 // 38 // This functor can take the PhiloxRandom input from either device memory `key` 39 // and `counter` or a stack value `gen`. If both `key` and `counter` are not 40 // nullptr, they provide the input; otherwise `gen` provides the input. 41 template <class Distribution> 42 struct FillPhiloxRandom<CPUDevice, Distribution> { 43 void operator()(OpKernelContext* ctx, const CPUDevice& d, const uint64* key, 44 const uint64* counter, random::PhiloxRandom gen, 45 typename Distribution::ResultElementType* data, int64_t size, 46 Distribution dist); 47 }; 48 49 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM 50 typedef Eigen::GpuDevice GPUDevice; 51 // Declares the partially GPU-specialized functor struct. 52 template <class Distribution> 53 struct FillPhiloxRandom<GPUDevice, Distribution> { 54 void operator()(OpKernelContext* ctx, const GPUDevice& d, const uint64* key, 55 const uint64* counter, random::PhiloxRandom gen, 56 typename Distribution::ResultElementType* data, int64_t size, 57 Distribution dist); 58 }; 59 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM 60 61 } // namespace functor 62 } // namespace tensorflow 63 64 #endif // TENSORFLOW_CORE_KERNELS_RANDOM_OP_H_ 65