Skip to content

unsloth/Mistral-Small-24B-Instruct-2501 RuntimeError: The size of tensor a (128) must match the size of tensor b (160) at non-singleton dimension 3 #1600

Closed

Description

@ichrnkv

Hi there!
I am trying to infer the new Mistral3 24B, getting a Runtime error related to dimension sizes:
RuntimeError: The size of tensor a (128) must match the size of tensor b (160) at non-singleton dimension 3

Steps to reproduce:

from unsloth import FastLanguageModel


model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Mistral-Small-24B-Instruct-2501",
    load_in_4bit = True,
)
FastLanguageModel.for_inference(model)

messages = [
    {"role": "user", "content": "Describe the tallest tower in the world."},
]
inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors = "pt").to("cuda")
_ = model.generate(input_ids = inputs, max_new_tokens = 1024, use_cache = True)

Has anyone encountered something similar?

Activity

  1. changed the title [-]Mistral Small 2501 RuntimeError: The size of tensor a (128) must match the size of tensor b (160) at non-singleton dimension 3[/-] [+]unsloth/Mistral-Small-24B-Instruct-2501 RuntimeError: The size of tensor a (128) must match the size of tensor b (160) at non-singleton dimension 3[/+] on Feb 1, 2025
  2. mkturkcan commented on Feb 1, 2025

    @mkturkcan

    Seconding this; I am getting the same error.

  3. danielhanchen commented on Feb 2, 2025

    @danielhanchen
    Member

    It should work now hopefully! Apologies! Also better to do:

    from unsloth import FastLanguageModel
    
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "unsloth/Mistral-Small-24B-Instruct-2501",
        load_in_4bit = True,
    )
    FastLanguageModel.for_inference(model)
    
    messages = [
        {"role": "user", "content": "Describe the tallest tower in the world."},
    ]
    inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors = "pt").to("cuda")
    from transformers import TextStreamer
    text_streamer = TextStreamer(tokenizer)
    _ = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 1024, use_cache = True)
  4. ichrnkv commented on Feb 2, 2025

    @ichrnkv
    Author

    hmm. weird, I still get the same error.
    I tried clearing the model cache and reinstalling unsloth

    ~/.local/lib/python3.10/site-packages/unsloth/kernels/rope_embedding.py in forward(ctx, Q, cos, sin, position_ids)
        171         half = Q.shape[-1]//2
        172         RH_Q = torch.cat((-Q[..., half:], Q[..., :half]), dim = -1)
    --> 173         Q *= cos
        174         Q.addcmul_(RH_Q, sin)
        175         # RH_Q *= sin
    
    RuntimeError: The size of tensor a (128) must match the size of tensor b (160) at non-singleton dimension 3
    

    upd: also tried to use original mistralai/Mistral-Small-24B-Instruct-2501 with AutoModelForCausalLM by transformers. Seems to work correctly:

    from transformers import AutoModelForCausalLM, AutoTokenizer
    
    model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-Small-24B-Instruct-2501", device_map="auto")
    tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-Small-24B-Instruct-2501")
    
    messages = [
        {"role": "system", "content": "You are DeepSeek4."},
        {"role": "user", "content": "What is your name?"}
    ]
    
    model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
    
    generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True)
    tokenizer.batch_decode(generated_ids)[0]
  5. macksin commented on Feb 3, 2025

    @macksin

    @ichrnkv got the exact same error for the: unsloth/Llama-3.2-1B-Instruct model.
    Also got a correct inference using the AutoModelForCausalLM.

    #851 seems related.

  6. ichrnkv commented on Feb 3, 2025

    @ichrnkv
    Author

    @macksin downgrading to unsloth==2025.1.5 and transformers==4.47.1 fixed my problem with Mistral3
    @danielhanchen FYI

  7. sunghyunkwon1992 commented on Feb 3, 2025

    @sunghyunkwon1992

    For Mistal-Small-24B, downgrading to 2025.1.5 prevented loading the model, but !pip install -U transformers==4.47.1 worked like a charm.

  8. macksin commented on Feb 4, 2025

    @macksin

    For Mistal-Small-24B, downgrading to 2025.1.5 prevented loading the model, but !pip install -U transformers==4.47.1 worked like a charm.

    Worked, THANKS

  9. rolandtannous commented on Jun 29, 2025

    @rolandtannous
    Contributor

    Hello ,

    This should now work with the latest version of Unsloth.
    Please make sure to upgrade your installation to the latest version using

    pip install --upgrade unsloth-zoo
    pip install --upgrade unsloth
    

    Will close this for now.
    Please feel free to comment back if you are still facing the same issue after trying the above.
    Thank you for using Unsloth

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