Skip to content

Commit cac7aa5

Browse files
author
Chris Warren-Smith
committed
LLAMA: nitro: allow for larger files to be loaded
1 parent 14e884c commit cac7aa5

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

llama/llama-sb.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ void Llama::reset() {
144144
}
145145

146146
int Llama::max_tool_result_size() {
147-
// ~3 bytes/tok, 25% of ctx
148-
return (llama_n_ctx(_ctx) / 4) * 3;
147+
// 75% of space available
148+
llama_memory_t mem = llama_get_memory(_ctx);
149+
llama_pos pos_max = llama_memory_seq_pos_max(mem, 0);
150+
int n_ctx = llama_n_ctx(_ctx);
151+
int space_available = n_ctx - (pos_max + 1);
152+
return std::max(256, (space_available * 3) / 4);
149153
}
150154

151155
bool Llama::is_memory_flush() {
@@ -388,6 +392,14 @@ string Llama::all(LlamaIter &iter) {
388392
return out;
389393
}
390394

395+
float Llama::memory_kv_percent() {
396+
llama_memory_t mem = llama_get_memory(_ctx);
397+
llama_pos pos_max = llama_memory_seq_pos_max(mem, 0);
398+
int n_ctx = llama_n_ctx(_ctx);
399+
int kv_used = (pos_max < 0) ? 0 : (int)pos_max + 1;
400+
return 100.0f * kv_used / n_ctx;
401+
}
402+
391403
LlamaMemoryInfo Llama::memory_info() {
392404
LlamaMemoryInfo info = {};
393405

llama/llama-sb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct Llama {
103103

104104
// memory info
105105
LlamaMemoryInfo memory_info();
106+
float memory_kv_percent();
106107

107108
// creates an embedding vector of the given dimension for the given text
108109
bool embed_text(const std::string &text, std::vector<float> &out, int embed_dim);

llama/nitro.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,9 +1979,9 @@ float AgentState::tokens_per_sec() const {
19791979
}
19801980

19811981
std::string AgentState::memory_info_status() const {
1982-
LlamaMemoryInfo m = llama->memory_info();
1983-
auto message = m.kv_percent > 75 ? "(Warning: Approaching limit)" : "";
1984-
return std::format("\n[INFO] KV Cache: {}%{}", m.kv_percent, message);
1982+
float kv_percent = llama->memory_kv_percent();
1983+
auto message = kv_percent > 75 ? "(Warning: Approaching limit)" : "";
1984+
return std::format("\n[INFO] KV Cache: {}%{}", kv_percent, message);
19851985
}
19861986

19871987
std::string AgentState::memory_info_text() const {

0 commit comments

Comments
 (0)