linspace_op.cc 3.2 KB
Newer Older
Z
zhoukunsheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2019 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. */

15
#include <string>
16 17 18

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
19
#include "paddle/fluid/framework/op_version_registry.h"
20 21
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/ternary.h"
Z
zhoukunsheng 已提交
22 23 24 25 26 27 28 29 30 31

namespace paddle {
namespace operators {

class LinspaceOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  framework::OpKernelType GetExpectedKernelType(
32
      const framework::ExecutionContext &ctx) const override {
Z
zhoukunsheng 已提交
33
    return framework::OpKernelType(
34 35
        framework::proto::VarType::Type(ctx.Attr<int>("dtype")),
        ctx.GetPlace());
Z
zhoukunsheng 已提交
36
  }
37 38

  framework::OpKernelType GetKernelTypeForVar(
39 40
      const std::string &var_name,
      const framework::Tensor &tensor,
41
      const framework::OpKernelType &expected_kernel_type) const override {
42
    if (platform::is_xpu_place(tensor.place())) {
43 44
      return framework::OpKernelType(
          expected_kernel_type.data_type_, tensor.place(), tensor.layout());
45 46
    }
    return expected_kernel_type;
47
  }
Z
zhoukunsheng 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
};

class LinspaceOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("Start",
             "First entry in the sequence. It is a tensor of shape [1], should "
             "be of type float32 or float64.");
    AddInput("Stop",
             "Last entry in the sequence. It is a tensor of shape [1], should "
             "be of type float32 or float64.");
    AddInput("Num",
             "Number of entry in the sequence. It is a tensor of shape [1], "
             "should be of type int32.");
62
    AddAttr<int>("dtype", "The output data type.");
Z
zhoukunsheng 已提交
63 64 65 66 67 68 69 70 71 72
    AddOutput("Out", "A sequence of numbers.");
    AddComment(R"DOC(
    Return fixed number of evenly spaced values within a given interval. First entry is start, and last entry is stop. In the case when Num is 1, only Start is returned. Like linspace function of numpy.
)DOC");
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
73 74
DECLARE_INFER_SHAPE_FUNCTOR(linspace,
                            LinspaceInferShapeFunctor,
75
                            PD_INFER_META(phi::LinspaceRawInferMeta));
76
REGISTER_OPERATOR(
77 78 79
    linspace,
    ops::LinspaceOp,
    ops::LinspaceOpMaker,
80 81 82
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    LinspaceInferShapeFunctor);
83

84 85
REGISTER_OP_VERSION(linspace).AddCheckpoint(
    R"ROC(
86 87
      Upgrade linspace to add a new attribute [dtype].
    )ROC",
88 89
    paddle::framework::compatible::OpVersionDesc().NewAttr(
        "dtype", "In order to change output data type ", 5));