-
Notifications
You must be signed in to change notification settings - Fork 443
[model_free_ptq] Multi-gpu support, validate on meta model #2448
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
Draft
kylesayrs
wants to merge
4
commits into
main
Choose a base branch
from
kylesayrs/model_free_multi-gpu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−29
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/llmcompressor/entrypoints/model_free/device_balancer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import functools | ||
| import inspect | ||
| from threading import Lock | ||
| from typing import List, Optional, Union | ||
|
|
||
| import torch | ||
| from loguru import logger | ||
|
|
||
| from llmcompressor.entrypoints.model_free.helpers import gpu_if_available | ||
|
|
||
| __all__ = ["DeviceLoadBalancer"] | ||
|
|
||
|
|
||
| class DeviceLoadBalancer: | ||
| """ | ||
| Load balancer for distributing jobs across multiple GPU devices. | ||
| Tracks device usage and provides the least busy device when requested. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| device: Optional[ | ||
| Union[torch.device, str, List[Union[torch.device, str]]] | ||
| ] = None, | ||
| ): | ||
| """ | ||
| Initialize the load balancer with device(s). | ||
|
|
||
| :param device: Device specification - can be: | ||
| - None: auto-select available GPU (cuda, xpu, npu) or fallback to CPU | ||
| - Single device: torch.device or str (e.g., "cuda:0") | ||
| - List of devices: List[torch.device | str] for multi-GPU support | ||
| """ | ||
| # Parse device argument into list of devices | ||
| if isinstance(device, list): | ||
| # Multi-GPU: validate and convert each device | ||
| device_list = [gpu_if_available(d) for d in device] | ||
| else: | ||
| # Single device: create list with single device | ||
| device_list = [gpu_if_available(device)] | ||
|
|
||
| self.devices = device_list | ||
| self.device_usage = {device: 0 for device in self.devices} | ||
| self.lock = Lock() | ||
|
|
||
| def get_device(self) -> torch.device: | ||
| """ | ||
| Get the least busy device. Thread-safe. | ||
|
|
||
| :return: The device with the fewest active jobs | ||
| """ | ||
| with self.lock: | ||
| # Find device with minimum usage | ||
| device = min(self.device_usage.keys(), key=lambda d: self.device_usage[d]) | ||
| self.device_usage[device] += 1 | ||
| return device | ||
|
|
||
| def release_device(self, device: torch.device): | ||
| """ | ||
| Release a device back to the pool. Thread-safe. | ||
|
|
||
| :param device: The device to release | ||
| """ | ||
| with self.lock: | ||
| if device in self.device_usage: | ||
| self.device_usage[device] -= 1 | ||
| else: | ||
| logger.warning(f"Attempted to release unknown device: {device}") | ||
|
|
||
| @staticmethod | ||
| def inject_device(func): | ||
| """ | ||
| Decorator that manages device lifecycle for functions. | ||
|
|
||
| The decorated function should have a 'device' parameter. When calling | ||
| the wrapped function, pass a DeviceLoadBalancer instance in place of | ||
| the device parameter. The decorator will automatically: | ||
| 1. Get a device from the load balancer | ||
| 2. Call the function with that device | ||
| 3. Release the device when complete (even if an exception occurs) | ||
|
|
||
| :param func: Function to decorate (must have a 'device' parameter) | ||
| :return: Wrapped function that accepts load_balancer instead of device | ||
| """ | ||
|
|
||
| @functools.wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| signature = inspect.signature(func) | ||
| bound_args = signature.bind(*args, **kwargs) | ||
| bound_args.apply_defaults() | ||
| kwargs = dict(bound_args.arguments) | ||
|
|
||
| load_balancer: DeviceLoadBalancer = kwargs.pop("device") | ||
| device = load_balancer.get_device() | ||
| kwargs["device"] = device | ||
|
|
||
| try: | ||
| return func(**kwargs) | ||
| finally: | ||
| load_balancer.release_device(device) | ||
|
|
||
| return wrapper | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While using a decorator to manage the device lifecycle is a clever approach, the current implementation of
inject_deviceintroduces ambiguity. It requires the decorated function'sdeviceparameter to accept aDeviceLoadBalancerinstance at the call site, which is then replaced by atorch.deviceobject within the function. This name-based argument type override can be confusing for developers and static analysis tools.A more explicit and less magical pattern would be to remove the decorator and use a
try...finallyblock directly in the functions that require a device. This would improve readability and maintainability.For example,
process_fileinsrc/llmcompressor/entrypoints/model_free/process.pycould be refactored as follows:This approach is clearer and aligns with how
validate_filehandles theload_balancerargument, promoting consistency across the codebase.