@@ -285,6 +285,9 @@ public class PreTrainedTokenizer: Tokenizer {
285285
286286 private let cleanUpTokenizationSpaces : Bool
287287
288+ /// Cache for compiled Jinja templates keyed by their literal template string
289+ private var compiledChatTemplateCache : [ String : Template ] = [ : ]
290+
288291 public required init ( tokenizerConfig: Config , tokenizerData: Config ) throws {
289292 var addedTokens : [ String : Int ] = [ : ]
290293 var specialTokens : [ String : Int ] = [ : ]
@@ -332,6 +335,15 @@ public class PreTrainedTokenizer: Tokenizer {
332335 model = try TokenizerModel . from ( tokenizerConfig: tokenizerConfig, tokenizerData: tokenizerData, addedTokens: addedTokens)
333336 }
334337
338+ private func compiledTemplate( for templateString: String ) throws -> Template {
339+ if let cached = compiledChatTemplateCache [ templateString] {
340+ return cached
341+ }
342+ let compiled = try Template ( templateString)
343+ compiledChatTemplateCache [ templateString] = compiled
344+ return compiled
345+ }
346+
335347 func preTokenize( _ text: String , options: PreTokenizerOptions ) -> [ String ] {
336348 guard let preTokenizer else { return [ text] }
337349 return preTokenizer ( text: text, options: options)
@@ -530,7 +542,7 @@ public class PreTrainedTokenizer: Tokenizer {
530542 throw TokenizerError . missingChatTemplate
531543 }
532544
533- let template = try Template ( selectedChatTemplate)
545+ let template = try compiledTemplate ( for : selectedChatTemplate)
534546 var context : [ String : Any ] = [
535547 " messages " : messages,
536548 " add_generation_prompt " : addGenerationPrompt,
0 commit comments