-
Notifications
You must be signed in to change notification settings - Fork 78
pd: support fp8 kvcache in insert_blocks_to_device #693
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,12 +228,22 @@ def insert_blocks_to_device( | |
| dst_block_indices: torch.Tensor, | ||
| ) -> None: | ||
| """Copy blocks from src_cache to dst_cache on HPU.""" | ||
| # WA: https://github.com/pytorch/pytorch/issues/169656 | ||
| view_as_uint = src_cache.dtype in [torch.float8_e4m3fn, torch.float8_e5m2] | ||
| if view_as_uint: | ||
| src_cache = src_cache.view(torch.uint8) | ||
|
Collaborator
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. view as uint8? Can you explain more, how it helps here?
Contributor
Author
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. index_cpu doesn't support fp8 data type. view as uint8 here only for data movement. |
||
| if isinstance(dst_cache, tuple): | ||
| _src_cache = src_cache[:, src_block_indices] | ||
| for i in range(len(dst_cache)): | ||
| dst_cache[i].index_copy_(0, dst_block_indices, _src_cache[i].to(dst_cache[i].device)) | ||
| indexed_cache = _src_cache[i] | ||
| if view_as_uint: | ||
| indexed_cache = indexed_cache.view(src_cache.dtype) | ||
|
||
| dst_cache[i].index_copy_(0, dst_block_indices, indexed_cache.to(dst_cache[i].device)) | ||
| else: | ||
| dst_cache.index_copy_(0, dst_block_indices, src_cache[src_block_indices].to(dst_cache.device)) | ||
| indexed_cache = src_cache[src_block_indices] | ||
| if view_as_uint: | ||
| indexed_cache = indexed_cache.view(src_cache.dtype) | ||
|
||
| dst_cache.index_copy_(0, dst_block_indices, indexed_cache.to(dst_cache.device)) | ||
| torch.hpu.synchronize() | ||
|
|
||
| @classmethod | ||
|
|
||
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.
The original dtype information is lost after converting
src_cacheto uint8. Later references tosrc_cache.dtypeon lines 240 and 245 will returntorch.uint8instead of the original FP8 dtype. Store the original dtype in a variable before the conversion:original_dtype = src_cache.dtypeand useoriginal_dtypein the view conversions.