Skip to content

Feature Request: gemma4 31B chat template quadratic scan cost #29906

Description

@hasanyazarr

Prerequisites

  • I am running the latest code. Mention the version if possible as well.
  • I carefully followed the README.md.
  • I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • I reviewed the Discussions, and have a new and useful enhancement to share.

Feature Description

File: models/templates/google-gemma-4-31B-it.jinja, lines 219-230

What it does: For every message, it scans backwards to find the previous non-tool role. After the role is found, the loop does not stop.

What it should be: Google updated the Gemma 4 template. We should either update it to match or remove this scan.

Motivation

Environment: Mac M4 Pro, commit 436f6f8, tested template render times:

The current template scales quadratically with message count. Tested with 64 messages: 11.3 ms -> 3.1 ms and with 184 messages: 72.2 ms -> 10.9 ms (current implementation -> scan-removed version). Rendered output is byte-identical.

The template in the repo has not been updated since #21704 (2026-04-10).

Possible Implementation

Google made the same fix (merged on 2026-07-15): https://huggingface.co/google/gemma-4-31B-it/discussions/118

Possible options are (a) update the whole template to match Google's current implementation or (b) remove this scan. Which option do you prefer?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions