C++ core (libmemba.so): - include/memba/state.h — C API (state_new/free/save/load/get_size) - src/state.cpp — MEMB file format: magic, version, SHA-256 model_id, CRC-32, opaque llama_state_*_data() blob - src/cli.cpp — minimal demo binary with greedy sampler - CMakeLists.txt + build.sh with llama.cpp submodule, CUDA auto-detect Python SDK (memba): - core.py — file I/O via llama-cpp-python's exposed C functions, unwraps _LlamaContext to access raw context pointer (≥0.3.x) - session.py — high-level Session with auto-save/load, ChatML wrapper for instruct models, raw mode for base models - cli.py — typer-based: chat (REPL), run (one-shot), list, rm, info Examples: - 01_basic_save_load.py, 02_chat_session.py Experiments (throwaway POCs documenting product-direction findings): - recall_poc.py — git log → state → cross-process query - mood_poc.py — batch sentiment trajectory, Mamba vs Transformer - mood_stream_poc.py, mood_batch_poc.py — variants - diag_saveload.py — minimal save/load isolation test - README.md documents the headline finding: save/load is byte-identical, but Falcon-Mamba-7B-Instruct does not retain facts across conversation turns even in-process — limits viable products to single-prompt analysis and persona priming. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
88 lines
3.2 KiB
C
88 lines
3.2 KiB
C
#pragma once
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* ── File format constants ───────────────────────────────────────
|
|
*
|
|
* On-disk layout (little-endian throughout):
|
|
*
|
|
* [ magic : char[4] ] "MEMB"
|
|
* [ version : uint32 ] MEMBA_FILE_VERSION
|
|
* [ model_id : char[64] ] hex-encoded SHA-256 of first 1 KiB of GGUF
|
|
* [ n_ctx : uint32 ] llama_n_ctx() at save time
|
|
* [ llama_ver: uint32 ] reserved (0 for now)
|
|
* [ data_size: uint64 ] byte length of the opaque state blob
|
|
* [ data : uint8[] ] llama_state_get_data() blob
|
|
* [ crc32 : uint32 ] CRC-32 of data[] only
|
|
*/
|
|
#define MEMBA_FILE_MAGIC "MEMB"
|
|
#define MEMBA_FILE_VERSION 1u
|
|
#define MEMBA_MODEL_ID_LEN 64
|
|
|
|
/* ── Error codes ─────────────────────────────────────────────────*/
|
|
#define MEMBA_OK 0
|
|
#define MEMBA_ERR_IO -1 /* file open / read / write failed */
|
|
#define MEMBA_ERR_MAGIC -2 /* bad magic bytes */
|
|
#define MEMBA_ERR_VERSION -3 /* unsupported file version */
|
|
#define MEMBA_ERR_MODEL_ID -4 /* model identity mismatch */
|
|
#define MEMBA_ERR_CRC -5 /* CRC-32 checksum mismatch */
|
|
#define MEMBA_ERR_ALLOC -6 /* memory allocation failed */
|
|
#define MEMBA_ERR_CTX -7 /* null or invalid llama_context */
|
|
|
|
/* Opaque handle — one per llama_context you want to checkpoint. */
|
|
typedef struct memba_state memba_state_t;
|
|
|
|
struct llama_context; /* forward declaration */
|
|
|
|
/**
|
|
* Create a handle that wraps @p ctx.
|
|
*
|
|
* @param ctx Active llama_context. Must remain alive for the handle's
|
|
* entire lifetime.
|
|
* @param model_path Path to the GGUF file. Used to compute the model identity
|
|
* fingerprint embedded in every state file. Pass NULL to
|
|
* skip identity checking (strongly discouraged).
|
|
* @return New handle, or NULL on allocation failure.
|
|
*/
|
|
memba_state_t* memba_state_new(struct llama_context* ctx, const char* model_path);
|
|
|
|
/** Free a handle created by memba_state_new(). Does NOT free ctx. */
|
|
void memba_state_free(memba_state_t* state);
|
|
|
|
/**
|
|
* Serialise the current SSM hidden state to @p path.
|
|
*
|
|
* Thread-safety: the caller must ensure no concurrent llama_decode() calls
|
|
* on @p ctx while this function executes.
|
|
*
|
|
* @return MEMBA_OK on success, negative error code on failure.
|
|
*/
|
|
int memba_state_save(memba_state_t* state, const char* path);
|
|
|
|
/**
|
|
* Restore state from @p path into the wrapped llama_context.
|
|
*
|
|
* Validates magic, version, model_id, and CRC-32 before applying.
|
|
*
|
|
* @return MEMBA_OK on success, negative error code on failure.
|
|
*/
|
|
int memba_state_load(memba_state_t* state, const char* path);
|
|
|
|
/**
|
|
* Return the serialised byte size of the current state (useful for logging).
|
|
* Returns 0 if the handle is NULL or ctx is invalid.
|
|
*/
|
|
size_t memba_state_get_size(memba_state_t* state);
|
|
|
|
/** Human-readable description of an error code. Never returns NULL. */
|
|
const char* memba_error_string(int err);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|