Skip to content

Add helpful error messages for fast_generate when fast_inference=False#3820

Merged
danielhanchen merged 2 commits into
mainfrom
fix/fast-generate-wrapper-helpful-errors
Jan 2, 2026
Merged

Add helpful error messages for fast_generate when fast_inference=False#3820
danielhanchen merged 2 commits into
mainfrom
fix/fast-generate-wrapper-helpful-errors

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

Summary

When users load a model with fast_inference=False but then try to use vLLM-style arguments with fast_generate, they previously got confusing errors. This adds a wrapper that detects common mistakes and provides helpful guidance.

Error cases handled:

  • Using sampling_params: explains to use HuggingFace generate arguments instead
  • Using lora_request: explains LoRA weights are already merged into the model
  • Passing text strings directly: shows how to tokenize input first with example code

Changes

  • Add make_fast_generate_wrapper function to _utils.py
  • Apply wrapper in llama.py when fast_inference=False
  • Apply wrapper in vision.py when fast_inference=False

Example error messages

When using sampling_params:

Unsloth: `sampling_params` is only supported when `fast_inference=True` (vLLM).
Since `fast_inference=False`, use HuggingFace generate arguments instead:
  model.fast_generate(**tokens.to('cuda'), max_new_tokens=64, temperature=1.0, top_p=0.95)

When passing string input:

Unsloth: Passing text strings to `fast_generate` is only supported when `fast_inference=True` (vLLM).
Since `fast_inference=False`, you must tokenize the input first:

  messages = tokenizer.apply_chat_template(
      [{"role": "user", "content": "Your prompt here"}],
      tokenize=True, add_generation_prompt=True,
      return_tensors="pt", return_dict=True
  )
  output = model.fast_generate(
      **messages.to('cuda'),
      max_new_tokens=64,
      temperature=1.0,
  )

Test plan

  • Tested with Qwen3-4B-GRPO (fast_inference=False, 3 training steps + inference)
  • Tested with Gemma3-1B-GRPO (3 training steps + inference)
  • Tested with Gemma3-4B-Vision (wrapper error handling)
  • Verified all error types are caught: sampling_params, lora_request, string input, list of strings

danielhanchen and others added 2 commits January 2, 2026 13:58
When users load a model with fast_inference=False but then try to use
vLLM-style arguments with fast_generate, they previously got confusing
errors. This adds a wrapper that detects common mistakes and provides
helpful guidance:

- Using sampling_params: explains to use HF generate args instead
- Using lora_request: explains LoRA weights are already merged
- Passing text strings: shows how to tokenize input first

Changes:
- Add make_fast_generate_wrapper to _utils.py
- Apply wrapper in llama.py when fast_inference=False
- Apply wrapper in vision.py when fast_inference=False
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @danielhanchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the user experience by preventing confusing errors when fast_generate is used incorrectly without vLLM. It provides clear, actionable error messages with examples, helping users understand how to properly tokenize inputs or use HuggingFace generation arguments instead of vLLM-specific parameters, thereby streamlining the development process and reducing debugging time.

Highlights

  • Improved Error Handling: Introduced helpful and descriptive error messages for fast_generate when fast_inference=False, guiding users on correct usage.
  • Common Mistake Detection: The new wrapper detects and provides guidance for common user mistakes such as using sampling_params, lora_request, or passing raw text strings directly to fast_generate when vLLM is not enabled.
  • Wrapper Implementation: A new function, make_fast_generate_wrapper, has been added to _utils.py to encapsulate this error-checking logic.
  • Integration: The make_fast_generate_wrapper is applied to model.generate in llama.py and vision.py when fast_inference=False.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces helpful error messages for fast_generate when fast_inference=False, which will significantly improve user experience by guiding them away from common vLLM-style API misuses. The implementation is clean, adding a wrapper function that correctly identifies invalid arguments and provides clear, actionable error messages. The changes are consistently applied across llama.py and vision.py. I have one minor suggestion to improve code conciseness.

Comment thread unsloth/models/_utils.py
Comment on lines +2408 to +2414
first_arg = args[0]
is_string_input = False

if isinstance(first_arg, str):
is_string_input = True
elif isinstance(first_arg, (list, tuple)) and len(first_arg) > 0:
if isinstance(first_arg[0], str):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic to detect string input can be simplified into a single boolean expression for better readability and conciseness. The current implementation is correct, but a more Pythonic way would be to combine the checks using boolean short-circuiting.

            is_string_input = isinstance(first_arg, str) or (
                isinstance(first_arg, (list, tuple)) and first_arg and isinstance(first_arg[0], str)
            )

@danielhanchen danielhanchen merged commit 4682c45 into main Jan 2, 2026
4 checks passed
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
…apper-helpful-errors

Add helpful error messages for fast_generate when fast_inference=False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant