Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor(modeling_llama): make RotaryEmbedding default path explicit#39831

Open
pco111 wants to merge 6 commits into
huggingface:mainfrom
pco111:fix/issue-39753
Open

refactor(modeling_llama): make RotaryEmbedding default path explicit#39831
pco111 wants to merge 6 commits into
huggingface:mainfrom
pco111:fix/issue-39753

Conversation

@pco111

@pco111 pco111 commented Jul 31, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR refactors LlamaRotaryEmbedding to make the initialization for the default rope_type more explicit, as suggested in issue #39753.

Instead of relying on the ROPE_INIT_FUNCTIONS dictionary for the default case, the code now uses a direct call to _compute_default_rope_parameters. The dictionary lookup is reserved for non-default rope_type values. This change improves code readability and maintainability by making the default execution path clearer, aligning with the "explicit is better than implicit" philosophy.

Fixes #39753

Before submitting

Who can review?

@ArthurZucker @gante

Following the suggestion in issue huggingface#39753, this commit refactors the
LlamaRotaryEmbedding (and its copies across other models) to make the
initialization for the default rope_type more explicit.

Instead of relying on the ROPE_INIT_FUNCTIONS dictionary for the default
case, the code now uses a direct call to _compute_default_rope_parameters,
reserving the dictionary lookup for non-default rope types. This improves
code readability and maintainability.

Closes huggingface#39753
Comment on lines +87 to +90
if self.rope_type == "default":
self.rope_init_fn = _compute_default_rope_parameters
else:
self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hey! the idea is not to impor new fucntion, but to explicitly write what the default is! SO torch.arange(......) 😉

pco111 and others added 5 commits August 3, 2025 11:27
…and clarify the processing method of the default rope_type. By directly calculating the default parameters, improve the readability and maintainability of the code, while retaining dictionary search for non-default types.
…and clarify the processing method of the default rope_type. By directly calculating the default parameters, improve the readability and maintainability of the code, while retaining dictionary searches for non-default types.
…and clarify the processing method of the default rope_type. By directly calculating the default parameters, improve the readability and maintainability of the code, while retaining dictionary searches for non-default types.
@pco111

pco111 commented Aug 3, 2025

Copy link
Copy Markdown
Contributor Author

Hi @ArthurZucker, thanks for the feedback! I agree with your proposal and tried several revisions today, but I haven't been able to pass the CI test. Since I may not have enough time to completely solve this problem recently, I'm afraid I have to temporarily close this PR and come back later. Thank you for your review! 😃

Sorry, but let me give it a couple more tries😂

@pco111 pco111 closed this Aug 3, 2025
@pco111 pco111 reopened this Aug 3, 2025
@github-actions

github-actions Bot commented Aug 3, 2025

Copy link
Copy Markdown
Contributor

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

run-slow: arcee, aria, bamba, bitnet, cohere, cohere2, csm, deepseek_v3, dia, diffllama, doge, dots1, emu3, ernie4_5, ernie4_5_moe, evolla

@gante

gante commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

Hey @pco111 👋

@zucchini-nlp is working on a larger refactor of RoPE, which may conflict with this PR 🤔

(@zucchini-nlp can you confirm whether this part of the problem can be worked on independently?)

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

self.config = config
if self.rope_type == "default":
self.rope_init_fn = _compute_default_rope_parameters
base = self.config.rope_theta

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perfect, the only issue I have is that llama does not do partial! and should always have head dim!
It's gonna be a little bit of manual work but let's remove what was not there with the model: no partial when model does not do partial, no head dim if model does not have head dim in config!

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.

Inv frequency has not default, going against our philosophy

3 participants