uniform_random_op.cc 9.4 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
#include "paddle/fluid/operators/uniform_random_op.h"
L
Leo Chen 已提交
15

16
#include <string>
L
Leo Chen 已提交
17

18
#include "paddle/fluid/framework/infershape_utils.h"
Y
Yi Wang 已提交
19 20
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
21
#include "paddle/fluid/platform/bfloat16.h"
22
#include "paddle/phi/core/generator.h"
23
#include "paddle/phi/infermeta/nullary.h"
Y
yaoxuefeng 已提交
24

Y
Yu Yang 已提交
25 26
namespace paddle {
namespace operators {
Y
Yu Yang 已提交
27

28 29
namespace {
template <typename T>
30 31 32 33
inline void UniformRealDistribution(T *data,
                                    const int64_t &size,
                                    const float &min,
                                    const float &max,
34
                                    const unsigned int seed) {
35 36 37
  VLOG(4) << "[CPU] UniformRandomKernel<T>";
  std::uniform_real_distribution<T> dist(static_cast<T>(min),
                                         static_cast<T>(max));
38
  auto engine = phi::GetCPURandomEngine(seed);
39 40 41 42 43 44 45 46

  for (int64_t i = 0; i < size; ++i) {
    data[i] = dist(*engine);
  }
}

template <>
inline void UniformRealDistribution(paddle::platform::bfloat16 *data,
47 48 49 50
                                    const int64_t &size,
                                    const float &min,
                                    const float &max,
                                    const unsigned int seed) {
51 52
  VLOG(4) << "[CPU] UniformRandomKernel<bfloat16>";
  std::uniform_real_distribution<float> dist(min, max);
53
  auto engine = phi::GetCPURandomEngine(seed);
54 55 56 57 58 59 60

  for (int64_t i = 0; i < size; ++i) {
    data[i] = static_cast<paddle::platform::bfloat16>(dist(*engine));
  }
}
}  // namespace

Q
qijun 已提交
61 62 63 64
// 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 已提交
65
class CPUUniformRandomKernel : public framework::OpKernel<T> {
Q
qijun 已提交
66
 public:
C
chengduo 已提交
67
  void Compute(const framework::ExecutionContext &ctx) const override {
68
    phi::DenseTensor *tensor = nullptr;
Y
Yancey1989 已提交
69
    auto out_var = ctx.OutputVar("Out");
70 71
    std::vector<int64_t> new_shape;
    auto list_new_shape_tensor =
72
        ctx.MultiInput<phi::DenseTensor>("ShapeTensorList");
73 74
    if (list_new_shape_tensor.size() > 0 || ctx.HasInput("ShapeTensor")) {
      if (ctx.HasInput("ShapeTensor")) {
75
        auto *shape_tensor = ctx.Input<phi::DenseTensor>("ShapeTensor");
76
        new_shape = GetNewDataFromShapeTensor(shape_tensor);
77
      } else if (list_new_shape_tensor.size() > 0) {
78
        new_shape = GetNewDataFromShapeTensorList(list_new_shape_tensor);
79 80 81
      }
    }

82 83
    if (out_var->IsType<phi::SelectedRows>()) {
      auto *selected_rows = out_var->GetMutable<phi::SelectedRows>();
84
      tensor = selected_rows->mutable_value();
85 86
      auto shape = ctx.Attr<std::vector<int64_t>>("shape");
      if (!new_shape.empty()) shape = new_shape;
87
      tensor->Resize(phi::make_ddim(shape));
88
      selected_rows->mutable_rows()->reserve(shape[0]);
89 90
    } else if (out_var->IsType<phi::DenseTensor>()) {
      tensor = out_var->GetMutable<phi::DenseTensor>();
91
      if (!new_shape.empty()) tensor->Resize(phi::make_ddim(new_shape));
Y
Yancey1989 已提交
92
    } else {
93 94 95 96 97
      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 已提交
98
    }
C
chengduo 已提交
99
    T *data = tensor->mutable_data<T>(ctx.GetPlace());
Y
yaoxuefeng 已提交
100
    int64_t size = tensor->numel();
L
Leo Chen 已提交
101

102
    UniformRealDistribution<T>(
103 104 105 106
        data,
        size,
        ctx.Attr<float>("min"),
        ctx.Attr<float>("max"),
107
        static_cast<unsigned int>(ctx.Attr<int>("seed")));
Y
yaoxuefeng 已提交
108

109 110 111 112 113 114
    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) {
115
      PADDLE_ENFORCE_GT(
116 117
          size,
          (diag_num - 1) * (diag_step + 1),
118 119 120 121
          platform::errors::InvalidArgument(
              "ShapeInvalid: the diagonal's elements is equal (num-1) "
              "* (step-1) with num %d, step %d,"
              "It should be smaller than %d, but received %d",
122 123 124 125
              diag_num,
              diag_step,
              (diag_num - 1) * (diag_step + 1),
              size));
126 127 128 129 130
      for (int64_t i = 0; i < diag_num; ++i) {
        int64_t pos = i * diag_step + i;
        data[pos] = diag_val;
      }
    }
Q
qijun 已提交
131 132 133
  }
};

Y
Yu Yang 已提交
134
class UniformRandomOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
135 136 137
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

138
 protected:
139
  phi::KernelKey GetExpectedKernelType(
C
chengduo 已提交
140
      const framework::ExecutionContext &ctx) const override {
141
    return phi::KernelKey(
142
        static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype")),
Q
QI JUN 已提交
143
        ctx.GetPlace());
Y
Yu Yang 已提交
144
  }
145

146
  phi::KernelKey GetKernelTypeForVar(
147
      const std::string &var_name,
148
      const phi::DenseTensor &tensor,
149
      const phi::KernelKey &expected_kernel_type) const override {
150
    if (var_name == "ShapeTensorList" || var_name == "ShapeTensor") {
151 152 153
      return phi::KernelKey(phi::Backend::ALL_BACKEND,
                            expected_kernel_type.layout(),
                            expected_kernel_type.dtype());
154
    }
155 156
    return phi::KernelKey(
        tensor.place(), tensor.layout(), expected_kernel_type.dtype());
157
  }
Y
Yu Yang 已提交
158 159
};

