koboldcpp/examples
Georgi Gerganov cd963fee6a
save-load-state : refactor tests and improve readability (#23196)
* save-load-state : refactor into separate phase functions

- Split monolithic main() into 4 self-contained phase functions, each
  managing its own context/sampler/batch lifecycle
- Each function tokenizes internally using its local ctx instance
- main() is now a clean orchestrator: init -> run phases -> assert results
- Proper resource cleanup on every exit path (return {} on error)

Assisted-by: llama.cpp:local pi

* save-load-state : use params.out_file instead of separate state_file

- Remove state_file parameter from all phase functions
- Each function accesses params.out_file directly
- Initialize params.out_file in main alongside params.prompt

Assisted-by: llama.cpp:local pi

* save-load-state : use smart pointers for ctx and smpl

- Replace raw llama_context* with llama_context_ptr
- Replace raw llama_sampler* with llama_sampler_ptr
- Remove all manual llama_free() and llama_sampler_free() calls
- Keep llama_batch as raw (managed manually with llama_batch_free)

Assisted-by: llama.cpp:local pi

* save-load-state : add local llama_batch_ptr RAII wrapper

- Add llama_batch_ptr struct holding llama_batch by value
- Calls llama_batch_free() in destructor
- Eliminates all manual llama_batch_free() calls

Assisted-by: llama.cpp:local pi

* save-load-state : replace printf/fprintf with logging macros

- Add log.h include
- Replace fprintf(stderr, ...) errors with LOG_ERR
- Replace fprintf(stderr, ...) info with LOG_TRC
- Replace printf output with LOG

Assisted-by: llama.cpp:local pi

* save-load-state : refactor tests to check results inline

Each follow-up phase now accepts an expected result and performs
the comparison internally instead of collecting results in main().

Assisted-by: llama.cpp:local pi

* save-load-state : improve test output readability

Add phase labels, remove redundant run prefixes, and show
PASS after each test.

Assisted-by: llama.cpp:local pi

* pi : add rule about git signing

* save-load-state : simplify llama_batch_ptr

Change get() to return a reference and remove operator*().
Use batch.get() throughout for consistency.

Assisted-by: llama.cpp:local pi

* save-load-state : extract generate_tokens helper

Factor out the repeated token generation loop into a shared
helper function used by all phases.

Assisted-by: llama.cpp:local pi

* save-load-state : update comments to use test terminology

Replace "Phase" with "Test" and list each test's steps
as bullet points.

Assisted-by: llama.cpp:local pi

* save-load-state : rename test functions

Rename to test_baseline, test_state_load, test_seq_cp_host,
test_seq_cp_device. Update comments and logs accordingly.

Assisted-by: llama.cpp:local pi

* pi : add rule to never git push without confirmation

Assisted-by: llama.cpp:local pi

* common : add model_only option to common_init_from_params

Add bool model_only parameter to skip context creation,
sampler init, and context-dependent setup.

Use in save-load-state to initialize only the model,
with each test creating its own context.

Assisted-by: llama.cpp:local pi

---------

Co-authored-by: ggerganov <ggerganov@users.noreply.github.com>
2026-05-19 09:46:34 +03:00
..
batched libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
batched.swift examples : remove references to make in examples [no ci] (#15457) 2025-08-21 06:12:28 +02:00
convert-llama2c-to-ggml libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
debug common: fix missing exports in llama-common (#22340) 2026-04-27 08:06:39 +03:00
deprecation-warning Fix locale-dependent float printing in GGUF metadata (#17331) 2026-03-04 09:30:40 +01:00
diffusion examples: refactor diffusion generation (#22590) 2026-05-04 20:19:30 +08:00
embedding libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
eval-callback common: fix missing exports in llama-common (#22340) 2026-04-27 08:06:39 +03:00
gen-docs spec : refactor params (#22397) 2026-04-28 09:07:33 +03:00
gguf Fix locale-dependent float printing in GGUF metadata (#17331) 2026-03-04 09:30:40 +01:00
gguf-hash Fix locale-dependent float printing in GGUF metadata (#17331) 2026-03-04 09:30:40 +01:00
idle libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
llama-eval llama-eval : add per-task summary stats (#23151) 2026-05-19 09:46:05 +03:00
llama.android android : libcommon -> libllama-common (#22076) 2026-04-18 11:19:40 +02:00
llama.swiftui llama : deprecate llama_kv_self_ API (#14030) 2025-06-06 14:11:15 +03:00
lookahead libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
lookup spec : refactor params (#22397) 2026-04-28 09:07:33 +03:00
model-conversion model-conversion : add causal-convert-mmproj target [no ci] (#22969) 2026-05-12 15:15:40 +02:00
parallel libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
passkey libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
retrieval libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
save-load-state save-load-state : refactor tests and improve readability (#23196) 2026-05-19 09:46:34 +03:00
simple Fix locale-dependent float printing in GGUF metadata (#17331) 2026-03-04 09:30:40 +01:00
simple-chat Fix locale-dependent float printing in GGUF metadata (#17331) 2026-03-04 09:30:40 +01:00
simple-cmake-pkg examples : add missing code block end marker [no ci] (#17756) 2025-12-04 14:17:30 +01:00
speculative spec : fix vocab compat checks in spec example (#22426) 2026-04-30 08:18:25 +03:00
speculative-simple spec : parallel drafting support (#22838) 2026-05-11 19:09:43 +03:00
sycl sycl : fix error when use -mg 1 error (#23140) 2026-05-18 08:11:19 +03:00
training libs : rename libcommon -> libllama-common (#21936) 2026-04-17 11:11:46 +03:00
CMakeLists.txt examples : add debug utility/example (#18464) 2026-01-07 10:42:19 +01:00
convert_legacy_llama.py metadata: Detailed Dataset Authorship Metadata (#8875) 2024-11-13 21:10:38 +11:00
json_schema_pydantic_example.py py : type-check all Python scripts with Pyright (#8341) 2024-07-07 15:04:39 -04:00
json_schema_to_grammar.py ci : switch from pyright to ty (#20826) 2026-03-21 08:54:34 +01:00
llama.vim chore : correct typos [no ci] (#20041) 2026-03-05 08:50:21 +01:00
pydantic_models_to_grammar.py ci : switch from pyright to ty (#20826) 2026-03-21 08:54:34 +01:00
pydantic_models_to_grammar_examples.py llama : move end-user examples to tools directory (#13249) 2025-05-02 20:27:13 +02:00
reason-act.sh scripts : make the shell scripts cross-platform (#14341) 2025-06-30 10:17:18 +02:00
regex_to_grammar.py py : switch to snake_case (#8305) 2024-07-05 07:53:33 +03:00
server-llama2-13B.sh scripts : make the shell scripts cross-platform (#14341) 2025-06-30 10:17:18 +02:00
server_embd.py llama : fix FA when KV cache is not used (i.e. embeddings) (#12825) 2025-04-08 19:54:51 +03:00
ts-type-to-grammar.sh scripts : make the shell scripts cross-platform (#14341) 2025-06-30 10:17:18 +02:00