/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 | | // infer the spec types from the GGUF metadata of a draft model; empty if unknown |
18 | | std::vector<enum common_speculative_type> common_speculative_types_from_gguf(const std::string & path); |
19 | | |
20 | | // convert string to type |
21 | | enum common_speculative_type common_speculative_type_from_name(const std::string & name); |
22 | | |
23 | | // convert type to string |
24 | | std::string common_speculative_type_to_str(enum common_speculative_type type); |
25 | | |
26 | | // return the max number of draft tokens based on the speculative parameters |
27 | | int32_t common_speculative_n_max(const common_params_speculative * spec); |
28 | | |
29 | | common_params common_base_params_to_speculative(const common_params & params); |
30 | | |
31 | | struct common_speculative_output_limits { |
32 | | int32_t total; |
33 | | int32_t per_seq; |
34 | | }; |
35 | | |
36 | | // return the output limits needed for speculative decoding |
37 | | common_speculative_output_limits common_speculative_get_output_limits( |
38 | | int32_t n_batch, int32_t n_parallel, int32_t n_draft); |
39 | | |
40 | | common_speculative * common_speculative_init(common_params_speculative & params, uint32_t n_seq); |
41 | | |
42 | | void common_speculative_free(common_speculative * spec); |
43 | | |
44 | | struct common_speculative_draft_params { |
45 | | // this flag is used to chain the drafts through all the available implementations |
46 | | // after the first successful draft from an implementation, we set it |
47 | | // to false to prevent further drafts for that sequence |
48 | | // at the end of the draft() call, all drafting flags will be reset to false |
49 | | bool drafting = false; |
50 | | |
51 | | // overrides individual configurations (-1 disabled) |
52 | | // can be used to constraint the max draft based on the remaining context size |
53 | | int32_t n_max = -1; |
54 | | |
55 | | llama_pos n_past; |
56 | | llama_token id_last; |
57 | | |
58 | | // TODO: remove in the future by keeping track of the prompt from the _begin() call and the consecutive accept calls |
59 | | const llama_tokens * prompt; |
60 | | |
61 | | // the generated draft from the last _draft() call |
62 | | llama_tokens * result; |
63 | | }; |
64 | | |
65 | | common_speculative_draft_params & common_speculative_get_draft_params(common_speculative * spec, llama_seq_id seq_id); |
66 | | |
67 | | // optionally call once at the beginning of a new generation |
68 | | void common_speculative_begin(common_speculative * spec, llama_seq_id seq_id, const llama_tokens & prompt); |
69 | | |
70 | | // process the batch and update the internal state of the speculative context |
71 | | bool common_speculative_process(common_speculative * spec, const llama_batch & batch); |
72 | | |
73 | | // generate drafts for the sequences specified with `common_speculative_get_draft_params` |
74 | | void common_speculative_draft(common_speculative * spec); |
75 | | |
76 | | // informs the speculative context that n_accepted tokens were accepted by the target model |
77 | | void common_speculative_accept(common_speculative * spec, llama_seq_id, uint16_t n_accepted); |
78 | | |
79 | | // (optional) get/set internal state |
80 | | bool common_speculative_get_state(common_speculative * spec, llama_seq_id seq_id, std::vector<uint8_t> & data); |
81 | | void common_speculative_set_state(common_speculative * spec, llama_seq_id seq_id, const std::vector<uint8_t> & data); |
82 | | |
83 | | // print statistics about the speculative decoding |
84 | | void common_speculative_print_stats(const common_speculative * spec); |
85 | | |
86 | | struct common_speculative_deleter { |
87 | 0 | void operator()(common_speculative * s) { common_speculative_free(s); } |
88 | | }; |
89 | | |
90 | | typedef std::unique_ptr<common_speculative, common_speculative_deleter> common_speculative_ptr; |
91 | | |
92 | | struct common_speculative_init_result { |
93 | | common_speculative_init_result(common_params & params, llama_model * model_tgt, llama_context * ctx_tgt); |
94 | | ~common_speculative_init_result(); |
95 | | |
96 | | llama_model * model(); |
97 | | llama_context * context(); |
98 | | |
99 | | private: |
100 | | struct impl; |
101 | | std::unique_ptr<impl> pimpl; |
102 | | }; |
103 | | |
104 | | using common_speculative_init_result_ptr = std::unique_ptr<common_speculative_init_result>; |
105 | | |
106 | | common_speculative_init_result_ptr common_speculative_init_from_params(common_params & params, llama_model * model_tgt, llama_context * ctx_tgt); |