2025-01-03 10:18:53 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "llama.h"
|
|
|
|
|
#include "llama-cparams.h"
|
2025-03-13 12:35:44 +02:00
|
|
|
#include "llama-graph.h"
|
2025-01-03 10:18:53 +02:00
|
|
|
#include "llama-adapter.h"
|
|
|
|
|
|
|
|
|
|
#include "ggml-cpp.h"
|
2025-05-12 14:44:49 +02:00
|
|
|
#include "ggml-opt.h"
|
2025-01-03 10:18:53 +02:00
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
struct llama_model;
|
2025-06-13 13:47:55 +03:00
|
|
|
class llama_batch_allocr;
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
class llama_io_read_i;
|
|
|
|
|
class llama_io_write_i;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-09-24 16:53:48 +02:00
|
|
|
// "memory" as in abstract memory for the context
|
2025-06-05 15:29:22 +03:00
|
|
|
struct llama_memory_i;
|
2025-06-21 08:03:46 +03:00
|
|
|
struct llama_memory_context_i;
|
2025-05-31 10:24:04 +03:00
|
|
|
|
2025-09-24 16:53:48 +02:00
|
|
|
// "memory" as in physical memory for a buffer type, in bytes
|
|
|
|
|
struct llama_memory_breakdown_data {
|
|
|
|
|
size_t model = 0; // memory allocated for the model
|
|
|
|
|
size_t context = 0; // memory allocated for the context
|
|
|
|
|
size_t compute = 0; // memory allocated for temporary compute buffers
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-03 10:18:53 +02:00
|
|
|
struct llama_context {
|
2025-03-13 12:35:44 +02:00
|
|
|
// init scheduler and compute buffers, reserve worst-case graphs
|
|
|
|
|
llama_context(
|
|
|
|
|
const llama_model & model,
|
|
|
|
|
llama_context_params params);
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
~llama_context();
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
void synchronize();
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-05-02 17:48:36 +03:00
|
|
|
const llama_model & get_model() const;
|
|
|
|
|
const llama_cparams & get_cparams() const;
|
|
|
|
|
|
|
|
|
|
ggml_backend_sched_t get_sched() const;
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
uint32_t n_ctx() const;
|
|
|
|
|
uint32_t n_ctx_per_seq() const;
|
|
|
|
|
uint32_t n_batch() const;
|
|
|
|
|
uint32_t n_ubatch() const;
|
|
|
|
|
uint32_t n_seq_max() const;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
uint32_t n_threads() const;
|
|
|
|
|
uint32_t n_threads_batch() const;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-06-05 15:29:22 +03:00
|
|
|
llama_memory_t get_memory() const;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-08-21 19:13:45 +03:00
|
|
|
// return true if the memory was updated
|
|
|
|
|
bool memory_update(bool optimize);
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
enum llama_pooling_type pooling_type() const;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
float * get_logits();
|
|
|
|
|
float * get_logits_ith(int32_t i);
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
float * get_embeddings();
|
|
|
|
|
float * get_embeddings_ith(int32_t i);
|
|
|
|
|
float * get_embeddings_seq(llama_seq_id seq_id);
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
void attach_threadpool(
|
|
|
|
|
ggml_threadpool_t threadpool,
|
|
|
|
|
ggml_threadpool_t threadpool_batch);
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
void detach_threadpool();
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
void set_n_threads(int32_t n_threads, int32_t n_threads_batch);
|
|
|
|
|
|
|
|
|
|
void set_abort_callback(bool (*abort_callback)(void * data), void * abort_callback_data);
|
|
|
|
|
|
|
|
|
|
void set_embeddings (bool value);
|
|
|
|
|
void set_causal_attn(bool value);
|
2025-03-14 13:47:05 +01:00
|
|
|
void set_warmup(bool value);
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
void set_adapter_lora(
|
|
|
|
|
llama_adapter_lora * adapter,
|
|
|
|
|
float scale);
|
|
|
|
|
|
|
|
|
|
bool rm_adapter_lora(
|
|
|
|
|
llama_adapter_lora * adapter);
|
|
|
|
|
|
|
|
|
|
void clear_adapter_lora();
|
|
|
|
|
|
|
|
|
|
bool apply_adapter_cvec(
|
|
|
|
|
const float * data,
|
|
|
|
|
size_t len,
|
|
|
|
|
int32_t n_embd,
|
|
|
|
|
int32_t il_start,
|
|
|
|
|
int32_t il_end);
|
|
|
|
|
|
2025-05-31 10:24:04 +03:00
|
|
|
// process a single ubatch with a specific graph type
|
2025-06-21 08:03:46 +03:00
|
|
|
// if memory_context is provided, it will be applied first to the context's memory
|
2025-05-31 10:24:04 +03:00
|
|
|
// ret contains the status of the graph computation
|
|
|
|
|
// returns nullptr only if ret != GGML_STATUS_SUCCESS
|
2025-07-18 08:29:28 +03:00
|
|
|
llm_graph_result * process_ubatch(
|
2025-06-21 08:03:46 +03:00
|
|
|
const llama_ubatch & ubatch,
|
|
|
|
|
llm_graph_type gtype,
|
|
|
|
|
llama_memory_context_i * mctx,
|
|
|
|
|
ggml_status & ret);
|
2025-05-31 10:24:04 +03:00
|
|
|
|
2025-06-13 13:47:55 +03:00
|
|
|
int encode(const llama_batch & batch_inp);
|
|
|
|
|
int decode(const llama_batch & batch_inp);
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// state save/load
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
size_t state_get_size();
|
|
|
|
|
size_t state_get_data( uint8_t * dst, size_t size);
|
|
|
|
|
size_t state_set_data(const uint8_t * src, size_t size);
|
|
|
|
|
|
2025-08-14 14:59:50 +03:00
|
|
|
size_t state_seq_get_size(llama_seq_id seq_id, llama_state_seq_flags flags);
|
|
|
|
|
size_t state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, size_t size, llama_state_seq_flags flags);
|
|
|
|
|
size_t state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size, llama_state_seq_flags flags);
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
bool state_load_file(
|
|
|
|
|
const char * filepath,
|
|
|
|
|
llama_token * tokens_out,
|
|
|
|
|
size_t n_token_capacity,
|
|
|
|
|
size_t * n_token_count_out);
|
|
|
|
|
|
|
|
|
|
bool state_save_file(
|
|
|
|
|
const char * filepath,
|
|
|
|
|
const llama_token * tokens,
|
|
|
|
|
size_t n_token_count);
|
|
|
|
|
|
|
|
|
|
size_t state_seq_load_file(
|
|
|
|
|
llama_seq_id seq_id,
|
|
|
|
|
const char * filepath,
|
|
|
|
|
llama_token * tokens_out,
|
|
|
|
|
size_t n_token_capacity,
|
|
|
|
|
size_t * n_token_count_out);
|
|
|
|
|
|
|
|
|
|
size_t state_seq_save_file(
|
|
|
|
|
llama_seq_id seq_id,
|
|
|
|
|
const char * filepath,
|
|
|
|
|
const llama_token * tokens,
|
|
|
|
|
size_t n_token_count);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// perf
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
llama_perf_context_data perf_get_data() const;
|
|
|
|
|
void perf_reset();
|
|
|
|
|
|
2025-09-24 16:53:48 +02:00
|
|
|
std::map<ggml_backend_buffer_type_t, llama_memory_breakdown_data> memory_breakdown() const;
|
|
|
|
|
|
2025-05-12 14:44:49 +02:00
|
|
|
//
|
|
|
|
|
// training
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void opt_init(struct llama_model * model, struct llama_opt_params lopt_params);
|
|
|
|
|
|
finetune: SGD optimizer, more CLI args (#13873)
* examples/finetune -opt SGD (stochastic gradient descent) memory opt
add unit tested GGML_OPT_OPTIMIZER_SGD to ggml - avoids allocating
m, v tensors.
support finetune.cpp arg -opt SGD (or sgd). (default adamw as before)
llama 3.2-1b-F32 result: observed 11gb gpu ram (41 sec/epoch)
when using SGD instead of 19gb (55 sec/epoch) using adamw.
(wikipedia 100 lines finetune)
(
using the same GPU memory, adamw can only do before OOM 512
batch/context, reaching:
train: [███████▉] data=0000140/0000140 loss=0.02575±0.00099 acc=99.52±0.03% t=00:00:47 ETA=00:00:00
val: [███████▉] data=0000008/0000008 loss=4.76565±0.28810 acc=41.46±0.77% t=00:00:00 ETA=00:00:00
SGD is superior, though it converges slower, with max before OOM 1728
batch/context (esp see the better validation perf):
train: [███████▉] data=0000039/0000039 loss=0.00371±0.00010 acc=99.96±0.01% t=00:00:41 ETA=00:00:00
val: [███████▉] data=0000003/0000003 loss=5.11406±0.76034 acc=48.01±0.69% t=00:00:01 ETA=00:00:00
)
note: when finetuning long enough (or w/ enough -lr),
validation accuracy *eventually* drops ('catastrophic forgetting')
-lr-half (halflife) option useful for SGD to avoid oscillation or
super slow underdamped learning (makes setting -lr more forgiving).
terminal -lr for now is set by lr-halvings i.e. if you want at most
1/8 the inital -lr you set -lr-halvings 3.
note: objective loss not directly comparable between adamw, sgd? -
check perplexity or accuracy or consider relative improvements
for convergence
new finetune args -wd 1e-9 to enable weight decay in sgd or adamw,
and max -epochs N (default 2 as before)
cache (1 - wd*alpha) in 'adamw' opt struct -
no noticeable perf benefit, disabled (still done
for new SGD though)
since opt. memory is pre-allocated, the ggml_opt_get_optimizer_params
would probably be able to change between SGD and AdamW with each epoch
but would need to use adamw for the first (unconfirmed - no cmdline arg
to set such a policy yet)
test-opt checks adamw as before and now sgd (except for a few disabled
tests for sgd only; probably just needs logging values and adding
alternate reference values); tolerance on the 'regression'
test is broader for sgd (so we don't need many more epochs)
* Vulkan: Implement GGML_OP_OPT_STEP_SGD
* tests: Fix OPT_STEP_SGD test-backend-ops
* SGD op param store weight-decay and not 1-alpha*wd
* minor + cosmetic changes
* fix vulkan sgd
* try CI fix
---------
Co-authored-by: 0cc4m <picard12@live.de>
Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
2025-08-14 03:03:57 -07:00
|
|
|
// TODO: more flexible combinations of logical/physical batch size and context size
|
2025-05-12 14:44:49 +02:00
|
|
|
void opt_epoch(
|
|
|
|
|
ggml_opt_dataset_t dataset,
|
|
|
|
|
ggml_opt_result_t result_train,
|
|
|
|
|
ggml_opt_result_t result_eval,
|
|
|
|
|
int64_t idata_split,
|
|
|
|
|
ggml_opt_epoch_callback callback_train,
|
|
|
|
|
ggml_opt_epoch_callback callback_eval);
|
|
|
|
|
|
|
|
|
|
void opt_epoch_iter(
|
|
|
|
|
ggml_opt_dataset_t dataset,
|
|
|
|
|
ggml_opt_result_t result,
|
|
|
|
|
const std::vector<llama_token> & tokens,
|
|
|
|
|
const std::vector<llama_token> & labels_sparse,
|
|
|
|
|
llama_batch & batch,
|
|
|
|
|
ggml_opt_epoch_callback callback,
|
|
|
|
|
bool train,
|
|
|
|
|
int64_t idata_in_loop,
|
|
|
|
|
int64_t ndata_in_loop,
|
|
|
|
|
int64_t t_loop_start);
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
private:
|
|
|
|
|
//
|
|
|
|
|
// output
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Make sure enough space is available for outputs.
|
|
|
|
|
// Returns max number of outputs for which space was reserved.
|
2025-06-13 13:47:55 +03:00
|
|
|
uint32_t output_reserve(int32_t n_outputs);
|
2025-03-13 12:35:44 +02:00
|
|
|
|
2025-07-24 16:31:48 +03:00
|
|
|
void output_reorder();
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
//
|
|
|
|
|
// graph
|
|
|
|
|
//
|
|
|
|
|
|
2025-05-02 17:48:36 +03:00
|
|
|
public:
|
2025-07-17 19:08:33 +03:00
|
|
|
uint32_t graph_max_nodes() const;
|
2025-03-13 12:35:44 +02:00
|
|
|
|
2025-07-17 19:08:33 +03:00
|
|
|
// can reuse the llm_graph_result instance of the context (for example to update a memory module)
|
|
|
|
|
llm_graph_result * get_gf_res_reserve() const;
|
2025-03-13 12:35:44 +02:00
|
|
|
|
2025-05-02 17:48:36 +03:00
|
|
|
// returns the result of ggml_backend_sched_graph_compute_async execution
|
2025-05-31 10:24:04 +03:00
|
|
|
ggml_status graph_compute(ggml_cgraph * gf, bool batched);
|
|
|
|
|
|
|
|
|
|
// reserve a graph with a dummy ubatch of the specified size
|
2025-08-31 06:49:03 -07:00
|
|
|
ggml_cgraph * graph_reserve(uint32_t n_tokens, uint32_t n_seqs, uint32_t n_outputs, const llama_memory_context_i * mctx, bool split_only = false);
|
2025-05-02 17:48:36 +03:00
|
|
|
|
|
|
|
|
private:
|
2025-07-17 19:08:33 +03:00
|
|
|
llm_graph_params graph_params(
|
2025-07-18 08:29:28 +03:00
|
|
|
llm_graph_result * res,
|
2025-07-17 19:08:33 +03:00
|
|
|
const llama_ubatch & ubatch,
|
|
|
|
|
const llama_memory_context_i * mctx,
|
|
|
|
|
llm_graph_type gtype) const;
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
llm_graph_cb graph_get_cb() const;
|
|
|
|
|
|
|
|
|
|
// TODO: read/write lora adapters and cvec
|
|
|
|
|
size_t state_write_data(llama_io_write_i & io);
|
|
|
|
|
size_t state_read_data (llama_io_read_i & io);
|
|
|
|
|
|
2025-08-14 14:59:50 +03:00
|
|
|
size_t state_seq_write_data(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags);
|
|
|
|
|
size_t state_seq_read_data (llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags);
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// members
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
const llama_model & model;
|
|
|
|
|
|
|
|
|
|
llama_cparams cparams;
|
|
|
|
|
llama_adapter_cvec cvec;
|
|
|
|
|
llama_adapter_loras loras;
|
|
|
|
|
|
|
|
|
|
llama_cross cross; // TODO: tmp for handling cross-attention - need something better probably
|
|
|
|
|
|
2025-05-02 17:48:36 +03:00
|
|
|
std::unique_ptr<llama_memory_i> memory;
|
2025-03-13 12:35:44 +02:00
|
|
|
|
|
|
|
|
// decode output (2-dimensional array: [n_outputs][n_vocab])
|
|
|
|
|
size_t logits_size = 0; // capacity (of floats) for logits
|
|
|
|
|
float * logits = nullptr;
|
|
|
|
|
|
2025-01-03 10:18:53 +02:00
|
|
|
// embeddings output (2-dimensional array: [n_outputs][n_embd])
|
|
|
|
|
// populated only when pooling_type == LLAMA_POOLING_TYPE_NONE
|
|
|
|
|
size_t embd_size = 0; // capacity (of floats) for embeddings
|
|
|
|
|
float * embd = nullptr;
|
|
|
|
|
|
|
|
|
|
// sequence embeddings output (map of [n_embd] vectors)
|
|
|
|
|
// populated only when pooling_type != LLAMA_POOLING_TYPE_NONE
|
|
|
|
|
std::map<llama_seq_id, std::vector<float>> embd_seq;
|
|
|
|
|
|
2025-06-13 13:47:55 +03:00
|
|
|
// reuse the batch_allocr to avoid unnecessary memory allocations
|
2025-06-20 10:14:14 +03:00
|
|
|
std::unique_ptr<llama_batch_allocr> balloc;
|
2025-06-13 13:47:55 +03:00
|
|
|
|
|
|
|
|
uint32_t n_outputs = 0; // number of actually-used outputs in the current ubatch or last logical batch
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
std::vector<int32_t> output_ids; // map batch token positions to ids of the logits and embd buffers
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-07-24 16:31:48 +03:00
|
|
|
struct swap_info {
|
|
|
|
|
uint32_t i0;
|
|
|
|
|
uint32_t i1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<swap_info> output_swaps;
|
|
|
|
|
|
2025-01-03 10:18:53 +02:00
|
|
|
ggml_backend_sched_ptr sched;
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
ggml_backend_t backend_cpu = nullptr;
|
|
|
|
|
std::vector<ggml_backend_ptr> backends;
|
|
|
|
|
|
2025-05-12 14:44:49 +02:00
|
|
|
// training
|
|
|
|
|
ggml_opt_context_t opt_ctx = nullptr;
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
ggml_threadpool_t threadpool = nullptr;
|
|
|
|
|
ggml_threadpool_t threadpool_batch = nullptr;
|
|
|
|
|
|
2025-01-03 10:18:53 +02:00
|
|
|
ggml_abort_callback abort_callback = nullptr;
|
|
|
|
|
void * abort_callback_data = nullptr;
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
std::vector<std::pair<ggml_backend_t, ggml_backend_set_n_threads_t>> set_n_threads_fns;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
// buffer types used for the compute buffer of each backend
|
|
|
|
|
std::vector<ggml_backend_t> backend_ptrs;
|
|
|
|
|
std::vector<ggml_backend_buffer_type_t> backend_buft;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-07-17 19:08:33 +03:00
|
|
|
llm_graph_result_ptr gf_res_prev;
|
|
|
|
|
llm_graph_result_ptr gf_res_reserve;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
// host buffer for the model output (logits and embeddings)
|
|
|
|
|
ggml_backend_buffer_ptr buf_output;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
bool has_evaluated_once = false;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-08-01 06:38:12 +03:00
|
|
|
// env: LLAMA_GRAPH_REUSE_DISABLE
|
|
|
|
|
bool graph_reuse_disable = false;
|
|
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
// perf
|
|
|
|
|
mutable int64_t t_start_us = 0;
|
|
|
|
|
mutable int64_t t_load_us = 0;
|
|
|
|
|
mutable int64_t t_p_eval_us = 0;
|
|
|
|
|
mutable int64_t t_eval_us = 0;
|
2025-01-03 10:18:53 +02:00
|
|
|
|
2025-03-13 12:35:44 +02:00
|
|
|
mutable int64_t t_compute_start_us = 0;
|
|
|
|
|
mutable int64_t n_queued_tokens = 0;
|
|
|
|
|
|
|
|
|
|
mutable int32_t n_p_eval = 0; // number of tokens in eval calls for the prompt (with batch size > 1)
|
|
|
|
|
mutable int32_t n_eval = 0; // number of eval calls
|
2025-07-17 19:08:33 +03:00
|
|
|
|
|
|
|
|
mutable int32_t n_reused = 0; // number of times the previous graph was reused
|
2025-03-13 12:35:44 +02:00
|
|
|
};
|