Skip to content

Conversation

albertvillanova
Copy link
Member

Fix TrainingArguments.parallelism_config NameError with accelerate<1.10.1:

NameError: name 'ParallelismConfig' is not defined
RuntimeError: Type resolution failed for <class 'transformers.training_args.TrainingArguments'>. Try declaring the class in global scope or removing line of `from __future__ import annotations` which opts in Postponed Evaluation of Annotations (PEP 563)

This PR fixes a runtime compatibility issue in TrainingArguments with accelerate<1.10.1.

Problem

With accelerate<1.10.1, the module accelerate.parallelism_config does not exist.
TrainingArguments currently annotates the field as:

parallelism_config: Optional["ParallelismConfig"] = field(default=None)

When HfArgumentParser calls typing.get_type_hints on this dataclass, Python 3.12 attempts to resolve the ForwardRef("ParallelismConfig") and fails.

See failing CI in downstream TRL: https://github.com/huggingface/trl/actions/runs/17635808933/job/50111711153

Minimal Reproducible Example

pip install transformers accelerate==1.4.0
from transformers import HfArgumentParser, TrainingArguments

# This will trigger get_type_hints on TrainingArguments
parser = HfArgumentParser(TrainingArguments)
Traceback (most recent call last):
  File "huggingface/transformers/src/transformers/hf_argparser.py", line 258, in _add_dataclass_arguments
    type_hints: dict[str, type] = get_type_hints(dtype)
                                  ^^^^^^^^^^^^^^^^^^^^^
  File ".pyenv/versions/3.12.9/lib/python3.12/typing.py", line 2277, in get_type_hints
    value = _eval_type(value, base_globals, base_locals, base.__type_params__)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".pyenv/versions/3.12.9/lib/python3.12/typing.py", line 430, in _eval_type
    ev_args = tuple(
              ^^^^^^
  File ".pyenv/versions/3.12.9/lib/python3.12/typing.py", line 431, in <genexpr>
    _eval_type(
  File ".pyenv/versions/3.12.9/lib/python3.12/typing.py", line 415, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".pyenv/versions/3.12.9/lib/python3.12/typing.py", line 947, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'ParallelismConfig' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "huggingface/transformers/src/transformers/hf_argparser.py", line 143, in __init__
    self._add_dataclass_arguments(dtype)
  File "huggingface/transformers/src/transformers/hf_argparser.py", line 260, in _add_dataclass_arguments
    raise RuntimeError(
RuntimeError: Type resolution failed for <class 'transformers.training_args.TrainingArguments'>. Try declaring the class in global scope or removing line of `from __future__ import annotations` which opts in Postponed Evaluation of Annotations (PEP 563)

Solution

  • Always define the symbol ParallelismConfig at module scope: it is only used as type annotation
    • Use the real class if accelerate>=1.10.1, otherwise fall back to Any.
  • Replace the string annotation with Optional[ParallelismConfig] to avoid creating a ForwardRef

This ensures get_type_hints(TrainingArguments) works across Python versions and with older accelerate installs.

@albertvillanova albertvillanova changed the title Fix TrainingArguments.parallelism_config NameError with accelerate < 1.10.1 Fix TrainingArguments.parallelism_config NameError with accelerate<1.10.1 Sep 11, 2025
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@qgallouedec qgallouedec left a comment

Choose a reason for hiding this comment

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

LGTM, waiting for @SunMarc for final review

@qgallouedec qgallouedec requested a review from SunMarc September 11, 2025 14:15
Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Indeed, thanks for this fix ! cc @S1ro1 for visibility, this created some typing issue for ppl relying on older version of accelerate

@SunMarc SunMarc enabled auto-merge (squash) September 11, 2025 15:09
@S1ro1
Copy link
Member

S1ro1 commented Sep 11, 2025

Thanks for the fix, didn't know this would break type-checkers, my bad.

@SunMarc SunMarc merged commit 02ea2b3 into huggingface:main Sep 14, 2025
23 checks passed
ErfanBaghaei pushed a commit to ErfanBaghaei/transformers that referenced this pull request Sep 25, 2025
…10.1 (huggingface#40818)

Fix ParallelismConfig type for accelerate < 1.10.1

Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com>
vijayabhaskar-ev pushed a commit to vijayabhaskar-ev/transformers that referenced this pull request Oct 2, 2025
…10.1 (huggingface#40818)

Fix ParallelismConfig type for accelerate < 1.10.1

Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com>
yuchenxie4645 pushed a commit to yuchenxie4645/transformers that referenced this pull request Oct 4, 2025
…10.1 (huggingface#40818)

Fix ParallelismConfig type for accelerate < 1.10.1

Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com>
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.

6 participants