Skip to content

[LoRA] use removeprefix to preserve sanity. #11493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2025
Merged

Conversation

sayakpaul
Copy link
Member

@sayakpaul sayakpaul commented May 5, 2025

What does this PR do?

We have multiple recurrences of using the following patterns in our LoRA codebase:

  • k.replace(f"{prefix}.", "")
  • k[len(f"{prefix}.") :]

It's just mind-twisting sometimes and I think removeprefix() just makes it easier.

I have run the integration tests and ensured they are passing.

@sayakpaul sayakpaul requested a review from BenjaminBossan May 5, 2025 11:48
@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.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

Just for other reviewers: The change is not equivalent, as this will strictly check for prefixes, i.e. the new code corresponds to k[len(f"{prefix}.") :] if k.startswith(prefix) else k. Also, removeprefix was introduced in Python 3.9, but 3.8 is EOL so this should be fine.

@sayakpaul
Copy link
Member Author

The change is not equivalent, as this will strictly check for prefixes, i.e. the new code corresponds to k[len(f"{prefix}.") :] if k.startswith(prefix) else k.

@BenjaminBossan also to be clear that in our codebase prefix is quite exclusively used to handle what you meant. So, IMO, removeprefix() is a better alternative to tackle that. Am I mistaken to think that?

@sayakpaul sayakpaul requested a review from a-r-r-o-w May 5, 2025 12:36
@BenjaminBossan
Copy link
Member

So, IMO, removeprefix() is a better alternative to tackle that. Am I mistaken to think that?

No, I agree, I just wanted to highlight the difference in case there is an example where the "prefix" is actually allowed to be in the middle of the string.

@sayakpaul
Copy link
Member Author

For the purposes of this PR and for the use of prefix in the LoRA codebase, I think we're safe there.

Copy link
Member

@a-r-r-o-w a-r-r-o-w left a comment

Choose a reason for hiding this comment

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

Just some nits. Personal preference is to use f-strings but either is okay

@sayakpaul
Copy link
Member Author

Fix for the failing tests #11504.

@sayakpaul sayakpaul merged commit 10bee52 into main May 6, 2025
33 checks passed
@sayakpaul sayakpaul deleted the use-removeprefix branch May 6, 2025 06:48
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.

4 participants