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 exl2-quantized models #1965

Merged
merged 2 commits into from
May 30, 2024
Merged

Add support for exl2-quantized models #1965

merged 2 commits into from
May 30, 2024

Conversation

danieldk
Copy link
Member

What does this PR do?

Add support for exl2 quantization

Mostly straightforward, changes to existing code:

  • Wrap quantizer parameters in a small wrapper to avoid passing around untyped tuples and needing to repack them as a dict.
  • Move scratch space computation to warmup, because we need the maximum input sequence length to avoid allocating huge scratch buffers that OOM.

Draft: needs a rebase, exllama kernels seem non-deterministic, so logprobs sometimes change slightly?

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 changed the title Feature/exl2 Add support for exl2-quantized models May 28, 2024
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@danieldk danieldk force-pushed the feature/exl2 branch 5 times, most recently from f3e8eac to 8e03024 Compare May 28, 2024 15:26
@danieldk danieldk marked this pull request as ready for review May 28, 2024 15:26
Copy link
Collaborator

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

Things seem to be working.

In general though, I feel like the PR is currently adding way too many indirections than necessary, every logic should be in their appropriate files and never add more.

  • layers/tensor_parallel is about the actual parallel logic, it can have some slight variations based on quantization, but only to change higher order loading logic.
  • weights.py is all about creating the actual individual tensors required on the model. This one knows about quantization and how to shard tensors.
  • layers/gptq anything else that is GPTQ specific, not about loading/sharding tensors and more about running init phase (scratch buffers) and actual forwards.

docs/source/basic_tutorials/launcher.md Outdated Show resolved Hide resolved
revision="3.0bpw",
# Set max input length to avoid OOM due to extremely large
# scratch buffer.
max_input_length=1024,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still need that ? I thought you fixed it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed that it does not unconditionally use a 4096 input length, but the value given by warmup. The default is probably still too much. E.g., in Llama2 8B for the output layer

4096 length * 16 batch size 128,256 pieces * 2 sizeof(float16) = 15.7GiB

The scratch buffer is allocated for the worst-case.

integration-tests/models/test_flash_gemma_exl2.py Outdated Show resolved Hide resolved
launcher/src/main.rs Outdated Show resolved Hide resolved
q_invperm: Optional[torch.Tensor] = None

@property
def device(self) -> torch.device:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove that. weigths.qweight.device is just as easy to read and clearer imho.

server/text_generation_server/utils/weights.py Outdated Show resolved Hide resolved

w = Exl2Weight(**tensors)
w.q_scale_max /= 256
w.q_perm = w.q_perm.short()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why ?

Can't we make the dataclass immutable (or at least treat it as is).
imho once loaded, nothing should be modified in tensors and always just passed as-is. Nothing should be required at runtime.

Copy link
Member Author

@danieldk danieldk May 29, 2024

Choose a reason for hiding this comment

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

Moved this to a proper dataclass constructor + __post_init__.

# Find the size of the scratch space.
for layer in LAYERS:
FIXED_BYTES = max(
FIXED_BYTES, layer.scratch_space_fixed(max_input_len=max_total_tokens)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering why we need 2 loops.

Do you have the link in the original repo for that logic ?

Copy link
Member Author

@danieldk danieldk May 29, 2024

Choose a reason for hiding this comment

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

https://github.com/turboderp/exllamav2/blob/8a57be1edfd0c3e6387a14876a359ef95f477739/exllamav2/model.py#L477

We need to know to know the max memory use of all layers to allocate the scratch buffer, before we can pass (slices of) the scratch buffer to the layers in their post-init.

Copy link
Member Author

@danieldk danieldk May 29, 2024

Choose a reason for hiding this comment

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

Also replaced the loop with a more readable for-comprehension now.

server/text_generation_server/utils/weights.py Outdated Show resolved Hide resolved
@danieldk danieldk force-pushed the feature/exl2 branch 2 times, most recently from d14c046 to 4057345 Compare May 29, 2024 18:53
Mostly straightforward, changes to existing code:

* Wrap quantizer parameters in a small wrapper to avoid passing
  around untyped tuples and needing to repack them as a dict.
* Move scratch space computation to warmup, because we need the
  maximum input sequence length to avoid allocating huge
  scratch buffers that OOM.
This test fails somewhat regularly due to non-determinism and this
test is primarily to verify that we are loading a model which doesn't
have `float16` as the default dtype correctly.
@danieldk danieldk requested a review from Narsil May 30, 2024 09:15
Copy link
Collaborator

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

LGTM

@danieldk danieldk merged commit 967ced2 into main May 30, 2024
9 checks passed
@danieldk danieldk deleted the feature/exl2 branch May 30, 2024 09:28
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.

3 participants