-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Remove the per-op completion handler in gpu::eval #4048
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| // Copyright © 2023-2024 Apple Inc. | ||
| #include <memory> | ||
|
|
||
| #include "mlx/backend/gpu/eval.h" | ||
| #include "mlx/backend/metal/device.h" | ||
| #include "mlx/backend/metal/utils.h" | ||
|
|
@@ -44,27 +42,23 @@ void eval(array& arr) { | |
| debug_set_primitive_buffer_label(command_buffer, arr.primitive()); | ||
| arr.primitive().eval_gpu(arr.inputs(), outputs); | ||
| } | ||
| std::unordered_set<std::shared_ptr<array::Data>> buffers; | ||
| // Skip a donated output's buffer since holding it blocks allocator reuse. | ||
| const auto& out_data = arr.data_shared_ptr(); | ||
| for (auto& in : arr.inputs()) { | ||
| buffers.insert(in.data_shared_ptr()); | ||
| } | ||
| for (auto& s : arr.siblings()) { | ||
| buffers.insert(s.data_shared_ptr()); | ||
| if (in.data_shared_ptr() != out_data) { | ||
| encoder.hold_buffer(in.data_shared_ptr()); | ||
| } | ||
| } | ||
| // Remove the output if it was donated to by an input | ||
| if (auto it = buffers.find(arr.data_shared_ptr()); it != buffers.end()) { | ||
| buffers.erase(it); | ||
| for (auto& sib : arr.siblings()) { | ||
| if (sib.data_shared_ptr() != out_data) { | ||
| encoder.hold_buffer(sib.data_shared_ptr()); | ||
| } | ||
| } | ||
|
|
||
| if (encoder.needs_commit()) { | ||
| encoder.end_encoding(); | ||
| scheduler::notify_new_task(s); | ||
| encoder.commit([s, buffers = std::move(buffers)]() { | ||
| scheduler::notify_task_completion(s); | ||
| }); | ||
| } else { | ||
| command_buffer->addCompletedHandler( | ||
| [buffers = std::move(buffers)](MTL::CommandBuffer* cbuf) {}); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The downside of this approach is that buffers are now retained until the command encoder is committed, which I think could increase peak memory a lot? Can you check the memory usage in a few popular models? I support reducing the completion handler to have lower overhead but we probably need to add some heuristics. |
||
| encoder.commit([s]() { scheduler::notify_task_completion(s); }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
I think you can just store buffers in the set.