transformers - ✅(Solved) Fix IndentationError when importing DebertaV2Model on Python 3.13 - @torch.jit.script fails to parse function with comment between decorator and def [3 pull requests, 6 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44855Fetched 2026-04-08 01:01:48
View on GitHub
Comments
6
Participants
4
Timeline
22
Reactions
0
Author
Timeline (top)
commented ×6referenced ×5cross-referenced ×3mentioned ×3

Importing DebertaV2Model from transformers (or any library that depends on it, such as gliner) raises an IndentationError on Python 3.13. The error originates in torch.jit.script when it attempts to re-parse the source of a JIT-scripted function that has a comment placed between the @torch.jit.script decorator and the def statement.

Error Message

Importing DebertaV2Model from transformers (or any library that depends on it, such as gliner) raises an IndentationError on Python 3.13. The error originates in torch.jit.script when it attempts to re-parse the source of a JIT-scripted function that has a comment placed between the @torch.jit.script decorator and the def statement.

Root Cause

In modeling_deberta_v2.py, several functions are decorated with @torch.jit.script with a comment in between:

@torch.jit.script
# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

When @torch.jit.script is applied, PyTorch's JIT frontend (torch/_sources.py) calls inspect.getsource() on the function, dedents the source, and passes it to ast.parse(). On Python 3.13, the comment between the decorator and def is included in the extracted snippet. After dedenting, the snippet becomes:

@torch.jit.script
# Copied from ...
def c2p_dynamic_expand(...):
    return ...

Python 3.13's ast.parse (backed by a stricter parser) then fails with IndentationError: expected an indented block after function definition on line 3, seemingly not associating the return statement as the function body when a comment precedes the def in this context.

Fix Action

Workaround

Remove the comments between @torch.jit.script and each def:

# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
@torch.jit.script
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

PR fix notes

PR #44916: fix(deberta-v2): move "Copied from" comments above @torch.jit.script for Python 3.13 compat

Description (problem / solution / changelog)

Summary

Importing DebertaV2Model (or anything that depends on it, e.g. gliner) raises IndentationError on Python 3.13 because torch.jit.script calls inspect.getsource(), dedents the snippet, and passes it to ast.parse(). Python 3.13's stricter parser rejects a comment placed between a decorator and the def statement.

Root Cause

Three functions in modeling_deberta_v2.py had a # Copied from ... comment between @torch.jit.script and def:

@torch.jit.script
# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
def c2p_dynamic_expand(...):
    ...

Fix

Move each # Copied from comment to the line above its @torch.jit.script decorator:

+# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
 @torch.jit.script
-# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
 def c2p_dynamic_expand(...):

Applied to all three affected functions (c2p_dynamic_expand, p2c_dynamic_expand, pos_dynamic_expand).

Fixes #44855

Changed files

  • src/transformers/models/deberta_v2/modeling_deberta_v2.py (modified, +3/-3)
  • src/transformers/models/gpt_neox/configuration_gpt_neox.py (modified, +5/-1)

PR #44917: fix(gpt-neox): preserve rotary_pct across save/load cycle

Description (problem / solution / changelog)

Summary

GPTNeoXConfig.convert_rope_params_to_dict unconditionally overwrote rope_parameters["partial_rotary_factor"] with the default 0.25 when rotary_pct was absent from kwargs.

On every from_pretrained call, rotary_pct is not present as a top-level key in config.json (it is stored inside rope_parameters), so the value was silently reset to 0.25.

Fix

Replace the unconditional assignment with a conditional + setdefault:

-self.rope_parameters["partial_rotary_factor"] = kwargs.pop("rotary_pct", 0.25)
+rotary_pct = kwargs.pop("rotary_pct", None)
+if rotary_pct is not None:
+    self.rope_parameters["partial_rotary_factor"] = rotary_pct
+else:
+    self.rope_parameters.setdefault("partial_rotary_factor", 0.25)

When rotary_pct is absent (the reload path), setdefault only fills in 0.25 if partial_rotary_factor is not already present, so an existing value is preserved.

Fixes #44913

Changed files

  • src/transformers/models/gpt_neox/configuration_gpt_neox.py (modified, +5/-1)

PR #44986: fix: remove Copied from comments between @torch.jit.script and def for Python 3.13 compat

Description (problem / solution / changelog)

Summary

Fixes #44855

On Python 3.13, placing a # Copied from comment between @torch.jit.script and the function definition causes an IndentationError. This happens because torch.jit.script calls inspect.getsource() followed by ast.parse(), and Python 3.13's stricter parser fails to associate the function body with the def when a comment intervenes between the decorator and the function signature.

As suggested by @Rocketknight1, the fix is to remove the # Copied from comments entirely from these locations. The three affected functions (c2p_dynamic_expand, p2c_dynamic_expand, pos_dynamic_expand) are identical to their source in modeling_deberta.py and will remain in sync without the copy markers.

Changes

  • modeling_deberta_v2.py: Removed 3 # Copied from comments between @torch.jit.script decorators and def statements
  • modeling_sew_d.py: Removed the same 3 # Copied from comments (same bug pattern)

Coordination

Testing

  • Verified DebertaV2Model imports successfully after the change
  • Verified sew_d module imports successfully after the change
  • Ruff linting and formatting checks pass

AI assistance disclosure

This PR was prepared with AI assistance (Claude Code). The change was reviewed line-by-line by the submitter before opening.

Changed files

  • src/transformers/models/deberta_v2/modeling_deberta_v2.py (modified, +0/-3)
  • src/transformers/models/sew_d/modeling_sew_d.py (modified, +0/-3)

Code Example

@torch.jit.script
# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

---

@torch.jit.script
# Copied from ...
def c2p_dynamic_expand(...):
    return ...

---

# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
@torch.jit.script
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

---

python -c "from gliner import GLiNER"
RAW_BUFFERClick to expand / collapse

Description

Importing DebertaV2Model from transformers (or any library that depends on it, such as gliner) raises an IndentationError on Python 3.13. The error originates in torch.jit.script when it attempts to re-parse the source of a JIT-scripted function that has a comment placed between the @torch.jit.script decorator and the def statement.

Root Cause

In modeling_deberta_v2.py, several functions are decorated with @torch.jit.script with a comment in between:

@torch.jit.script
# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

When @torch.jit.script is applied, PyTorch's JIT frontend (torch/_sources.py) calls inspect.getsource() on the function, dedents the source, and passes it to ast.parse(). On Python 3.13, the comment between the decorator and def is included in the extracted snippet. After dedenting, the snippet becomes:

@torch.jit.script
# Copied from ...
def c2p_dynamic_expand(...):
    return ...

Python 3.13's ast.parse (backed by a stricter parser) then fails with IndentationError: expected an indented block after function definition on line 3, seemingly not associating the return statement as the function body when a comment precedes the def in this context.

Affected Functions

All in transformers/models/deberta_v2/modeling_deberta_v2.py:

  • c2p_dynamic_expand (line 104)
  • p2c_dynamic_expand (line 110)
  • pos_dynamic_expand (line 116)

Workaround

Remove the comments between @torch.jit.script and each def:

# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
@torch.jit.script
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

Related Issues

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Environment

Python3.13.8
torch2.10.0+cu128
transformers5.3.0
gliner0.2.24
OSUbuntu 24.04.4 LTS (Noble Numbat), kernel 6.17.0-19-generic, x86_64

To Reproduce

python -c "from gliner import GLiNER"

Expected behavior

The import should pass.

extent analysis

Fix Plan

To resolve the IndentationError when importing DebertaV2Model from transformers, follow these steps:

  • Modify the affected functions in transformers/models/deberta_v2/modeling_deberta_v2.py:
    • c2p_dynamic_expand (line 104)
    • p2c_dynamic_expand (line 110)
    • pos_dynamic_expand (line 116)
  • Remove the comments between @torch.jit.script and each def statement.

Code Changes

# Before
@torch.jit.script
# Copied from transformers.models.deberta.modeling_deberta.c2p_dynamic_expand
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

# After
@torch.jit.script
def c2p_dynamic_expand(c2p_pos, query_layer, relative_pos):
    return c2p_pos.expand([...])

Verification

After applying the fix, verify that the import passes without raising an IndentationError:

python -c "from gliner import GLiNER"

If the import is successful, the fix has worked.

Extra Tips

  • This issue is specific to Python 3.13 due to changes in the ast.parse function.
  • The fix involves modifying the transformers library, so ensure you have the necessary permissions and consider submitting a pull request to the library maintainers.
  • If you are using a virtual environment, make sure to apply the fix in the correct environment.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

The import should pass.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING