Skip to content

Support quantization for non-multiples of 32. #328

Closed

Description

@mzbac

Yayi 30b k,v layer has [input_dims=7168, out_dims=112], so it failed to quantize due to error all dimensions should be divisible by 32 for now. FYI, here is the implementation of the yayi2 30b model's k,v layer -> https://huggingface.co/wenge-research/yayi2-30b/blob/main/modeling_yayi.py#L180

Activity

  1. awni commented on Dec 31, 2023

    @awni
    Member

    Yes that's a known limitation of our quantization at the moment. Will mark this as an ehnancment.

    In the meantime one thing you could do as a workaround so you aren't blocked by this is to concatenate the K and V projections and do them as a single matmul (112 * 2 / 32 = 7).

  2. mzbac commented on Dec 31, 2023

    @mzbac
    ContributorAuthor

    Yes that's a known limitation of our quantization at the moment. Will mark this as an ehnancment.

    In the meantime one thing you could do as a workaround so you aren't blocked by this is to concatenate the K and V projections and do them as a single matmul (112 * 2 / 32 = 7).

    Hi Awni, thanks for the reply. Would you be able to give a bit more detail about the workaround? By the way, happy new year! :)

  3. awni commented on Dec 31, 2023

    @awni
    Member

    Yes so there are two projections, one for the keys and one for the values.

    k = x @ Wk.T
    v = x @ Wv.T
    

    Instead you could do something like:

    k, v = mx.split(x @ mx.concatenate([Wk, Wv], axis=0).T, 2)
    

    Now you can pre compute the concatenated matrix and quantize it since its dimensions should be divisible by 32.

  4. mzbac commented on Jan 1, 2024

    @mzbac
    ContributorAuthor

    Thanks @awni , this workaround works. I have added it to the yayi2 example. :)

  5. changed the title [-]Quantization failed for the yayi2 30b model [/-] [+]Support quantization for non-multiples of 32.[/+] on Jan 1, 2024
  6. awni commented on Jan 1, 2024

    @awni
    Member

    Nice.

    I think (at least for the non-group axis) fixing this is mostly a matter of bounds checking in the matmul.. but @angeloskath can say more about if / when we could support more flexible quantization.

  7. awni commented on Jan 10, 2024

    @awni
    Member
  8. awni commented on Jan 19, 2024

    @awni
    Member

    @mzbac not sure you saw, in 0.0.10 this should be fixed. We can add yayi2 to mlx-lm now 😃

  9. mzbac commented on Jan 19, 2024

    @mzbac
    ContributorAuthor

    @mzbac not sure you saw, in 0.0.10 this should be fixed. We can add yayi2 to mlx-lm now 😃

    Nice, I will take a look. Hopefully, it just needs to be mapped to the llama arch and it will work.

  10. awni commented on Jan 19, 2024

    @awni
    Member

    Oh that would be amazing.

  11. mzbac commented on Jan 26, 2024

    @mzbac
    ContributorAuthor

    Please close this now as MLX supports non-32 dims quantization and I have tested it on the yayi2 model, and it works as expected.

  12. added a commit that references this issue on Sep 30, 2026
    1512665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions