Coverage Report

Created: 2026-07-16 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/common/speculative.h
Line
Count
Source
1
#pragma once
2
3
#include "llama.h"
4
#include "common.h"
5
6
struct common_speculative;
7
8
// comma separated list the provided types
9
std::string common_speculative_type_name_str(const std::vector<enum common_speculative_type> & types);
10
11
// comma separated list of all types
12
const char * common_speculative_all_types_str();
13
14
// parse user provided types
15
std::vector<enum common_speculative_type> common_speculative_types_from_names(const std::vector<std::string> & names);
16
17
// convert string to type
18
enum common_speculative_type common_speculative_type_from_name(const std::string & name);
19
20
// convert type to string
21
std::string common_speculative_type_to_str(enum common_speculative_type type);
22
23
// return the max number of draft tokens based on the speculative parameters
24
int32_t common_speculative_n_max(const common_params_speculative * spec);
25
26
common_params common_base_params_to_speculative(const common_params & params);
27
28
common_speculative * common_speculative_init(common_params_speculative & params, uint32_t n_seq);
29
30
void common_speculative_free(common_speculative * spec);
31
32
struct common_speculative_draft_params {
33
    // this flag is used to chain the drafts through all the available implementations
34
    // after the first successful draft from an implementation, we set it
35
    //   to false to prevent further drafts for that sequence
36
    // at the end of the draft() call, all drafting flags will be reset to false
37
    bool drafting = false;
38
39
    // overrides individual configurations (-1 disabled)
40
    // can be used to constraint the max draft based on the remaining context size
41
    int32_t n_max = -1;
42
43
    llama_pos   n_past;
44
    llama_token id_last;
45
46
    // TODO: remove in the future by keeping track of the prompt from the _begin() call and the consecutive accept calls
47
    const llama_tokens * prompt;
48
49
    // the generated draft from the last _draft() call
50
    llama_tokens * result;
51
};
52
53
common_speculative_draft_params & common_speculative_get_draft_params(common_speculative * spec, llama_seq_id seq_id);
54
55
// optionally call once at the beginning of a new generation
56
void common_speculative_begin(common_speculative * spec, llama_seq_id seq_id, const llama_tokens & prompt);
57
58
// process the batch and update the internal state of the speculative context
59
bool common_speculative_process(common_speculative * spec, const llama_batch & batch);
60
61
// true if any implementation requires target post-norm embeddings to be extracted
62
bool common_speculative_need_embd(common_speculative * spec);
63
64
// true if any implementation requires target nextn embeddings to be extracted
65
bool common_speculative_need_embd_nextn(common_speculative * spec);
66
67
// generate drafts for the sequences specified with `common_speculative_get_draft_params`
68
void common_speculative_draft(common_speculative * spec);
69
70
// informs the speculative context that n_accepted tokens were accepted by the target model
71
void common_speculative_accept(common_speculative * spec, llama_seq_id, uint16_t n_accepted);
72
73
// (optional) get/set internal state
74
bool common_speculative_get_state(common_speculative * spec, llama_seq_id seq_id, std::vector<uint8_t> & data);
75
void common_speculative_set_state(common_speculative * spec, llama_seq_id seq_id, const std::vector<uint8_t> & data);
76
77
// print statistics about the speculative decoding
78
void common_speculative_print_stats(const common_speculative * spec);
79
80
struct common_speculative_deleter {
81
0
    void operator()(common_speculative * s) { common_speculative_free(s); }
82
};
83
84
typedef std::unique_ptr<common_speculative, common_speculative_deleter> common_speculative_ptr;
85
86
struct common_speculative_init_result {
87
    common_speculative_init_result(common_params & params, llama_model * model_tgt, llama_context * ctx_tgt);
88
    ~common_speculative_init_result();
89
90
    llama_model   * model();
91
    llama_context * context();
92
93
private:
94
    struct impl;
95
    std::unique_ptr<impl> pimpl;
96
};
97
98
using common_speculative_init_result_ptr = std::unique_ptr<common_speculative_init_result>;
99
100
common_speculative_init_result_ptr common_speculative_init_from_params(common_params & params, llama_model * model_tgt, llama_context * ctx_tgt);