mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-20 21:40:57 +00:00
**Description**: This PR including following works: 1. provide stream and related synchronization abstractions in onnxruntime. 2. enhance onnxruntime's execution planner / executor / memory arena to support execute multiple streams in parallel. 3. deprecate the parallel executor for cpu. 4. deprecate the Fence mechanism. 5. update the cuda / tensorrt EP to support the stream mechanism, support running different request in different cuda stream. **Motivation and Context** - Why is this change required? currently, the execution plan is just a linear list of those primitives, ort will execute them step by step. For any given graph, ORT will serialize it to a fixed execution order. This sequential execution design simplifies most scenarios, but it has the following limitations: 1. it is difficult to enable inter-node parallelization, we have a half-baked parallel executor but it is very difficult to make it work with GPU. 2. The fence mechanism can work with single gpu stream + cpu thread case, but when extend to multiple stream, it is difficult to manage the cross GPU stream synchronizations. 3. our cuda EP rely on the BFCArena to make the memory management work with the GPU async kernels, but current BFCArena is not aware of the streams, so it doesn't behavior correctly when run with multiple streams. This PR enhance our existing execution plan and executor to support multiple stream execution. we use an unified algorithm to mange both single stream and multiple stream scenarios. This PR mainly focus on the infrastructure support for multiple stream execution, that is said, given a valid stream assignment, onnxruntime can execute it correctly. How to generate a good stream assignment for a given model will be in the future PR. Co-authored-by: Cheng Tang <chenta@microsoft.com@orttrainingdev9.d32nl1ml4oruzj4qz3bqlggovf.px.internal.cloudapp.net> Co-authored-by: Cheng Tang <chenta@microsoft.com> Co-authored-by: RandySheriffH <48490400+RandySheriffH@users.noreply.github.com> Co-authored-by: Randy Shuai <rashuai@microsoft.com> Co-authored-by: cao lei <jslhcl@gmail.com> Co-authored-by: Lei Cao <leca@microsoft.com>
116 lines
5.2 KiB
C++
116 lines
5.2 KiB
C++
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
#include "core/framework/execution_steps.h"
|
|
#include "core/framework/sequential_executor.h"
|
|
namespace onnxruntime {
|
|
BarrierStep::BarrierStep(size_t id) : SequentialExecutionPlan::ExecutionStep(),
|
|
barrier_id_{id} {}
|
|
|
|
Status BarrierStep::Execute(StreamExecutionContext& ctx,
|
|
size_t /*stream_idx*/,
|
|
SessionScope& /*session_scope*/,
|
|
const bool& /*terminate_flag*/,
|
|
bool& continue_flag) {
|
|
continue_flag = ctx.DecCountDownBarrier(barrier_id_);
|
|
return Status::OK();
|
|
}
|
|
|
|
std::string BarrierStep::ToString() const {
|
|
return ::onnxruntime::MakeString("Set a barrier with id: ",
|
|
barrier_id_, ", count: ", 2, ".");
|
|
}
|
|
#ifdef ENABLE_TRAINING
|
|
bool BarrierStep::IsBarrier() const { return true; }
|
|
#endif
|
|
WaitOnEPStep::WaitOnEPStep(WaitNotificationFn handle,
|
|
NotificationIndex idx) : SequentialExecutionPlan::ExecutionStep(),
|
|
wait_handle_(handle),
|
|
notification_idx_(idx) {}
|
|
|
|
Status WaitOnEPStep::Execute(StreamExecutionContext& ctx,
|
|
size_t stream_idx,
|
|
SessionScope& /*session_scope*/,
|
|
const bool& /*terminate_flag*/,
|
|
bool& continue_flag) {
|
|
ORT_ENFORCE(wait_handle_, "WaitOnEPStep.wait_handle is null");
|
|
wait_handle_(*ctx.GetDeviceStream(stream_idx), *ctx.GetNotification(notification_idx_));
|
|
// update streams clock status
|
|
if (ctx.GetDeviceStream(stream_idx)) {
|
|
ctx.GetDeviceStream(stream_idx)->UpdateStreamClock(ctx.GetNotification(notification_idx_)->GetStreamSyncTable());
|
|
}
|
|
LOGS(ctx.GetLogger(), INFO) << "stream " << stream_idx << " wait on Notification with id: " << notification_idx_;
|
|
continue_flag = true;
|
|
return Status::OK();
|
|
}
|
|
|
|
std::string WaitOnEPStep::ToString() const {
|
|
return ::onnxruntime::MakeString("WaitOnEPStep: wait on notification with id: ",
|
|
notification_idx_, ". ");
|
|
}
|
|
|
|
LaunchKernelStep::LaunchKernelStep(NodeIndex index) : SequentialExecutionPlan::ExecutionStep(),
|
|
node_index_{index} {}
|
|
|
|
Status LaunchKernelStep::Execute(StreamExecutionContext& ctx,
|
|
size_t stream_idx,
|
|
SessionScope& session_scope,
|
|
const bool& terminate_flag,
|
|
bool& continue_flag) {
|
|
#ifdef ENABLE_TRAINING
|
|
auto* node_to_execute = ctx.GetNodeToExecute();
|
|
if (node_to_execute && node_to_execute->count(node_index_) == 0) {
|
|
continue_flag = true;
|
|
return Status::OK();
|
|
}
|
|
#endif
|
|
onnxruntime::Status status = ExecuteKernel(ctx, node_index_, stream_idx, terminate_flag, session_scope);
|
|
continue_flag = status.IsOK();
|
|
return status;
|
|
}
|
|
|
|
std::string LaunchKernelStep::ToString() const {
|
|
return ::onnxruntime::MakeString("Launch kernel with node id: ", node_index_, ". ");
|
|
}
|
|
|
|
ActivateNotificationStep::ActivateNotificationStep(
|
|
NotificationIndex notification_index) : SequentialExecutionPlan::ExecutionStep(),
|
|
notification_idx_(notification_index) {}
|
|
|
|
Status ActivateNotificationStep::Execute(StreamExecutionContext& ctx,
|
|
size_t stream_idx,
|
|
SessionScope& /*session_scope*/,
|
|
const bool& /*terminate_flag*/,
|
|
bool& continue_flag) {
|
|
if (ctx.GetNotification(notification_idx_)) {
|
|
ctx.GetNotification(notification_idx_)->ActivateAndUpdate();
|
|
}
|
|
LOGS(ctx.GetLogger(), INFO) << "stream " << stream_idx
|
|
<< " activate notification with index " << notification_idx_;
|
|
continue_flag = true;
|
|
return Status::OK();
|
|
}
|
|
|
|
std::string ActivateNotificationStep::ToString() const {
|
|
return ::onnxruntime::MakeString("ActivateNotificationStep: activate notification with id: ",
|
|
notification_idx_, ". ");
|
|
}
|
|
|
|
TriggerDownstreamStep::TriggerDownstreamStep(size_t trigger_point_index) : SequentialExecutionPlan::ExecutionStep(),
|
|
trigger_point_index_(trigger_point_index) {}
|
|
|
|
Status TriggerDownstreamStep::Execute(StreamExecutionContext& ctx,
|
|
size_t /*stream_idx*/,
|
|
SessionScope& session_scope,
|
|
const bool& terminate_flag,
|
|
bool& continue_flag) {
|
|
ScheduleDownstream(ctx, trigger_point_index_, ctx.SingleThreadMode(), terminate_flag, session_scope);
|
|
continue_flag = true;
|
|
return Status::OK();
|
|
}
|
|
|
|
std::string TriggerDownstreamStep::ToString() const {
|
|
return ::onnxruntime::MakeString("TriggerDownstreamStep: trigger downstream of trigger point: ",
|
|
trigger_point_index_, ".");
|
|
}
|
|
|
|
} // namespace onnxruntime
|