uniform_random_op.cu 2.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
Luo Tao 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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. */
14
#include "paddle/fluid/operators/uniform_random_op.h"
15

Q
qijun 已提交
16 17 18 19
namespace paddle {
namespace operators {

template <typename T>
Y
Yu Yang 已提交
20
class GPUUniformRandomKernel : public framework::OpKernel<T> {
Q
qijun 已提交
21 22
 public:
  void Compute(const framework::ExecutionContext& context) const override {
Y
Yancey1989 已提交
23
    framework::Tensor* tensor = nullptr;
Y
fix ci  
Yancey1989 已提交
24
    auto out_var = context.OutputVar("Out");
25 26 27 28 29 30
    std::vector<int64_t> new_shape;
    auto list_new_shape_tensor =
        context.MultiInput<framework::Tensor>("ShapeTensorList");
    if (list_new_shape_tensor.size() > 0 || context.HasInput("ShapeTensor")) {
      if (context.HasInput("ShapeTensor")) {
        auto* shape_tensor = context.Input<framework::Tensor>("ShapeTensor");
31
        new_shape = GetNewDataFromShapeTensor(shape_tensor);
32
      } else if (list_new_shape_tensor.size() > 0) {
33
        new_shape = GetNewDataFromShapeTensorList(list_new_shape_tensor);
34 35 36
      }
    }

37 38
    if (out_var->IsType<phi::SelectedRows>()) {
      auto* selected_rows = out_var->GetMutable<phi::SelectedRows>();
39
      tensor = selected_rows->mutable_value();
T
tangwei12 已提交
40
      auto shape = context.Attr<std::vector<int64_t>>("shape");
41
      if (!new_shape.empty()) shape = new_shape;
42
      tensor->Resize(phi::make_ddim(shape));
43 44 45
      selected_rows->mutable_rows()->reserve(shape[0]);
    } else if (out_var->IsType<framework::LoDTensor>()) {
      tensor = out_var->GetMutable<framework::LoDTensor>();
46
      if (!new_shape.empty()) tensor->Resize(phi::make_ddim(new_shape));
Y
Yancey1989 已提交
47
    } else {
48 49 50 51 52
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Expected type of Output(out) in uniform_random_op must be Tensor, "
          "SelectedRows. But got "
          "unsupport type: %s.",
          framework::ToTypeName(out_var->Type())));
Y
Yancey1989 已提交
53
    }
54
    UniformRandom<T>(context, tensor);
Q
qijun 已提交
55 56 57 58 59
  }
};

}  // namespace operators
}  // namespace paddle
Y
Yu Yang 已提交
60

61 62 63 64 65 66
REGISTER_OP_CUDA_KERNEL(uniform_random,
                        paddle::operators::GPUUniformRandomKernel<float>,
                        paddle::operators::GPUUniformRandomKernel<double>);
REGISTER_OP_CUDA_KERNEL(uniform_random_batch_size_like,
                        paddle::operators::GPUUniformRandomKernel<float>,
                        paddle::operators::GPUUniformRandomKernel<double>);