-
Notifications
You must be signed in to change notification settings - Fork 431
Add k_scale and v_scale to persistent attention #1322
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?
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -130,6 +130,8 @@ def run( | |||||||||||||
kv_cache: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]], | ||||||||||||||
out: Optional[torch.Tensor] = None, | ||||||||||||||
lse: Optional[torch.Tensor] = None, | ||||||||||||||
k_scale: Optional[torch.Tensor] = None, | ||||||||||||||
v_scale: Optional[torch.Tensor] = None, | ||||||||||||||
profiler_buffer: Optional[torch.Tensor] = None, | ||||||||||||||
) -> Tuple[torch.Tensor, torch.Tensor]: | ||||||||||||||
if profiler_buffer is None: | ||||||||||||||
|
@@ -147,6 +149,8 @@ def run( | |||||||||||||
head_dim_qk = q.shape[2] | ||||||||||||||
if self._sm_scale is None: | ||||||||||||||
self._sm_scale = 1.0 / math.sqrt(head_dim_qk) | ||||||||||||||
if k_scale is not None: | ||||||||||||||
self._sm_scale *= k_scale | ||||||||||||||
|
||||||||||||||
# profiler_buffer is optional | ||||||||||||||
profiler_args = (profiler_buffer,) if self._use_profiler else () | ||||||||||||||
|
@@ -169,5 +173,11 @@ def run( | |||||||||||||
self._sm_scale, | ||||||||||||||
*profiler_args, | ||||||||||||||
) | ||||||||||||||
if v_scale is not None: | ||||||||||||||
# TODO(Zihao): fused into kernel | ||||||||||||||
if out.itemsize == 1: | ||||||||||||||
out = (out.to(float) * v_scale).to(out.dtype) | ||||||||||||||
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. When the out.copy_((out.to(float) * v_scale).to(out.dtype)) 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. Can you also implement fusing the multiply inside the kernel in the persistent kernel template? Also, it will be good to specify an out_dtype, for quantized input such as fp8, we usually use bf16 as output data type. You can check Lines 1198 to 1203 in 435f341
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. In that case we need to pass in |
||||||||||||||
else: | ||||||||||||||
out *= v_scale | ||||||||||||||
|
||||||||||||||
return out, lse |
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.
Modifying the instance attribute
self._sm_scale
in-place can lead to incorrect behavior ifrun()
is called multiple times, as the scaling factor will accumulate with each call. The effective scale should be calculated in a local variable within therun
method.