Skip to content

num_return_sequences: RuntimeError in unsloth/models/llama.py #120

Closed

Description

@malcolmsharpe

Specifying num_return_sequences to generate causes a RuntimeError in unsloth/models/llama.py.

Reproduction notebook (edited from the very useful DPO example notebook). (Note: everything before the final cell is unedited from the DPO example notebook.)

The cell code:

# Reproduce num_return_sequences crash.
chat = [
    {"role": "system", "content": ""},
    {"role": "user", "content": "Hello world."},
]

tokenized_chat = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=True, return_tensors='pt').to('cuda')

# Generating without `num_return_sequences` is fine.
outputs = model.generate(tokenized_chat, max_new_tokens=128, do_sample=True)
# Generating with `num_return_sequences` causes a RuntimeError.
outputs = model.generate(tokenized_chat, max_new_tokens=128, do_sample=True, num_return_sequences=4)

The error:

/usr/local/lib/python3.10/dist-packages/unsloth/models/llama.py in LlamaAttention_fast_forward_inference(self, hidden_states, past_key_value, position_ids)
    110     # assert(n_kv_heads * n_groups == n_heads)
    111 
--> 112     Xn = hidden_states.view(self.hidden_size)
    113     K1, V1 = past_key_value
    114     seq_len = K1.shape[-2]

RuntimeError: shape '[4096]' is invalid for input of size 16384

Activity

  1. danielhanchen commented on Jan 23, 2024

    @danielhanchen
    Member

    @malcolmsharpe WHOOPS I was working to make inference 2x faster, but I accidentally forgot about multi batched inference

  2. Stopwolf commented on Jan 23, 2024

    @Stopwolf

    Maybe this helps, and maybe not.. but I get the same error even though I don't use num_return_sequences. What I do is the following:

    outputs = new_model.generate(
        **inputs,
        do_sample=True,
        max_new_tokens=1024,
        top_p=0.9,
        repetition_penalty=1.20,
        num_beams=4,
        temperature=0.7,
    )
    

    Seems that in this case, num_beams produces this error

  3. danielhanchen commented on Jan 23, 2024

    @danielhanchen
    Member

    @Stopwolf Ye my bad - I'm working on a fix to both - should be fixed by today! Extreme apologies again!

  4. malcolmsharpe commented on Jan 23, 2024

    @malcolmsharpe
    Author

    No worries. I really can't emphasize enough how helpful that DPO notebook has been in getting started doing experiments with the technique. Big thanks for that.

  5. danielhanchen commented on Jan 24, 2024

    @danielhanchen
    Member

    @malcolmsharpe Thanks! Ok taking a bit longer than I expected to fix things - hopefully by today!

  6. danielhanchen commented on Jan 26, 2024

    @danielhanchen
    Member

    FINALLY it works yikes so so sorry @malcolmsharpe and @Stopwolf on the issue.

    outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True,
                             num_return_sequences = 4, do_sample = True,)
    tokenizer.batch_decode(outputs)
    

    image

  7. danielhanchen commented on Jan 26, 2024

    @danielhanchen
    Member

    Update Unsloth only with pip install --upgrade --force-reinstall --no-cache-dir git+https://github.com/unslothai/unsloth.git

  8. added
    fixed
    fixed - pending confirmation
    and removed
    fixed
    on Jan 27, 2024
  9. malcolmsharpe commented on Jan 27, 2024

    @malcolmsharpe
    Author

    Thanks, it works now.

  10. malcolmsharpe commented on Jan 29, 2024

    @malcolmsharpe
    Author

    (Edit: When this happens and doesn't happen is more subtle than I thought. It can still happen on the old commit. I still need to sort that out.)

    (Edit 2: Please disregard this comment in favor of the issue: #144.)

    It seems that num_return_sequences may have broken again since my previous comment. (I haven't made a repro yet, but I'm guessing the cause might be obvious since you've been working in the area recently. Please let me know if you want more details.) Instead of an error, it's just giving nonsense output when num_return_sequences is 2 or larger.

    # Output is sensible when num_return_sequences=1
    [('<|system|>\nYou are a helpful chatbot that assists teachers.</s> \n<|user|>\nPlease write a simple middle school algebra problem in which the student must solve for a variable.</s> \n<|assistant|>\n',
      'Solve for x: 2x + 3 = 7\n\nTo solve for x, we need to isolate the variable on one side of the equation. \n\nFirst, we can subtract 3 from both sides of the equation to get:\n\n2x = 4\n\nNow, we can divide both sides by 2 to get:\n\nx = 2\n\nTherefore, the solution to the equation is x = 2.')]
    
    # Output is nonsense when num_return_sequences=2
    [('<|system|>\nYou are a helpful chatbot that assists teachers.</s> \n<|user|>\nPlease write a simple middle school algebra problem in which the student must solve for a variable.</s> \n<|assistant|>\n',
      'Science math problem-level math problem solving math problem solving a script for me a science experiment that is an English, and easy for a program for me a comprehensive guide on how-level chatbot: Can you are you are we use a.'),
     ('<|system|>\nYou are a helpful chatbot that assists teachers.</s> \n<|user|>\nPlease write a simple middle school algebra problem in which the student must solve for a variable.</s> \n<|assistant|>\n',
      'Sci science experiment with math lesson plan for a story that is the lesson plan for me with the chatbot: Can you program that introductions for \n')]
    

    The bad output was observed in commit 05624642802c7f90dcc7aeea0e1c8d447cde006e.
    I observed good output in commit 1ecc0185a5759c7a0c95dfc96aceea5023cebdfc.

  11. danielhanchen commented on Jan 30, 2024

    @danielhanchen
    Member

    @malcolmsharpe I'll check on it!!!

  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

    fixed - pending confirmation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions