accelerate icon indicating copy to clipboard operation
accelerate copied to clipboard

Optimize the time complexity of get_balanced_memory

Open Jintao-Huang opened this issue 1 year ago • 1 comments

When loading very large MoE models it stalls here; I found this part has O(n^2) complexity. If you convert it to a set, it can be reduced to O(n).

https://github.com/huggingface/accelerate/blob/8b493524c8630dc2eb85ac18906fdcf642eae805/src/accelerate/utils/modeling.py#L1043-L1045

Jintao-Huang avatar Sep 09 '25 15:09 Jintao-Huang

Hey @Jintao-Huang! You're very right, that's definitely a free lunch. I opened a PR to push that small fix, run tests and benchmark to see what speedup this allows on real-world scenarios! Feel free to review (/stamp), very much looking for your feedback :) Thanks!

SamuelBarryCS avatar Sep 12 '25 07:09 SamuelBarryCS