-
Notifications
You must be signed in to change notification settings - Fork 490
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
Fix bugs in _choose_num_blocks() added in #346 #354
Merged
Merged
Changes from all 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 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 |
---|---|---|
|
@@ -174,9 +174,13 @@ def __init__( | |
self.quant_type = quant_type | ||
logger.info(f"Model weights are loaded in {get_dtype_name(torch_dtype, quant_type)} format") | ||
|
||
# For attention cache in GPU or RAM | ||
cache_values_per_block = 2 * self.block_config.hidden_size * attn_cache_tokens | ||
self._cache_bytes_per_block = cache_values_per_block * torch.finfo(self.torch_dtype).bits // 8 | ||
|
||
# For disk cache | ||
self.cache_dir = cache_dir | ||
self.max_disk_space = max_disk_space | ||
self.adapters = adapters | ||
|
||
assert num_blocks is None or block_indices is None, "Please specify num_blocks or block_indices, not both" | ||
|
@@ -197,9 +201,6 @@ def __init__( | |
logger.info(f"Attention cache for all blocks will consume up to {self.attn_cache_bytes / gib:.2f} GiB") | ||
|
||
self.alloc_timeout = alloc_timeout | ||
if cache_dir is None: | ||
cache_dir = DEFAULT_CACHE_DIR | ||
self.max_disk_space = max_disk_space | ||
|
||
assert isinstance(throughput, float) or throughput in ["auto", "eval"] | ||
if throughput in ["auto", "eval"]: | ||
|
@@ -243,20 +244,24 @@ def _choose_num_blocks(self) -> int: | |
else: | ||
total_memory = torch.cuda.get_device_properties(self.device).total_memory | ||
|
||
block_size = get_block_size(self.block_config, "memory", dtype=self.torch_dtype, quant_type=self.quant_type) | ||
|
||
gib = 1024**3 | ||
# Estimate of GPU memory used in rpc_backward (2 GiB for BLOOM, proportional for other models) | ||
autograd_memory = 2 * gib * num_devices / 14336 * self.block_config.hidden_size | ||
|
||
if adapters: | ||
block_size = get_block_size(self.block_config, "memory", dtype=self.torch_dtype, quant_type=self.quant_type) | ||
total_memory_per_block = block_size + self._cache_bytes_per_block | ||
if self.adapters: | ||
# Delay import of petals.utils.peft to avoid unnecessary import of bitsandbytes | ||
from petals.utils.peft import estimate_adapter_memory_per_block | ||
|
||
adapter_memory_per_block = estimate_adapter_memory_per_block( | ||
self.block_config, self.torch_dtype, self.adapters, self.cache_dir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
total_memory_per_block += estimate_adapter_memory_per_block( | ||
self.block_config, | ||
self.torch_dtype, | ||
self.adapters, | ||
use_auth_token=self.use_auth_token, | ||
cache_dir=self.cache_dir, | ||
max_disk_space=self.max_disk_space, | ||
) | ||
total_memory_per_block = block_size + adapter_memory_per_block + self._cache_bytes_per_block | ||
|
||
num_blocks = math.floor((total_memory - autograd_memory) / total_memory_per_block) | ||
assert num_blocks >= 1, "Your GPU does not have enough memory to serve at least one block" | ||
|
This file contains 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.
adapters
is not defined