Skip to content

Commit 981f211

Browse files
committed
Merge upstream llama.cpp master
Merge ggml-org/llama.cpp master at 63e66fd into BeeLlama v0.3.0, preserving fork-specific CI/CD and DFlash integration.
2 parents d33c4c0 + 63e66fd commit 981f211

128 files changed

Lines changed: 4447 additions & 1177 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/arg.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,9 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
11761176
// we define here to make sure it's included in llama-gen-docs
11771177
if (ex == LLAMA_EXAMPLE_COMPLETION) {
11781178
params.use_jinja = false; // disable jinja by default
1179-
11801179
} else if (ex == LLAMA_EXAMPLE_MTMD) {
11811180
params.use_jinja = false; // disable jinja by default
11821181
params.sampling.temp = 0.2; // lower temp by default for better quality
1183-
11841182
} else if (ex == LLAMA_EXAMPLE_SERVER) {
11851183
params.n_parallel = -1; // auto by default
11861184
}
@@ -1201,7 +1199,6 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
12011199
sampler_type_names.pop_back(); // remove last semicolon
12021200
}
12031201

1204-
12051202
/**
12061203
* filter options by example
12071204
* rules:
@@ -1215,7 +1212,6 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
12151212
}
12161213
};
12171214

1218-
12191215
add_opt(common_arg(
12201216
{"-h", "--help", "--usage"},
12211217
"print usage and exit",
@@ -3181,6 +3177,13 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
31813177
params.timeout_write = value;
31823178
}
31833179
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
3180+
add_opt(common_arg(
3181+
{"--sse-ping-interval"}, "N",
3182+
string_format("server SSE ping interval in seconds (-1 = disabled, default: %d)", params.sse_ping_interval),
3183+
[](common_params & params, int value) {
3184+
params.sse_ping_interval = value;
3185+
}
3186+
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSE_PING_INTERVAL"));
31843187
add_opt(common_arg(
31853188
{"--threads-http"}, "N",
31863189
string_format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
@@ -4477,7 +4480,6 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
44774480
params.sampling.top_k = 0;
44784481
params.sampling.min_p = 0.01f;
44794482
params.use_jinja = true;
4480-
//params.default_template_kwargs["reasoning_effort"] = "\"high\"";
44814483
}
44824484
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
44834485

@@ -4496,7 +4498,6 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
44964498
params.sampling.top_k = 0;
44974499
params.sampling.min_p = 0.01f;
44984500
params.use_jinja = true;
4499-
//params.default_template_kwargs["reasoning_effort"] = "\"high\"";
45004501
}
45014502
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
45024503

common/common.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,6 @@ common_init_result_ptr common_init_from_params(common_params & params, bool mode
13901390
if (params.warmup) {
13911391
LOG_INF("%s: warming up the model with an empty run - please wait ... (--no-warmup to disable)\n", __func__);
13921392

1393-
llama_set_warmup(lctx, true);
1394-
13951393
std::vector<llama_token> tmp;
13961394
llama_token bos = llama_vocab_bos(vocab);
13971395
llama_token eos = llama_vocab_eos(vocab);
@@ -1422,7 +1420,6 @@ common_init_result_ptr common_init_from_params(common_params & params, bool mode
14221420
llama_memory_clear(llama_get_memory(lctx), true);
14231421
llama_synchronize(lctx);
14241422
llama_perf_context_reset(lctx);
1425-
llama_set_warmup(lctx, false);
14261423

14271424
// reset samplers to reset RNG state after warmup to the seeded state
14281425
res->reset_samplers();
@@ -2011,36 +2008,37 @@ bool common_replay_last_token(struct llama_context * ctx, llama_token last_token
20112008

20122009
bool common_prompt_batch_decode(
20132010
struct llama_context * ctx,
2014-
const std::vector<llama_token> & tokens,
2011+
const std::vector<llama_token> & all_tokens,
2012+
int n_new,
20152013
int & n_past,
20162014
int n_batch,
20172015
std::string_view state_path,
20182016
bool save_state) {
2019-
const int n_eval = tokens.size();
2020-
if (n_eval == 0) {
2017+
if (n_new == 0) {
20212018
return true;
20222019
}
2020+
const int offset = all_tokens.size() - n_new;
20232021

2024-
if (save_state && n_eval > 1) {
2025-
const int n_tokens_before_last = n_eval - 1;
2022+
if (save_state && n_new > 1) {
2023+
const int n_tokens_before_last = n_new - 1;
20262024

2027-
GGML_ASSERT(n_eval <= n_batch);
2025+
GGML_ASSERT(n_new <= n_batch);
20282026

20292027
// Decode all but the last token so we can save the memory state before decoding the last token.
20302028
// This is done so we can restore the session state later and replay the last token.
20312029
// Memory implementations in recurrent/hybrid models don't support removing tokens from their
20322030
// memory, so we can't just remove the last token from the memory and replay the last token which
20332031
// is the reason for this logic.
2034-
if (llama_decode(ctx, llama_batch_get_one(const_cast<llama_token*>(tokens.data()), n_tokens_before_last))) {
2032+
if (llama_decode(ctx, llama_batch_get_one(const_cast<llama_token*>(all_tokens.data() + offset), n_tokens_before_last))) {
20352033
LOG_ERR("%s : failed to eval\n", __func__);
20362034
return false;
20372035
}
20382036
n_past += n_tokens_before_last;
20392037

2040-
llama_state_save_file(ctx, state_path.data(), tokens.data(), n_tokens_before_last);
2041-
LOG_INF("saved session before last token to %s, n_tokens = %d\n", state_path.data(), n_tokens_before_last);
2038+
llama_state_save_file(ctx, state_path.data(), all_tokens.data(), all_tokens.size());
2039+
LOG_INF("saved session before last token to %s, n_new = %zu\n", state_path.data(), all_tokens.size());
20422040

2043-
llama_token last_token = tokens.back();
2041+
llama_token last_token = all_tokens.back();
20442042
llama_batch batch = llama_batch_get_one(&last_token, 1);
20452043
int32_t pos = n_past;
20462044
batch.pos = &pos;
@@ -2051,11 +2049,11 @@ bool common_prompt_batch_decode(
20512049
}
20522050
n_past++;
20532051
} else {
2054-
if (llama_decode(ctx, llama_batch_get_one(const_cast<llama_token*>(tokens.data()), n_eval))) {
2052+
if (llama_decode(ctx, llama_batch_get_one(const_cast<llama_token*>(all_tokens.data() + offset), n_new))) {
20552053
LOG_ERR("%s : failed to eval\n", __func__);
20562054
return false;
20572055
}
2058-
n_past += n_eval;
2056+
n_past += n_new;
20592057
}
20602058

20612059
return true;

common/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ struct common_params_sampling {
282282
std::vector<llama_token> reasoning_budget_forced; // forced sequence (message + end tag)
283283
std::string reasoning_budget_message; // message injected before end tag when budget exhausted
284284
bool reasoning_budget_tracking = false; // track reasoning state even with unlimited budget
285+
bool reasoning_control = false; // create the budget sampler on demand so reasoning can be ended at runtime
285286

286287
bool backend_sampling = false;
287288

@@ -742,6 +743,7 @@ struct common_params {
742743
bool reuse_port = false; // allow multiple sockets to bind to the same port
743744
int32_t timeout_read = 3600; // http read timeout in seconds
744745
int32_t timeout_write = timeout_read; // http write timeout in seconds
746+
int32_t sse_ping_interval = 30; // SSE ping interval in seconds
745747
int32_t n_threads_http = -1; // number of threads to process HTTP requests (TODO: support threadpool)
746748
int32_t n_cache_reuse = 0; // min chunk size to reuse from the cache via KV shifting
747749
bool cache_prompt = true; // whether to enable prompt caching
@@ -1079,7 +1081,8 @@ void common_batch_add(
10791081
// tokens from memory, so this approach works across all model architectures.
10801082
bool common_prompt_batch_decode(
10811083
struct llama_context * ctx,
1082-
const std::vector<llama_token> & embd,
1084+
const std::vector<llama_token> & all_tokens,
1085+
int n_new,
10831086
int & n_past,
10841087
int n_batch,
10851088
std::string_view state_path,

common/sampling.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,13 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, st
294294
}
295295
}
296296

297-
// reasoning budget sampler. Tracking mode observes reasoning state even when the token budget is unlimited.
297+
// reasoning budget sampler. Tracking/control modes observe reasoning state even when the token budget is unlimited.
298298
const bool need_rbudget_for_grammar =
299299
params.grammar_lazy ||
300300
(grmr && params.grammar.type == COMMON_GRAMMAR_TYPE_OUTPUT_FORMAT);
301301
if (has_reasoning_tags &&
302-
(need_rbudget_for_grammar || params.reasoning_budget_tokens >= 0 || params.reasoning_budget_tracking)) {
302+
(need_rbudget_for_grammar || params.reasoning_budget_tokens >= 0 ||
303+
params.reasoning_budget_tracking || params.reasoning_control)) {
303304
rbudget = common_reasoning_budget_init(
304305
vocab,
305306
params.reasoning_budget_start,

common/speculative.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,6 +3821,46 @@ static uint32_t common_get_enabled_speculative_configs(const std::vector<common_
38213821
return result;
38223822
}
38233823

3824+
int32_t common_speculative_n_max(const common_params_speculative * spec) {
3825+
int32_t n_max = 0;
3826+
3827+
for (const auto type : spec->types) {
3828+
switch (type) {
3829+
case COMMON_SPECULATIVE_TYPE_DRAFT_SIMPLE:
3830+
case COMMON_SPECULATIVE_TYPE_DRAFT_EAGLE3:
3831+
case COMMON_SPECULATIVE_TYPE_DRAFT_MTP:
3832+
n_max = std::max(n_max, std::max(0, spec->draft.n_max));
3833+
break;
3834+
case COMMON_SPECULATIVE_TYPE_NGRAM_SIMPLE:
3835+
n_max = std::max(n_max, (int32_t) spec->ngram_simple.size_m);
3836+
break;
3837+
case COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K:
3838+
n_max = std::max(n_max, (int32_t) spec->ngram_map_k.size_m);
3839+
break;
3840+
case COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K4V:
3841+
n_max = std::max(n_max, (int32_t) spec->ngram_map_k4v.size_m);
3842+
break;
3843+
case COMMON_SPECULATIVE_TYPE_NGRAM_MOD:
3844+
n_max = std::max(n_max, std::max(0, spec->ngram_mod.n_max));
3845+
break;
3846+
case COMMON_SPECULATIVE_TYPE_NGRAM_CACHE:
3847+
n_max = std::max(n_max, (int32_t) 8);
3848+
break;
3849+
case COMMON_SPECULATIVE_TYPE_SUFFIX:
3850+
case COMMON_SPECULATIVE_TYPE_COPYSPEC:
3851+
case COMMON_SPECULATIVE_TYPE_RECYCLE:
3852+
case COMMON_SPECULATIVE_TYPE_DFLASH:
3853+
n_max = std::max(n_max, std::max(0, spec->n_max));
3854+
break;
3855+
case COMMON_SPECULATIVE_TYPE_NONE:
3856+
case COMMON_SPECULATIVE_TYPE_COUNT:
3857+
break;
3858+
}
3859+
}
3860+
3861+
return n_max;
3862+
}
3863+
38243864
// initialization of the speculative decoding system
38253865
//
38263866
common_speculative * common_speculative_init(common_params_speculative & params, uint32_t n_seq) {
@@ -3829,8 +3869,6 @@ common_speculative * common_speculative_init(common_params_speculative & params,
38293869
{
38303870
uint32_t enabled_configs = common_get_enabled_speculative_configs(params.types);
38313871

3832-
bool has_draft_model_path = !params.draft.mparams.path.empty();
3833-
38343872
bool has_draft_simple = (enabled_configs & (1u << COMMON_SPECULATIVE_TYPE_DRAFT_SIMPLE));
38353873
bool has_draft_eagle3 = false; // TODO PR-18039: if params.speculative.eagle3
38363874
bool has_mtp = (enabled_configs & (1u << COMMON_SPECULATIVE_TYPE_DRAFT_MTP)) && params.draft.ctx_dft != nullptr;
@@ -3863,16 +3901,6 @@ common_speculative * common_speculative_init(common_params_speculative & params,
38633901
if (has_ngram_cache) {
38643902
configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_NGRAM_CACHE, params));
38653903
}
3866-
if (has_draft_simple) {
3867-
if (!has_draft_model_path) {
3868-
LOG_WRN("%s: draft model is not specified - cannot use 'draft' type\n", __func__);
3869-
has_draft_simple = false;
3870-
}
3871-
} else if (has_draft_model_path && !has_mtp && !has_draft_eagle3) {
3872-
LOG_WRN("%s: draft model is specified but 'draft' speculative type is not explicitly enabled - enabling it\n", __func__);
3873-
has_draft_simple = true;
3874-
}
3875-
38763904
if (has_draft_simple) {
38773905
configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_DRAFT_SIMPLE, params));
38783906
}

common/speculative.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ enum common_speculative_type common_speculative_type_from_name(const std::string
5858
// convert type to string
5959
std::string common_speculative_type_to_str(enum common_speculative_type type);
6060

61+
// return the max number of draft tokens based on the speculative parameters
62+
int32_t common_speculative_n_max(const common_params_speculative * spec);
63+
6164
common_speculative * common_speculative_init(common_params_speculative & params, uint32_t n_seq);
6265

6366
void common_speculative_free(common_speculative * spec);

conversion/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
"Mamba2ForCausalLM": "mamba",
137137
"MambaForCausalLM": "mamba",
138138
"MambaLMHeadModel": "mamba",
139+
"MellumForCausalLM": "mellum",
139140
"MiMoV2FlashForCausalLM": "mimo",
140141
"MiMoV2ForCausalLM": "mimo",
141142
"MiniCPM3ForCausalLM": "minicpm",
@@ -216,6 +217,7 @@
216217
"Starcoder2ForCausalLM": "starcoder",
217218
"Step3p5ForCausalLM": "step3",
218219
"StepVLForConditionalGeneration": "step3",
220+
"Step3p7ForConditionalGeneration": "step3",
219221
"T5EncoderModel": "t5",
220222
"T5ForConditionalGeneration": "t5",
221223
"T5WithLMHeadModel": "t5",
@@ -284,6 +286,7 @@
284286
"Sarashina2VisionForCausalLM": "sarashina2",
285287
"SmolVLMForConditionalGeneration": "smolvlm",
286288
"StepVLForConditionalGeneration": "step3",
289+
"Step3p7ForConditionalGeneration": "step3",
287290
"UltravoxModel": "ultravox",
288291
"VoxtralForConditionalGeneration": "ultravox",
289292
"YoutuVLForConditionalGeneration": "youtuvl",

conversion/base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,15 @@ def get_vocab_base_pre(self, tokenizer) -> str:
16571657
if chkhsh == "36f3066e97b7f3994b379aaacde306c1444c6ae84e81a5ae3cd2b7ed3b8c42d4":
16581658
# ref: https://huggingface.co/openbmb/MiniCPM5-1B
16591659
res = "minicpm5"
1660+
if chkhsh == "f241072145675bf8322086f115aebad05e9f869557a238bf2150a2a417d1bf60":
1661+
# ref: https://huggingface.co/ibm-granite/granite-embedding-97m-multilingual-r2
1662+
res = "granite-embed-multi-97m"
1663+
if chkhsh == "789696f5946cc0fc59371f39f6097cafed196b3acded6140432f26bbb1ae1669":
1664+
# ref: https://huggingface.co/ibm-granite/granite-embedding-311m-multilingual-r2
1665+
res = "granite-embed-multi-311m"
1666+
if chkhsh == "9dcf830ee9990cdbf78cc523a5f7bd9ad8f3f9890c2d3581d2785ad10f07049d":
1667+
# ref: https://huggingface.co/JetBrains/Mellum2-12B-A2.5B-Base
1668+
res = "mellum2"
16601669

16611670
if res is None:
16621671
logger.warning("\n")
@@ -2593,7 +2602,7 @@ def get_model_architecture(hparams: dict[str, Any], model_type: ModelType) -> st
25932602
# Step3-VL keeps text config under text_config but uses a custom top-level architecture.
25942603
# For text conversion we route to a dedicated text-only class.
25952604
# TODO: refactor this later to avoid adding exception here
2596-
if model_type == ModelType.TEXT and arch in ("StepVLForConditionalGeneration", "Sarashina2VisionForCausalLM", "Exaone4_5_ForConditionalGeneration"):
2605+
if model_type == ModelType.TEXT and arch in ("StepVLForConditionalGeneration", "Sarashina2VisionForCausalLM", "Exaone4_5_ForConditionalGeneration", "Step3p7ForConditionalGeneration"):
25972606
return arch
25982607

25992608
# if "architectures" is found in the sub-config, use that instead

conversion/bert.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ def set_gguf_parameters(self):
603603
self.gguf_writer.add_sliding_window_pattern(sliding_window_pattern)
604604
self.gguf_writer.add_rope_scaling_type(gguf.RopeScalingType.NONE)
605605
self.gguf_writer.add_vocab_size(self.hparams["vocab_size"])
606+
# FFN activation: ModernBert uses a GLU pair (ffn_up output is 2*n_ff). The
607+
# original ModernBERT uses GELU (-> GeGLU); some derivatives such as IBM
608+
# Granite Embedding 97m R2 use SiLU (-> SwiGLU). Persist this so the
609+
# llama.cpp graph can pick the matching activation.
610+
if hidden_act := self.hparams.get("hidden_activation"):
611+
self.gguf_writer.add_hidden_act(hidden_act)
606612

607613
@classmethod
608614
def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:

conversion/mellum.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import annotations
2+
3+
from typing import Iterable, TYPE_CHECKING
4+
5+
import torch
6+
7+
if TYPE_CHECKING:
8+
from torch import Tensor
9+
10+
from .base import ModelBase, TextModel, gguf, logger
11+
12+
13+
@ModelBase.register("MellumForCausalLM")
14+
class MellumModel(TextModel):
15+
model_arch = gguf.MODEL_ARCH.MELLUM
16+
17+
def set_gguf_parameters(self):
18+
super().set_gguf_parameters()
19+
if (moe_intermediate_size := self.hparams.get("moe_intermediate_size")) is not None:
20+
self.gguf_writer.add_expert_feed_forward_length(moe_intermediate_size)
21+
logger.info(f"gguf: expert feed forward length = {moe_intermediate_size}")
22+
23+
use_sliding_window = self.hparams.get("use_sliding_window")
24+
sliding_window = self.hparams.get("sliding_window")
25+
if (use_sliding_window is True or use_sliding_window is None) and sliding_window is not None:
26+
self.gguf_writer.add_sliding_window(sliding_window)
27+
logger.info(f"gguf: sliding window = {sliding_window}")
28+
self.gguf_writer.add_sliding_window_pattern([t == "sliding_attention" for t in self.hparams["layer_types"]])
29+
logger.info(f"gguf: sliding window pattern length = {len(self.hparams['layer_types'])}")
30+
31+
_experts: list[dict[str, Tensor]] | None = None
32+
33+
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
34+
if name.find("experts") != -1:
35+
n_experts = self.find_hparam(["num_local_experts", "num_experts"])
36+
assert bid is not None
37+
38+
if self._experts is None:
39+
self._experts = [{} for _ in range(self.block_count)]
40+
41+
self._experts[bid][name] = data_torch
42+
43+
if len(self._experts[bid]) >= n_experts * 3:
44+
for w_name in ["down_proj", "gate_proj", "up_proj"]:
45+
datas: list[Tensor] = []
46+
47+
for xid in range(n_experts):
48+
ename = f"model.layers.{bid}.mlp.experts.{xid}.{w_name}.weight"
49+
datas.append(self._experts[bid][ename])
50+
del self._experts[bid][ename]
51+
52+
data_torch = torch.stack(datas, dim=0)
53+
54+
merged_name = f"model.layers.{bid}.mlp.experts.{w_name}.weight"
55+
56+
yield from super().modify_tensors(data_torch, merged_name, bid)
57+
return
58+
else:
59+
return
60+
61+
yield from super().modify_tensors(data_torch, name, bid)

0 commit comments

Comments
 (0)