new_executor_defs.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2021 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 <map>
#include <string>
#include <unordered_map>
#include <vector>

#include "paddle/fluid/framework/operator.h"
22
#include "paddle/fluid/platform/event.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

namespace paddle {
namespace framework {

using OpKernelComputeFunc = std::function<void(const ExecutionContext&)>;
using OpKernelMap =
    std::unordered_map<OpKernelType, OpKernelComputeFunc, OpKernelType::Hash>;

struct OpKernelFunc {
  OpKernelComputeFunc compute_func_;
  OperatorBase* operator_base_;
};

struct VariableMetaInfo {
  int var_ref_count_;
};

struct VariableScope {
  std::vector<Variable*> var_list;
  std::map<std::string, int> name2id;
};

45 46 47 48
struct EventRun {
  explicit EventRun(size_t op_id) : op_id_(op_id) {}
  size_t op_id_;
};
49 50
struct NextInstruction {
  std::vector<size_t> direct_run_;
51 52 53
  std::vector<EventRun> event_wait_run_;
  std::vector<EventRun> synchronize_run_;
  std::vector<size_t> all_next_ops_;
54 55
};

56 57 58 59 60 61 62 63
struct EventInter {
  explicit EventInter(size_t var_id, std::shared_ptr<platform::CudaEvent> event,
                      bool is_sync)
      : var_id_(var_id), event_(event), is_sync_(is_sync) {}
  size_t var_id_;
  std::shared_ptr<platform::CudaEvent> event_;
  bool is_sync_;
};
64 65 66 67 68 69 70

struct InstructionInfo {
  std::vector<size_t> dependecy_count_;
};

struct Instruction {
  OpKernelFunc kernel_func_;
71 72 73
  std::shared_ptr<RuntimeContext> runtime_ctx_;
  std::shared_ptr<RuntimeInferShapeContext> infershape_ctx_;
  std::shared_ptr<ExecutionContext> execution_ctx_;
74 75 76 77 78
  std::map<std::string, std::vector<int>> input_index_;
  std::map<std::string, std::vector<int>> output_index_;

  std::vector<size_t> gc_check_var_list;
  NextInstruction next_instruction_;
79 80 81 82 83 84 85 86 87 88

  std::vector<EventInter> intput_events_;
  std::vector<EventInter> output_events_;

  platform::DeviceContext* dev_ctx_;  // not owned
};

enum class OpFuncType {
  kQueueAsync,  // GPU Kernel or d2h, h2d, send, recv, broadcast
  kQueueSync,   // CPU kernel, block host
89 90 91 92 93 94 95 96
};

struct OpFuncNode {
  // int unsed;
  std::map<std::string, std::vector<int>> input_index;
  std::map<std::string, std::vector<int>> output_index;

  OpKernelComputeFunc kernel_func_;
97 98
  platform::DeviceContext* dev_ctx_;  // not owned
  OpFuncType type_;
99 100 101 102
};

}  // namespace framework
}  // namespace paddle