diff --git a/paddle/fluid/operators/randint_op.cc b/paddle/fluid/operators/randint_op.cc deleted file mode 100644 index ddfcf64107c8c57dd4c28fe47e9d220db1fa6f9c..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/randint_op.cc +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright (c) 2020 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. - -#include -#include - -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/operators/uniform_random_op.h" -#include "paddle/fluid/platform/enforce.h" -#include "paddle/phi/core/generator.h" - -namespace paddle { -namespace operators { - -class RandintOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext* ctx) const override { - PADDLE_ENFORCE_EQ( - ctx->HasOutput("Out"), - true, - platform::errors::InvalidArgument("Output(Out) of RandintOp is null.")); - PADDLE_ENFORCE_LT( - ctx->Attrs().Get("low"), - ctx->Attrs().Get("high"), - platform::errors::InvalidArgument("randint's low must less then high, " - "but received: low = %d, high = %d.", - ctx->Attrs().Get("low"), - ctx->Attrs().Get("high"))); - - if (ctx->HasInputs("ShapeTensorList")) { - // top prority shape - auto inputs_name = ctx->Inputs("ShapeTensorList"); - PADDLE_ENFORCE_GT( - inputs_name.size(), - 0, - platform::errors::InvalidArgument( - "Input(ShapeTensorList)'size of Op(randint) can't be zero." - "Please check the Attr(shape)'s size of" - "Op(fluid.layers.randint).)")); - auto out_dims = std::vector(inputs_name.size(), -1); - ctx->SetOutputDim("Out", phi::make_ddim(out_dims)); - - return; - } - - auto& shape = ctx->Attrs().Get>("shape"); - if (ctx->HasInput("ShapeTensor") && shape.empty()) { - auto shape_dims = ctx->GetInputDim("ShapeTensor"); - PADDLE_ENFORCE_EQ(shape_dims.size(), - 1, - platform::errors::InvalidArgument( - "ShapeError: Input(ShapeTensor)' dimension size of " - "Op(randint) must be 1." - "But received ShapeTensor's dimensions = %d.", - shape_dims.size())); - int num_ele = 1; - for (int i = 0; i < shape_dims.size(); ++i) { - num_ele *= shape_dims[i]; - } - auto vec_dims = std::vector(num_ele, -1); - auto out_dims = phi::make_ddim(vec_dims); - ctx->SetOutputDim("Out", out_dims); - return; - } - - std::vector tensor_shape; - tensor_shape.reserve(shape.size()); - for (auto dim : shape) { - tensor_shape.push_back(static_cast(dim)); - } - ctx->SetOutputDim("Out", phi::make_ddim(tensor_shape)); - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey( - static_cast(ctx.Attr("dtype")), - ctx.GetPlace()); - } -}; - -class RandintOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("ShapeTensor", - "(Tensor or Tensor, optional) . If provided, " - "randint" - "according to " - "this given shape. It means that it has a higher priority than " - "Attr(shape) but a lower priority than Input(ShapeTensor).") - .AsDispensable(); - AddInput("ShapeTensorList", - "(vector> or vector>, optional). " - "If provided, randint use this. The shape of the tensor " - "must be [1], it has the highest priority comparing with " - "Input(ShapeTensor) and attr(shape).") - .AsDuplicable() - .AsDispensable(); - AddOutput("Out", "The output tensor of randint op"); - AddComment(R"DOC( -This operator initializes a tensor with random integers sampled from a -uniform distribution. The random result is in set [low, high). -)DOC"); - AddAttr>("shape", "The shape of the output tensor.") - .SetDefault({}); - AddAttr("low", - "The lower bound on the range of random values to generate."); - AddAttr("high", - "The upper bound on the range of random values to generate."); - AddAttr("dtype", "Output tensor data type. [Default INT64].") - .SetDefault(framework::proto::VarType::INT64); - AddAttr("seed", - "Random seed used for generating samples. " - "0 means use a seed generated by the system." - "Note that if seed is not 0, this operator will always " - "generate the same random numbers every time. [default 0].") - .SetDefault(0); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -REGISTER_OPERATOR( - randint, - ops::RandintOp, - ops::RandintOpMaker, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker) diff --git a/paddle/fluid/operators/unity_build_rule.cmake b/paddle/fluid/operators/unity_build_rule.cmake index d0efaed9e0719c97d78dc0951822cdcc19518dfd..45c246b00c7fda7eb9a9d0cde0de86f501135f25 100644 --- a/paddle/fluid/operators/unity_build_rule.cmake +++ b/paddle/fluid/operators/unity_build_rule.cmake @@ -223,7 +223,6 @@ register_unity_group( quantize_op.cc mkldnn/quantize_mkldnn_op.cc queue_generator_op.cc - randint_op.cc random_crop_op.cc randperm_op.cc range_op.cc @@ -503,7 +502,6 @@ register_unity_group( pull_box_sparse_op.cu) register_unity_group( cu - randint_op.cu random_crop_op.cu randperm_op.cu range_op.cu diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 6c7eb39cd0d9b444d8825445e4a746bb9a929ff0..27fa51511cad57add3d7735ae37d34b71c6d7fa1 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1545,6 +1545,16 @@ extra : attrs : [float moving_rate = 0.9] +- op : randint + outputs : + out : Out + int_array: + shape : + data_type : int64_t + tensor_name : ShapeTensor + tensors_name : ShapeTensorList + manual_signature : [randint] + - op : real backward : real_grad inputs : diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 6aecd1ead1d55096bedee079f5836dbdfa987b33..7ec37a8d06302976b1038a72a06baaa5f3cb0bc7 100644 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -196,6 +196,17 @@ func : one_hot_raw data_type : x +- op : randint + args : (int low, int high, IntArray shape = {}, DataType dtype = DataType::INT64, int seed = 0) + output : Tensor(out) + infer_meta : + func : RandintInferMeta + param : [low, high, shape, dtype] + kernel : + func : randint + param : [low, high, shape, dtype] + data_type : dtype + - op : reduce args : (Tensor x, int ring_id = 0, int root_id = 0, int reduce_type = 0) output : Tensor(out)