Repository: huggingface/transformers
Pull request: Fix TrainingArguments.parallelism_config NameError with accelerate<1.10.1

Fix `TrainingArguments.parallelism_config` NameError with accelerate<1.10.1:
```python
NameError: name 'ParallelismConfig' is not defined
```
```python
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:
```python
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
```bash
pip install transformers accelerate==1.4.0
```
```python
from transformers import HfArgumentParser, TrainingArguments

# This will trigger get_type_hints on TrainingArguments
parser = HfArgumentParser(TrainingArguments)
```
```python
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.

Changed files:
filename: src/transformers/training_args.py
patch: @@ -77,8 +77,10 @@
 
     from .trainer_pt_utils import AcceleratorConfig
 
-    if is_accelerate_available("1.10.1"):
-        from accelerate.parallelism_config import ParallelismConfig
+if is_accelerate_available("1.10.1"):
+    from accelerate.parallelism_config import ParallelismConfig
+else:
+    ParallelismConfig = Any
 
 if is_torch_xla_available():
     import torch_xla.core.xla_model as xm
@@ -1264,7 +1266,7 @@ class TrainingArguments:
             )
         },
     )
-    parallelism_config: Optional["ParallelismConfig"] = field(
+    parallelism_config: Optional[ParallelismConfig] = field(
         default=None,
         metadata={"help": ("Parallelism configuration for the training run. Requires Accelerate `1.10.1`")},
     )