Fix GPT-OSS BlockMask error during inference#3982
Conversation
GPT-OSS models use eager attention during inference because flex attention returns incorrect results (likely due to left padding). However, when _attn_implementation is set to "flex_attention", transformers creates BlockMask objects which cause a TypeError when passed to the eager attention path: TypeError: unsupported operand type(s) for +=: 'Tensor' and 'BlockMask' This fix excludes GPT-OSS from using flex_attention, keeping it on the eager path to avoid the BlockMask/Tensor type mismatch.
Summary of ChangesHello @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 addresses a critical runtime error in GPT-OSS models during inference. By preventing these models from being configured with Highlights
🧠 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. Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request effectively resolves a TypeError that occurs with GPT-OSS models during inference. The fix correctly prevents the use of flex_attention for these models by adding a specific check for the gpt_oss model type within the prefer_flex_attn_if_supported function. The change is clear, targeted, and directly addresses the problem described. The implementation is solid.
GPT-OSS models use eager attention during inference because flex attention returns incorrect results (likely due to left padding). However, when _attn_implementation is set to "flex_attention", transformers creates BlockMask objects which cause a TypeError when passed to the eager attention path: TypeError: unsupported operand type(s) for +=: 'Tensor' and 'BlockMask' This fix excludes GPT-OSS from using flex_attention, keeping it on the eager path to avoid the BlockMask/Tensor type mismatch.
Summary
flex_attentioninprefer_flex_attn_if_supported()TypeError: unsupported operand type(s) for +=: 'Tensor' and 'BlockMask'during inference/generationProblem
GPT-OSS models use eager attention during inference because flex attention returns incorrect results (likely due to left padding issues). See the comment in
gpt_oss.py:However, when
_attn_implementationis set to"flex_attention", transformers createsBlockMaskobjects. When these are passed to the eager attention path during inference, it causes:Fix
Check
config.model_typeand skip settingflex_attentionforgpt_ossmodels, keeping them on the eager path.Testing
_attn_implementation: eager