Skip to content

Commit 30ace25

Browse files
committed
Optimize: Initialize Jinja template in __init__ instead of __call__ to avoid redundant parsing
Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent baa60ae commit 30ace25

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,12 @@ def __init__(self, clip_model_path: str, verbose: bool = True, use_gpu: bool = T
28082808
if not os.path.exists(clip_model_path):
28092809
raise ValueError(f"Clip model path does not exist: {clip_model_path}")
28102810

2811+
# Pre-compile Jinja template
2812+
self.chat_template = ImmutableSandboxedEnvironment(
2813+
trim_blocks=True,
2814+
lstrip_blocks=True,
2815+
).from_string(self.CHAT_FORMAT)
2816+
28112817
def _init_mtmd_context(self, llama_model: llama.Llama):
28122818
"""Initialize mtmd context with the llama model."""
28132819
if self.mtmd_ctx is not None:
@@ -2931,16 +2937,12 @@ def __call__(
29312937
] + messages
29322938

29332939
image_urls = self.get_image_urls(messages)
2934-
template = ImmutableSandboxedEnvironment(
2935-
trim_blocks=True,
2936-
lstrip_blocks=True,
2937-
).from_string(self.CHAT_FORMAT)
29382940

29392941
# Get the default media marker
29402942
media_marker = self._mtmd_cpp.mtmd_default_marker().decode('utf-8')
29412943

29422944
# Replace image URLs with media markers in the template
2943-
text = template.render(
2945+
text = self.chat_template.render(
29442946
messages=messages,
29452947
tools=tools,
29462948
add_generation_prompt=True,

0 commit comments

Comments
 (0)