Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host IR: make stream synchronization non blocking #3608

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions csrc/host_ir/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ HostIrEvaluator::HostIrEvaluator(
HostIrEvaluatorParams params)
: container_(std::move(container)),
communicator_(communicator),
params_(params) {
params_(params),
my_device_index_(communicator_ ? communicator_->deviceId() : 0) {
const DeviceIdxType device_index =
(communicator_ != nullptr && communicator_->is_available())
? communicator_->deviceId()
Expand Down Expand Up @@ -274,7 +275,19 @@ void HostIrEvaluator::handle(SetCurrentStream* set_current_stream) {
}

void HostIrEvaluator::handle(Synchronize* synchronize) {
getCUDAStream(synchronize->stream()).synchronize();
cudaStream_t current_stream =
c10::cuda::getCurrentCUDAStream(
static_cast<c10::DeviceIndex>(my_device_index_))
.stream();
cudaStream_t stream_to_sync = getCUDAStream(synchronize->stream()).stream();

cudaEvent_t event = {};
wujingyue marked this conversation as resolved.
Show resolved Hide resolved
NVFUSER_CUDA_RT_SAFE_CALL(
cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
NVFUSER_CUDA_RT_SAFE_CALL(cudaEventRecord(event, stream_to_sync));
NVFUSER_CUDA_RT_SAFE_CALL(
cudaStreamWaitEvent(current_stream, event, cudaEventWaitDefault));
wujingyue marked this conversation as resolved.
Show resolved Hide resolved
NVFUSER_CUDA_RT_SAFE_CALL(cudaEventDestroy(event));
}

void HostIrEvaluator::handle(PostOnStream* post_ir) {
Expand Down
1 change: 1 addition & 0 deletions csrc/host_ir/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class HostIrEvaluator final : public OptOutDispatch {
using StreamKey = std::variant<int64_t, Stream*>;
std::unordered_map<StreamKey, c10::cuda::CUDAStream> streams_;
std::unordered_map<Expr*, c10::intrusive_ptr<c10d::Work>> works_;
const int64_t my_device_index_;
};

} // namespace hir
Expand Down
3 changes: 3 additions & 0 deletions csrc/multidevice/communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/
// clang-format on
#include <cuda_utils.h>
#include <multidevice/communicator.h>
#include <options.h>

Expand Down Expand Up @@ -196,6 +197,8 @@ Communicator::Communicator(
return;
}

NVFUSER_CUDA_RT_SAFE_CALL(cudaSetDevice(local_rank_));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a long time I suspected this was going to be required at some point. Anyway, this is a recommended (if not required) practice. Without it, cudaEventRecord throws cudaErrorInvalidResourceHandle in a multi-GPU scenario.


#ifdef NVFUSER_DISTRIBUTED
c10d::TCPStoreOptions store_opts;
{
Expand Down
Loading