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

Add support for FP8 KV cache scales #2628

Merged
merged 3 commits into from
Oct 24, 2024
Merged

Conversation

danieldk
Copy link
Member

@danieldk danieldk commented Oct 9, 2024

What does this PR do?

Since FP8 only has limited dynamic range, we can scale keys/values before storing them into the cache (and unscale them in attention). To avoid rescaling the cache as the absmax values change, good scales are usually determined per layer using calibration calibration data and stored in the checkpoint.

This change adds support for for using key-value scales and loading them
from checkpoints in the two most common formats:

  • Separate per-layer k_scale and v_scale scalars.
  • Per-layer kv_scale scalar (older format).

Currently, scales are only used with an float8_e4m3fn cache.

Besides adding support for key/value scales, the fp8_quantize function is also extended to support quantization with a kernel vendored from vLLM. This is slightly faster than the PyTorch implementation, but also scales in FP32, potentially improving accuracy.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@danieldk danieldk force-pushed the feature/fp8-kv-cache-scale branch 3 times, most recently from 08c0b3f to 98efcb4 Compare October 21, 2024 17:25
@danieldk danieldk marked this pull request as ready for review October 22, 2024 09:03
Since FP8 only has limited dynamic range, we can scale keys/values
before storing them into the cache (and unscale them in attention). To
avoid rescaling the cache as the absmax values change, good scales are
usually determined per layer using calibration calibration data and stored
in the checkpoint.

This change adds support for for using key-value scales and loading them
from checkpoints in the two most common formats:

- Separate per-layer `k_scale` and `v_scale` scalars.
- Per-layer `kv_scale` scalar (older format).

Currently, scales are only used with an `float8_e4m3fn` cache.

Besides adding support for key/value scales, the `fp8_quantize` function
is also extended to support quantization with a kernel vendored from
vLLM. This is slightly faster than the PyTorch implementation, but also
scales in FP32, potentially improving accuracy.
Copy link
Collaborator

@mht-sharma mht-sharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the PR @danieldk. This will help me enable FP8 KV cache on ROCm next.

Copy link
Collaborator

@mht-sharma mht-sharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@danieldk danieldk merged commit eab07f7 into main Oct 24, 2024
10 of 12 checks passed
@danieldk danieldk deleted the feature/fp8-kv-cache-scale branch October 24, 2024 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants