From a8b3a3739c668db8f857b8f90d64af4335da9113 Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 6 Aug 2026 20:49:07 +0100 Subject: [PATCH] Add memory breakdown bindings --- llama_cpp/_internals.py | 13 +++++++++++++ llama_cpp/llama_cpp.py | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/llama_cpp/_internals.py b/llama_cpp/_internals.py index b0fe94d01f..3621f85950 100644 --- a/llama_cpp/_internals.py +++ b/llama_cpp/_internals.py @@ -348,6 +348,19 @@ def get_logits(self): def get_logits_ith(self, i: int): return llama_cpp.llama_get_logits_ith(self.ctx, i) + def get_memory_breakdown(self): + count = ctypes.c_size_t() + + entries = llama_cpp.llama_get_memory_breakdown_entries( + self.ctx, + ctypes.byref(count), + ) + + return [ + entries[i] + for i in range(count.value) + ] + def get_embeddings(self): return llama_cpp.llama_get_embeddings(self.ctx) diff --git a/llama_cpp/llama_cpp.py b/llama_cpp/llama_cpp.py index 64399fe317..221712e146 100644 --- a/llama_cpp/llama_cpp.py +++ b/llama_cpp/llama_cpp.py @@ -589,6 +589,14 @@ class llama_token_data_array(ctypes.Structure): llama_token_data_array_p = ctypes.POINTER(llama_token_data_array) +class llama_memory_breakdown_entry(ctypes.Structure): + _fields_ = [ + ("name", ctypes.c_char_p), + ("model", ctypes.c_size_t), + ("context", ctypes.c_size_t), + ("compute", ctypes.c_size_t), + ] + # typedef bool (*llama_progress_callback)(float progress, void * user_data); llama_progress_callback = ctypes.CFUNCTYPE( ctypes.c_bool, ctypes.c_float, ctypes.c_void_p @@ -3250,6 +3258,21 @@ def llama_get_logits_ith( llama_get_logits(ctx) + i*n_vocab""" ... +# // Get memory breakdown by backend buffer type. +# LLAMA_API const llama_memory_breakdown_entry * llama_get_memory_breakdown_entries( +# const struct llama_context * ctx, +# size_t * count); +@ctypes_function( + "llama_get_memory_breakdown_entries", + [llama_context_p_ctypes, ctypes.POINTER(ctypes.c_size_t)], + ctypes.POINTER(llama_memory_breakdown_entry), +) +def llama_get_memory_breakdown_entries( + ctx: llama_context_p, + count: CtypesPointer[ctypes.c_size_t], + /, +) -> CtypesPointer[llama_memory_breakdown_entry]: + ... # // Get all output token embeddings. # // when pooling_type == LLAMA_POOLING_TYPE_NONE or when using a generative model,