Fix Transformers v5 cached decoding in DoLa

#2

This PR addresses the cached-decoding regression reported in #1.

The issue appears to be the same Transformers v5 cache-handling compatibility problem previously fixed for transformers-community/group-beam-search: https://huggingface.co/transformers-community/group-beam-search/discussions/4

The goal is to make DoLa generation preserve deterministic behavior when switching between use_cache=False and use_cache=True, while keeping compatibility with current Transformers 4.x behavior.

I pushed the cache-compatibility fix for this PR.

The change updates the DoLa generation loop to follow the Transformers v5 cache protocol: the first decoding iteration is treated as prefill and uses the full prompt, while later cached decoding steps use only the newest token. Uncached decoding keeps the full-prefix behavior.

This addresses the regression reported in #1 and follows the same compatibility pattern already accepted for transformers-community/group-beam-search: https://huggingface.co/transformers-community/group-beam-search/discussions/4

I tested the PR in Colab with Qwen/Qwen3-0.6B, deterministic decoding, and the local PR checkout as custom_generate.

Transformers 5 test

python: 3.12.13
torch: 2.11.0+cu128
transformers: 5.12.0
cuda: True
model: Qwen/Qwen3-0.6B
custom_generate: /content/dola
device: cuda:0

use_cache=False:

ids: [304, 279, 2355, 7982, 1849, 11, 323, 1083, 304, 12785, 8357, 13, 576, 6028, 990, 374, 311, 8317, 9072, 4802, 504, 825, 1992, 311, 2441, 13, 80532, 646, 387, 1483]
text: ' in the power distribution system, and also in industrial applications. The primary use is to transfer electric energy from one place to another. Transformers can be used'

use_cache=True:

ids: [304, 279, 2355, 7982, 1849, 11, 323, 1083, 304, 12785, 8357, 13, 576, 6028, 990, 374, 311, 8317, 9072, 4802, 504, 825, 1992, 311, 2441, 13, 80532, 646, 387, 1483]
text: ' in the power distribution system, and also in industrial applications. The primary use is to transfer electric energy from one place to another. Transformers can be used'

Transformers 4 compatibility test

python: 3.12.12
torch: 2.9.0+cu126
transformers: 4.57.6
cuda: True
model: Qwen/Qwen3-0.6B
custom_generate: /content/dola
device: cuda:0

use_cache=False:

ids: [304, 279, 2355, 7982, 1849, 11, 323, 1083, 304, 12785, 8357, 13, 576, 6028, 990, 374, 311, 8317, 9072, 4802, 504, 825, 1992, 311, 2441, 13, 80532, 646, 387, 1483]
text: ' in the power distribution system, and also in industrial applications. The primary use is to transfer electric energy from one place to another. Transformers can be used'

use_cache=True:

ids: [304, 279, 2355, 7982, 1849, 11, 323, 1083, 304, 12785, 8357, 13, 576, 6028, 990, 374, 311, 8317, 9072, 4802, 504, 825, 1992, 311, 2441, 13, 80532, 646, 387, 1483]
text: ' in the power distribution system, and also in industrial applications. The primary use is to transfer electric energy from one place to another. Transformers can be used'

So the PR fixes the Transformers v5 cached-decoding behavior and preserves the same deterministic output under Transformers 4.57.6.

lavrenko changed pull request status to open
Transformers Community org

Nice

RaushanTurganbay changed pull request status to merged

Sign up or log in to comment