Skip to content

Enhance get_balanced_memory to ensure adequate GPU allocation for small models with large layers - #47426

Open
xin3he wants to merge 2 commits into
huggingface:mainfrom
xin3he:xinhe/memory
Open

Enhance get_balanced_memory to ensure adequate GPU allocation for small models with large layers#47426
xin3he wants to merge 2 commits into
huggingface:mainfrom
xin3he:xinhe/memory

Conversation

@xin3he

@xin3he xin3he commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

CI

What does this PR do?

Fixes intel/auto-round#1889

For W2A16 facebook/opt-125m, we have below param size:

'model': 185272320, 
'model.decoder.embed_tokens': 154435584,

When there are two GPUs, the current logic will split the param size by //2 and the embedding is dispatched on CUDA:1
max_memory = {0: 97288832, 1: 84647280640}

lm_eval requires embedding on cuda:0 to match the input device.

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. These often are low-quality, or fix extremely minor issues that occur rarely or never in practice.
As a result, we're instituting a rule that first-time contributors should not use code agents to submit PRs or issues.
We'd also ask autonomous "OpenClaw"-like agents not to open any PRs or issues.

Issues/PRs from first-time contributors that violate this rule will probably just be closed without review, and we
might block you, especially if you open more than one or appear to be deliberately ignoring this. We especially do not
want new contributors to jump in on random issues to contribute an agent-written fix. This creates lots of noise
for reviewers and other users and will almost certainly get you blocked.

For more information, please read CONTRIBUTING.md.

  • (First-time contributors only): I confirm that this PR description and code is not written by an LLM or code agent

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the
    Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

…mall models with large layers; add regression test for allocation correctness.
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 30235719757:2
Result: failure | Jobs: 2 | Tests: 6 | Failures: 1 | Duration: 33s

Code quality check failed: test jobs were skipped. Fix the code quality issues and push again to run tests.

@Rocketknight1

Copy link
Copy Markdown
Member

cc @SunMarc for accelerate

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tacking this ! Left a comment.

Comment on lines +327 to +328
if modules_list:
max_layer_size, _ = get_max_layer_size(modules_list, module_sizes, no_split_module_classes)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should get the max_layer_size only from the leaves as the leaves should include the embeddings. I feel that we are taking into account modules that shouldn't be counted. For the no_split_module, it will still be accounted in the buffer above so that's fine to not deal with this case. Also we should only modify the GPU0 memory for this case if possible. In the last GPU, we leave enough space for the rest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated

… by ensuring minimum memory for the first GPU when not using low zero memory.

Signed-off-by: Xin He <xin3.he@intel.com>
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.

[Regresion]: multi-cards tuning+eval causes exceptions at eval phase

3 participants