Skip to content

Conversation

@MercuryYen
Copy link
Contributor

Summary

This PR updates the LlamaModel_fast_forward function signature in unsloth/models/llama.py to make input_ids optional, defaulting to None.

Motivation

Currently, Unsloth requires input_ids to be present, which prevents the model from accepting inputs_embeds only. This change aligns the Unsloth implementation with the original Hugging Face Transformers source code, where input_ids is optional.

This fixes an issue where missing input_ids would raise a TypeError even if embeddings were provided.

Reference

This restores parity with the upstream implementation referenced in the code comments:
https://github.com/huggingface/transformers/blob/f7650253c477dd48c3cf3ff3d7b4f44f93ee62b9/src/transformers/models/llama/modeling_llama.py#L382

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MercuryYen, 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 flexibility of the LlamaModel_fast_forward function within Unsloth by making the input_ids parameter optional. This crucial change allows the model to process pre-computed inputs_embeds directly, which is vital for various advanced use cases and ensures compatibility with the upstream Hugging Face Transformers library. The modification also addresses a specific TypeError, improving the robustness and usability of the model.

Highlights

  • Function Signature Update: The LlamaModel_fast_forward function signature in unsloth/models/llama.py has been updated to make the input_ids parameter optional, defaulting to None.
  • Support for inputs_embeds: This change enables the model to accept inputs_embeds directly without requiring input_ids, aligning with the behavior of the original Hugging Face Transformers implementation.
  • Bug Fix: The modification resolves a TypeError that previously occurred when input_ids were missing, even if embeddings were provided.
  • Hugging Face Parity: The update restores parity with the upstream Hugging Face Transformers source code, ensuring consistent functionality.
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
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 correctly updates the LlamaModel_fast_forward function signature to make input_ids optional, aligning with the Hugging Face Transformers API. This change enables the use of inputs_embeds without input_ids, fixing a TypeError. The change is well-motivated and correctly implemented. I've added one comment regarding a minor improvement to related error messages for consistency. Since the error messages are outside the diff, I've added the comment to the changed line for visibility.

def LlamaModel_fast_forward(
self,
input_ids: torch.LongTensor,
input_ids: Optional[torch.LongTensor] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This change correctly makes input_ids optional, aligning with the Hugging Face API and enabling the use of inputs_embeds alone.

While reviewing, I noticed a minor inconsistency in the error messages within this function. The error messages on lines 802 and 810 refer to decoder_input_ids and decoder_inputs_embeds, but the function's parameters are input_ids and inputs_embeds.

For better clarity, you might want to update these error messages to use the correct parameter names. For example:

  • Line 802: "Unsloth: You cannot specify both input_ids and inputs_embeds at the same time"
  • Line 810: "Unsloth: You have to specify either input_ids or inputs_embeds"

Since these lines are outside the current diff, I'm mentioning it here for your consideration.

@danielhanchen danielhanchen merged commit ded942c into unslothai:main Nov 20, 2025
1 check passed
@danielhanchen
Copy link
Contributor

Thanks a lot @MercuryYen !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants