Skip to content

jinja : evaluate loops lazily and accumulate output directly - #32061

Open
Neroued wants to merge 1 commit into
ggml-org:masterfrom
Neroued:perf/jinja-loops
Open

Neroued wants to merge 1 commit into
ggml-org:masterfrom
Neroued:perf/jinja-loops

Conversation

@Neroued

@Neroued Neroued commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Loop-rendering optimizations for #32002, rebased on current master.

Evaluates ranges, loop bindings, filters, and loop metadata on demand, and accumulates output directly. Fixes output lost through nested break/continue and avoids unpacking elements that execution never reaches.

Uses the upstream scope-chain implementation. Deferred filters retain live bindings while a render is active, including loops saved beyond their original scope; their captures are released on return or error. An exhausted iterator stays exhausted. Existing for/else behavior is preserved and checked against Jinja2.

Additional information

Full CPU build and related chat/template tests passed on x86. Jinja tests and Python/Jinja2 comparisons passed on x86 and DGX Spark. Targeted ASan/UBSan checks with leak detection passed: 26 tests, 38 assertions.

Before: upstream master 225a5345b. After: this standalone change. Median of five renders after warmup, in milliseconds; templates compiled once. Timing includes JSON-to-Value conversion, execution, output gathering and context destruction, and excludes template compilation and input JSON parsing. GCC 13.3, C++17, Release; Python 3.11 / Jinja2 3.1.6. Runs use one pinned CPU core.

CPU Workload Before After Python Jinja2
Ryzen 9950X3D GLM-4.7, 1,601 messages 17.863 9.370 9.759
Ryzen 9950X3D Qwen3.5, 1,601 messages 18.670 11.674 13.379
Ryzen 9950X3D Early-exit suffix loops, N=4096 582.801 4.232 8.852
DGX Spark GLM-4.7, 1,601 messages 17.165 9.657 10.552
DGX Spark Qwen3.5, 1,601 messages 21.558 13.170 15.304
DGX Spark Early-exit suffix loops, N=4096 643.401 4.782 10.535

The two C++ builds produce identical text and marked string parts on these workloads; rendered text also matches Jinja2. The benchmark harness remains in the independent reference branch. This PR contains implementation changes and correctness tests only.

Requirements

  • I have read and agree with the contributing guidelines.
  • AI usage disclosure: YES. GPT-6 Astra assisted with implementation, review, tests, and benchmarking.

@github-actions github-actions Bot added testing Everything test related jinja parser Issues related to the jinja parser labels Nov 11, 2026
@ggml-gh-bot

This comment was marked as resolved.

@CISC

CISC commented Nov 11, 2026

Copy link
Copy Markdown
Member

Same thing here, unless changes can stand on their own review must wait.

@Neroued
Neroued force-pushed the perf/jinja-loops branch 4 times, most recently from c8ba7ec to 73839c1 Compare November 14, 2026 13:28
@CISC

CISC commented Nov 21, 2026

Copy link
Copy Markdown
Member

Oh hey, forgot about this one, mind rebasing and marking ready for review?

@Neroued
Neroued marked this pull request as ready for review November 22, 2026 06:28
@Neroued

Neroued commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@CISC

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jinja parser Issues related to the jinja parser testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants