Python 3.13.8 breaks DeBERTa-v2 and SEW-D model loading
Author: albertvillanova
Pull request: no
Labels: name: bug

Python 3.13.8 breaks DeBERTa-v2 and SEW-D model loading.

### Describe the bug

Python 3.13.8 contains a regression in the `inspect` module that breaks PyTorch JIT script compilation when comments appear between decorators and function definitions. This makes DeBERTa-v2 and SEW-D models completely unusable on Python 3.13.8.

### Reproduction

```python
# Python 3.13.8
from transformers import AutoModel

# This fails with IndentationError
model = AutoModel.from_pretrained("microsoft/deberta-v2-xlarge")
```
  
Error:
```python
IndentationError: expected an indented block after function definition on line 3
```

### Root Cause

The issue is in these files:
- transformers/models/deberta_v2/modeling_deberta_v2.py (lines 105-108, 111-114, 117-120)
- transformers/models/sew_d/modeling_sew_d.py (similar pattern)

Both use:
```python
@torch.jit.script
# Copied from transformers.models.deberta.modeling_deberta.xxx
def function_name(...):
    ...
```

Python 3.13.8's `inspect.BlockFinder` incorrectly treats the comment as the start of a lambda/generator expression, truncating the source before the function body, which then fails to parse.

- ✅ Works in Python 3.13.7 and earlier
- ❌ Broken in Python 3.13.8
- 🔄 Expected to be fixed in Python 3.13.9

### Upstream Bug

This is a Python bug tracked at: 
- https://github.com/python/cpython/issues/139783

### Impact

Affected:
- ❌ All DeBERTa-v2 models cannot be loaded
- ❌ All SEW-D models cannot be loaded
- ❌ Any downstream library using these models (e.g., trl, llm-blender)

Not Affected:
- ✅ Other model architectures work fine
- ✅ Config loading works for all models

### Workaround

- For users: Use Python 3.13.7 or earlier, or wait for Python 3.13.9

- For transformers maintainers (optional): Consider removing the comment between decorator and function: move comment above the decorator
```python
# Copied from transformers.models.deberta.modeling_deberta.xxx
@torch.jit.script
def function_name(...):
    ...
```

### Environment

- Python: 3.13.8
- transformers version: 4.57.0
- PyTorch: 2.8.0

### Additional Context

This issue was discovered while using trl library's PairRMJudge, which uses llm-blender, which loads DeBERTa models via AutoModel.from_pretrained().

Related discussions:
- TRL issue:
  - https://github.com/huggingface/trl/issues/4239
- CPython bug: 
  - https://github.com/python/cpython/issues/139783


Comments:
Sadly, we cannot move the `# Copied from ...` statement above the decorator, otherwise it will break as well 😓 

We do want to refactor our models with modular, so these copied statements shouldn't be needed at that point.

The issue has been resolved with the release of Python 3.13.9: https://docs.python.org/release/3.13.9/whatsnew/changelog.html#python-3-13-9-final
> Release date: 2025-10-14
> - [gh-139783](https://github.com/python/cpython/issues/139783): Fix [inspect.getsourcelines()](https://docs.python.org/release/3.13.9/library/inspect.html#inspect.getsourcelines) for the case when a decorator is followed by a comment or an empty line.

All relevant `trl` tests now pass successfully on this version:
```
Successfully set up CPython (3.13.9)

[gw3] PASSED tests/test_judges.py::TestJudges::test_pair_rm_judge
```