random_crop_op.h 5.1 KB
Newer Older
Y
yuyang18 已提交
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
// Copyright (c) 2018 PaddlePaddle 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.

#pragma once

#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/detail/safe_ref.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/for_range.h"
#include "thrust/random.h"

namespace paddle {
namespace operators {

template <typename DeviceContext>
struct Random;

template <>
struct Random<platform::CPUDeviceContext> {
  using Engine = std::minstd_rand;

  template <typename T>
  using UniformIntDist = std::uniform_int_distribution<T>;
};

template <>
struct Random<platform::CUDADeviceContext> {
  using Engine = thrust::minstd_rand;

  template <typename T>
  using UniformIntDist = thrust::uniform_int_distribution<T>;
};

template <typename T>
HOSTDEVICE inline void RandomCropImpl(const T* x, size_t* x_dim, T* out,
                                      size_t* out_dim, int i, int rank,
                                      int64_t prod_x_remain,
                                      int64_t prod_out_remain, size_t* offset) {
  size_t x_length = x_dim[rank];
  size_t out_length = out_dim[rank];

  int64_t x_stride = prod_x_remain / x_length;
  int64_t out_stride = prod_out_remain / out_length;
  size_t offset_i = offset[i];
  if (x_stride == 1 && out_stride == 1) {
    // In the final stage, copy from offset.
    x += offset_i;
    for (size_t i = 0; i < out_length; ++i) {
      *out++ = *x++;
    }
  } else {
    x += offset_i * x_stride;
    for (size_t i = 0; i < out_length; ++i) {
      RandomCropImpl<T>(x, x_dim, out, out_dim, i + 1, rank, x_stride,
                        out_stride, offset);
      x += x_stride;
      out += out_stride;
    }
  }
}

template <typename DeviceContext, typename T>
struct RandomCropFunctor {
  const T* x_;
  T* out_;
  size_t x_dim_[9];
  size_t out_dim_[9];
  size_t prod_same_dim_;

  size_t prod_x_dim_;
  size_t prod_out_dim_;

  int num_same_dim_;
  int rank_;

  int64_t seed_;

  RandomCropFunctor(const T* x, T* out, int64_t seed)
      : x_(x),
        out_(out),
        prod_same_dim_(1),
        prod_x_dim_(1),
        prod_out_dim_(1),
        seed_(seed) {
    std::fill(x_dim_, x_dim_ + sizeof(x_dim_) / sizeof(size_t), 0);
    std::fill(out_dim_, out_dim_ + sizeof(out_dim_) / sizeof(size_t), 0);
  }

  HOSTDEVICE void operator()(size_t i) {
    typename Random<DeviceContext>::Engine engine(seed_);
    engine.discard(i * (rank_ - num_same_dim_));

    int64_t prod_x_unsame = (prod_x_dim_ / prod_same_dim_);
    int64_t prod_out_unsame = (prod_out_dim_ / prod_same_dim_);

    const T* x = x_ + i * prod_x_unsame;
    T* out = out_ + i * prod_out_unsame;

    size_t offset[9];
    for (int i = num_same_dim_; i < rank_; ++i) {
      typename Random<DeviceContext>::template UniformIntDist<size_t> dist(
          0, x_dim_[i] - out_dim_[i]);
      offset[i] = dist(engine);
    }
    RandomCropImpl<T>(x, x_dim_, out, out_dim_, num_same_dim_, rank_,
                      prod_x_unsame, prod_out_unsame, offset);
  }
};

template <typename DeviceContext, typename T>
class RandomCropKernel : public framework::OpKernel<T> {
 public:
  virtual void Compute(const framework::ExecutionContext& context) const {
    int64_t seed =
        *context.Input<framework::LoDTensor>("Seed")->data<int64_t>();
    auto& x = detail::Ref(context.Input<framework::LoDTensor>("X"));
    auto& out = detail::Ref(context.Output<framework::LoDTensor>("Out"));

    RandomCropFunctor<DeviceContext, T> functor{
        x.data<T>(), out.mutable_data<T>(context.GetPlace()), seed};

    auto& out_dim = out.dims();
    auto& x_dim = x.dims();

    auto rank = x_dim.size();
    while (rank-- > 0) {
      functor.x_dim_[rank] = x_dim[rank];
      functor.out_dim_[rank] = out_dim[rank];
      functor.prod_x_dim_ *= x_dim[rank];
      functor.prod_out_dim_ *= out_dim[rank];
      if (x_dim[rank] != out_dim[rank]) {
        PADDLE_ENFORCE_EQ(functor.prod_same_dim_, 1);
        functor.num_same_dim_ = rank;
      } else {
        functor.prod_same_dim_ *= out_dim[rank];
      }
    }
    functor.rank_ = x_dim.size();

    platform::ForRange<DeviceContext> for_range(
        context.template device_context<DeviceContext>(),
        functor.prod_same_dim_);

    for_range(functor);

    Random<platform::CPUDeviceContext>::Engine engine(seed);
    engine.discard(functor.prod_same_dim_ *
                   (functor.rank_ - functor.num_same_dim_));

    *context.Output<framework::LoDTensor>("SeedOut")->mutable_data<int64_t>(
        platform::CPUPlace()) = engine();
  }
};

}  // namespace operators
}  // namespace paddle