-
Notifications
You must be signed in to change notification settings - Fork 69
feat(comm/attn_offload.py): support selective ckpt and cpu offload #383
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
Merged
sunpengsdu
merged 5 commits into
InternLM:develop
from
huangting4201:feat/selective-ckpt-cpu-offload
Dec 31, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88a08d0
feat(comm/attn_offload.py): support selective ckpt and cpu offload
huangting4201 230d81d
feat(comm/attn_offload.py): fix ci lint err
huangting4201 d195810
feat(attn_offload.py): update attn offload manager
huangting4201 132f34c
Merge branch 'develop' into feat/selective-ckpt-cpu-offload
huangting4201 850dec6
fix(conflicts): fix conflicts from merging develop
huangting4201 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .attn_offload import get_offload_manager, initialize_offload_manager | ||
|
|
||
| __all__ = ["initialize_offload_manager", "get_offload_manager"] |
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,135 @@ | ||
| import torch | ||
|
|
||
| from internlm.utils.common import get_current_device | ||
|
|
||
| global_attn_offload = None | ||
|
|
||
|
|
||
| class AttnOffloadManager: | ||
| """ | ||
| A manager for attention output CPU offloading and GPU prefetch loading. | ||
| """ | ||
|
|
||
| def __init__(self, enable_cpu_offload: bool = False) -> None: | ||
| # cpu offload overlapping | ||
| self.cpu_offload = enable_cpu_offload | ||
| # layer id mapping to flash attn output | ||
| self.fa_output_mapping = {} | ||
| self.fa_stream = torch.cuda.Stream() | ||
| self.d2h_final_event = torch.cuda.Event() | ||
| self.h2d_final_event = torch.cuda.Event() | ||
| # prepare for tensor buffer | ||
| self.tensor_id_to_tensor_bufs = {} | ||
|
|
||
| def get_tensor_buf_for_offloaded_tensor(self, tensor, layer_id, tensor_id): | ||
| """Get tensor buffer for offloaded tensor.""" | ||
| layer_id = layer_id % 2 | ||
| if layer_id not in self.tensor_id_to_tensor_bufs: | ||
| self.tensor_id_to_tensor_bufs[layer_id] = {} | ||
|
|
||
| if tensor_id not in self.tensor_id_to_tensor_bufs[layer_id]: | ||
| allocate_new_buf = True | ||
| else: | ||
| tensor_buf = self.tensor_id_to_tensor_bufs[layer_id][tensor_id] | ||
| allocate_new_buf = tensor_buf.size() == tensor.size() and tensor_buf.dtype == tensor.dtype | ||
|
|
||
| if allocate_new_buf: | ||
| # supposed to only execute once | ||
| buffer = torch.empty( | ||
| tensor.size(), | ||
| dtype=tensor.dtype, | ||
| layout=tensor.layout, | ||
| device=tensor.device, | ||
| ) | ||
|
|
||
| self.tensor_id_to_tensor_bufs[layer_id][tensor_id] = buffer | ||
|
|
||
| return self.tensor_id_to_tensor_bufs[layer_id][tensor_id] | ||
|
|
||
| def insert_fa_output_with_layer(self, layer_idx, output): | ||
| assert layer_idx not in self.fa_output_mapping | ||
| if self.cpu_offload is False: | ||
| self.fa_output_mapping[layer_idx] = output | ||
| return | ||
|
|
||
| tensors = [] | ||
| for tensor_id, tensor in enumerate(output): | ||
| if tensor is None: | ||
| tensors.append(None) | ||
| continue | ||
| tensor_buf = self.get_tensor_buf_for_offloaded_tensor(tensor, layer_idx, tensor_id) | ||
| tensor_buf.copy_(tensor) | ||
| tensors.append(tensor_buf) | ||
| self.fa_output_mapping[layer_idx] = tensors | ||
| torch.cuda.current_stream(get_current_device()).synchronize() | ||
huangting4201 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def get_fa_output_with_layer(self, layer_idx): | ||
| assert layer_idx in self.fa_output_mapping | ||
| if self.cpu_offload is False: | ||
| return self.fa_output_mapping.pop(layer_idx) | ||
|
|
||
| # if layer_idx < gpc.config.isp_num_layers - 1: | ||
| # self.fa_stream.wait_event(self.h2d_final_event) | ||
|
|
||
| torch.cuda.current_stream(get_current_device()).synchronize() | ||
| self.fa_stream.synchronize() | ||
| return self.fa_output_mapping.pop(layer_idx) | ||
|
|
||
| def offload_fa_output_with_layer(self, layer_idx): | ||
| assert layer_idx in self.fa_output_mapping | ||
| # if layer_idx > 0: | ||
| # self.fa_stream.wait_event(self.d2h_final_event) | ||
|
|
||
| self.fa_stream.synchronize() | ||
|
|
||
| with torch.cuda.stream(self.fa_stream): | ||
| _gpu_tensors = self.fa_output_mapping.pop(layer_idx) | ||
| _cpu_tensors = [] | ||
| for _tensor in _gpu_tensors: | ||
| if _tensor is None: | ||
| _cpu_tensors.append(_tensor) | ||
| continue | ||
|
|
||
| _cpu_backup = torch.empty( | ||
| _tensor.size(), | ||
| dtype=_tensor.dtype, | ||
| layout=_tensor.layout, | ||
| device="cpu", | ||
| pin_memory=True, | ||
| ) | ||
| _cpu_backup.copy_(_tensor, non_blocking=True) | ||
| _cpu_tensors.append(_cpu_backup) | ||
|
|
||
| # _cpu_tensors.append(_tensor.to("cpu", non_blocking=False)) | ||
|
|
||
| self.fa_output_mapping[layer_idx] = _cpu_tensors | ||
|
|
||
| # self.fa_stream.record_event(self.d2h_final_event) | ||
|
|
||
| def preload_fa_output_with_layer(self, layer_idx): | ||
| assert layer_idx in self.fa_output_mapping | ||
| self.fa_stream.synchronize() | ||
|
|
||
| # Important: get device before with stream, in stream get device is error | ||
| _device = get_current_device() | ||
| with torch.cuda.stream(self.fa_stream): | ||
| _cpu_tensors = self.fa_output_mapping.pop(layer_idx) | ||
| self.fa_output_mapping[layer_idx] = [ | ||
| _tensor.to(device=_device, non_blocking=True) if _tensor is not None else _tensor | ||
| for _tensor in _cpu_tensors | ||
| ] | ||
|
|
||
| # self.fa_stream.record_event(self.h2d_final_event) | ||
|
|
||
|
|
||
| def initialize_offload_manager(enable_cpu_offload: bool = False): | ||
| global global_attn_offload | ||
| if global_attn_offload is None: | ||
| global_attn_offload = AttnOffloadManager(enable_cpu_offload) | ||
|
|
||
| return global_attn_offload | ||
|
|
||
|
|
||
| def get_offload_manager(): | ||
| assert global_attn_offload is not None | ||
| return global_attn_offload | ||
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.