assign_value_op.cc 3.5 KB
Newer Older
1
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
X
xuwei06 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/assign_value_op.h"
W
wanghuancoder 已提交
16

17
#include <string>
W
wanghuancoder 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace framework {
class InferShapeContext;
class OpDesc;
template <typename T>
class EmptyGradOpMaker;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
namespace platform {
struct CPUPlace;
}  // namespace platform
}  // namespace paddle
X
xuwei06 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45

namespace paddle {
namespace operators {

class AssignValueOp : public framework::OperatorWithKernel {
 public:
  AssignValueOp(const std::string &type,
                const framework::VariableNameMap &inputs,
                const framework::VariableNameMap &outputs,
                const framework::AttributeMap &attrs)
      : OperatorWithKernel(type, inputs, outputs, attrs) {}

  void InferShape(framework::InferShapeContext *ctx) const override {
46 47 48
    PADDLE_ENFORCE_EQ(
        ctx->HasOutput("Out"), true,
        platform::errors::NotFound("Output(Out) of assign_op is not found."));
X
xuwei06 已提交
49 50 51 52 53
    auto shape = ctx->Attrs().Get<std::vector<int>>("shape");
    ctx->SetOutputDim("Out", framework::make_ddim(shape));
  }

 protected:
54
  framework::OpKernelType GetExpectedKernelType(
X
xuwei06 已提交
55 56
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
57 58
        framework::proto::VarType::Type(ctx.Attr<int>("dtype")),
        ctx.GetPlace());
X
xuwei06 已提交
59 60 61 62 63
  }
};

class AssignValueOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
64
  void Make() override {
X
xuwei06 已提交
65 66 67 68 69
    AddOutput("Out", "(Tensor) Output tensor of assign_value operator.");
    AddAttr<std::vector<int>>("shape",
                              "(vector<int>) "
                              "Shape of values.");
    AddAttr<int>("dtype", "data type of values")
70 71
        .InEnum({framework::proto::VarType::BOOL,
                 framework::proto::VarType::INT32,
72 73
                 framework::proto::VarType::FP32,
                 framework::proto::VarType::INT64});
74 75
    AddAttr<std::vector<int>>("bool_values", "store the bool values")
        .SetDefault({});
76
    AddAttr<std::vector<float>>("fp32_values", "store the float32 values")
X
xuwei06 已提交
77
        .SetDefault({});
78 79 80
    AddAttr<std::vector<int>>("int32_values", "store the int32 values")
        .SetDefault({});
    AddAttr<std::vector<int64_t>>("int64_values", "store the int64 values")
X
xuwei06 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
        .SetDefault({});
    AddComment(R"DOC(
AssignValue operator

$$Out = values$$
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

H
hong 已提交
95 96 97 98
REGISTER_OPERATOR(
    assign_value, ops::AssignValueOp, ops::AssignValueOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
99 100
REGISTER_OP_CPU_KERNEL(assign_value, ops::AssignValueKernel<bool>,
                       ops::AssignValueKernel<int>,
101 102
                       ops::AssignValueKernel<float>,
                       ops::AssignValueKernel<int64_t>);