Skip to content

Conversation

SamuelBarryCS
Copy link
Contributor

@SamuelBarryCS SamuelBarryCS commented Sep 12, 2025

What

  • Fixes Optimize the time complexity of get_balanced_memory #3768 , i.e. reduces the complexity of checking whether modules are leaves by switching from repeated list membership checks (O(N)) to a set-based approach (O(1) average).
  • This optimization lowers the overhead of loading large models, especially MoEs!

How to review

  • Read diff

Tests performed

  • Functionality/logic unchanged -> existing tests continue to pass.

Benchmark results

  • On a representative setup of 3 models (one small, large and very large MoE) , runtime scales much better with the set-based approach.
  • TLDR: ~10–50× faster membership handling for large module lists.
image

@SamuelBarryCS SamuelBarryCS marked this pull request as ready for review September 12, 2025 07:45
@SamuelBarryCS SamuelBarryCS changed the title [WIP] Lower complexity of get_balanced_memory by adding a set Lower complexity of get_balanced_memory by adding a set Sep 12, 2025
@SamuelBarryCS
Copy link
Contributor Author

SamuelBarryCS commented Sep 12, 2025

cc @Jintao-Huang @S1ro1 for review/ stamp :)
Thanks!

@S1ro1
Copy link
Member

S1ro1 commented Sep 16, 2025

Can you please clean up before merge? will stamp after. LGTM rn.

@SamuelBarryCS
Copy link
Contributor Author

Can you please clean up before merge? will stamp after. LGTM rn.

Sure thing @S1ro1 - done now!
Thanks!

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks !

@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.

@SunMarc SunMarc merged commit 409b356 into huggingface:main Sep 17, 2025
23 of 25 checks passed
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.

Optimize the time complexity of get_balanced_memory
4 participants