-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Make each thread have its own default stream #3281
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
|
|
||
| namespace mlx::core::gpu { | ||
|
|
||
| void init() {} | ||
|
|
||
| void new_stream(Stream) {} | ||
|
|
||
| void eval(array&) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright © 2026 Apple Inc. | ||
|
|
||
| #include "mlx/stream.h" | ||
| #include "mlx/backend/cpu/device_info.h" | ||
| #include "mlx/backend/gpu/device_info.h" | ||
| #include "mlx/scheduler.h" | ||
|
|
||
| #include <array> | ||
| #include <optional> | ||
| #include <shared_mutex> | ||
|
|
||
| namespace mlx::core { | ||
|
|
||
| namespace { | ||
|
|
||
| auto& default_stream_storage(Device d) { | ||
| // Each device has its own default stream in each thread. | ||
| static thread_local auto default_streams = []() { | ||
| std::array<std::vector<std::optional<Stream>>, 2> streams; | ||
| streams[static_cast<size_t>(Device::cpu)].resize(cpu::device_count()); | ||
| streams[static_cast<size_t>(Device::gpu)].resize(gpu::device_count()); | ||
| return streams; | ||
| }(); | ||
| return default_streams[static_cast<size_t>(d.type)].at(d.index); | ||
| } | ||
|
|
||
| auto& all_streams() { | ||
| static std::tuple<std::vector<Stream>, std::shared_mutex> streams_and_mtx; | ||
| return streams_and_mtx; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Stream default_stream(Device d) { | ||
| if (!gpu::is_available() && d.type == Device::gpu) { | ||
| throw std::invalid_argument( | ||
| "[default_stream] Cannot get gpu stream without gpu backend."); | ||
| } | ||
| auto& s = default_stream_storage(d); | ||
| if (!s.has_value()) { | ||
| s = new_stream(d.type); | ||
| } | ||
| return s.value(); | ||
| } | ||
|
|
||
| void set_default_stream(Stream s) { | ||
| if (!gpu::is_available() && s.device == Device::gpu) { | ||
| throw std::invalid_argument( | ||
| "[set_default_stream] Cannot set gpu stream without gpu backend."); | ||
| } | ||
| default_stream_storage(s.device) = s; | ||
| } | ||
|
|
||
| std::vector<Stream> get_streams() { | ||
| auto& [streams, mtx] = all_streams(); | ||
| std::shared_lock lock(mtx); | ||
| return streams; | ||
| } | ||
|
|
||
| Stream new_stream(Device d) { | ||
| if (!gpu::is_available() && d == Device::gpu) { | ||
| throw std::invalid_argument( | ||
| "[new_stream] Cannot make gpu stream without gpu backend."); | ||
| } | ||
| auto& [streams, mtx] = all_streams(); | ||
| std::unique_lock lock(mtx); | ||
| int index = streams.size(); | ||
| auto& s = streams.emplace_back(index, d); | ||
| scheduler::scheduler().new_thread(d.type); | ||
| if (d == Device::gpu) { | ||
| gpu::new_stream(s); | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| } // namespace mlx::core | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well this is not necessarily a bug in this code but
Devicecan have any index weirdly. ie I can makeDevice(Device::gpu, 7)and pass it todefault_streamwhich will access out of bounds memory.So for this code to be correct I think the constructor of
Deviceneeds to check that0 <= index < device_count(dev_type).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking index in
Device::Devicewould break code likeis_available(Device::gpu)which constructs an invalid device first and then checks it.I changed
default_stream_storageto do bound check by usingdefault_streams.at(d.index).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that makes sense. I thought of that but then I thought it is generally weird that we can create arbitrary devices but maybe that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is indeed a weird design, I think the API should be
is_available(DeviceType type, int index)which should be compatible with most C++ code but would require API change in mlx-c, not sure if we should change it @andresy.