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

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14

15
#include "paddle/fluid/operators/fill_op.h"
Y
Yi Wang 已提交
16
#include "paddle/fluid/framework/op_registry.h"
Y
Yu Yang 已提交
17 18 19 20 21 22

namespace paddle {
namespace operators {

class FillOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
23
  void Make() override {
Y
Yu Yang 已提交
24 25 26 27 28 29 30 31 32 33
    AddComment(R"DOC(Fill operator

Fill an tensor with `value` and `shape`. The type of the tensor is specify by
`dtype`.
)DOC");
    AddOutput("Out", "(LoDTensor) The output tensor.");
    AddAttr<std::vector<float>>(
        "value", "The float values of tensor, which are flatten in row major");
    AddAttr<std::vector<int>>("shape", "The shape of output tensor");
    AddAttr<int>("dtype", "The data type of output tensor, Default is float")
34
        .SetDefault(framework::proto::VarType::FP32);
Y
Yu Yang 已提交
35 36 37 38 39 40 41
    AddAttr<bool>("force_cpu",
                  "Whether the output tensor must be at CPU memory or not. "
                  "Default is false.")
        .SetDefault(false);
  }
};

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
class FillOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* context) const override {
    PADDLE_ENFORCE_EQ(context->HasOutput("Out"), true,
                      "Output(Out) of FillOp should not be null.");
    auto& shape = context->Attrs().Get<std::vector<int>>("shape");
    context->SetOutputDim("Out", framework::make_ddim(shape));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::proto::VarType::Type(ctx.Attr<int>("dtype")),
        ctx.GetPlace());
  }
};

class FillOpVarTypeInference : public framework::VarTypeInference {
Y
Yu Yang 已提交
63
 public:
64 65 66 67 68
  void operator()(framework::InferVarTypeContext* ctx) const override {
    auto data_type = static_cast<framework::proto::VarType::Type>(
        boost::get<int>(ctx->GetAttr("dtype")));
    auto& out_var_name = ctx->Output("Out").front();
    ctx->SetDataType(out_var_name, data_type);
Y
Yu Yang 已提交
69 70 71 72 73 74
  }
};

}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;
75 76 77 78 79 80
REGISTER_OPERATOR(fill, ops::FillOp, ops::FillOpMaker,
                  ops::FillOpVarTypeInference,
                  paddle::framework::EmptyGradOpMaker);
REGISTER_OP_CPU_KERNEL(fill, ops::FillKernel<float>, ops::FillKernel<double>,
                       ops::FillKernel<int64_t>, ops::FillKernel<int>,
                       ops::FillKernel<paddle::platform::float16>);