|
| 1 | +// Copyright (c) 2018 PaddlePaddle 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 | +#pragma once |
| 16 | + |
| 17 | +#include "paddle/fluid/framework/op_registry.h" |
| 18 | +#include "paddle/fluid/operators/detail/safe_ref.h" |
| 19 | +#include "paddle/fluid/platform/device_context.h" |
| 20 | +#include "paddle/fluid/platform/for_range.h" |
| 21 | +#include "thrust/random.h" |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace operators { |
| 25 | + |
| 26 | +template <typename DeviceContext> |
| 27 | +struct Random; |
| 28 | + |
| 29 | +template <> |
| 30 | +struct Random<platform::CPUDeviceContext> { |
| 31 | + using Engine = std::minstd_rand; |
| 32 | + |
| 33 | + template <typename T> |
| 34 | + using UniformIntDist = std::uniform_int_distribution<T>; |
| 35 | +}; |
| 36 | + |
| 37 | +template <> |
| 38 | +struct Random<platform::CUDADeviceContext> { |
| 39 | + using Engine = thrust::minstd_rand; |
| 40 | + |
| 41 | + template <typename T> |
| 42 | + using UniformIntDist = thrust::uniform_int_distribution<T>; |
| 43 | +}; |
| 44 | + |
| 45 | +template <typename T> |
| 46 | +HOSTDEVICE inline void RandomCropImpl(const T* x, size_t* x_dim, T* out, |
| 47 | + size_t* out_dim, int i, int rank, |
| 48 | + int64_t prod_x_remain, |
| 49 | + int64_t prod_out_remain, size_t* offset) { |
| 50 | + size_t x_length = x_dim[rank]; |
| 51 | + size_t out_length = out_dim[rank]; |
| 52 | + |
| 53 | + int64_t x_stride = prod_x_remain / x_length; |
| 54 | + int64_t out_stride = prod_out_remain / out_length; |
| 55 | + size_t offset_i = offset[i]; |
| 56 | + if (x_stride == 1 && out_stride == 1) { |
| 57 | + // In the final stage, copy from offset. |
| 58 | + x += offset_i; |
| 59 | + for (size_t i = 0; i < out_length; ++i) { |
| 60 | + *out++ = *x++; |
| 61 | + } |
| 62 | + } else { |
| 63 | + x += offset_i * x_stride; |
| 64 | + for (size_t i = 0; i < out_length; ++i) { |
| 65 | + RandomCropImpl<T>(x, x_dim, out, out_dim, i + 1, rank, x_stride, |
| 66 | + out_stride, offset); |
| 67 | + x += x_stride; |
| 68 | + out += out_stride; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +template <typename DeviceContext, typename T> |
| 74 | +struct RandomCropFunctor { |
| 75 | + const T* x_; |
| 76 | + T* out_; |
| 77 | + size_t x_dim_[9]; |
| 78 | + size_t out_dim_[9]; |
| 79 | + size_t prod_same_dim_; |
| 80 | + |
| 81 | + size_t prod_x_dim_; |
| 82 | + size_t prod_out_dim_; |
| 83 | + |
| 84 | + int num_same_dim_; |
| 85 | + int rank_; |
| 86 | + |
| 87 | + int64_t seed_; |
| 88 | + |
| 89 | + RandomCropFunctor(const T* x, T* out, int64_t seed) |
| 90 | + : x_(x), |
| 91 | + out_(out), |
| 92 | + prod_same_dim_(1), |
| 93 | + prod_x_dim_(1), |
| 94 | + prod_out_dim_(1), |
| 95 | + seed_(seed) { |
| 96 | + std::fill(x_dim_, x_dim_ + sizeof(x_dim_) / sizeof(size_t), 0); |
| 97 | + std::fill(out_dim_, out_dim_ + sizeof(out_dim_) / sizeof(size_t), 0); |
| 98 | + } |
| 99 | + |
| 100 | + HOSTDEVICE void operator()(size_t i) { |
| 101 | + typename Random<DeviceContext>::Engine engine(seed_); |
| 102 | + engine.discard(i * (rank_ - num_same_dim_)); |
| 103 | + |
| 104 | + int64_t prod_x_unsame = (prod_x_dim_ / prod_same_dim_); |
| 105 | + int64_t prod_out_unsame = (prod_out_dim_ / prod_same_dim_); |
| 106 | + |
| 107 | + const T* x = x_ + i * prod_x_unsame; |
| 108 | + T* out = out_ + i * prod_out_unsame; |
| 109 | + |
| 110 | + size_t offset[9]; |
| 111 | + for (int i = num_same_dim_; i < rank_; ++i) { |
| 112 | + typename Random<DeviceContext>::template UniformIntDist<size_t> dist( |
| 113 | + 0, x_dim_[i] - out_dim_[i]); |
| 114 | + offset[i] = dist(engine); |
| 115 | + } |
| 116 | + RandomCropImpl<T>(x, x_dim_, out, out_dim_, num_same_dim_, rank_, |
| 117 | + prod_x_unsame, prod_out_unsame, offset); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +template <typename DeviceContext, typename T> |
| 122 | +class RandomCropKernel : public framework::OpKernel<T> { |
| 123 | + public: |
| 124 | + virtual void Compute(const framework::ExecutionContext& context) const { |
| 125 | + int64_t seed = |
| 126 | + *context.Input<framework::LoDTensor>("Seed")->data<int64_t>(); |
| 127 | + auto& x = detail::Ref(context.Input<framework::LoDTensor>("X")); |
| 128 | + auto& out = detail::Ref(context.Output<framework::LoDTensor>("Out")); |
| 129 | + |
| 130 | + RandomCropFunctor<DeviceContext, T> functor{ |
| 131 | + x.data<T>(), out.mutable_data<T>(context.GetPlace()), seed}; |
| 132 | + |
| 133 | + auto& out_dim = out.dims(); |
| 134 | + auto& x_dim = x.dims(); |
| 135 | + |
| 136 | + auto rank = x_dim.size(); |
| 137 | + while (rank-- > 0) { |
| 138 | + functor.x_dim_[rank] = x_dim[rank]; |
| 139 | + functor.out_dim_[rank] = out_dim[rank]; |
| 140 | + functor.prod_x_dim_ *= x_dim[rank]; |
| 141 | + functor.prod_out_dim_ *= out_dim[rank]; |
| 142 | + if (x_dim[rank] != out_dim[rank]) { |
| 143 | + PADDLE_ENFORCE_EQ(functor.prod_same_dim_, 1); |
| 144 | + functor.num_same_dim_ = rank; |
| 145 | + } else { |
| 146 | + functor.prod_same_dim_ *= out_dim[rank]; |
| 147 | + } |
| 148 | + } |
| 149 | + functor.rank_ = x_dim.size(); |
| 150 | + |
| 151 | + platform::ForRange<DeviceContext> for_range( |
| 152 | + context.template device_context<DeviceContext>(), |
| 153 | + functor.prod_same_dim_); |
| 154 | + |
| 155 | + for_range(functor); |
| 156 | + |
| 157 | + Random<platform::CPUDeviceContext>::Engine engine(seed); |
| 158 | + engine.discard(functor.prod_same_dim_ * |
| 159 | + (functor.rank_ - functor.num_same_dim_)); |
| 160 | + |
| 161 | + *context.Output<framework::LoDTensor>("SeedOut")->mutable_data<int64_t>( |
| 162 | + platform::CPUPlace()) = engine(); |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +} // namespace operators |
| 167 | +} // namespace paddle |
0 commit comments