uniform_random_op.cc 10.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
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 15
#include "paddle/fluid/operators/uniform_random_op.h"
#include <string>
Y
Yi Wang 已提交
16 17
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
Y
Yu Yang 已提交
18 19
namespace paddle {
namespace operators {
Y
Yu Yang 已提交
20

Q
qijun 已提交
21 22 23 24
// It seems that Eigen::Tensor::random in GPU will SEGFAULT.
// Use std::random and thrust::random(thrust is a std library in CUDA) to
// implement uniform random.
template <typename T>
Y
Yu Yang 已提交
25
class CPUUniformRandomKernel : public framework::OpKernel<T> {
Q
qijun 已提交
26
 public:
C
chengduo 已提交
27 28
  void Compute(const framework::ExecutionContext &ctx) const override {
    framework::Tensor *tensor = nullptr;
Y
Yancey1989 已提交
29
    auto out_var = ctx.OutputVar("Out");
30 31 32 33 34 35
    std::vector<int64_t> new_shape;
    auto list_new_shape_tensor =
        ctx.MultiInput<framework::Tensor>("ShapeTensorList");
    if (list_new_shape_tensor.size() > 0 || ctx.HasInput("ShapeTensor")) {
      if (ctx.HasInput("ShapeTensor")) {
        auto *shape_tensor = ctx.Input<framework::Tensor>("ShapeTensor");
36
        new_shape = GetNewDataFromShapeTensor(shape_tensor);
37
      } else if (list_new_shape_tensor.size() > 0) {
38
        new_shape = GetNewDataFromShapeTensorList(list_new_shape_tensor);
39 40 41 42
      }
    }

    if (out_var->IsType<framework::SelectedRows>()) {
C
chengduo 已提交
43
      auto *selected_rows = out_var->GetMutable<framework::SelectedRows>();
44
      tensor = selected_rows->mutable_value();
45 46
      auto shape = ctx.Attr<std::vector<int64_t>>("shape");
      if (!new_shape.empty()) shape = new_shape;
Y
Yancey1989 已提交
47
      tensor->Resize(framework::make_ddim(shape));
48
      selected_rows->mutable_rows()->reserve(shape[0]);
49 50 51
    } else if (out_var->IsType<framework::LoDTensor>()) {
      tensor = out_var->GetMutable<framework::LoDTensor>();
      if (!new_shape.empty()) tensor->Resize(framework::make_ddim(new_shape));
Y
Yancey1989 已提交
52
    } else {
Y
Yancey1989 已提交
53 54
      PADDLE_THROW(
          "uniform_random_op's output only"
T
tangwei12 已提交
55
          "supports SelectedRows and LoDTensor");
Y
Yancey1989 已提交
56
    }
C
chengduo 已提交
57
    T *data = tensor->mutable_data<T>(ctx.GetPlace());
Q
Qiao Longfei 已提交
58
    unsigned int seed = static_cast<unsigned int>(ctx.Attr<int>("seed"));
Q
qijun 已提交
59 60 61 62 63 64
    std::minstd_rand engine;
    if (seed == 0) {
      seed = std::random_device()();
    }
    engine.seed(seed);
    std::uniform_real_distribution<T> dist(
Q
Qiao Longfei 已提交
65 66
        static_cast<T>(ctx.Attr<float>("min")),
        static_cast<T>(ctx.Attr<float>("max")));
67
    int64_t size = tensor->numel();
Q
qijun 已提交
68
    for (int64_t i = 0; i < size; ++i) {
Q
qijun 已提交
69 70
      data[i] = dist(engine);
    }
71 72 73 74 75 76 77
    unsigned int diag_num =
        static_cast<unsigned int>(ctx.Attr<int>("diag_num"));
    unsigned int diag_step =
        static_cast<unsigned int>(ctx.Attr<int>("diag_step"));
    auto diag_val = static_cast<T>(ctx.Attr<float>("diag_val"));
    if (diag_num > 0) {
      PADDLE_ENFORCE_GT(size, (diag_num - 1) * (diag_step + 1),
78 79 80 81 82
                        "ShapeError: the diagonal's elements is equal (num-1) "
                        "* (step-1) with num %d, step %d,"
                        "It should be smaller than %d, but received %d",
                        diag_num, diag_step, (diag_num - 1) * (diag_step + 1),
                        size);
83 84 85 86 87
      for (int64_t i = 0; i < diag_num; ++i) {
        int64_t pos = i * diag_step + i;
        data[pos] = diag_val;
      }
    }
Q
qijun 已提交
88 89 90
  }
};

Y
Yu Yang 已提交
91
class UniformRandomOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
92 93 94
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

C
chengduo 已提交
95
  void InferShape(framework::InferShapeContext *ctx) const override {
96 97
    PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true,
                      "Output(Out) of UniformRandomOp should not be null.");
98

99 100 101 102 103 104 105
    PADDLE_ENFORCE_LT(ctx->Attrs().Get<float>("min"),
                      ctx->Attrs().Get<float>("max"),
                      "uniform_random's min must less then max");
    PADDLE_ENFORCE_GE(ctx->Attrs().Get<int>("diag_num"), 0,
                      "diag_num must greater than or equal 0");
    PADDLE_ENFORCE_GE(ctx->Attrs().Get<int>("diag_step"), 0,
                      "diag_step must greater than or equal 0");
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    if (ctx->HasInputs("ShapeTensorList")) {
      // top prority shape
      auto inputs_name = ctx->Inputs("ShapeTensorList");
      PADDLE_ENFORCE_GT(
          inputs_name.size(), 0,
          "Input(ShapeTensorList)'size of Op(uniform_random) can't be zero."
          "Please check the Attr(shape)'s size of"
          "Op(fluid.layers.uniform_random).)");
      auto out_dims = std::vector<int>(inputs_name.size(), -1);
      ctx->SetOutputDim("Out", framework::make_ddim(out_dims));

      return;
    }
    auto &shape = ctx->Attrs().Get<std::vector<int64_t>>("shape");
    if (ctx->HasInput("ShapeTensor") && shape.empty()) {
      auto shape_dims = ctx->GetInputDim("ShapeTensor");
      PADDLE_ENFORCE_EQ(
          shape_dims.size(), 1,
125 126 127 128
          "ShapeError: Input(ShapeTensor)' dimension size of "
          "Op(uniform_random) must be 1."
          "But received ShapeTensor's dimensions = %d, shape = [%s]",
          shape_dims.size(), shape_dims);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      int num_ele = 1;
      for (int i = 0; i < shape_dims.size(); ++i) {
        num_ele *= shape_dims[i];
      }
      auto vec_dims = std::vector<int64_t>(num_ele, -1);
      auto out_dims = framework::make_ddim(vec_dims);
      ctx->SetOutputDim("Out", out_dims);
      return;
    }

