File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -1404,6 +1404,14 @@ extern "C" {
14041404 ggml_opt_epoch_callback callback_eval,
14051405 int64_t resume_from_batch);
14061406
1407+ // Optimizer state persistence
1408+ LLAMA_API bool llama_opt_save_state (struct llama_context * lctx, const char * filename);
1409+ LLAMA_API bool llama_opt_load_state (struct llama_context * lctx, const char * filename);
1410+
1411+ // Clean up optimizer context to free memory and allow reinitialization
1412+ // Call this before calling llama_opt_init() again on the same context
1413+ LLAMA_API void llama_opt_cleanup (struct llama_context * lctx);
1414+
14071415 // LoRA training parameters
14081416 enum llama_lora_target_module {
14091417 LLAMA_LORA_TARGET_ATTN_Q = 1 << 0 ,
Original file line number Diff line number Diff line change @@ -2327,6 +2327,9 @@ void llama_context::opt_epoch(
23272327}
23282328
23292329int64_t llama_context::opt_get_iter () {
2330+ if (!opt_ctx) {
2331+ return 0 ; // Return 0 if optimizer not initialized
2332+ }
23302333 return ggml_opt_get_iter (opt_ctx);
23312334}
23322335
@@ -2344,6 +2347,16 @@ bool llama_context::opt_load_state(const char* filename) {
23442347 return ggml_opt_load_state (opt_ctx, filename);
23452348}
23462349
2350+ void llama_context::opt_cleanup () {
2351+ if (opt_ctx) {
2352+ ggml_opt_free (opt_ctx);
2353+ opt_ctx = nullptr ;
2354+ should_load_optimizer_tensors = false ;
2355+ optimizer_tensors_loaded = false ;
2356+ pending_optimizer_checkpoint_path.clear ();
2357+ }
2358+ }
2359+
23472360//
23482361// interface implementation
23492362//
@@ -2903,3 +2916,15 @@ void llama_opt_epoch(
29032916int64_t llama_opt_get_iter (struct llama_context * ctx) {
29042917 return ctx->opt_get_iter ();
29052918}
2919+
2920+ bool llama_opt_save_state (struct llama_context * ctx, const char * filename) {
2921+ return ctx->opt_save_state (filename);
2922+ }
2923+
2924+ bool llama_opt_load_state (struct llama_context * ctx, const char * filename) {
2925+ return ctx->opt_load_state (filename);
2926+ }
2927+
2928+ void llama_opt_cleanup (struct llama_context * ctx) {
2929+ ctx->opt_cleanup ();
2930+ }
Original file line number Diff line number Diff line change @@ -166,6 +166,9 @@ struct llama_context {
166166 // Optimizer state persistence
167167 bool opt_save_state (const char * filename);
168168 bool opt_load_state (const char * filename);
169+
170+ // Clean up optimizer context to free memory and allow reinitialization
171+ void opt_cleanup ();
169172
170173 void opt_epoch_iter (
171174 ggml_opt_dataset_t dataset,
You can’t perform that action at this time.
0 commit comments