Skip to content

fix: use dtype instead of deprecated torch_dtype - #735

Open
xyf5432 wants to merge 2 commits into
PrunaAI:mainfrom
xyf5432:fix/use-dtype-instead-of-torch-dtype
Open

fix: use dtype instead of deprecated torch_dtype#735
xyf5432 wants to merge 2 commits into
PrunaAI:mainfrom
xyf5432:fix/use-dtype-instead-of-torch-dtype

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 28, 2026

Copy link
Copy Markdown

Summary

config.torch_dtype and the torch_dtype keyword argument were deprecated in transformers 4.56 (PR #39782) and replaced by dtype. This PR updates the AutoModelForCausalLM.from_pretrained call in src/pruna/algorithms/hqq.py (line 272, HQQ quantization) to select the keyword from the installed transformers version using packaging.version, so transformers < 4.56 keeps working. The quantization_config argument is unchanged.

Changes

  • src/pruna/algorithms/hqq.py: add a _dtype_kwargs helper that returns {"dtype": ...} on transformers >= 4.56 and {"torch_dtype": ...} otherwise; the from_pretrained call passes it as **_dtype_kwargs(torch.float16 if ... else ...).

Test

  • The modified file compiles (python -m py_compile).
  • On transformers >= 4.56 the call uses dtype and emits no deprecation warning; on older versions it passes torch_dtype unchanged.

Fixes #734

…4.56

config.torch_dtype and the torch_dtype keyword argument were deprecated in transformers 4.56 (PR #39782). Pass dtype based on the installed transformers version (packaging.version), falling back to torch_dtype on older versions.
@begumcig
begumcig self-requested a review September 4, 2026 08:13

@begumcig begumcig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hii @xyf5432 thank you so much for contributing! This has been on my to do list forever, but never got to do it!! I just left one small comment regarding the imports but already looking super good! Thanks again :)

Comment thread src/pruna/algorithms/hqq.py Outdated
from packaging.version import Version

def _dtype_kwargs(dtype):
"""`dtype` keyword of `from_pretrained` exists since transformers 4.56 (PR #39782);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we import transformers here instead, rather than on top of the file?

Address review feedback: import transformers and packaging.version inside
_dtype_kwargs instead of the module top level. Fix ruff findings
(line-too-long, docstring formatting, blank lines).
Copilot AI lite review requested due to automatic review settings September 4, 2026 08:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change is small, targeted, and aligns with the stated deprecation-avoidance goal while preserving compatibility with older transformers versions.

Pull request overview

This PR updates the HQQ quantization path to avoid the torch_dtype deprecation introduced in transformers 4.56 by selecting the appropriate from_pretrained keyword (dtype vs torch_dtype) based on the installed transformers version.

Changes:

  • Added a small helper (_dtype_kwargs) that returns {"dtype": ...} for transformers >= 4.56 and {"torch_dtype": ...} otherwise.
  • Updated the AutoModelForCausalLM.from_pretrained(...) call to pass the selected keyword via **_dtype_kwargs(...).
File summaries
File Description
src/pruna/algorithms/hqq.py Adds version-gated dtype kwarg selection and updates the HQQ fallback from_pretrained call to use it.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +52 to +57
import transformers
from packaging.version import Version

if Version(transformers.__version__) >= Version("4.56"):
return {"dtype": dtype}
return {"torch_dtype": dtype}
@xyf5432

xyf5432 commented Sep 4, 2026

Copy link
Copy Markdown
Author

Done in 3097d26: moved the import transformers / packaging.version imports inside _dtype_kwargs and fixed the ruff findings. Thanks for the review!

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.

Use dtype instead of deprecated torch_dtype for transformers >= 4.56

3 participants