    PADDLE_ENFORCE_EQ(
        shape.empty(), false,
        "if there is no Input(ShapeTensorList) and no Input(ShapeTensor),the "
        "attr(shape) information must "
        "be set by Attr(shape).");
    std::vector<int64_t> tensor_shape;
    tensor_shape.reserve(shape.size());
Q
QI JUN 已提交
146
    for (auto dim : shape) {
147
      tensor_shape.push_back(static_cast<int64_t>(dim));
Q
qijun 已提交
148
    }
149
    ctx->SetOutputDim("Out", framework::make_ddim(tensor_shape));
Y
Yu Yang 已提交
150
  }
Y
Yu Yang 已提交
151

152
 protected:
153
  framework::OpKernelType GetExpectedKernelType(
C
chengduo 已提交
154
      const framework::ExecutionContext &ctx) const override {
Y
Yu Yang 已提交
155
    return framework::OpKernelType(
156
        static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype")),
Q
QI JUN 已提交
157
        ctx.GetPlace());
Y
Yu Yang 已提交
158
  }
159 160 161 162 163 164 165 166 167 168

  framework::OpKernelType GetKernelTypeForVar(
      const std::string &var_name, const Tensor &tensor,
      const framework::OpKernelType &expected_kernel_type) const override {
    if (var_name == "ShapeTensorList" || var_name == "ShapeTensor") {
      return expected_kernel_type;
    }
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(), tensor.layout());
  }
Y
Yu Yang 已提交
169 170
};

