Skip to content
Closed
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
128 changes: 116 additions & 12 deletions src/model_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ bool ModelManager::register_param_tensors(Resource& resource, ModelComponent com
for (const auto& state : new_states) {
if (!state->is_int8_weight)
continue;
const auto scale = weight_scales.find(state->name + "_scale");
state->resource->has_int8_weights_ = true;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const auto scale = weight_scales.find(state->name + "_scale");
if (scale != weight_scales.end()) {
state->lora_int8_partner = scale->second;
scale->second->lora_int8_partner = state.get();
Expand Down Expand Up @@ -495,7 +496,9 @@ bool ModelManager::unregister_tensor_states(const std::unordered_set<TensorState
}
return true;
}
std::unordered_set<Resource*> affected_resources;
for (auto* state : target_states) {
affected_resources.insert(state->resource);
if (state->lora_int8_partner) {
state->lora_int8_partner->lora_int8_partner = nullptr;
state->lora_int8_partner = nullptr;
Expand All @@ -514,6 +517,11 @@ bool ModelManager::unregister_tensor_states(const std::unordered_set<TensorState
return s == nullptr || target_states.count(s.get()) > 0;
}),
tensor_states_.end());
for (Resource* resource : affected_resources) {
resource->has_int8_weights_ = std::any_of(tensor_states_.begin(), tensor_states_.end(), [&](const auto& s) {
return s->resource == resource && s->is_int8_weight;
});
}

if (registered_tensor_size != nullptr) {
if (released_size > *registered_tensor_size) {
Expand Down Expand Up @@ -635,7 +643,7 @@ bool ModelManager::load_tensors_to_params_backend(const std::vector<TensorState*
}
state->metadata_validated = true;
}
if (!state->loaded_to_params_backend) {
if (!state->loaded_to_params_backend && !state->staged_to_compute_backend) {
need_load.push_back(state);
}
}
Expand Down Expand Up @@ -724,9 +732,22 @@ bool ModelManager::has_lora_int8_pair(const TensorState& state) {
}

bool ModelManager::needs_lora_staging(const TensorState& state) {
return state.component != ModelComponent::LoRA &&
(has_lora_int8_pair(state) || (!state.resource->shared_weights_key_.empty() &&
state.resource->lora_targets_.count(state.name) != 0));
if (state.component == ModelComponent::LoRA) {
return false;
}
if (has_lora_int8_pair(state)) {
return true;
}
const auto& resource = *state.resource;
if (resource.shared_weights_key_.empty() || resource.lora_targets_.empty()) {
return false;
}
if (resource.lora_targets_.count(state.name) != 0) {
return true;
}
// INT8 storage blocks mix merged and plain weights, so plain weights move too and the source can be dropped.
// Custom CPU operators keep their host weights.
return resource.has_int8_weights_ && state.usage_op != GGML_OP_CUSTOM;
}

bool ModelManager::stage_tensors_to_compute_backend(const std::vector<TensorState*>& states) {
Expand Down Expand Up @@ -761,12 +782,30 @@ bool ModelManager::stage_tensors_to_compute_backend(const std::vector<TensorStat
}

for (const auto& pair : states_by_staging_target) {
ggml_backend_t compute_backend = std::get<0>(pair.first);
ggml_backend_buffer_type_t staging_buft = std::get<1>(pair.first);
const std::vector<TensorState*>& target_states = pair.second;
ggml_backend_t compute_backend = std::get<0>(pair.first);
ggml_backend_buffer_type_t staging_buft = std::get<1>(pair.first);
std::vector<TensorState*> target_states = pair.second;
if (target_states.empty()) {
continue;
}
{
// Stage one storage block at a time so each source can be dropped as soon as it is copied.
std::unordered_map<const TensorState*, size_t> storage_index;
for (size_t i = 0; i < params_storage_blocks_.size(); ++i) {
for (const TensorState* state : params_storage_blocks_[i]->states)
storage_index.emplace(state, i);
}
auto index_of = [&](const TensorState* state) {
const auto* owner = state->lora_int8_partner && !state->is_int8_weight && has_lora_int8_pair(*state)
? state->lora_int8_partner
: state;
const auto found = storage_index.find(owner);
return found == storage_index.end() ? SIZE_MAX : found->second;
};
std::stable_sort(target_states.begin(), target_states.end(), [&](const TensorState* lhs, const TensorState* rhs) {
return index_of(lhs) < index_of(rhs);
});
}

const size_t alignment = ggml_backend_buft_get_alignment(staging_buft);
size_t backend_limit = ggml_backend_buft_get_max_size(staging_buft);
Expand All @@ -775,10 +814,74 @@ bool ModelManager::stage_tensors_to_compute_backend(const std::vector<TensorStat
backend_limit = MAX_RESIDENCY_BLOCK_BYTES;
}

const int64_t t0 = ggml_time_ms();
size_t staged_bytes = 0;
size_t staged_blocks = 0;
auto stage_chunk = [&](const std::vector<TensorState*>& chunk, size_t bytes) -> bool {
const int64_t t0 = ggml_time_ms();
size_t staged_bytes = 0;
size_t staged_blocks = 0;
const Resource* staging_resource = std::get<2>(pair.first);
std::unordered_set<TensorState*> protected_states(states.begin(), states.end());
// A segmented run restages evicted copies from the resident source, so only a whole-model call drops it.
const bool whole_resource = std::all_of(tensor_states_.begin(), tensor_states_.end(), [&](const auto& state) {
return state->resource != staging_resource || state->component == ModelComponent::LoRA ||
state->sources.empty() || should_ignore(*state) || protected_states.count(state.get()) != 0;
});
protect_prefetched_states(protected_states);
// A staged copy replaces its source; the source is read from the file again when the copy is evicted.
auto drop_staged_storage = [&]() {
if (!whole_resource) {
return;
}
for (auto it = params_storage_blocks_.begin(); it != params_storage_blocks_.end();) {
ParamsStorageBlock* storage = it->get();
bool droppable = storage->buffer != nullptr && storage->mmap_tensor_stores.empty();
bool merged = false;
for (const TensorState* state : storage->states) {
if (!droppable)
break;
if (state == nullptr || state->tensor == nullptr || state->sources.empty() ||
state->tensor->view_src != nullptr) {
droppable = false;
} else if (state->staged_to_compute_backend) {
droppable = state->resource == staging_resource && needs_lora_staging(*state);
merged = true;
} else {
// An idle sibling sharing this storage reads the file again when it runs.
droppable = state->resource != staging_resource && state->pin_count == 0 &&
protected_states.count(const_cast<TensorState*>(state)) == 0;
}
}
if (!droppable || !merged) {
++it;
continue;
}
const std::unordered_set<TensorState*> dropped(storage->states.begin(), storage->states.end());
for (auto& staging : compute_staging_blocks_) {
for (auto& [state, original] : staging->staged_tensors) {
if (dropped.count(state) != 0 && original != nullptr) {
original->buffer = nullptr;
original->data = nullptr;
original->extra = nullptr;
}
}
}
for (TensorState* state : storage->states) {
if (!state->staged_to_compute_backend) {
state->tensor->buffer = nullptr;
state->tensor->data = nullptr;
state->tensor->extra = nullptr;
state->applied_lora_epoch = UINT64_MAX;
}
state->loaded_to_params_backend = false;
}
LOG_DEBUG("model manager dropped %.2f MiB of merged weight sources on %s",
ggml_backend_buffer_get_size(storage->buffer) / (1024.0 * 1024.0),
ggml_backend_buffer_name(storage->buffer));
ggml_backend_buffer_free(storage->buffer);
storage->buffer = nullptr;
storage->states.clear();
it = params_storage_blocks_.erase(it);
}
};
auto stage_chunk = [&](const std::vector<TensorState*>& chunk, size_t bytes) -> bool {
if (chunk.empty()) {
return true;
}
Expand Down Expand Up @@ -846,6 +949,7 @@ bool ModelManager::stage_tensors_to_compute_backend(const std::vector<TensorStat
staged_bytes += ggml_backend_buffer_get_size(compute_buffer);
++staged_blocks;
compute_staging_blocks_.push_back(std::move(block));
drop_staged_storage();
return true;
};

Expand Down
1 change: 1 addition & 0 deletions src/model_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ModelManager : public DeviceResidencyManager {
bool warned_split_lora_skip_ = false;
std::vector<LoraSpec> loras_;
std::set<std::string> lora_targets_;
bool has_int8_weights_ = false;
std::string shared_weights_key_;
std::set<ModelLoader::FileId> lora_sources_;
bool applying_loras_ = false;
Expand Down
3 changes: 2 additions & 1 deletion src/model_manager_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ void ModelManager::reuse_shared_weights(std::vector<TensorState*>& need_load) {
}
size_t reused = 0;
need_load.erase(std::remove_if(need_load.begin(), need_load.end(), [&](auto* state) {
// A staged copy without its source keeps its own weights.
if (state->resource->shared_weights_key_.empty() || state->sources.empty() ||
state->tensor->view_src || split_buffer_type_for(*state))
state->staged_to_compute_backend || state->tensor->view_src || split_buffer_type_for(*state))
return false;
const auto found = resident.find({group(*state), state->name});
if (found == resident.end())
Expand Down