Repository navigation
Studio: Fix torch_dtype deprecation warning on startup and ASR load - #6999
Conversation
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
| 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
- In Hugging Face
transformers(version 4.56+), usedtypeinstead oftorch_dtypeto specify model precision during loading (e.g., infrom_pretrained), astorch_dtypeis deprecated intransformers. However, continue usingtorch_dtypefordiffuserscomponents where it remains the standard convention.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
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_dtypekwarg todtypein 4.56.0 and now warns when the old name is passed. Two first-party call sites still passtorch_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.56only acceptstorch_dtype, so passingdtypethere wouldTypeErrorinstead of just warning.Fix
Added
utils/transformers_dtype.pywith a smalldtype_kwargs(value)helper that picks the kwarg name the installed transformers accepts:dtypeon new,torch_dtypeon old. Detection mirrors the project's canonicalunsloth_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.5in the default venv (transformers 4.57.6): the oldtorch_dtypekwarg prints the deprecation line and the newdtypekwarg 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 thedtypeintroduction 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 onlytorch_dtypesites.