save_op.cc 3.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 <stdint.h>
#include <fstream>
#include <numeric>
18
#include <string>
19
#include <vector>
Y
Yu Yang 已提交
20

21
#include "paddle/fluid/operators/save_op.h"
Y
Yu Yang 已提交
22 23 24

namespace paddle {
namespace operators {
25
class SaveOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
26
 public:
27 28 29 30 31 32 33
  using framework::OperatorWithKernel::OperatorWithKernel;

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

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
34
    auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
T
tangwei12 已提交
35
    return framework::OpKernelType(data_type, ctx.device_context());
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  }
};

class SaveOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor ) Input LoDTensor and SelectedRows to be saved");
    AddComment(R"DOC(
Save operator

This operator will serialize and write LoDTensor / SelectedRows variable to file on disk.
)DOC");
    AddAttr<bool>("overwrite",
                  "(boolean, default true)"
                  "Overwrite the output file if exist")
        .SetDefault(true);
    AddAttr<bool>("save_as_fp16",
                  "(boolean, default false)"
                  "If true, the tensor will be converted to float16 data "
                  "type and then saved. Otherwise, the tensor will be "
                  "directly saved without data type conversion.")
        .SetDefault(false);
    AddAttr<std::string>("file_path",
                         "(string)"
                         "The \"file_path\" where the variable will be saved.")
        .AddCustomChecker(
            [](const std::string &path) { return !path.empty(); });
    AddOutput(LOOKUP_TABLE_PATH,
              "(string)"
              "for pserver: The \"kLookupTablePath\" where checkpoint notify "
              "to save lookup table variables"
              " to directory specified.")
        .AsDispensable();
  }
};

class SaveOpVarTypeInference : public framework::VarTypeInference {
 public:
  void operator()(framework::InferVarTypeContext *ctx) const override {
    auto var_type = framework::proto::VarType::RAW;
76
    ctx->InsertVar(LOOKUP_TABLE_PATH, var_type);
77 78 79
  }
};

T
tangwei12 已提交
80 81
}  // namespace operators
}  // namespace paddle
Y
Yu Yang 已提交
82 83 84

namespace ops = paddle::operators;

85
REGISTER_OPERATOR(save, ops::SaveOp, ops::SaveOpProtoMaker,
Z
Zeng Jinle 已提交
86
                  ops::SaveOpVarTypeInference);
87 88 89 90 91

REGISTER_OP_CPU_KERNEL(
    save, ops::SaveOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::SaveOpKernel<paddle::platform::CPUDeviceContext, double>,
    ops::SaveOpKernel<paddle::platform::CPUDeviceContext, int>,
92
    ops::SaveOpKernel<paddle::platform::CPUDeviceContext, uint8_t>,
93
    ops::SaveOpKernel<paddle::platform::CPUDeviceContext, int8_t>,
94
    ops::SaveOpKernel<paddle::platform::CPUDeviceContext, int16_t>,
95
    ops::SaveOpKernel<paddle::platform::CPUDeviceContext, int64_t>);