load_op.cc 2.4 KB
Newer Older
1
/* Copyright (c) 2016 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 16 17
#include <string>

#include "paddle/fluid/operators/load_op.h"
Y
Yu Yang 已提交
18 19 20 21

namespace paddle {
namespace operators {

22
class LoadOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
23
 public:
24 25 26 27 28 29 30 31
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext *ctx) const override {}

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    framework::OpKernelType kt = framework::OpKernelType(
L
lujun 已提交
32
        framework::proto::VarType::FP32, ctx.GetPlace());
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    return kt;
  }
};

class LoadOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddOutput("Out", "The LoDTensor / SelectedRows need to be loaded");
    AddAttr<bool>(
        "load_as_fp16",
        "If true, the tensor will be first loaded and then "
        "converted to float16 data type. Otherwise, the tensor will be "
        "directly loaded without data type conversion. Default is false.")
        .SetDefault(false);
    AddAttr<std::string>("file_path",
                         R"(Variable will be loaded from "file_path")")
        .AddCustomChecker(
            [](const std::string &path) { return !path.empty(); });
    AddComment(
        "Load operator will load a LoDTensor / SelectedRows variable from disk "
        "file.");
  }
};

Y
Yu Yang 已提交
57 58 59 60 61
}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;

REGISTER_OPERATOR(load, ops::LoadOp, ops::LoadOpProtoMaker);
62 63 64 65 66

REGISTER_OP_CPU_KERNEL(
    load, ops::LoadOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::LoadOpKernel<paddle::platform::CPUDeviceContext, double>,
    ops::LoadOpKernel<paddle::platform::CPUDeviceContext, int>,
L
lujun 已提交
67
    ops::LoadOpKernel<paddle::platform::CPUDeviceContext, int8_t>,
68
    ops::LoadOpKernel<paddle::platform::CPUDeviceContext, int64_t>);