Skip to content

Conversation

@mmathew23
Copy link
Collaborator

Transformers v5 refactored rope theta. It now sits inside a RopeParameters object. This PR both fixes the issue for the transformers v5 refactor and keep it backwards compatible.

@danielhanchen the llama.py code has a comment # [TODO] Hack to pass in config - need to remove later. Is that still relevant?

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @mmathew23, 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 breaking change introduced in Transformers v5, where the rope_theta configuration parameter was moved into a RopeParameters object. The changes ensure that the system can correctly retrieve rope_theta from both the new RopeParameters structure and the older direct access method, maintaining full compatibility across different Transformers versions.

Highlights

  • Transformers v5 Compatibility: Updates the code to correctly handle the rope_theta parameter, which was refactored into a RopeParameters object in Transformers v5.
  • Backward Compatibility: Implements a fallback mechanism to ensure that the code remains compatible with older versions of Transformers where rope_theta was directly accessible on the config object.
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 aims to add backward compatibility for a change in transformers where rope_theta was moved into a RopeParameters object. The changes correctly identify the need to handle both cases. However, I've found a couple of areas where the implementation could be made more robust. Specifically, both changes introduce bare except: blocks, which can hide bugs and make debugging difficult. In unsloth/models/llama.py, the fallback logic for rope_theta has a latent bug that could lead to a KeyError. I've provided suggestions to catch more specific exceptions and to fix the potential bug, which will make the code more robust and maintainable.

Comment on lines +1556 to +1558
except:
base = getattr(config, "rope_parameters", {})
base = base["rope_theta"]
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Using a bare except: is generally discouraged as it can catch unexpected errors (like SystemExit or KeyboardInterrupt) and hide bugs. It's better to catch specific exceptions. In this case, you're expecting an AttributeError.

Additionally, the current implementation has a latent bug: if config.rope_theta doesn't exist and config.rope_parameters also doesn't exist, getattr will return an empty dictionary, and the subsequent access base["rope_theta"] will raise a KeyError. This can be confusing to debug.

A cleaner and safer approach is to catch AttributeError specifically and then directly access config.rope_parameters["rope_theta"]. This will provide a more informative error (AttributeError or KeyError) if neither rope_theta nor rope_parameters are found as expected.

Suggested change
except:
base = getattr(config, "rope_parameters", {})
base = base["rope_theta"]
except AttributeError:
base = config.rope_parameters["rope_theta"]

if "RopeParameters" in config:
try:
exec(f"from {config_filepath} import RopeParameters", globals())
except:
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using a bare except: is risky because it catches all exceptions, including system-exiting ones like SystemExit or KeyboardInterrupt, and can hide bugs by silently continuing the loop. It's better to catch a more specific exception. If you expect import-related issues, except ImportError: would be appropriate. If other errors from exec are possible, except Exception: is a safer alternative to a bare except.

Suggested change
except:
except Exception:

Copy link

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

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.

ℹ️ 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 on lines +1554 to +1558
try:
base = config.rope_theta
except:
base = getattr(config, "rope_parameters", {})
base = base["rope_theta"]

Choose a reason for hiding this comment

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

P1 Badge Access rope_parameters without subscripting

With transformers v5 configs where rope_theta was moved into a RopeParameters object, LlamaRotaryEmbedding.__init__ falls back to base = getattr(config, "rope_parameters", {}) and immediately indexes base["rope_theta"]. In v5 rope_parameters is a dataclass object, not a dict, so when configs no longer expose config.rope_theta this path raises 'RopeParameters' object is not subscriptable, preventing model initialization in those environments. The fallback needs to read the attribute rather than subscript the object.

Useful? React with 👍 / 👎.

@danielhanchen danielhanchen merged commit 172886d into unslothai:main Nov 29, 2025
1 check passed
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