serializer.cc 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#include "paddle/fluid/jit/serializer.h"

17 18 19 20 21
#include <set>

#include "paddle/fluid/platform/device_context.h"

#include "paddle/fluid/jit/executor_function.h"
22
#include "paddle/fluid/jit/pe_function.h"
23 24
#include "paddle/fluid/jit/serializer_utils.h"

25 26
DECLARE_string(jit_engine_type);

27 28 29
namespace paddle {
namespace jit {

30 31 32
Layer Deserializer::operator()(const std::string& path,
                               const phi::Place& place) {
  const auto& pdmodel_paths = utils::PdmodelFilePaths(path);
33 34
  // set is ordered
  std::set<std::string> param_names_set;
35 36
  std::vector<std::shared_ptr<FunctionInfo>> infos;
  Name2VariableMap params_dict;
37
  for (auto& it : pdmodel_paths) {
38
    auto& func_name = it.first;
39
    auto program_desc = LoadProgram(it.second);
40

41 42 43
    // TODO(dev): load int/float attrs
    std::vector<std::string> persist_var_names;
    auto all_var_desc = program_desc.Block(0).AllVars();
44
    for (auto* desc_ptr : all_var_desc) {
45
      if (utils::IsPersistable(desc_ptr)) {
46
        persist_var_names.emplace_back(desc_ptr->Name());
47 48 49
      }
    }

50 51 52
    param_names_set.insert(persist_var_names.begin(), persist_var_names.end());
    infos.emplace_back(std::make_shared<FunctionInfo>(
        func_name, persist_var_names, program_desc));
53 54
  }

55 56
  ReadTensorData(path + PDPARAMS_SUFFIX, param_names_set, place, &params_dict);
  // ReadAttributeData();
57

58
  Layer layer = Layer(infos, params_dict, place);
59

60
  for (auto& info : infos) {
61 62 63 64 65 66 67 68 69 70 71 72 73
    if (FLAGS_jit_engine_type == "Executor") {
      VLOG(3) << "Add function type: ExecutorFunction.";
      layer.SetFunction(
          info->FunctionName(),
          utils::MakeFunction<ExecutorFunction>(info, params_dict, place));
    } else if (FLAGS_jit_engine_type == "PE") {
      VLOG(3) << "Add function type: PEFunction.";
      layer.SetFunction(
          info->FunctionName(),
          utils::MakeFunction<PEFunction>(info, params_dict, place));
    } else {
      PD_THROW("Invalid JitLayer funciton type.");
    }
74 75
  }

76
  return layer;
77 78
}

79 80 81
void Deserializer::ReadTensorData(const std::string& file_name,
                                  const std::set<std::string>& var_name,
                                  const phi::Place& place,
82
                                  Name2VariableMap* params_dict) const {
83 84 85
  VLOG(3) << "ReadTensorData from: " << file_name;
  std::ifstream fin(file_name, std::ios::binary);
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
86
  auto& dev_ctx = *pool.Get(place);
87 88 89 90 91 92
  for (auto it = var_name.begin(); it != var_name.end(); it++) {
    VLOG(3) << "load Tensor: " << *it;
    Variable v;
    // TODO(dev): Support framework::Vocab
    DenseTensor* dense_tesnor = v.GetMutable<DenseTensor>();
    framework::DeserializeFromStream(fin, dense_tesnor, dev_ctx);
93
    (*params_dict)[*it] = v;
94 95 96
  }
}

97 98 99
void Deserializer::ReadAttributeData(const std::string& file_path,
                                     Name2VariableMap* attrs_dict) const {}

100
framework::ProgramDesc Deserializer::LoadProgram(const std::string& file_name) {
101
  VLOG(3) << "LoadProgram from: " << file_name;
102 103 104 105 106 107 108 109 110
  std::ifstream fin(file_name, std::ios::in | std::ios::binary);
  fin.seekg(0, std::ios::end);
  std::string buffer(fin.tellg(), ' ');
  fin.seekg(0, std::ios::beg);
  fin.read(&buffer[0], buffer.size());
  fin.close();
  return framework::ProgramDesc(buffer);
}

111
Layer Load(const std::string& file_path, const phi::Place& place) {
112
  auto deserializer = Deserializer();
113
  return deserializer(file_path, place);
114 115 116 117
}

}  // namespace jit
}  // namespace paddle