Skip to content

Conversation

@gante
Copy link
Member

@gante gante commented May 2, 2025

What does this PR do?

Fixes #37874

Problem:
nn.Parameter() defined in __init__ -> we initialize in the meta device -> whatever is stored in this parameter at init time gets shredded

Solution:
Don't init the sinusoidal embeddings in nn.Parameter, e.g. use a non-persistent buffer. The buffer solution is used in other models with sinusoidal embeddings, e.g. here

@github-actions github-actions bot marked this pull request as draft May 2, 2025 15:19
@github-actions
Copy link
Contributor

github-actions bot commented May 2, 2025

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers and trigger CI.

@gante gante marked this pull request as ready for review May 2, 2025 15:30
from datasets import load_dataset

@classmethod
def setUpClass(cls):
Copy link
Member Author

@gante gante May 2, 2025

Choose a reason for hiding this comment

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

prevents regressions: the model here is super small, these 2 tests take <10s on CPU

most of the time is in loading the model and dataset, having setUpClass cuts the run time from 15 to <10s

@gante gante requested a review from Rocketknight1 May 2, 2025 15:36
@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.

module.weight.data.normal_(mean=0.0, std=std)
if module.padding_idx is not None:
module.weight.data[module.padding_idx].zero_()
elif isinstance(module, MusicgenSinusoidalPositionalEmbedding):
Copy link
Member Author

Choose a reason for hiding this comment

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

(we were initializing these weights buffers twice)

self.assertTrue(
output_values.shape == (2, 1, 36480)
) # input values take shape 32000 and we generate from there
torch.testing.assert_close(output_values[0, 0, -16:].cpu(), EXPECTED_VALUES, rtol=1e-4, atol=1e-4)
Copy link
Member Author

Choose a reason for hiding this comment

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

slow ci becomes green with this tol update

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Yep, LGTM!

@gante gante merged commit af2866a into huggingface:main May 6, 2025
20 checks passed
@gante gante deleted the fix_sinusoidal_init branch May 6, 2025 13:49
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request May 14, 2025
* fix init (meta device -> bad numbers)

* fast test

* dont init sinusoidal twice

* make fixup
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.

Speech2TextForConditionalGeneration broken in transformers 4.51.x

3 participants