stream_analyzer.cc 4.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 22 23 24
// 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.

#include "paddle/fluid/framework/new_executor/stream_analyzer.h"
#include <unordered_set>

namespace paddle {
namespace framework {

/*
 * Parse the var_ids that need to be associated with an event.
 * The caller should guarantee front_op and back_op satisfy the
 * following conditions:
25
 *   1. kQueueSync -> kQueueAsync
26 27 28 29 30 31 32 33
 *   2. kQueueAsync -> kQueueSync
 *
 * For example: matmul(gpu) -> out_var -> memcpy_d2h
 * out_var should be associated with an event.
 */
std::vector<size_t> StreamAnalyzer::ParseEventVarIds(
    const Instruction& cur_instr, const Instruction& next_instr) {
  std::unordered_set<size_t> unique_var_ids;
34
  for (auto& item : cur_instr.Outputs()) {
35 36 37 38
    unique_var_ids.insert(item.second.begin(), item.second.end());
  }

  std::vector<size_t> new_event_var_ids;
39
  for (auto& item : next_instr.Inputs()) {
40
    for (auto var_id : item.second) {
41
      if (unique_var_ids.count(var_id) > 0 &&
42
          next_instr.NoDataTransformVars().count(var_id) == 0) {
43 44 45 46 47 48 49 50 51
        new_event_var_ids.push_back(var_id);
      }
    }
  }
  return new_event_var_ids;
}

void StreamAnalyzer::AssociateInputWithEvents(
    const std::vector<size_t>& new_event_var_id, Instruction* next_instr,
52
    platform::DeviceType waiter_type) {
53 54 55 56 57 58 59
  for (auto var_id : new_event_var_id) {
    if (var_id2event_.count(var_id) == 0) {
      auto device_event = std::make_shared<platform::DeviceEvent>(
          place_, platform::GenerateDeviceEventFlag());
      var_id2event_.emplace(var_id, std::move(device_event));
    }
    // Add events for next_instr.inputs
60
    next_instr->AddInputEvent(var_id, var_id2event_.at(var_id), waiter_type);
61 62 63
  }
}

64 65 66
void StreamAnalyzer::Schedule(const std::vector<size_t>& downstream_ops,
                              std::vector<Instruction>* instructions,
                              size_t op_index) {
67
  auto& cur_instr = instructions->at(op_index);
68
  auto& next_instruction = cur_instr.NextInstructions();
69 70 71
  std::vector<size_t> event_var_ids;
  for (auto next_op_id : downstream_ops) {
    auto& next_instr = instructions->at(next_op_id);
72

73
    if (IsDirectRun(cur_instr, next_instr)) {
74
      next_instruction.AddDirectRun(next_op_id);
75
    } else {
76 77 78 79 80
      // Always insert events between different stream
      auto new_event_var_ids = ParseEventVarIds(cur_instr, next_instr);
      event_var_ids.insert(event_var_ids.end(), new_event_var_ids.begin(),
                           new_event_var_ids.end());

81 82
      auto waiter_type = GetWaiterType(next_instr);
      AssociateInputWithEvents(new_event_var_ids, &next_instr, waiter_type);
83

84
      if (waiter_type == platform::kCPU) {  // GPU -> CPU
85
        next_instruction.AddSyncRun(next_op_id);
86
      } else {  // GPU -> GPU(different stream)
87
        next_instruction.ADDEventRun(next_op_id);
88 89
      }
    }
90 91
  }
  // Create events for these cross-stream vars
92
  VLOG(3) << cur_instr.OpBase()->Type()
93 94
          << " event_var_ids.size: " << event_var_ids.size();
  for (auto var_id : event_var_ids) {
95 96
    cur_instr.AddOutputEvent(var_id, var_id2event_.at(var_id),
                             platform::kCUDA /*not used*/);
97 98 99 100
  }
}

platform::DeviceContext* StreamAnalyzer::ParseDeviceContext(
101 102
    const OpFuncNode& op_func_node) {
  auto& op_type = op_func_node.operator_base_->Type();
103 104 105 106 107 108 109 110 111 112 113 114
  auto* dev_ctx = op_func_node.dev_ctx_;
  if (op_type == interpretercore::kMemcpyH2D) {
    VLOG(3) << "Get dev_ctx from d2h_context_pool_";
    dev_ctx = d2h_ctx_pool_.Get(place_);
  } else if (op_type == interpretercore::kMemcpyD2H) {
    VLOG(3) << "Get dev_ctx from h2d_context_pool_";
    dev_ctx = h2d_ctx_pool_.Get(place_);
  }

  return dev_ctx;
}

115 116 117 118 119 120 121 122 123
/*
 * NOTE(dev): The following cases are considered as directly run:
 *
 *  1. with same dev_ctx_, such as: CPU -> CPU, GPU -> GPU
 *  2. D2H -> CPU
 *  3. CPU -> H2D
 */
bool StreamAnalyzer::IsDirectRun(Instruction& cur_instr,
                                 const Instruction& next_instr) {
124
  return (&cur_instr.DeviceContext() == &next_instr.DeviceContext() ||
125 126 127 128 129
          interpretercore::IsMemcpyD2H(cur_instr) ||
          interpretercore::IsMemcpyH2D(next_instr));
}

platform::DeviceType StreamAnalyzer::GetWaiterType(const Instruction& instr) {
130
  if (instr.KernelType() == OpFuncType::kQueueSync) {
131 132 133 134 135 136
    return platform::kCPU;
  } else {
    return platform::kCUDA;
  }
}

137 138
}  // namespace framework
}  // namespace paddle