Skip to content

[Feature support] Qwen model support #50

Closed

Description

@peterjc123

Hi, just found this wonderful project. May I ask how could I make the sufficient changes to support the model architecture Qwen-1.8B/7B/14B? The differences of that to LLAMA is that the Q, K, V projection matrices have biases and these weights are fused.

https://huggingface.co/Qwen/Qwen-1_8B-Chat/blob/1d0f68de57b88cfde81f3c3e537f24464d889081/modeling_qwen.py#L269

def __init__(self):
        self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)

def forward(self):
        mixed_x_layer = self.c_attn(hidden_states)

        query, key, value = mixed_x_layer.split(self.split_size, dim=2)

Is it enough to modify the code here to the following?

out = torch.matmul(X, W, out = out)

torch.addmm(bias, X, W, out = out)

Activity

  1. danielhanchen commented on Dec 24, 2023

    @danielhanchen
    Member

    @peterjc123 Thanks! Unsloth was just integrated with Llama-factory, so I think you can use https://github.com/hiyouga/LLaMA-Factory/blob/main/tests/llamafy_qwen.py which will llama-fy Qwen, however I'm assuming Qwen has biases right? Maybe llama-fying won't even work.

    I think maybe if you split the matrices to Q, K, V as you provided, and probably change that line to addmm (though I can't confirm if addmm even works on a vector + matrix), then maybe it works?

  2. peterjc123 commented on Dec 24, 2023

    @peterjc123
    Author

    https://github.com/hiyouga/LLaMA-Factory/blob/main/tests/llamafy_qwen.py which will llama-fy Qwen, however I'm assuming Qwen has biases right? Maybe llama-fying won't even work.

    Yes, that won't work because it will convert to a LLaMa2 model with attention_bias=True, which doesn't seem to be supported in this project. (As can be seen here, only the weights of the QKV and MLP layers are taken into consideration)

    I think maybe if you split the matrices to Q, K, V as you provided, and probably change that line to addmm (though I can't confirm if addmm even works on a vector + matrix), then maybe it works?

    I can do that, but after a second look, I guess rewriting that line to addmm is not right because this function is used for both the forward pass and the backward pass. Maybe The correct thing to do is to do an additional add in the forward pass and pass an addition argument back in the backward pass (the gradient of the bias term)

  3. danielhanchen commented on Dec 24, 2023

    @danielhanchen
    Member

    @peterjc123 Oh my it seems like Qwen support will be much more complicated - I never took an exact look at Qwen, but if it has biases, it will change especially the fast LoRA patching.

  4. peterjc123 commented on Dec 25, 2023

    @peterjc123
    Author

    It will be easier if the biases are non-trainable(by skipping the changes in the backward passes). I'll try out it first.

  5. danielhanchen commented on Dec 25, 2023

    @danielhanchen
    Member

    @peterjc123 Cool! Tell me how it goes! If you need help - ask away!

  6. hiyouga commented on Dec 28, 2023

    @hiyouga

    def original_apply_qkv(self, X):
    Q = self.q_proj(X)
    K = self.k_proj(X)
    V = self.v_proj(X)
    return Q, K, V
    pass

    Qn, Kn, Vn = original_apply_qkv(self, Xn)

    I think it might be possible to consider the bias term?

    Additionally, I'll perform some empirical validation of the fine-tuning performance using llamafied Qwen w/ unsloth later.

  7. peterjc123 commented on Dec 28, 2023

    @peterjc123
    Author

    @hiyouga @danielhanchen Last time, I tried to perform llamafy_qwen first and then import the model through unsloth but it throws an error about the property max_seq_len of FastLlamaModel is not found.

  8. danielhanchen commented on Dec 28, 2023

    @danielhanchen
    Member

    @peterjc123 @hiyouga Oh I think I fixed the max_seq_len issue. In terms of the bias term, I can provide a temporary patch which turns off the manual autograd engine for linear layers with biases, but the rest of the optimizations will stay put. This might make Unsloth 20% slower than the full optims which I'll do for a future release.

  9. danielhanchen commented on Dec 28, 2023

    @danielhanchen
    Member

    Actually I just noticed Qwen has a vocab size of 150,000 something? The CUDA max thread size is 2^16 = 65,536, so Llama is fine (32,000). I think some other models like Yi are cutting it close with a 64,000 vocab, whilst Qwen will definitely not work anyways for now.

    There was another feature request for me to add in more than 2^16 vocab sizes, which will require a complete change of the CE Loss. I can temporarily disable the fast route for the CE Loss if in fact the vocab size exceeds 2^16, and use Pytorch's internal CE Loss.

  10. danielhanchen commented on Dec 29, 2023

    @danielhanchen
    Member

    @hiyouga @peterjc123 Just added prelim Qwen support! :)

  11. danielhanchen commented on Jan 4, 2024

    @danielhanchen
    Member

    Closing this now since Qwen is now supported!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions