Skip to content
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

[Bugfix]: DeepseekR1 model load fails with weights tied error #13335

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cennn
Copy link
Contributor

@cennn cennn commented Feb 15, 2025

Fix: 12541
This PR resolves the bug causing the DeepseekR1 model to fail during loading due to weight-tied errors.

The implemented changes are as follows:
1 Make MLACommonImpl inherit from Module for easy use of _process_weights_after_loading during parameter decompression.
2 Set tie_weights=False in maybe_offload_to_cpu to prevent weight-tying issues.
3 Add gc.collect to _process_weights_after_loading to tackle potential memory leaks that may lead to "cuda out of memory" errors.

It should be noted that due to the offload operation and the use of gc.collect, the model loading time is approximately 2 minutes longer. The startup command utilized is llm = LLM("deepseek-ai/DeepSeek-R1", tensor_parallel_size = 8, cpu_offload_gb = 50, trust_remote_code = True). (on 8*H100 80G).

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@mgoin
Copy link
Member

mgoin commented Feb 15, 2025

I think this is a good call although calling gc.collect after every single module is very conservative. I would like to not increase the loading time of weights by so much. Maybe just using torch.cuda.empty_cache would be faster? FYI @LucasWilkinson

output = functional_call(module,
device_state,
args=args,
kwargs=kwargs)
kwargs=kwargs,
tie_weights=False)
Copy link
Member

Choose a reason for hiding this comment

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

this line should be the key to fix the bug

@youkaichao
Copy link
Member

I think this is a good call although calling gc.collect after every single module is very conservative. I would like to not increase the loading time of weights by so much. Maybe just using torch.cuda.empty_cache would be faster? FYI @LucasWilkinson

@mgoin I think you need gc.collect() to gc python variables, otherwise calling torch.cuda.empty_cache will not work.

I suspect torch.cuda.empty_cache would be even slower as it calls cuda malloc functions.

# cleared, leading to "cuda out of memory" errors.
# TODO: Investigate the cause of the memory leak. It may be
# related to specific Python versions.
gc.collect()
Copy link
Member

Choose a reason for hiding this comment

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

ideally we can run this just for every layer. but we don't have the concept of layer here.

# scenarios involving CPU offloading. When CPU offloading is
# in use, we transfer the parameters to the device for
# processing and then move them back to the CPU afterwards.
with device_loading_context(module.impl, target_device):
Copy link
Member

Choose a reason for hiding this comment

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

@mgoin I think ultimately we need to use the tensor subclass approach for cpu offloading, then we don't need to manually move cpu tensors back and forth. that would require #12158 though.

@BigCousin-z
Copy link

Is there any plan to implement the cpu_offload_gb technology in vLLM for Mixture of Experts (MoE) models? Specifically, can it support the configuration of offloading strategies based on usage scenarios or specified layers?

@BigCousin-z
Copy link

@cennn @youkaichao

@mgoin
Copy link
Member

mgoin commented Feb 17, 2025

@cennn @youkaichao Maybe we could compromise by making a utility that doesn't run GC for every single module, but instead does it every N calls. What do you think of something like this for now?

class GarbageCollector:
    def __init__(self, collection_frequency: int):
        self.counter = 0
        self.collection_frequency = collection_frequency
    
    def maybe_collect(self):
        self.counter += 1
        if self.counter >= self.collection_frequency:
            self.counter = 0
            gc.collect()

def _process_weights_after_loading(model: nn.Module, model_config: ModelConfig,
                               target_device: torch.device) -> None:
    collector = GarbageCollector(collection_frequency=5)
    
    for _, module in model.named_modules():
        quant_method = getattr(module, "quant_method", None)
        if isinstance(quant_method, QuantizeMethodBase):
            with device_loading_context(module, target_device):
                quant_method.process_weights_after_loading(module)
            collector.maybe_collect()

    for _, module in model.named_modules():
        if isinstance(module, Attention) and \
            hasattr(module.impl, "process_weights_after_loading"):
            with device_loading_context(module.impl, target_device):
                module.impl.process_weights_after_loading(model_config.dtype)
            collector.maybe_collect()

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.

[Bug]: DeepseekR1 model load fails with weights tied error
4 participants