Skip to content

wrong number of dimensions - #3880

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
f14-bertolotti:f14-wrong-ndim
Jan 13, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
f14-bertolotti:f14-wrong-ndim

Conversation

@f14-bertolotti

Copy link
Copy Markdown
Contributor

I believe that batch_seq_len is a typo and it should be batch, seq_len instead.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @f14-bertolotti, 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 addresses a critical dimension unpacking error in a kernel function, ensuring that tensor shapes are correctly interpreted and assigned to their respective variables. This fix is essential for the proper functioning of the swiglu_DWf_DW_dfg_kernel, preventing potential runtime errors or incorrect computations due to misaligned dimensions.

Highlights

  • Dimension Unpacking Correction: The unpacking of tensor dimensions in the swiglu_DWf_DW_dfg_kernel function within unsloth/kernels/swiglu.py has been corrected. Previously, e.shape was incorrectly unpacked into two variables (batch_seq_len, hd), assuming a 2D tensor. This has been updated to batch, seq_len, hd = e.shape, correctly accounting for a 3D tensor and aligning with the expected dimensions for batch, sequence length, and hidden dimension.

🧠 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. ↩

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

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.

Code Review

This pull request correctly addresses an issue in swiglu_DWf_DW_dfg_kernel where unpacking a 3-dimensional tensor's shape into two variables would cause a runtime error. The fix aligns the code with the tensor's expected shape. I've included one suggestion to enhance code clarity by using an explicit assertion for the tensor's dimension, as the unpacked variables are otherwise unused in the function.

Comment thread unsloth/kernels/swiglu.py Outdated

def swiglu_DWf_DW_dfg_kernel(DW, e, g):
batch_seq_len, hd = e.shape
batch, seq_len, hd = e.shape

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

While this change correctly fixes the unpacking of e.shape, the variables batch, seq_len, and hd are not used within this function. This can be slightly confusing for future readers. To make the code's intent clearer, which appears to be asserting the tensor's dimensionality, consider using an explicit assertion. This avoids creating unused variables and makes the check's purpose more obvious.

Suggested change
batch, seq_len, hd = e.shape
assert e.ndim == 3, "Input tensor `e` is expected to be 3-dimensional"

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d1cea5919

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread unsloth/kernels/swiglu.py Outdated
Comment on lines +130 to +131
def swiglu_DWf_DW_dfg_kernel(DW, e, g):
batch_seq_len, hd = e.shape
batch, seq_len, hd = e.shape

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Handle 2D e/g in backward kernel

This change assumes e is 3D (batch, seq_len, hd), but the only caller of swiglu_DWf_DW_dfg_kernel (the LoRA_MLP backward path) explicitly flattens e and g to 2D with view(-1, e.shape[-1]) before invoking the backward kernel. With that callsite, unpacking three dimensions will raise a ValueError at runtime during backward, breaking any training path that uses the SwiGLU LoRA kernel. Keeping the 2D unpack (or accepting both 2D/3D) avoids the crash.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

@f14-bertolotti Thanks but as Codex mentioned above, it's flattened already to 2dims :)

Comment thread unsloth/kernels/swiglu.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

I changed it to a comment if that helps - thanks again!

@danielhanchen
danielhanchen merged commit b96a04c into unslothai:main Jan 13, 2026
@f14-bertolotti

Copy link
Copy Markdown
Contributor Author

I see, I am a bit puzzled because the forward kernel wants a 3D tensor while the backward kernel wants a 2D tensor instead.

abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
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.

2 participants