Skip to content

Commit 992fde1

Browse files
committed
give up, chat template is shit
1 parent a26c4cc commit 992fde1

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

convert_hf_to_gguf.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ def load_hparams(dir_model: Path):
432432
if "llm_config" in config:
433433
# rename for InternVL
434434
config["text_config"] = config["llm_config"]
435+
if "language_config" in config:
436+
# rename for Janus Pro
437+
config["text_config"] = config["language_config"]
435438
return config
436439

437440
@classmethod
@@ -1975,6 +1978,31 @@ def prepare_tensors(self):
19751978
raise ValueError(f"Unprocessed experts: {experts}")
19761979

19771980

1981+
@ModelBase.register("JanusProForCausalLM")
1982+
class JanusProModel(TextModel):
1983+
model_arch = gguf.MODEL_ARCH.LLAMA
1984+
undo_permute = True
1985+
1986+
def __init__(self, *args, **kwargs):
1987+
super().__init__(*args, **kwargs)
1988+
self.hparams["num_attention_heads"] = self.hparams.get("num_attention_heads", 32)
1989+
self.hparams["num_key_value_heads"] = self.hparams.get("num_key_value_heads", 32)
1990+
self.hparams["hidden_size"] = self.hparams.get("hidden_size", 4096)
1991+
self.hparams["intermediate_size"] = self.hparams.get("intermediate_size", 11008)
1992+
self.hparams["rms_norm_eps"] = self.hparams.get("rms_norm_eps", 1e-6)
1993+
1994+
def set_gguf_parameters(self):
1995+
super().set_gguf_parameters()
1996+
self.gguf_writer.add_chat_template("janus-pro")
1997+
1998+
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
1999+
if "language_model." in name:
2000+
name = name.replace("language_model.", "")
2001+
return super().modify_tensors(data_torch, name, bid)
2002+
else:
2003+
return []
2004+
2005+
19782006
@ModelBase.register(
19792007
"LlavaForConditionalGeneration", # pixtral
19802008
"Mistral3ForConditionalGeneration", # mistral small 3.1
@@ -6222,6 +6250,9 @@ def split_str_to_n_bytes(split_str: str) -> int:
62226250

62236251

62246252
def get_model_architecture(hparams: dict[str, Any], model_type: ModelType) -> str:
6253+
# exception: Janus Pro
6254+
if "aligner_config" in hparams:
6255+
return "JanusProForCausalLM"
62256256
# TODO @ngxson : this won't work correctly if the model has both audio & vision encoders
62266257
# maybe we should fallback to text model's arch in that case, since not many models have both
62276258
text_config = hparams.get("text_config", {})

src/llama-chat.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
6464
{ "bailing", LLM_CHAT_TEMPLATE_BAILING },
6565
{ "llama4", LLM_CHAT_TEMPLATE_LLAMA4 },
6666
{ "smolvlm", LLM_CHAT_TEMPLATE_SMOLVLM },
67+
{ "janus-pro", LLM_CHAT_TEMPLATE_JANUS_PRO },
6768
};
6869

6970
llm_chat_template llm_chat_template_from_str(const std::string & name) {
@@ -508,6 +509,22 @@ int32_t llm_chat_apply_template(
508509
if (add_ass) {
509510
ss << LU8("<|Assistant|>");
510511
}
512+
} else if (tmpl == LLM_CHAT_TEMPLATE_JANUS_PRO) {
513+
// variant of DeepSeek-V2, used by Janus Pro
514+
for (auto message : chat) {
515+
std::string role(message->role);
516+
ss << LU8("<|begin▁of▁sentence|>");
517+
if (role == "system") {
518+
//ss << message->content << "\n\n";
519+
} else if (role == "user") {
520+
ss << "<|User|>" << message->content << "\n\n";
521+
} else if (role == "assistant") {
522+
ss << "<|Assistant|>" << message->content << LU8("<|end▁of▁sentence|>");
523+
}
524+
}
525+
if (add_ass) {
526+
ss << "<|Assistant|>";
527+
}
511528
} else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_3) {
512529
// ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
513530
// EXAONE-3.0-7.8B-Instruct

src/llama-chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum llm_chat_template {
4343
LLM_CHAT_TEMPLATE_BAILING,
4444
LLM_CHAT_TEMPLATE_LLAMA4,
4545
LLM_CHAT_TEMPLATE_SMOLVLM,
46+
LLM_CHAT_TEMPLATE_JANUS_PRO,
4647
LLM_CHAT_TEMPLATE_UNKNOWN,
4748
};
4849

0 commit comments

Comments
 (0)