save_op.cc 4.8 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
#include "paddle/fluid/operators/save_op.h"

Y
Yu Yang 已提交
17
#include <stdint.h>
18

Y
Yu Yang 已提交
19 20
#include <fstream>
#include <numeric>
21
#include <string>
22
#include <vector>
Y
Yu Yang 已提交
23 24 25

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

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

 protected:
33
  phi::KernelKey GetExpectedKernelType(
34
      const framework::ExecutionContext &ctx) const override {
35
    auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
36
    return phi::KernelKey(data_type, ctx.GetPlace());
37 38 39 40 41 42
  }
};

class SaveOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
43 44
    AddInput("X",
             "(Tensor ) Input phi::DenseTensor and SelectedRows to be saved");
45 46 47
    AddComment(R"DOC(
Save operator

48
This operator will serialize and write phi::DenseTensor / SelectedRows variable to file on disk.
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 76 77
)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;
78
    ctx->InsertVar(LOOKUP_TABLE_PATH, var_type);
79 80 81
  }
};

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

namespace ops = paddle::operators;

87 88 89
REGISTER_OPERATOR(save,
                  ops::SaveOp,
                  ops::SaveOpProtoMaker,
Z
Zeng Jinle 已提交
90
                  ops::SaveOpVarTypeInference);
91 92 93 94 95 96 97 98 99 100 101 102 103

PD_REGISTER_KERNEL(save,
                   CPU,
                   ALL_LAYOUT,
                   ops::SaveKernel,
                   float,
                   double,
                   int,
                   uint8_t,
                   int8_t,
                   int16_t,
                   int64_t,
                   phi::dtype::float16,
104 105 106
                   phi::dtype::bfloat16) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
107 108 109 110 111 112 113 114 115 116 117 118 119

PD_REGISTER_KERNEL(save_sr,
                   CPU,
                   ALL_LAYOUT,
                   ops::SaveSelectedRowsKernel,
                   float,
                   double,
                   int,
                   uint8_t,
                   int8_t,
                   int16_t,
                   int64_t,
                   phi::dtype::float16,
120 121 122
                   phi::dtype::bfloat16) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
123 124 125 126 127 128 129 130 131 132 133 134 135 136

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_KERNEL(save,
                   GPU,
                   ALL_LAYOUT,
                   ops::SaveKernel,
                   float,
                   double,
                   int,
                   uint8_t,
                   int8_t,
                   int16_t,
                   int64_t,
                   phi::dtype::float16,
137 138 139
                   phi::dtype::bfloat16) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
140 141 142 143 144 145 146 147 148 149 150 151 152

PD_REGISTER_KERNEL(save_sr,
                   GPU,
                   ALL_LAYOUT,
                   ops::SaveSelectedRowsKernel,
                   float,
                   double,
                   int,
                   uint8_t,
                   int8_t,
                   int16_t,
                   int64_t,
                   phi::dtype::float16,
153 154 155
                   phi::dtype::bfloat16) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
156
#endif