Skip to content

Studio: Fix torch_dtype deprecation warning on startup and ASR load - #6999

Merged
oobabooga merged 11 commits into
unslothai:mainfrom
oobabooga:torch-dtype-deprecation
Jul 13, 2026
Merged

oobabooga merged 11 commits into
unslothai:mainfrom
oobabooga:torch-dtype-deprecation

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Starting Studio prints torch_dtype is deprecated! Use dtype instead! on the console. Reported on Discord. Non-fatal (Studio starts fine), but noisy.

Problem

transformers renamed the torch_dtype kwarg to dtype in 4.56.0 and now warns when the old name is passed. Two first-party call sites still pass torch_dtype:

  • core/rag/embeddings.py: the RAG embedder, warmed in a daemon thread at startup, so this is the line users see at boot.
  • core/inference/inference.py: the Whisper ASR pipeline, fires on ASR model load.

A bare rename is not safe: our floor is transformers>=4.51.3, and <4.56 only accepts torch_dtype, so passing dtype there would TypeError instead of just warning.

Fix

Added utils/transformers_dtype.py with a small dtype_kwargs(value) helper that picks the kwarg name the installed transformers accepts: dtype on new, torch_dtype on old. Detection mirrors the project's canonical unsloth_zoo.hf_utils.HAS_TORCH_DTYPE ("torch_dtype" in PretrainedConfig.__doc__), derived locally so the lean, torch-optional startup path doesn't drag in unsloth_zoo's heavy package init. Both call sites now build their dtype kwarg through it.

Verification

Loaded unsloth/bge-small-en-v1.5 in the default venv (transformers 4.57.6): the old torch_dtype kwarg prints the deprecation line and the new dtype kwarg is silent. The model loads as fp16 either way. Confirmed the guard picks the correct name across the whole supported range: 4.46.3, 4.51.3 floor, 4.56.0, 4.57.6, and the 5.3/5.5/5.10 sidecars. The docstring drop and the dtype introduction both land in 4.56.0, so there is no version where the guard passes an unaccepted kwarg. Swept the backend: these two were the only torch_dtype sites.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a version-safe utility, dtype_kwargs, to handle the renaming of the torch_dtype keyword argument to dtype in Hugging Face transformers (starting with version 4.56.0). This ensures compatibility across different versions of transformers without raising deprecation warnings or TypeError exceptions. The utility is integrated into the inference and RAG embedding modules. The feedback points out that checking __doc__ to determine the supported keyword argument is fragile because running Python with the -OO optimization flag strips docstrings. It is recommended to use packaging.version.Version to compare transformers.__version__ instead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +23 to +34
def _has_torch_dtype_kwarg() -> bool:
"""True if the installed transformers still expects the legacy ``torch_dtype``
name (i.e. predates the ``dtype`` rename). False when ``dtype`` is the accepted
name, or when transformers is missing/broken (prefer the modern name)."""
try:
try:
from transformers import PreTrainedConfig as _Config
except ImportError:
from transformers import PretrainedConfig as _Config
return "torch_dtype" in (_Config.__doc__ or "")
except Exception:
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Checking __doc__ for conditional logic is fragile because running Python with the -OO optimization flag strips docstrings (setting __doc__ to None). In such environments, this check will return False even on older versions of transformers (where dtype is not supported), leading to a TypeError on startup or model load.

Since packaging is a required dependency of transformers, we can use packaging.version.Version to perform a robust version check instead.

Suggested change
def _has_torch_dtype_kwarg() -> bool:
"""True if the installed transformers still expects the legacy ``torch_dtype``
name (i.e. predates the ``dtype`` rename). False when ``dtype`` is the accepted
name, or when transformers is missing/broken (prefer the modern name)."""
try:
try:
from transformers import PreTrainedConfig as _Config
except ImportError:
from transformers import PretrainedConfig as _Config
return "torch_dtype" in (_Config.__doc__ or "")
except Exception:
return False
def _has_torch_dtype_kwarg() -> bool:
"""True if the installed transformers still expects the legacy ``torch_dtype``
name (i.e. predates the ``dtype`` rename). False when ``dtype`` is the accepted
name, or when transformers is missing/broken (prefer the modern name)."""
try:
import transformers
from packaging.version import Version
return Version(transformers.__version__) < Version("4.56.0")
except Exception:
return False
References
  1. In Hugging Face transformers (version 4.56+), use dtype instead of torch_dtype to specify model precision during loading (e.g., in from_pretrained), as torch_dtype is deprecated in transformers. However, continue using torch_dtype for diffusers components where it remains the standard convention.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: switched to a packaging.version.Version(transformers.version) < Version("4.56.0") check, dropping the doc sniffing entirely (confirmed -OO/PYTHONOPTIMIZE=2 sets doc to None on every class; packaging is a hard transformers dependency). Added a regression test that reproduces the stripped-docstring case.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 25b59992ca

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: ae162a6da5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga
oobabooga merged commit f60b982 into unslothai:main Jul 13, 2026
34 checks passed
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.

1 participant