linspace_op.cc 3.3 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

namespace paddle {
namespace operators {

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

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

38
  phi::KernelKey GetKernelTypeForVar(
39
      const std::string &var_name,
40
      const phi::DenseTensor &tensor,
41
      const phi::KernelKey &expected_kernel_type) const override {
42
    if (platform::is_xpu_place(tensor.place())) {
43 44
      return phi::KernelKey(
          tensor.place(), tensor.layout(), expected_kernel_type.dtype());
45
    }
46 47 48
    return phi::KernelKey(phi::Backend::ALL_BACKEND,
                          expected_kernel_type.layout(),
                          expected_kernel_type.dtype());
49
  }
Z
zhoukunsheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
};

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.");
64
    AddAttr<int>("dtype", "The output data type.");
Z
zhoukunsheng 已提交
65 66 67 68 69 70 71 72 73 74
    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;
75 76
DECLARE_INFER_SHAPE_FUNCTOR(linspace,
                            LinspaceInferShapeFunctor,
77
                            PD_INFER_META(phi::LinspaceRawInferMeta));
78
REGISTER_OPERATOR(
79 80 81
    linspace,
    ops::LinspaceOp,
    ops::LinspaceOpMaker,
82 83 84
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    LinspaceInferShapeFunctor);
85

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