Skip to content

Commit 99e7ece

Browse files
committed
Add support for adaptive_p and infill samplers and optimize the sampler logic.
Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent 736f2a1 commit 99e7ece

3 files changed

Lines changed: 182 additions & 17 deletions

File tree

llama_cpp/_internals.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,19 @@ def add_dry(
12081208
)
12091209
self._add_sampler(sampler)
12101210

1211+
def add_adaptive_p(
1212+
self,
1213+
target: float,
1214+
decay: float,
1215+
seed: int,
1216+
):
1217+
sampler = llama_cpp.llama_sampler_init_adaptive_p(
1218+
target,
1219+
decay,
1220+
seed
1221+
)
1222+
self._add_sampler(sampler)
1223+
12111224
def add_logit_bias(
12121225
self, n_vocab: int, logit_bias: Dict[int, float]
12131226
):
@@ -1221,6 +1234,10 @@ def add_logit_bias(
12211234
sampler = llama_cpp.llama_sampler_init_logit_bias(n_vocab, len(logit_bias), logit_bias_array)
12221235
self._add_sampler(sampler)
12231236

1237+
def add_infill(self, model: LlamaModel):
1238+
sampler = llama_cpp.llama_sampler_init_infill(model.vocab)
1239+
self._add_sampler(sampler)
1240+
12241241
def add_custom(
12251242
self, apply_func: Callable[[llama_cpp.llama_token_data_array], None]
12261243
):

llama_cpp/llama.py

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,10 @@ def _init_sampler(
764764
dry_penalty_last_n:int = 0,
765765
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
766766
penalize_nl: bool = True,
767+
adaptive_target : float = -1.0,
768+
adaptive_decay : float = 0.9,
769+
use_adaptive_p: bool = False,
770+
use_infill: bool = False,
767771
logit_bias: Optional[Dict[int, float]] = None,
768772
logits_processor: Optional[LogitsProcessorList] = None,
769773
grammar: Optional[LlamaGrammar] = None,
@@ -773,18 +777,6 @@ def _init_sampler(
773777
if logit_bias is not None:
774778
sampler.add_logit_bias(self.n_vocab(), logit_bias)
775779

776-
sampler.add_penalties(
777-
n_vocab=self._n_vocab,
778-
special_eos_id=self._token_eos,
779-
linefeed_id=self._token_nl,
780-
penalty_last_n=self.last_n_tokens_size,
781-
penalty_repeat=repeat_penalty,
782-
penalty_freq=frequency_penalty,
783-
penalty_present=presence_penalty,
784-
penalize_nl=penalize_nl,
785-
ignore_eos=False,
786-
)
787-
788780
if grammar is not None:
789781
sampler.add_grammar(self._model, grammar)
790782

@@ -794,6 +786,7 @@ def _init_sampler(
794786
sampler.add_greedy()
795787
else:
796788
if mirostat_mode == 1:
789+
sampler.add_temp(temp)
797790
mirostat_m = 100
798791
sampler.add_mirostat(
799792
self._n_vocab,
@@ -803,6 +796,7 @@ def _init_sampler(
803796
mirostat_m,
804797
)
805798
elif mirostat_mode == 2:
799+
sampler.add_temp(temp)
806800
sampler.add_mirostat_v2(
807801
self._seed,
808802
mirostat_tau,
@@ -811,15 +805,33 @@ def _init_sampler(
811805
else:
812806
n_probs = 0
813807
min_keep = max(1, n_probs)
808+
sampler.add_dry(self._model, dry_multiplier, dry_base, dry_allowed_length, dry_penalty_last_n, dry_seq_breakers)
814809
sampler.add_top_k(top_k)
815-
sampler.add_typical(typical_p, min_keep)
816-
sampler.add_top_n_sigma(top_n_sigma)
817810
sampler.add_top_p(top_p, min_keep)
811+
sampler.add_top_n_sigma(top_n_sigma)
818812
sampler.add_min_p(min_p, min_keep)
819-
sampler.add_temp(temp)
820-
sampler.add_dist(self._seed)
821813
sampler.add_xtc(xtc_probability, xtc_threshold, min_keep, self._seed)
822-
sampler.add_dry(self._model, dry_multiplier, dry_base, dry_allowed_length, dry_penalty_last_n, dry_seq_breakers)
814+
sampler.add_typical(typical_p, min_keep)
815+
sampler.add_temp(temp)
816+
if use_infill:
817+
sampler.add_infill(self._model)
818+
sampler.add_penalties(
819+
n_vocab=self._n_vocab,
820+
special_eos_id=self._token_eos,
821+
linefeed_id=self._token_nl,
822+
penalty_last_n=self.last_n_tokens_size,
823+
penalty_repeat=repeat_penalty,
824+
penalty_freq=frequency_penalty,
825+
penalty_present=presence_penalty,
826+
penalize_nl=penalize_nl,
827+
ignore_eos=False,
828+
)
829+
if use_adaptive_p:
830+
# only if user explicitly included adaptive-p sampler
831+
sampler.add_adaptive_p(adaptive_target,adaptive_decay, self._seed)
832+
else:
833+
# default: sample from distribution
834+
sampler.add_dist(self._seed)
823835
return sampler
824836

825837
def sample(
@@ -844,6 +856,10 @@ def sample(
844856
dry_penalty_last_n:int = 0,
845857
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
846858
penalize_nl: bool = True,
859+
adaptive_target : float = -1.0,
860+
adaptive_decay : float = 0.9,
861+
use_adaptive_p: bool = False,
862+
use_infill: bool = False,
847863
logit_bias: Optional[Dict[int, float]] = None,
848864
logits_processor: Optional[LogitsProcessorList] = None,
849865
grammar: Optional[LlamaGrammar] = None,
@@ -887,6 +903,10 @@ def sample(
887903
dry_penalty_last_n=dry_penalty_last_n,
888904
dry_seq_breakers=dry_seq_breakers,
889905
penalize_nl=penalize_nl,
906+
adaptive_target=adaptive_target,
907+
adaptive_decay=adaptive_decay,
908+
use_adaptive_p=use_adaptive_p,
909+
use_infill=use_infill,
890910
logit_bias=logit_bias,
891911
logits_processor=logits_processor,
892912
grammar=grammar,
@@ -924,6 +944,10 @@ def generate(
924944
dry_penalty_last_n:int = 0,
925945
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
926946
penalize_nl: bool = True,
947+
adaptive_target : float = -1.0,
948+
adaptive_decay : float = 0.9,
949+
use_adaptive_p: bool = False,
950+
use_infill: bool = False,
927951
logit_bias: Optional[Dict[int, float]] = None,
928952
logits_processor: Optional[LogitsProcessorList] = None,
929953
stopping_criteria: Optional[StoppingCriteriaList] = None,
@@ -971,6 +995,10 @@ def generate(
971995
dry_penalty_last_n=dry_penalty_last_n,
972996
dry_seq_breakers=dry_seq_breakers,
973997
penalize_nl=penalize_nl,
998+
adaptive_target=adaptive_target,
999+
adaptive_decay=adaptive_decay,
1000+
use_adaptive_p=use_adaptive_p,
1001+
use_infill=use_infill,
9741002
logit_bias=logit_bias,
9751003
logits_processor=logits_processor,
9761004
grammar=grammar,
@@ -1034,6 +1062,10 @@ def generate(
10341062
logits_processor=logits_processor,
10351063
grammar=grammar,
10361064
penalize_nl=penalize_nl,
1065+
adaptive_target=adaptive_target,
1066+
adaptive_decay=adaptive_decay,
1067+
use_adaptive_p=use_adaptive_p,
1068+
use_infill=use_infill,
10371069
idx=sample_idx,
10381070
)
10391071

@@ -1265,6 +1297,10 @@ def _create_completion(
12651297
dry_allowed_length: int = 2,
12661298
dry_penalty_last_n:int = 0,
12671299
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
1300+
adaptive_target : float = -1.0,
1301+
adaptive_decay : float = 0.9,
1302+
use_adaptive_p: bool = False,
1303+
use_infill: bool = False,
12681304
model: Optional[str] = None,
12691305
stopping_criteria: Optional[StoppingCriteriaList] = None,
12701306
logit_bias: Optional[Dict[int, float]] = None,
@@ -1464,6 +1500,10 @@ def logit_bias_processor(
14641500
presence_penalty=presence_penalty,
14651501
repeat_penalty=repeat_penalty,
14661502
stopping_criteria=stopping_criteria,
1503+
adaptive_target=adaptive_target,
1504+
adaptive_decay=adaptive_decay,
1505+
use_adaptive_p=use_adaptive_p,
1506+
use_infill=use_infill,
14671507
logit_bias=logit_bias,
14681508
logits_processor=logits_processor,
14691509
grammar=grammar,
@@ -1900,6 +1940,10 @@ def create_completion(
19001940
dry_allowed_length: int = 2,
19011941
dry_penalty_last_n:int = 0,
19021942
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
1943+
adaptive_target : float = -1.0,
1944+
adaptive_decay : float = 0.9,
1945+
use_adaptive_p: bool = False,
1946+
use_infill: bool = False,
19031947
model: Optional[str] = None,
19041948
stopping_criteria: Optional[StoppingCriteriaList] = None,
19051949
logit_bias: Optional[Dict[int, float]] = None,
@@ -1936,6 +1980,10 @@ def create_completion(
19361980
dry_allowed_length: Tokens that extend repetition beyond this receive exponentially increasing penalty: multiplier * base ^ (length of repeating sequence before token - allowed length). Default: `2`
19371981
dry_penalty_last_n: How many tokens to scan for repetitions. Default: `0`, where `0` is disabled and `-1` is context size.
19381982
dry_seq_breakers: Specify an array of sequence breakers for DRY sampling. Only a JSON array of strings is accepted. Default: `['\n', ':', '"', '*']`
1983+
adaptive-target: Adaptive-p: select tokens near this probability (valid range 0.0 to 1.0; negative = disabled) (default: %.2f) [(more info)](https://github.com/ggml-org/llama.cpp/pull/17927)
1984+
adaptive-decay: Adaptive-p: decay rate for target adaptation over time. lower values are more reactive, higher values are more stable. (valid range 0.0 to 0.99) (default: %.2f)
1985+
use_adaptive_p: The adaptive_p sampler is only checked when use_adaptive_p is true; the default is to use dist.
1986+
use_infill: Determines whether to activate the specialized fill-in-the-middle sampler that consolidates probabilities of tokens sharing common prefixes to ensure the generated text coherently bridges the gap between the prefix and suffix.
19391987
model: The name to use for the model in the completion object.
19401988
stopping_criteria: A list of stopping criteria to use.
19411989
logit_bias: A logit bias to use.
@@ -1977,6 +2025,10 @@ def create_completion(
19772025
dry_allowed_length=dry_allowed_length,
19782026
dry_penalty_last_n=dry_penalty_last_n,
19792027
dry_seq_breakers=dry_seq_breakers,
2028+
adaptive_target=adaptive_target,
2029+
adaptive_decay=adaptive_decay,
2030+
use_adaptive_p=use_adaptive_p,
2031+
use_infill=use_infill,
19802032
model=model,
19812033
stopping_criteria=stopping_criteria,
19822034
logit_bias=logit_bias,
@@ -2018,6 +2070,10 @@ def __call__(
20182070
dry_allowed_length: int = 2,
20192071
dry_penalty_last_n:int = 0,
20202072
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
2073+
adaptive_target : float = -1.0,
2074+
adaptive_decay : float = 0.9,
2075+
use_adaptive_p: bool = False,
2076+
use_infill: bool = False,
20212077
model: Optional[str] = None,
20222078
stopping_criteria: Optional[StoppingCriteriaList] = None,
20232079
logit_bias: Optional[Dict[int, float]] = None,
@@ -2054,6 +2110,10 @@ def __call__(
20542110
dry_allowed_length: Tokens that extend repetition beyond this receive exponentially increasing penalty: multiplier * base ^ (length of repeating sequence before token - allowed length). Default: `2`
20552111
dry_penalty_last_n: How many tokens to scan for repetitions. Default: `0`, where `0` is disabled and `-1` is context size.
20562112
dry_seq_breakers: Specify an array of sequence breakers for DRY sampling. Only a JSON array of strings is accepted. Default: `['\n', ':', '"', '*']`
2113+
adaptive-target: Adaptive-p: select tokens near this probability (valid range 0.0 to 1.0; negative = disabled) (default: %.2f) [(more info)](https://github.com/ggml-org/llama.cpp/pull/17927)
2114+
adaptive-decay: Adaptive-p: decay rate for target adaptation over time. lower values are more reactive, higher values are more stable. (valid range 0.0 to 0.99) (default: %.2f)
2115+
use_adaptive_p: The adaptive_p sampler is only checked when use_adaptive_p is true; the default is to use dist.
2116+
use_infill: Determines whether to activate the specialized fill-in-the-middle sampler that consolidates probabilities of tokens sharing common prefixes to ensure the generated text coherently bridges the gap between the prefix and suffix.
20572117
model: The name to use for the model in the completion object.
20582118
stopping_criteria: A list of stopping criteria to use.
20592119
logit_bias: A logit bias to use.
@@ -2095,6 +2155,10 @@ def __call__(
20952155
dry_allowed_length=dry_allowed_length,
20962156
dry_penalty_last_n=dry_penalty_last_n,
20972157
dry_seq_breakers=dry_seq_breakers,
2158+
adaptive_target=adaptive_target,
2159+
adaptive_decay=adaptive_decay,
2160+
use_adaptive_p=use_adaptive_p,
2161+
use_infill=use_infill,
20982162
model=model,
20992163
stopping_criteria=stopping_criteria,
21002164
logit_bias=logit_bias,
@@ -2133,6 +2197,10 @@ def create_chat_completion(
21332197
dry_allowed_length: int = 2,
21342198
dry_penalty_last_n:int = 0,
21352199
dry_seq_breakers: list[str] = ["\n", ":", "\"", "*"],
2200+
adaptive_target : float = -1.0,
2201+
adaptive_decay : float = 0.9,
2202+
use_adaptive_p: bool = False,
2203+
use_infill: bool = False,
21362204
model: Optional[str] = None,
21372205
logit_bias: Optional[Dict[int, float]] = None,
21382206
logits_processor: Optional[LogitsProcessorList] = None,
@@ -2174,6 +2242,10 @@ def create_chat_completion(
21742242
dry_allowed_length: Tokens that extend repetition beyond this receive exponentially increasing penalty: multiplier * base ^ (length of repeating sequence before token - allowed length). Default: `2`
21752243
dry_penalty_last_n: How many tokens to scan for repetitions. Default: `0`, where `0` is disabled and `-1` is context size.
21762244
dry_seq_breakers: Specify an array of sequence breakers for DRY sampling. Only a JSON array of strings is accepted. Default: `['\n', ':', '"', '*']`
2245+
adaptive-target: Adaptive-p: select tokens near this probability (valid range 0.0 to 1.0; negative = disabled) (default: %.2f) [(more info)](https://github.com/ggml-org/llama.cpp/pull/17927)
2246+
adaptive-decay: Adaptive-p: decay rate for target adaptation over time. lower values are more reactive, higher values are more stable. (valid range 0.0 to 0.99) (default: %.2f)
2247+
use_adaptive_p: The adaptive_p sampler is only checked when use_adaptive_p is true; the default is to use dist.
2248+
use_infill: Determines whether to activate the specialized fill-in-the-middle sampler that consolidates probabilities of tokens sharing common prefixes to ensure the generated text coherently bridges the gap between the prefix and suffix.
21772249
model: The name to use for the model in the completion object.
21782250
logit_bias: A logit bias to use.
21792251
logits_processor: A list of logits processors to use.
@@ -2220,6 +2292,10 @@ def create_chat_completion(
22202292
dry_allowed_length=dry_allowed_length,
22212293
dry_penalty_last_n=dry_penalty_last_n,
22222294
dry_seq_breakers=dry_seq_breakers,
2295+
adaptive_target=adaptive_target,
2296+
adaptive_decay=adaptive_decay,
2297+
use_adaptive_p=use_adaptive_p,
2298+
use_infill=use_infill,
22232299
model=model,
22242300
logit_bias=logit_bias,
22252301
logits_processor=logits_processor,

0 commit comments

Comments
 (0)