Skip to content

num_return_sequences in non-ampere colab produces nonsense response #144

Closed

Description

@malcolmsharpe

When specifying num_return_sequences to generate in a colab notebook with a non-ampere GPU (for example, V100), the response is nonsense.

Reproduction notebook (derived from the DPO example notebook): https://colab.research.google.com/drive/1gPm2OVBoA8DlFwTQwD-wu7v0Xq3q0S0d?usp=sharing

(Justification for claim "non-ampere": A100 works as expected. V100 does not.)

Snippet of generating code and example output:

# Generating without `num_return_sequences` produces sensible output.
outputs = model.generate(tokenized_chat, max_new_tokens=128, do_sample=True)
pprint.pprint(tokenizer.decode(outputs[0]))
# Generating with `num_return_sequences` produces nonsense.
outputs = model.generate(tokenized_chat, max_new_tokens=128, do_sample=True, num_return_sequences=2)
pprint.pprint(tokenizer.decode(outputs[0]))

('<|system|>\n'
 '</s> \n'
 '<|user|>\n'
 'Please recite the alphabet.</s> \n'
 '<|assistant|>\n'
 'A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, '
 'Z.</s>')
('<|system|>\n'
 '</s> \n'
 '<|user|>\n'
 'Please recite the alphabet.</s> \n'
 '<|assistant|>\n'
 'A,A B- A - A, Ab- A, a, A, A, A. A, I amp a, A,</s>')

Activity

  1. danielhanchen commented on Jan 30, 2024

    @danielhanchen
    Member

    @malcolmsharpe Oh my let me check - again apologies from my side!

  2. added
    fixed - pending confirmation
    on Jan 30, 2024
  3. danielhanchen commented on Jan 30, 2024

    @danielhanchen
    Member

    @malcolmsharpe I fixed it! Dumb me when I was trying to make inference 2x faster, I accidentally turned off the non training paths for inference.

    So loading the model for inference, then asking "What is a famous tall tower in Paris?" with num_return_sequences = 4:
    image

    Likewise, after training telling the model to continue the Fibonnaci sequence:
    image

    Hopefully it's fixed! I will be super duper careful from now on on editting the inference section - seems like I need to add tests for inference now on my side before I push any commits!!

    Again apologies on the issue! Hope it works!

  4. malcolmsharpe commented on Jan 30, 2024

    @malcolmsharpe
    Author

    It works now. Thanks for the quick fix. (num_return_sequences is handy for doing mid-training eval.)

  5. danielhanchen commented on Feb 4, 2024

    @danielhanchen
    Member

    @malcolmsharpe Hey again!! I just made inference 2x faster natively :) I also tried it on num_return_sequences and num_beams and batched inference, and are all faster (with no gibberish outputs :)) Just pushed it to the main branch, so it'll be absolutely awesome if you could test it out :))

    Call FastLanguageModel.for_inference(model) before doing inference to make it faster :)
    Call FastLanguageModel.for_training(model) to revert it back for finetuning.

    Faster.inference.mp4
  6. malcolmsharpe commented on Feb 4, 2024

    @malcolmsharpe
    Author

    Thanks. I'm on a different project at the moment, but I'll try it next time I'm using that feature.

    A question about switching between inference and training mode: will it work to do that while evaluating during training? (If so, presumably I can switch into inference mode at the start of on_evaluate in my callback, then switch back to training mode at the end?)

  7. danielhanchen commented on Feb 5, 2024

    @danielhanchen
    Member

    @malcolmsharpe Yep! Like this:

            class Eval_Callback(transformers.TrainerCallback):
                def on_evaluate(self, args, state, control, model, **kwargs):
                    data_loader = trainer.get_eval_dataloader(mmlu_dataset)
                    FastLanguageModel.for_inference(model)
                    trainer.log(...)
                    FastLanguageModel.for_training(model, use_gradient_checkpointing = True)
    
            trainer.add_callback(Eval_Callback)
  8. 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