-
Notifications
You must be signed in to change notification settings - Fork 287
NVfp4 #2408
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
base: main
Are you sure you want to change the base?
NVfp4 #2408
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2408
Note: Links to docs will display an error until the docs builds have been completed. ❌ 4 New FailuresAs of commit d85d39a with merge base 101c039 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
c58c5b0
to
3948f5d
Compare
3948f5d
to
1025236
Compare
1025236
to
1c007a4
Compare
1c007a4
to
a3d2874
Compare
a3d2874
to
034f892
Compare
034f892
to
92e0622
Compare
92e0622
to
b2c45a1
Compare
b2c45a1
to
7448f45
Compare
7448f45
to
fad58b5
Compare
fad58b5
to
b5a593d
Compare
b5a593d
to
2b4ba64
Compare
2b4ba64
to
5d50579
Compare
5d50579
to
29fa9ef
Compare
bde7328
to
b5e3a78
Compare
@vkuzo Curious if you agree we should roll this into the existing mx tensor? |
b5e3a78
to
8b5df79
Compare
8b5df79
to
79720b3
Compare
79720b3
to
f194e35
Compare
f194e35
to
b08a108
Compare
"scale": None, | ||
} | ||
|
||
quantized_weight = to_linear_activation_quantized( |
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.
can we just write the logic here instead of using to_linear_activation_quantized
? I remember same feedback on the mxfp4 inference tensor.
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.
So you can't just move the logic out here, the entirety of the forward behavior has to be "wrapped" by the subclass. Currently there are two ways to do that, without changing nn.modules.
- Like above; this is subclass composition
- The other is to copy the same behavior into the implementations of the ops,
e.g.
NVFP4's dispatch would need to copy:
ao/torchao/quantization/linear_activation_quantized_tensor.py
Lines 135 to 186 in 7192edf
@implements([torch.nn.functional.linear, aten.linear.default]) | |
def _(func, types, args, kwargs): | |
input_tensor, weight_tensor, bias = ( | |
args[0], | |
args[1], | |
args[2] if len(args) > 2 else None, | |
) | |
if isinstance(weight_tensor, LinearActivationQuantizedTensor): | |
return weight_tensor._quantized_linear_op(input_tensor, weight_tensor, bias) | |
raise NotImplementedError( | |
"LinearActivationQuantizedTensor: No specialized dispatch found for linear op" | |
) | |
@implements([aten.mm.default, aten.addmm.default]) | |
def _(func, types, args, kwargs): | |
if not args[0].is_floating_point(): | |
raise NotImplementedError( | |
"LinearActivationQuantizedTensor: expecting a floating point input" | |
) | |
if func == aten.addmm.default: | |
assert args[1].shape[-1] == args[2].shape[0], ( | |
f"need mat1 shape: {args[1].shape} final" | |
f"dim to match mat2 shape: {args[2].shape} first dim " | |
) | |
input_tensor, weight_tensor, bias = ( | |
args[1], | |
args[2], | |
args[0], | |
) | |
input_quant_func = weight_tensor.input_quant_func | |
original_weight_tensor = weight_tensor.original_weight_tensor | |
qtensor = input_quant_func(input_tensor, **weight_tensor.quant_kwargs) | |
return func(bias, qtensor, original_weight_tensor) | |
else: | |
# aten.mm.default | |
assert args[0].shape[-1] == args[1].shape[0], ( | |
f"need mat1 shape: {args[0].shape} final dim" | |
f"to match mat2 shape: {args[1].shape} first dim" | |
) | |
input_tensor, weight_tensor = ( | |
args[0], | |
args[1], | |
) | |
input_quant_func = weight_tensor.input_quant_func | |
original_weight_tensor = weight_tensor.original_weight_tensor | |
qtensor = input_quant_func(input_tensor, **weight_tensor.quant_kwargs) | |
return func(qtensor, original_weight_tensor) |
Not the end of the world. But for some subclasses that serve dual purpose (dyanmic + weight only, + static, + training) it can be alot of switch statements in the ops as opposed to having the base subclass + some sugar
M, K = a.shape[0], a.shape[1] | ||
N = b.shape[1] | ||
|
||
# Swizzle Dizzle |
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.
lol
Found in: pytorch/ao#2408 Pull Request resolved: #156461 Approved by: https://github.com/vkuzo
b08a108
to
ac989f2
Compare
ac989f2
to
e9708eb
Compare
b03916b
to
d62d63b
Compare
d62d63b
to
d85d39a
Compare
if per_tensor_scale is None: | ||
# We are doing single level scaling | ||
block_scale_fp8 = torch.clamp(block_scale, min=E4M3_EPS, max=F8E4M3_MAX).to( | ||
torch.float8_e4m3fn |
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 down up down pattern of casts is probs overkill
Stacked PRs:
Add NVFP4 Inference flow
Details:
I kept this separate for MX but realistically we should probably merge the two. Basic support for blocksize 16 + e4m3 scales.
Double Quant Update
Ignore previous comments, the double quant is actually really similar to NF4 where you just scale the fp32 scales prior to casting to e4m3 to try and reduce
scale quant error
.I have that implemented now in the Nvfp4 code if a tesor_scale is given, just need to figure out how to thread to cublas param
scale_in_d
or how we want to expose this. We currently don't expose the C matrix to the Python API so we could use alpha as @gau-nernst pointed out to me, however we dont expose alpha either 🙃. However if we wanted to use alpha we would need the value on the host, the sync would likely rule out this option. I might keep this double quant on hold until we have the public api, since I am thinking about addingscale
overloads to addmm. However I read the cublas docs many times and it feels as though passing to scale result should work since we don't set the d_mode and its default value should work.Early Perf
No double quant here
which is even worse than mxfp4..., will profile later
Micro Bench
LLama 70B mlp no TP:
Diffusers
Errors
Annoyingly we are getting an error due to the
view as fp4x2
+ packing https://fburl.com/cd92w431 because this is trying to be bitcast iside inside triton kernel which is very annoying. Not sure how this didn't show up until vllm / w/ mxfp4^ similar to this: triton-lang/triton#6054 but make the same changes in _inductor/utils.py as we did for float8em0
Numerics