Entropic 2.11.1
Local-first agentic inference engine
Loading...
Searching...
No Matches
secondary_model_loader.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
15
16#include "llama_cpp_backend.h"
17
18#include <algorithm>
19
20namespace entropic {
21
22namespace {
23auto logger = entropic::log::get("inference.secondary_loader");
24} // anonymous namespace
25
42 const std::string& role, const ModelConfig& config) {
43 std::lock_guard<std::mutex> lock(slots_mutex_);
44
45 const std::string new_path = config.path.string();
46 auto path_it = slot_paths_.find(role);
47 if (path_it != slot_paths_.end() && path_it->second == new_path) {
48 auto it = slots_.find(role);
49 if (it != slots_.end() && it->second->is_loaded()) {
50 return true;
51 }
52 }
53
54 auto backend = std::make_shared<LlamaCppBackend>();
55 if (!backend->load_and_activate(config)) {
56 logger->error("Failed to activate role '{}' from path: {}",
57 role, new_path);
58 return false;
59 }
60
61 slots_[role] = backend;
62 slot_paths_[role] = new_path;
63 logger->info("Activated secondary role '{}' from {}", role, new_path);
64 return true;
65}
66
74InferenceBackend* SecondaryModelLoader::get(const std::string& role) const {
75 std::lock_guard<std::mutex> lock(slots_mutex_);
76 auto it = slots_.find(role);
77 return (it == slots_.end()) ? nullptr : it->second.get();
78}
79
87std::shared_ptr<InferenceBackend> SecondaryModelLoader::get_shared(
88 const std::string& role) const {
89 std::lock_guard<std::mutex> lock(slots_mutex_);
90 auto it = slots_.find(role);
91 return (it == slots_.end()) ? std::shared_ptr<InferenceBackend>{}
92 : it->second;
93}
94
104bool SecondaryModelLoader::release_role(const std::string& role) {
105 std::lock_guard<std::mutex> lock(slots_mutex_);
106 auto it = slots_.find(role);
107 if (it == slots_.end()) {
108 return false;
109 }
110 if (it->second->is_loaded()) {
111 it->second->unload();
112 }
113 slots_.erase(it);
114 slot_paths_.erase(role);
115 logger->info("Released secondary role '{}'", role);
116 return true;
117}
118
126bool SecondaryModelLoader::is_loaded(const std::string& role) const {
127 std::lock_guard<std::mutex> lock(slots_mutex_);
128 auto it = slots_.find(role);
129 return it != slots_.end() && it->second->is_loaded();
130}
131
139std::vector<std::string> SecondaryModelLoader::loaded_roles() const {
140 std::lock_guard<std::mutex> lock(slots_mutex_);
141 std::vector<std::string> out;
142 out.reserve(slots_.size());
143 for (const auto& [role, backend] : slots_) {
144 if (backend->is_loaded()) {
145 out.push_back(role);
146 }
147 }
148 std::sort(out.begin(), out.end());
149 return out;
150}
151
163 std::lock_guard<std::mutex> lock(slots_mutex_);
164 for (auto& [role, backend] : slots_) {
165 backend->clear_prompt_cache();
166 }
167}
168
180 std::lock_guard<std::mutex> lock(slots_mutex_);
181 for (auto& [role, backend] : slots_) {
182 if (backend->is_loaded()) {
183 backend->unload();
184 }
185 }
186 slots_.clear();
187 slot_paths_.clear();
188}
189
190} // namespace entropic
Concrete base class for inference backends (80% logic).
Definition backend.h:69
std::shared_ptr< InferenceBackend > get_shared(const std::string &role) const
Get the backend for a role as a shared_ptr.
void clear_all_prompt_caches()
Fanout: clear prompt/KV cache on every loaded backend.
bool is_loaded(const std::string &role) const
Check whether a role is currently loaded and active.
std::vector< std::string > loaded_roles() const
Names of all roles with a currently-loaded backend.
bool release_role(const std::string &role)
Unload and drop a role.
InferenceBackend * get(const std::string &role) const
Get the backend for a role.
bool ensure_loaded(const std::string &role, const ModelConfig &config)
Lazily load and activate a model for a role.
LlamaCppBackend — llama.cpp C API integration.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Activate model on GPU (WARM → ACTIVE).
Unified lifecycle for non-primary inference backends.
Model configuration for a single tier.
Definition config.h:154
std::filesystem::path path
Resolved model file path.
Definition config.h:155