Y
Yu Yang 已提交
171
class UniformRandomOpMaker : public framework::OpProtoAndCheckerMaker {
Y
Yu Yang 已提交
172
 public:
Y
Yu Yang 已提交
173
  void Make() override {
174
    AddInput("ShapeTensor",
175 176
             "(Tensor<int64_t> or Tensor<int32_t>, optional) . If provided, "
             "uniform_random "
177
             "according to "
178
             "this given shape. It means that it has a higher priority than "
179
             "the shape attribute, while the shape attribute still should be "
T
tianshuo78520a 已提交
180
             "set correctly to guarantee shape inference in compile time.")
181 182
        .AsDispensable();
    AddInput("ShapeTensorList",
183 184 185 186
             "(vector<Tensor<int64_t>> or vector<Tensor<int32_t>>, optional). "
             "If provided, uniform_random use this. The shape of the tensor "
             "must be [1], it has the highest priority comparing with "
             "Input(ShapeTensor) and attr(shape).")
187 188
        .AsDuplicable()
        .AsDispensable();
Y
yuyang18 已提交
189
    AddOutput("Out", "The output tensor of uniform random op");
190
    AddComment(R"DOC(
191
This operator initializes a tensor with random values sampled from a
192
uniform distribution. The random result is in set [min, max).
193

Y
Yu Yang 已提交
194
)DOC");
195 196
    AddAttr<std::vector<int64_t>>("shape", "The shape of the output tensor")
        .SetDefault({});
Y
yuyang18 已提交
197
    AddAttr<float>("min", "Minimum value of uniform random. [default -1.0].")
198
        .SetDefault(-1.0f);
Y
yuyang18 已提交
199
    AddAttr<float>("max", "Maximun value of uniform random. [default 1.0].")
200
        .SetDefault(1.0f);
Q
qijun 已提交
201
    AddAttr<int>("seed",
202
                 "Random seed used for generating samples. "
203 204
                 "0 means use a seed generated by the system."
                 "Note that if seed is not 0, this operator will always "
Y
yuyang18 已提交
205
                 "generate the same random numbers every time. [default 0].")
Q
qijun 已提交
206
        .SetDefault(0);
207 208 209 210 211 212 213 214
    AddAttr<int>("diag_num",
                 "The number of diag elements. Note that if "
                 "diag_num is 0, it means without diag init.[default 0].")
        .SetDefault(0);
    AddAttr<int>("diag_step", "The step between two diag element.[default 0].")
        .SetDefault(0);
    AddAttr<float>("diag_val", "The value of diag element. [default 1.0].")
        .SetDefault(1.0f);
Y
yuyang18 已提交
215
    AddAttr<int>("dtype", "Output tensor data type. [default 5(FP32)].")
216
        .SetDefault(framework::proto::VarType::FP32);
Y
Yu Yang 已提交
217 218
  }
};
Y
Yancey1989 已提交
219 220 221

class UniformRandomOpVarTypeInference : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
222 223
  void operator()(framework::InferVarTypeContext *ctx) const override {
    auto out_var_name = ctx->Output("Out").front();
C
chengduo 已提交
224
    auto var_data_type = static_cast<framework::proto::VarType::Type>(
M
minqiyang 已提交
225
        boost::get<int>(ctx->GetAttr("dtype")));
C
chengduo 已提交
226

M
minqiyang 已提交
227 228 229
    if (ctx->GetType(out_var_name) !=
        framework::proto::VarType::SELECTED_ROWS) {
      ctx->SetType(out_var_name, framework::proto::VarType::LOD_TENSOR);
Y
Yancey1989 已提交
230
    }
M
minqiyang 已提交
231
    ctx->SetDataType(out_var_name, var_data_type);
Y
Yancey1989 已提交
232 233 234
  }
};

Y
Yu Yang 已提交
235 236 237
}  // namespace operators
}  // namespace paddle

H
hong 已提交
238 239 240 241 242 243
REGISTER_OPERATOR(
    uniform_random, paddle::operators::UniformRandomOp,
    paddle::operators::UniformRandomOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    paddle::operators::UniformRandomOpVarTypeInference);
Y
Yancey1989 已提交
244

Q
qijun 已提交
245
REGISTER_OP_CPU_KERNEL(uniform_random,
246 247
                       paddle::operators::CPUUniformRandomKernel<float>,
                       paddle::operators::CPUUniformRandomKernel<double>);
248 249 250
REGISTER_OP_CPU_KERNEL(uniform_random_batch_size_like,
                       paddle::operators::CPUUniformRandomKernel<float>,
                       paddle::operators::CPUUniformRandomKernel<double>);