Skip to content

updated applyChatTemplate with lazy memoization - #218

Merged
pcuenca merged 4 commits into
huggingface:mainfrom
tpae:tpae/performance-update
Sep 2, 2025
Merged

updated applyChatTemplate with lazy memoization#218
pcuenca merged 4 commits into
huggingface:mainfrom
tpae:tpae/performance-update

Conversation

@tpae

@tpae tpae commented Sep 1, 2025

Copy link
Copy Markdown
Contributor

Based on #217.

Cache compiled Jinja Template instances in PreTrainedTokenizer keyed by the template string; reuse in applyChatTemplate. No API or behavior changes.

Measured performance: Repeated calls with the same template are ~6x faster.

Cached (reused template): ~0.00049 s (0.49 ms) average
Uncached (unique template each call): ~0.00292 s (2.92 ms) average
Speedup: ~5.9–6.2x

Raw results:

Test Case '-[TokenizersTests.ChatTemplateTests testApplyChatTemplatePerformanceCached]' started.
swift-transformers/Tests/TokenizersTests/ChatTemplateTests.swift:276: Test Case '-[TokenizersTests.ChatTemplateTests testApplyChatTemplatePerformanceCached]' measured [Clock Monotonic Time, s] average: 0.000, relative standard deviation: 7.781%, values: [0.000339, 0.000385, 0.000339, 0.000385, 0.000316], performanceMetricID:com.apple.dt.XCTMetric_Clock.time.monotonic, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.000, maxStandardDeviation: 0.000
Test Case '-[TokenizersTests.ChatTemplateTests testApplyChatTemplatePerformanceCached]' passed (0.986 seconds).
Test Case '-[TokenizersTests.ChatTemplateTests testApplyChatTemplatePerformanceUncached]' started.
swift-transformers/Tests/TokenizersTests/ChatTemplateTests.swift:287: Test Case '-[TokenizersTests.ChatTemplateTests testApplyChatTemplatePerformanceUncached]' measured [Clock Monotonic Time, s] average: 0.003, relative standard deviation: 2.207%, values: [0.002790, 0.002796, 0.002700, 0.002880, 0.002734], performanceMetricID:com.apple.dt.XCTMetric_Clock.time.monotonic, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.000, maxStandardDeviation: 0.000
Test Case '-[TokenizersTests.ChatTemplateTests testApplyChatTemplatePerformanceUncached]' passed (0.932 seconds).

@FL33TW00D

Copy link
Copy Markdown
Contributor

@tpae great contribution thank you.

If you could format it with the pinned version of swiftformat we can merge!

brew install swiftformat@0.54.6
swiftformat --config .swiftformat .

@pcuenca pcuenca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very nice, thank you @tpae! 🙌

@tpae

tpae commented Sep 1, 2025

Copy link
Copy Markdown
Contributor Author

Amazing! Thank you!

My brew couldn't find swiftformat@0.54.6, but was able to find 0.57.2, and I ran the formatting.

@tpae
tpae requested a review from pcuenca September 1, 2025 17:08
@tpae

tpae commented Sep 1, 2025

Copy link
Copy Markdown
Contributor Author

Test was passing for me locally 🤔 i think 429 is rate limiting

@pcuenca

pcuenca commented Sep 2, 2025

Copy link
Copy Markdown
Member

Tests pass for me locally as well, and the CI worked for this PR too. So I think the token may not be passed to the CI from forks. I'll take a quick look, otherwise I'll merge and resolve later.

@pcuenca

pcuenca commented Sep 2, 2025

Copy link
Copy Markdown
Member

I'm merging. Also opened #222 which could be interesting for actual use cases, not just the CI.

@pcuenca

pcuenca commented Sep 2, 2025

Copy link
Copy Markdown
Member

Thanks a lot @tpae!

@pcuenca
pcuenca merged commit ed54f3c into huggingface:main Sep 2, 2025
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants