Repository navigation
fix: release weight staging of executed segments under layer split #2530
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: master
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 |
|---|---|---|
|
|
@@ -554,7 +554,8 @@ namespace sd { | |
| const auto params = collect_used_param_tensors(graph); | ||
| const auto& cached_plan = resolve_graph_cut_plan(graph); | ||
| auto manager = manager_; | ||
| const bool can_segment = !is_multi_device() && !sd_backend_is_cpu(session_.backend_) && | ||
| const bool can_segment = (!is_multi_device() || session_.layer_split_enabled_) && | ||
| !sd_backend_is_cpu(session_.backend_) && | ||
| session_.segmented_compute_ && | ||
| cached_plan.valid && cached_plan.has_cuts && cached_plan.segments.size() > 1; | ||
| // An empty workspace request is a lower bound on the full graph's memory demand. | ||
|
|
@@ -608,6 +609,16 @@ namespace sd { | |
| plan.segments.size(), plan.segments.size() == 1 ? "" : "s"); | ||
| } | ||
| SegmentGraphBindings bindings(session_.cut_cache_, plan, graph); | ||
| auto backend_for_tensor = [&](const ggml_tensor* tensor) -> ggml_backend_t { | ||
| auto it = session_.node_assignments_.find(tensor); | ||
| return it == session_.node_assignments_.end() ? session_.backend_ : it->second; | ||
| }; | ||
| auto segment_backend = [&](const ggml_graph_cut::Segment& seg) -> ggml_backend_t { | ||
| if (!session_.node_assignments_.empty() && !seg.internal_node_indices.empty()) { | ||
| return backend_for_tensor(ggml_graph_node(graph, seg.internal_node_indices.front())); | ||
| } | ||
| return session_.backend_; | ||
| }; | ||
| SegmentWeightPipeline weights(manager, session_.backend_, session_.owner_id(), | ||
| graph, plan, params_tensor_set_, | ||
| segmented && session_.prefetch_enabled_); | ||
|
|
@@ -636,7 +647,7 @@ namespace sd { | |
| }; | ||
| auto unused_cut_outputs = session_.cut_cache_.prune(segment.live_cut_names); | ||
| bindings.reset(segment); | ||
| if (!bindings.bind_cached_inputs(segment, task_.description.c_str())) { | ||
| if (!bindings.bind_cached_inputs(segment, task_.description.c_str(), segmented ? segment_backend(segment) : nullptr)) { | ||
| return fail_segment("input binding"); | ||
| } | ||
| ggml_context* segment_context = nullptr; | ||
|
|
@@ -664,13 +675,14 @@ namespace sd { | |
| for (int output_index : segment.output_node_indices) { | ||
| auto tensor = ggml_graph_node(graph, output_index); | ||
| if (ggml_graph_cut::is_graph_cut_tensor(tensor) && segment.future_cut_names.count(tensor->name) && | ||
| GraphCutTensorCache::can_bind_output(session_.backend_, tensor)) { | ||
| GraphCutTensorCache::can_bind_output(segmented ? backend_for_tensor(tensor) : session_.backend_, tensor)) { | ||
| cache_outputs.insert(tensor); | ||
| } | ||
| } | ||
| } | ||
| // Transfer storage from consumed cuts before releasing it to the bounded pool. | ||
| session_.cut_cache_.bind_outputs(cache_outputs, &unused_cut_outputs); | ||
| session_.cut_cache_.bind_outputs(cache_outputs, &unused_cut_outputs, | ||
| segmented ? backend_for_tensor : GraphCutTensorCache::BackendResolver{}); | ||
| unused_cut_outputs.clear(); | ||
| auto measurement = segmented ? measure(segment_graph, &cache_outputs) : full_measurement; | ||
| if (!session_.workspace_.prepare(measurement)) { | ||
|
|
@@ -711,6 +723,12 @@ namespace sd { | |
| if (!weights.segment_start(index, ensure_capacity)) { | ||
| return fail_segment("weight preparation"); | ||
| } | ||
| if (segmented && index > 0 && manager != nullptr) { | ||
| // segment_start released the previous segment's weight pins; now its device | ||
| // staging copies are free to drop before this segment allocates its workspace. | ||
| // Prefetched staging for later segments is pinned out of eviction by the pipeline. | ||
| manager->evict_compute_backend_params(weights.params(index - 1)); | ||
| } | ||
| // Preparing weights can execute LoRA graphs and reclaim an idle workspace. | ||
| if (!session_.workspace_.prepare(measurement)) { | ||
| last_compute_status_ = GGML_STATUS_ALLOC_FAILED; | ||
|
|
@@ -719,7 +737,8 @@ namespace sd { | |
| if (!ensure_capacity()) { | ||
| return fail_segment("workspace capacity check"); | ||
| } | ||
| if (!session_.cut_cache_.bind_outputs(cache_outputs)) { | ||
| if (!session_.cut_cache_.bind_outputs(cache_outputs, nullptr, | ||
| segmented ? backend_for_tensor : GraphCutTensorCache::BackendResolver{})) { | ||
|
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. Charge cut outputs to the backend that allocates them.
This can reject segments that fit. For example, a 512 MiB uncached cut on GPU1 with 256 MiB of primary budget left can make weight preparation reject the segment, even when GPU1 has room. The secondary request omits the same allocation. The segment can then pass its capacity check and fail later at the output cache allocation. Compute the cut estimate per target backend. Use the same resolver as
|
||
| last_compute_status_ = GGML_STATUS_ALLOC_FAILED; | ||
| return fail_segment("output cache allocation"); | ||
| } | ||
|
|
@@ -764,7 +783,8 @@ namespace sd { | |
| } | ||
| auto cache_status = session_.cache_.capture(segment_graph); | ||
| if (cache_status == GGML_STATUS_SUCCESS) { | ||
| cache_status = session_.cut_cache_.capture(graph, segment, task_.description.c_str()); | ||
| cache_status = session_.cut_cache_.capture(graph, segment, task_.description.c_str(), | ||
| segmented ? backend_for_tensor : GraphCutTensorCache::BackendResolver{}); | ||
| } | ||
| if (cache_status != GGML_STATUS_SUCCESS) { | ||
| last_compute_status_ = cache_status; | ||
|
|
@@ -817,11 +837,12 @@ namespace sd { | |
|
|
||
| size_t ExecutionSession::resident_bytes(ggml_backend_t backend) const { | ||
| size_t bytes = workspace_.bytes(backend); | ||
| if (backend == backend_) { | ||
| const auto add = [](size_t a, size_t b) { return b > SIZE_MAX - a ? SIZE_MAX : a + b; }; | ||
| bytes = add(bytes, cache_.resident_bytes(ggml_backend_get_device(backend))); | ||
| bytes = add(bytes, cut_cache_.resident_bytes(ggml_backend_get_device(backend))); | ||
| } | ||
| const auto add = [](size_t a, size_t b) { return b > SIZE_MAX - a ? SIZE_MAX : a + b; }; | ||
| // Cut and runner caches track their own buffer devices; count them per backend so | ||
| // layer-split residency on secondary devices is visible to capacity checks. | ||
| const auto dev = ggml_backend_get_device(backend); | ||
| bytes = add(bytes, cache_.resident_bytes(dev)); | ||
| bytes = add(bytes, cut_cache_.resident_bytes(dev)); | ||
| return bytes; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,28 +177,81 @@ namespace sd { | |
| return cached_tensor(tensors_, name); | ||
| } | ||
|
|
||
| ggml_tensor* GraphCutTensorCache::local_copy(ggml_backend_t consumer, const std::string& name, ggml_tensor* source) { | ||
| auto& per_backend = local_copies_[consumer]; | ||
| auto found = per_backend.find(name); | ||
| if (found != per_backend.end()) { | ||
| return found->second->tensor; | ||
| } | ||
| ggml_status status; | ||
| auto entry = CachedTensor::copy(consumer, name, source, status); | ||
|
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. Reclaim capacity before allocating consumer-local cuts.
If B retains an 8 GiB workspace and has only 1 GiB free, a 2 GiB local cut fails here even when the next segment needs only a 6 GiB workspace. Input binding then aborts before Include local-copy demand in capacity planning before this allocation, or retry the allocation through the existing reclamation path. Keep the allocation-free same-device path.
|
||
| if (entry == nullptr || status != GGML_STATUS_SUCCESS) { | ||
| LOG_ERROR("graph cut cache failed to copy tensor to consumer backend: %s", name.c_str()); | ||
| per_backend.erase(name); | ||
| return nullptr; | ||
| } | ||
| ggml_tensor* result = entry->tensor; | ||
| per_backend[name] = std::move(entry); | ||
| return result; | ||
| } | ||
|
|
||
| ggml_tensor* GraphCutTensorCache::get_local(ggml_backend_t consumer, const std::string& name) { | ||
| ggml_tensor* tensor = get(name); | ||
| if (tensor == nullptr || consumer == nullptr) { | ||
| return tensor; | ||
| } | ||
| ggml_backend_buffer_t buffer = ggml_graph_cut::tensor_buffer(tensor); | ||
| if (buffer == nullptr) { | ||
| return tensor; | ||
| } | ||
| ggml_backend_dev_t source_device = ggml_backend_buft_get_device(ggml_backend_buffer_get_type(buffer)); | ||
| ggml_backend_dev_t consumer_device = ggml_backend_get_device(consumer); | ||
| if (source_device == nullptr || source_device == consumer_device) { | ||
| return tensor; | ||
| } | ||
| return local_copy(consumer, name, tensor); | ||
| } | ||
|
|
||
| void GraphCutTensorCache::invalidate_local_copies(const std::string& name) { | ||
| for (auto& per_backend : local_copies_) { | ||
| per_backend.second.erase(name); | ||
| } | ||
| } | ||
|
|
||
| size_t GraphCutTensorCache::resident_bytes(ggml_backend_dev_t device) const { | ||
| return sd::resident_bytes(tensors_, device); | ||
| size_t bytes = sd::resident_bytes(tensors_, device); | ||
| for (const auto& per_backend : local_copies_) { | ||
| bytes += sd::resident_bytes(per_backend.second, device); | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
| bool GraphCutTensorCache::can_bind_output(ggml_backend_t backend, const ggml_tensor* tensor) { | ||
| return tensor != nullptr && tensor->view_src == nullptr && tensor->op != GGML_OP_NONE && | ||
| ggml_is_contiguous(tensor) && ggml_backend_supports_op(backend, tensor); | ||
| } | ||
|
|
||
| bool GraphCutTensorCache::bind_outputs(const std::unordered_set<const ggml_tensor*>& outputs, CachedTensors* unused_outputs) { | ||
| bool GraphCutTensorCache::bind_outputs(const std::unordered_set<const ggml_tensor*>& outputs, | ||
| CachedTensors* unused_outputs, | ||
| const BackendResolver& backend_for_output) { | ||
| for (const ggml_tensor* output : outputs) { | ||
| auto tensor = const_cast<ggml_tensor*>(output); | ||
| const auto cached = get(tensor->name); | ||
| if (cached != nullptr && tensor->buffer == cached->buffer && tensor->data == cached->data) { | ||
| continue; | ||
| } | ||
| ggml_backend_t target = backend_for_output ? backend_for_output(tensor) : nullptr; | ||
| if (target == nullptr) { | ||
| target = backend_; | ||
| } | ||
| std::unique_ptr<CachedTensor> entry; | ||
| if (unused_outputs != nullptr) { | ||
| auto match = std::find_if(unused_outputs->begin(), unused_outputs->end(), [&](const auto& item) { | ||
| const auto target_device = ggml_backend_get_device(target); | ||
| auto match = std::find_if(unused_outputs->begin(), unused_outputs->end(), [&](const auto& item) { | ||
| const auto* previous = item.second->tensor; | ||
| return tensor->type == previous->type && ggml_are_same_shape(tensor, previous) && | ||
| std::equal(std::begin(tensor->nb), std::end(tensor->nb), std::begin(previous->nb)); | ||
| std::equal(std::begin(tensor->nb), std::end(tensor->nb), std::begin(previous->nb)) && | ||
| ggml_backend_buft_get_device(ggml_backend_buffer_get_type(item.second->buffer)) == target_device; | ||
| }); | ||
| if (match != unused_outputs->end()) { | ||
| entry = std::move(match->second); | ||
|
|
@@ -207,7 +260,7 @@ namespace sd { | |
| } | ||
| } | ||
| if (entry == nullptr) { | ||
| entry = CachedTensor::allocate(backend_, tensor->name, tensor, unused_outputs != nullptr); | ||
| entry = CachedTensor::allocate(target, tensor->name, tensor, unused_outputs != nullptr); | ||
| } | ||
| if (entry == nullptr) { | ||
| if (unused_outputs != nullptr) { | ||
|
|
@@ -218,6 +271,7 @@ namespace sd { | |
| tensor->buffer = entry->tensor->buffer; | ||
| tensor->data = entry->tensor->data; | ||
| tensor->extra = entry->tensor->extra; | ||
| invalidate_local_copies(tensor->name); | ||
| tensors_[tensor->name] = std::move(entry); | ||
| } | ||
| return true; | ||
|
|
@@ -259,12 +313,18 @@ namespace sd { | |
| unused_outputs.insert(tensors_.extract(current)); | ||
| } | ||
| } | ||
| for (auto& per_backend : local_copies_) { | ||
| for (auto it = per_backend.second.begin(); it != per_backend.second.end();) { | ||
| it = keep_names.count(it->first) ? std::next(it) : per_backend.second.erase(it); | ||
| } | ||
| } | ||
| return unused_outputs; | ||
| } | ||
|
|
||
| ggml_status GraphCutTensorCache::capture(ggml_cgraph* graph, | ||
| const ggml_graph_cut::Segment& segment, | ||
| const char* log_desc) { | ||
| const char* log_desc, | ||
| const BackendResolver& backend_for_output) { | ||
| size_t copied_bytes = 0; | ||
| size_t copied_count = 0; | ||
| for (int index : segment.output_node_indices) { | ||
|
|
@@ -277,15 +337,20 @@ namespace sd { | |
| if (cached != nullptr && output->buffer == cached->buffer && output->data == cached->data) { | ||
| continue; | ||
| } | ||
| ggml_backend_t target = backend_for_output ? backend_for_output(output) : nullptr; | ||
| if (target == nullptr) { | ||
| target = backend_; | ||
| } | ||
| ggml_status status; | ||
| auto entry = CachedTensor::copy(backend_, output->name, ggml_graph_cut::cache_source_tensor(output), status); | ||
| auto entry = CachedTensor::copy(target, output->name, ggml_graph_cut::cache_source_tensor(output), status); | ||
| if (entry == nullptr) { | ||
| LOG_ERROR("%s failed to capture graph cut tensor: %s", log_desc, output->name); | ||
| return status; | ||
| } | ||
| const size_t size = ggml_backend_buffer_get_size(entry->buffer); | ||
| copied_bytes = size > SIZE_MAX - copied_bytes ? SIZE_MAX : copied_bytes + size; | ||
| ++copied_count; | ||
| invalidate_local_copies(output->name); | ||
| tensors_[output->name] = std::move(entry); | ||
| } | ||
| ggml_backend_synchronize(backend_); | ||
|
|
||
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.
Measure unbound cut outputs on their producer backend.
GraphExecution::measurereturnssession_.backend_for every tensor inexternal_outputs(src/core/execution_engine.cpp:402-403). In the segmented path,cache_outputsincludes cut outputs whose producer is assigned to another backend. Those outputs are still unbound whenmeasure(segment_graph, &cache_outputs)runs at line 687. Their final buffers are allocated on the producer backend at line 740-741. The workspace measurement therefore places these tensors on the primary backend, while execution places them on the producer backend. Transfer and buffer sizing for the segment can then differ from the actual allocation.Pass the same backend resolver used by the binding at lines 740-741 into
measure. Use it in theexternal_outputsbranch ofexternal_backendinstead ofsession_.backend_. Keep the primary-backend fallback for tensors without an assignment.