MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / opt_epoch_iter

Method opt_epoch_iter

smallthinker/src/llama-context.cpp:2028–2130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2026}
2027
2028void llama_context::opt_epoch_iter(
2029 ggml_opt_dataset_t dataset,
2030 ggml_opt_result_t result,
2031 const std::vector<llama_token> & tokens,
2032 const std::vector<llama_token> & labels_sparse,
2033 llama_batch & batch,
2034 ggml_opt_epoch_callback callback,
2035 bool train,
2036 int64_t idata_in_loop,
2037 int64_t ndata_in_loop,
2038 int64_t t_loop_start) {
2039 GGML_ASSERT(opt_ctx);
2040 const uint32_t n_ctx = llama_model_n_ctx_train(&model);
2041 const uint32_t n_batch = std::min(this->n_batch(), n_ctx);
2042 const uint32_t n_ubatch = std::min(this->n_ubatch(), n_batch);
2043
2044 llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
2045
2046 kv_self->clear();
2047
2048 for (uint32_t pos_ctx = 0; pos_ctx < n_ctx; pos_ctx += n_batch) {
2049 batch.n_tokens = n_batch;
2050 for (uint32_t pos_batch = 0; pos_batch < n_batch; ++pos_batch) {
2051 batch.token [pos_batch] = tokens[pos_ctx + pos_batch];
2052 batch.pos [pos_batch] = pos_ctx + pos_batch;
2053 batch.n_seq_id[pos_batch] = 1;
2054 batch.seq_id [pos_batch][0] = 0;
2055 batch.logits [pos_batch] = true;
2056 }
2057
2058 const auto n_tokens_all = batch.n_tokens;
2059
2060 n_queued_tokens += n_tokens_all;
2061
2062 // this indicates we are doing pooled embedding, so we ignore batch.logits and output all tokens
2063 const bool embd_pooled = cparams.embeddings && cparams.pooling_type != LLAMA_POOLING_TYPE_NONE;
2064
2065 embd_seq.clear();
2066
2067 int64_t n_outputs_all = n_tokens_all;
2068
2069 auto kv_state = kv_self->init_batch(batch, cparams.n_ubatch, embd_pooled, /* logits_all */ true);
2070 if (!kv_state || kv_state->get_status() != LLAMA_MEMORY_STATUS_SUCCESS) {
2071 LLAMA_LOG_ERROR("%s: could not initialize batch\n", __func__);
2072 break;
2073 }
2074
2075 // reserve output buffer
2076 if (output_reserve(n_outputs_all) < n_outputs_all) {
2077 LLAMA_LOG_ERROR("%s: could not reserve space for batch with %" PRId64 " outputs\n", __func__, n_outputs_all);
2078 GGML_ABORT("TODO: handle this error");
2079 };
2080
2081 uint32_t pos_batch = 0;
2082 do {
2083 const auto & ubatch = kv_state->get_ubatch();
2084
2085 n_outputs = ubatch.n_tokens;

Callers

nothing calls this directly

Calls 15

n_batchMethod · 0.95
n_ubatchMethod · 0.95
llama_model_n_ctx_trainFunction · 0.85
minFunction · 0.85
ggml_graph_sizeFunction · 0.85
ggml_opt_prepare_allocFunction · 0.85
ggml_opt_allocFunction · 0.85
ggml_opt_labelsFunction · 0.85
ggml_opt_evalFunction · 0.85
get_tokensMethod · 0.80
set_inputsMethod · 0.80
ggml_tensor_overheadFunction · 0.50

Tested by

no test coverage detected