Skip to content

Fix in-place modification of inputs_embeds in Kosmos-2.5 forward#43418

Merged
zucchini-nlp merged 2 commits intohuggingface:mainfrom
Neuro1729:Neuro1729-patch-1
Jan 23, 2026
Merged

Fix in-place modification of inputs_embeds in Kosmos-2.5 forward#43418
zucchini-nlp merged 2 commits intohuggingface:mainfrom
Neuro1729:Neuro1729-patch-1

Conversation

@Neuro1729
Copy link
Copy Markdown
Contributor

@Neuro1729 Neuro1729 commented Jan 22, 2026

What does this PR do?

Fixes a PyTorch autograd error when fine-tuning Kosmos2_5ForConditionalGeneration with PEFT (LoRA).

The model forward pass performed an in-place assignment on inputs_embeds, which can be a leaf tensor with requires_grad=True during training. This caused the following runtime error:

RuntimeError: a leaf Variable that requires grad is being used in an in-place operation

This PR avoids the in-place modification by cloning the tensor before assignment, allowing training to proceed correctly while preserving existing behavior.

I have verified the fix using the reproduction script I provided in the issue; the RuntimeError is resolved and training proceeds as expected.

Fixes #43416

cc @zucchini-nlp

@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: kosmos2_5

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Thanks!

@zucchini-nlp zucchini-nlp merged commit 95be7ef into huggingface:main Jan 23, 2026
19 checks passed
SangbumChoi pushed a commit to SangbumChoi/transformers that referenced this pull request Jan 23, 2026
…gingface#43418)

* Fix in-place modification of inputs_embeds in Kosmos-2.5 forward

* Removed trailing space
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.

[Kosmos-2.5] RuntimeError: a leaf Variable that requires grad is being used in an in-place operation during training

3 participants