pe_function.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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.

#pragma once

#include <iostream>
18
#include <string>
19 20 21 22
#include <vector>

#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/executor_cache.h"
23 24 25 26
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/variable.h"

27
#include "paddle/fluid/jit/base_function.h"
28
#include "paddle/fluid/jit/function_schema.h"
29
#include "paddle/fluid/jit/function_utils.h"
30 31 32 33 34 35

namespace paddle {
namespace jit {

class PEFunction : public BaseFunction {
 public:
36 37
  PEFunction(const std::shared_ptr<FunctionInfo> &info,
             const Name2VariableMap &params_dict,
38
             const phi::Place &place)
39
      : info_(info), place_(place) {
40
    utils::ShareParamsIntoScope(info_->ParamNames(), params_dict, &scope_);
41
    VLOG(6) << framework::GenScopeTreeDebugInfo(&scope_);
42
    info_->RemoveDescFeedFetch();
43
  }
44

45
  ~PEFunction() noexcept {}
46

47 48 49 50 51 52
  std::vector<Tensor> operator()(const std::vector<Tensor> &inputs) {
    auto dense_tensors = utils::ToDenseTensors(inputs);
    return utils::ToTensors(this->operator()(dense_tensors));
  }

  std::vector<DenseTensor> operator()(const std::vector<DenseTensor> &inputs) {
53 54
    std::string prog_string;
    std::hash<std::string> string_hash;
55

56
    auto &program_desc = info_->ProgramDesc();
57
    // TODO(dev): Serialize is very slow.
58 59 60
    const_cast<framework::ProgramDesc *>(&program_desc)
        ->Proto()
        ->SerializePartialToString(&prog_string);
61
    int64_t program_id = static_cast<int64_t>(string_hash(prog_string));
62

63
    const framework::BlockDesc &global_block = program_desc.Block(0);
64 65 66
    int64_t start_op_index = 0;
    int64_t end_op_index = static_cast<int64_t>(global_block.OpSize());

67
    utils::ShareIntoScope(info_->InputArgNames(), inputs, &scope_);
68 69
    std::vector<std::string> input_var_names = info_->InputArgNames();
    std::vector<std::string> output_var_names = info_->OutputArgNames();
70

71
    if (end_op_index > start_op_index) {
72
      auto cache_info = framework::GetExecutorInfoFromCache(program_desc,
73 74 75 76 77 78
                                                            place_,
                                                            start_op_index,
                                                            end_op_index,
                                                            /*is_grad=*/false,
                                                            program_id,
                                                            &scope_);
79 80 81 82 83 84 85 86 87
      auto &parallel_executor = cache_info.first;
      auto &skip_eager_delete_vars =
          framework::ExecutorInfoCache::Instance().SkipEagerDeleteVars(
              program_id, false);
      if (cache_info.second /*is_new_created*/) {
        parallel_executor->SkipMemoryReuse(/*scope_idx=*/0, input_var_names);
        skip_eager_delete_vars.insert(skip_eager_delete_vars.end(),
                                      output_var_names.begin(),
                                      output_var_names.end());
88

89
        framework::details::ParseSafeEagerDeletionSkipVars(
90
            program_desc,
91 92
            end_op_index,
            output_var_names,
93 94 95 96
            &skip_eager_delete_vars);
      }
      parallel_executor->RunWithoutFetch(skip_eager_delete_vars);
    }
97 98
    std::vector<DenseTensor> res;
    utils::FetchOuts(info_->OutputArgNames(), scope_, &res);
99 100
    return res;
  }
101

102 103
  const std::shared_ptr<FunctionInfo> &Info() const { return info_; }

104 105 106 107
 private:
  std::shared_ptr<FunctionInfo> info_;
  framework::Scope scope_;
  phi::Place place_;
108 109 110 111
};

}  // namespace jit
}  // namespace paddle