Skip to content
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

Make FP8 scale computation non-differentiable #25

Open
wants to merge 1 commit into
base: fp8_poc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions protoquant/float8/float8_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Float8Tensor(torch.Tensor):
def __new__(cls, data, scale, flavor):
# This is a non-differentiable constructor!
assert not data.requires_grad
assert not scale.requires_grad
# TODO(future): make bits8 easier to work with and switch to using it
# assert data.dtype == torch.bits8
assert scale.dtype == torch.float32
Expand Down
2 changes: 2 additions & 0 deletions protoquant/float8/float8_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ def float8_to_float32(x, flavor):
else: # e5m2
return _hfp8_to_float(x, E5M2_EBITS, E5M2_EXP_BIAS)

@torch.no_grad()
def amax_to_scale(amax, flavor):
if flavor == E4M3:
return E4M3_MAX_POS / torch.clamp(amax, min=EPS)
else: # e5m2
return E5M2_MAX_POS / torch.clamp(amax, min=EPS)

@torch.no_grad()
def tensor_to_scale(x, flavor):
amax = torch.max(torch.abs(x))
return amax_to_scale(amax, flavor)
Expand Down