#977·LMFlow

Use `dtype` instead of deprecated `torch_dtype` for transformers >= 4.56

Author: xyf5432Created Aug 28, 2026Updated Aug 28, 2026

In contrib/rlhflow/reward_modeling.py line 44, AutoModelForSequenceClassification.from_pretrained is called with torch_dtype=torch.bfloat16:

python
model = AutoModelForSequenceClassification.from_pretrained(
    model_args.model_name_or_path, num_labels=1, torch_dtype=torch.bfloat16
)

The torch_dtype keyword argument was deprecated in transformers 4.56 (PR #39782) and replaced by dtype. On transformers 4.56+ this call emits a DeprecationWarning, and the argument will be removed in a future release, breaking the script.

Suggested fix: choose the keyword based on the installed transformers version with packaging.version:

python
import transformers
from packaging.version import Version

def _dtype_kwargs(dtype):
    """`dtype` keyword of `from_pretrained` exists since transformers 4.56 (PR #39782);
    older versions use `torch_dtype`."""
    if Version(transformers.__version__) >= Version("4.56"):
        return {"dtype": dtype}
    return {"torch_dtype": dtype}

model = AutoModelForSequenceClassification.from_pretrained(
    model_args.model_name_or_path, num_labels=1, **_dtype_kwargs(torch.bfloat16)
)

This keeps compatibility with transformers < 4.56 and stops the deprecation warning on 4.56+.