Skip to content

Fix autothink and thinkdeeper for transformers>=5#320

Merged
codelion merged 2 commits into
algorithmicsuperintelligence:mainfrom
jacquerie:fix-autothink-and-thinkdeeper
Jul 18, 2026
Merged

Fix autothink and thinkdeeper for transformers>=5#320
codelion merged 2 commits into
algorithmicsuperintelligence:mainfrom
jacquerie:fix-autothink-and-thinkdeeper

Conversation

@jacquerie

Copy link
Copy Markdown
Contributor

Since version 5 of the transformers library, method apply_chat_template returns a BatchEncoding object, which wraps the output tensor in a dict-like object: https://github.com/huggingface/transformers/blob/7ea2320c76117e6742364808a666ef6f2fb40a67/MIGRATION_GUIDE_V5.md#4-apply_chat_template-returns-batchencoding

The code in autothink and thinkdeeper did not account for this, which made the following code throw:

import torch
from optillm.thinkdeeper import thinkdeeper_decode
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "Qwen/Qwen2.5-0.5B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)

messages = [
    {
        "role": "user",
        "content": "In a dance class of 20 students, 20% enrolled in contemporary dance, 25% of the remaining enrolled in jazz dance, and the rest enrolled in hip-hop dance. What percentage of the entire students enrolled in hip-hop dance?",
    },
]

thinkdeeper_decode(
    model,
    tokenizer,
    messages,
    {
        "do_sample": True,
        "temperature": 0.1,
        "max_new_tokens": 1024,
    }
)

This PR fixes this issue by noticing that, since OptiLLM explicitly depends on transformers at version 5 and above (

"transformers>=5.0.0,<5.13.0",
), it's safe to unwrap the BatchEncoding object and query its input_ids property.

@CLAassistant

CLAassistant commented Jul 18, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@jacquerie
jacquerie force-pushed the fix-autothink-and-thinkdeeper branch from 1d64c8a to 2b377a5 Compare July 18, 2026 10:23
…ump 0.3.22

Builds on the transformers>=5 fix: apply_chat_template now returns a
BatchEncoding, so the tensor must be unwrapped via .input_ids.

Two corrections to the original change:

1. Preserve the device transfer. `tokens.to(device)` was called without
   assigning the result, but Tensor.to() is not in-place -- it returns a new
   tensor. As written the tokens stayed on CPU, so on CUDA/MPS the subsequent
   model(input_ids=tokens, ...) would fail with a device mismatch. A CPU-only
   repro hides this. Chain it instead: .input_ids.to(self.model.device).

2. Apply the same fix to optillm/deepconf/processor.py, which has the identical
   apply_chat_template(return_tensors="pt") -> model(input_ids=tokens) pattern
   and was still passing a BatchEncoding.

Also updates the README badges (drop the GitHub stars badge, use the pepy.tech
downloads badge which reports actual counts) and bumps the version to 0.3.22.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codelion
codelion merged commit c8cbdde into algorithmicsuperintelligence:main Jul 18, 2026
6 checks passed
@jacquerie
jacquerie deleted the fix-autothink-and-thinkdeeper branch July 18, 2026 17:56
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