Skip to content

jinja : reuse parent-linked render scopes - #32059

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

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

Conversation

@Neroued

@Neroued Neroued commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Overview

Second part of the rendering optimizations discussed in #32002, following the Value/lookup change #32023.

Replaces variable-map copies on ordinary scope entry with parent lookup and reusable frames. Caller captures retain detached binding snapshots, owned by the callable and released when it is no longer needed. Render teardown clears remaining cycles; captured contexts stay safe to destroy after the root context ends.

Preserves upstream call-site macro lookup and caller snapshots, including their execution metadata. Executing a captured caller after its root render has ended reports an error.

Additional information

Jinja tests passed on x86 and DGX Spark. New tests cover prompt release of caller captures, saved callers, snapshot metadata, exception cleanup, and teardown. The combined stack also passes the relevant ASan/UBSan checks.

Before: values branch. After: this change. Median of five renders after warmup, in milliseconds; templates compiled once. Timing includes JSON-to-Value conversion, execution, output gathering and context destruction.

CPU Workload Before After
Ryzen 9950X3D GLM-4.7, 1,601 messages 18.836 12.659
Ryzen 9950X3D Qwen3.5, 1,601 messages 24.762 15.696
Ryzen 9950X3D Early-exit suffix loops, N=4096 535.274 558.805
DGX Spark GLM-4.7, 1,601 messages 23.271 15.156
DGX Spark Qwen3.5, 1,601 messages 29.629 18.275
DGX Spark Early-exit suffix loops, N=4096 633.199 641.011

The early-exit microbenchmark does not improve at this stage: it still uses eager loop setup. The separate loop change addresses that cost.

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

Can these changes be detached from the changes in #32023 for easier review? If not review must wait until merge and rebase.

@Neroued

Neroued commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Can these changes be detached from the changes in #32023 for easier review? If not review must wait until merge and rebase.

The changes aren't fully orthogonal, so splitting them into standalone PRs would require some rework. I'll rebase each PR as soon as its prerequisite is merged.

@Neroued
Neroued force-pushed the perf/jinja-scopes branch 3 times, most recently from 56df0cc to d5390c1 Compare November 12, 2026 13:40
@Neroued
Neroued marked this pull request as ready for review November 14, 2026 13:37
@ngxson

ngxson commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

I verified with fable 5.2, the verdict is: the gain is too tiny compared to the complexity it adds. So unless @CISC says otherwise, I'd recommend closing this for now

@CISC

CISC commented Nov 15, 2026

Copy link
Copy Markdown
Member

I'd recommend closing this for now

Agreed.

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.

3 participants