Y
Yu Yang 已提交
160
class UniformRandomOpMaker : public framework::OpProtoAndCheckerMaker {
Y
Yu Yang 已提交
161
 public:
Y
Yu Yang 已提交
162
  void Make() override {
163
    AddInput("ShapeTensor",
164 165
             "(Tensor<int64_t> or Tensor<int32_t>, optional) . If provided, "
             "uniform_random "
166
             "according to "
167
             "this given shape. It means that it has a higher priority than "
168
             "the shape attribute, while the shape attribute still should be "
T
tianshuo78520a 已提交
169
             "set correctly to guarantee shape inference in compile time.")
170 171
        .AsDispensable();
    AddInput("ShapeTensorList",
172 173 174 175
             "(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).")
176 177
        .AsDuplicable()
        .AsDispensable();
Y
yuyang18 已提交
178
    AddOutput("Out", "The output tensor of uniform random op");
179
    AddComment(R"DOC(
180
This operator initializes a tensor with random values sampled from a
181
uniform distribution. The random result is in set [min, max).
182

Y
Yu Yang 已提交
183
)DOC");
184 185
    AddAttr<std::vector<int64_t>>("shape", "The shape of the output tensor")
        .SetDefault({});
Y
yuyang18 已提交
186
    AddAttr<float>("min", "Minimum value of uniform random. [default -1.0].")
187 188
        .SetDefault(-1.0f)
        .SupportTensor();
Y
yuyang18 已提交
189
    AddAttr<float>("max", "Maximun value of uniform random. [default 1.0].")
190 191
        .SetDefault(1.0f)
        .SupportTensor();
Q
qijun 已提交
192
    AddAttr<int>("seed",
193
                 "Random seed used for generating samples. "
194 195
                 "0 means use a seed generated by the system."
                 "Note that if seed is not 0, this operator will always "
Y
yuyang18 已提交
196
                 "generate the same random numbers every time. [default 0].")
Q
qijun 已提交
197
        .SetDefault(0);
198 199 200 201 202 203 204 205
    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 已提交
206
    AddAttr<int>("dtype", "Output tensor data type. [default 5(FP32)].")
207
        .SetDefault(framework::proto::VarType::FP32);
Y
Yu Yang 已提交
208 209
  }
};
Y
Yancey1989 已提交
210 211 212

class UniformRandomOpVarTypeInference : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
213
  void operator()(framework::InferVarTypeContext *ctx) const override {
C
chengduo 已提交
214
    auto var_data_type = static_cast<framework::proto::VarType::Type>(
R
Ruibiao Chen 已提交
215
        PADDLE_GET_CONST(int, ctx->GetAttr("dtype")));
C
chengduo 已提交
216

217 218
    if (ctx->GetOutputType("Out") != framework::proto::VarType::SELECTED_ROWS) {
      ctx->SetOutputType("Out", framework::proto::VarType::LOD_TENSOR);
Y
Yancey1989 已提交
219
    }
220
    ctx->SetOutputDataType("Out", var_data_type);
Y
Yancey1989 已提交
221 222 223
  }
};

Y
Yu Yang 已提交
224 225 226
}  // namespace operators
}  // namespace paddle

227 228
DECLARE_INFER_SHAPE_FUNCTOR(uniform_random,
                            UniformRandomInferShapeFunctor,
229 230
                            PD_INFER_META(phi::UniformRandomInferMeta));

H
hong 已提交
231
REGISTER_OPERATOR(
232 233
    uniform_random,
    paddle::operators::UniformRandomOp,
H
hong 已提交
234 235 236
    paddle::operators::UniformRandomOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
237 238
    paddle::operators::UniformRandomOpVarTypeInference,
    UniformRandomInferShapeFunctor);
Y
Yancey1989 已提交
239

240 241 242 243 244
REGISTER_OP_CPU_KERNEL(
    uniform_random_batch_size_like,
    paddle::operators::CPUUniformRandomKernel<float>,
    paddle::operators::CPUUniformRandomKernel<double>,
    paddle::operators::CPUUniformRandomKernel<paddle::platform::bfloat16>);