fill_constant_batch_size_like_op.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/fill_constant_batch_size_like_op.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

namespace paddle {
namespace operators {

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

  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(
        ctx->HasInput("Input"),
        "Input(Input) of FillConstantBatchSizeLikeOp should not be null.");
    PADDLE_ENFORCE(
        ctx->HasOutput("Out"),
        "Output(Out) of FillConstantBatchSizeLikeOp should not be null.");

    auto &shape = ctx->Attrs().Get<std::vector<int>>("shape");
    PADDLE_ENFORCE_GT(shape.size(), 0);
    std::vector<int64_t> shape_int64(shape.size(), 0);
    std::transform(shape.begin(), shape.end(), shape_int64.begin(),
                   [](int a) { return static_cast<int64_t>(a); });
37
    auto output_dim = framework::make_ddim(shape_int64);
38

39 40 41
    int input_dim_idx = ctx->Attrs().Get<int>("input_dim_idx");
    PADDLE_ENFORCE_GE(input_dim_idx, 0);
    PADDLE_ENFORCE_GT(ctx->GetInputDim("Input").size(), input_dim_idx);
42

43 44 45 46 47 48
    int output_dim_idx = ctx->Attrs().Get<int>("output_dim_idx");
    PADDLE_ENFORCE_GE(output_dim_idx, 0);
    PADDLE_ENFORCE_GT(static_cast<int>(shape.size()), output_dim_idx);

    output_dim[output_dim_idx] = ctx->GetInputDim("Input")[input_dim_idx];
    ctx->SetOutputDim("Out", output_dim);
49 50 51
  }

 protected:
52
  framework::OpKernelType GetExpectedKernelType(
53
      const framework::ExecutionContext &ctx) const override {
Y
Yu Yang 已提交
54
    return framework::OpKernelType(
55
        static_cast<framework::proto::DataType>(ctx.Attr<int>("dtype")),
Y
Yu Yang 已提交
56
        ctx.device_context());
57 58 59 60 61 62
  }
};

class FillConstantBatchSizeLikeOpMaker
    : public framework::OpProtoAndCheckerMaker {
 public:
63
  FillConstantBatchSizeLikeOpMaker(OpProto *proto, OpAttrChecker *op_checker)
64
      : framework::OpProtoAndCheckerMaker(proto, op_checker) {
F
fengjiayi 已提交
65
    AddAttr<int>("dtype",
66 67
                 "(int, default 5 (FP32)) "
                 "Output data type")
68
        .SetDefault(framework::proto::DataType::FP32);
69 70
    AddInput("Input",
             "(Tensor) Tensor "
71
             "whose dim_idx th dimension is used to specify the batch_size");
72 73 74
    AddOutput("Out",
              "(Tensor) Tensor of specified shape will be filled "
              "with the specified value");
75
    AddAttr<std::vector<int>>("shape", "(vector<int>) The shape of the output");
76
    AddAttr<int>("input_dim_idx",
Y
Yang Yang(Tony) 已提交
77
                 "(int, default 0) The index of input's batch size dimension")
78 79
        .SetDefault(0);
    AddAttr<int>("output_dim_idx",
Y
Yang Yang(Tony) 已提交
80
                 "(int, default 0) The index of output's batch size dimension")
81 82 83
        .SetDefault(0);
    AddAttr<float>("value", "(float, default 0) The value to be filled")
        .SetDefault(0.0f);
K
kexinzhao 已提交
84 85 86 87 88 89
    AddComment(R"DOC(
FillConstantBatchSizeLike Operator.

Fill up a variable with specified constant value.

)DOC");
90 91 92 93 94 95
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
96 97 98 99
REGISTER_OPERATOR(fill_constant_batch_size_like,
                  ops::FillConstantBatchSizeLikeOp,
                  paddle::framework::EmptyGradOpMaker,
                  ops::FillConstantBatchSizeLikeOpMaker);
100 101
REGISTER_OP_CPU_KERNEL(
    fill_constant_batch_size_like,
Q
QI JUN 已提交
102 103 104 105 106 107 108
    ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
                                           float>,
    ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
                                           double>,
    ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
                                           int>,
    ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
Y
Yang Yang(Tony) 已提交
109
                                           int64_t>);