-
Notifications
You must be signed in to change notification settings - Fork 364
DTensor support for bfloat16 stochastic rounding #3266
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
Open
EduardDurech
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
EduardDurech:feat/dt_sr
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.
+17
−2
Open
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 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 |
|---|---|---|
|
|
@@ -5,6 +5,13 @@ | |
| # LICENSE file in the root directory of this source tree. | ||
| import torch | ||
| from torch import Tensor | ||
| try: | ||
| from torch.distributed.tensor import DTensor | ||
| except Exception: | ||
| try: | ||
| from torch.distributed._tensor import DTensor | ||
| except Exception: | ||
| DTensor = tuple() | ||
|
Contributor
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. rethrow this instead returning tuple |
||
|
|
||
|
|
||
| # https://github.com/TimDettmers/bitsandbytes/blob/dada530149212d64d4b69534716202659ef37ec8/bitsandbytes/functional.py#L339-L391 | ||
|
|
@@ -117,7 +124,7 @@ def dequant_with_qmap(codes: Tensor, qmap: Tensor, scale: Tensor): | |
| return out.view(codes.shape) | ||
|
|
||
|
|
||
| def _fp32_to_bf16_sr(x_f32: Tensor) -> Tensor: | ||
| def _fp32_to_bf16_sr(_x_f32: Tensor) -> Tensor: | ||
| # For an FP32 number [a31, ..., a16, a15, ..., a0] to be converted to BF16 | ||
| # - Round towards zero: [a31, ..., a16, 0, ..., 0] | ||
| # - Round away from zero: [a31, ..., a16+1, 0, ..., 0] | ||
|
|
@@ -127,6 +134,9 @@ def _fp32_to_bf16_sr(x_f32: Tensor) -> Tensor: | |
| # [a15, ..., a0] / 2^16, where the bit pattern [a15, ..., a0] is interpreted as uint16 | ||
| # | ||
| # we have to use int32 since most arithmetic ops are not implemented for uint32/int16/uint16 | ||
| is_dt = isinstance(_x_f32, DTensor) | ||
| x_f32 = _x_f32.to_local() if is_dt else _x_f32 | ||
|
|
||
| rand_16bit = torch.randint( | ||
| 0, 1 << 16, x_f32.shape, device=x_f32.device, dtype=torch.int32 | ||
| ) | ||
|
|
@@ -142,4 +152,9 @@ def _fp32_to_bf16_sr(x_f32: Tensor) -> Tensor: | |
| ) | ||
| # alternative, slightly faster | ||
| # x_f32_bits = (x_f32_bits + rand_16bit) & 0xFFFF0000 | ||
| return x_f32_bits.view(torch.float32).bfloat16() | ||
| x_bf16_trunc = x_f32_bits.view(torch.float32).bfloat16() | ||
|
|
||
| return DTensor.from_local( | ||
|
Contributor
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. looks reasonable, can we add a test to cover? |
||
| x_bf16_trunc, _x_f32.device_mesh, _x_f32.placements, | ||
| run_check=False, shape=tuple(_x_f32.shape), stride=tuple(_x_f32.stride()), | ||
| ) if is_dt else x_bf16_trunc | ||
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.
is this for different PyTorch versions? If yes, could you clarify which specific versions?
in general we support 3 most recent stable PyTorch releases max, so if it's older than that I'd just leave it out