Skip to content

Fix kernelize() crash for gpt_oss: missing @use_kernel_func_from_hub on apply_rotary_pos_emb - #45799

Merged
vasqu merged 9 commits into
huggingface:mainfrom
jiqing-feng:dev0
May 7, 2026
Merged

vasqu merged 9 commits into
huggingface:mainfrom
jiqing-feng:dev0

Conversation

@jiqing-feng

Copy link
Copy Markdown
Contributor

Problem

PR #45420 refactored kernel function registration to use a hidden _hidden_kernels dict + temporary register_module attach/detach cycle. This works correctly when the target function is decorated with @use_kernel_func_from_hub (which wraps it into an nn.Module instance via _create_func_module).

However, gpt_oss uses @use_kernelized_func(apply_rotary_pos_emb) where apply_rotary_pos_emb is a plain function — missing the @use_kernel_func_from_hub("rotary_pos_emb") decorator that other models (deepseek_v3, gemma3, etc.) have. This causes:

  1. register_module raises TypeError: ... is not a Module subclass
  2. In the finally block, delattr raises AttributeError since attach never succeeded

Fix

  1. modeling_gpt_oss.py: Add @use_kernel_func_from_hub("rotary_pos_emb") decorator to apply_rotary_pos_emb, consistent with all other models.
  2. modeling_utils.py: Make attach_hidden_kernels defensive — skip non-nn.Module entries with a warning instead of crashing. Use safe _modules dict access for detach.

Reproduce

from transformers import pipeline
generator = pipeline('text-generation', model='openai/gpt-oss-20b', model_kwargs={'use_kernels': True})

error:

......
  File "/opt/venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1077, in apply
    fn(self)
  File "/home/jiqing/transformers/src/transformers/modeling_utils.py", line 4581, in detach_hidden_kernels
    delattr(module, name)
  File "/opt/venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 2086, in __delattr__
    super().__delattr__(name)
AttributeError: 'GptOssAttention' object has no attribute 'apply_rotary_pos_emb'

Regression

Introduced in #45420 (commit 253809c).

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
@jiqing-feng
jiqing-feng marked this pull request as ready for review May 6, 2026 02:57
@jiqing-feng

Copy link
Copy Markdown
Contributor Author

Hi @vasqu . Would you please review this PR? Thanks!

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you add a regression test for gpt oss that runs with kernels --> just that it does not crash

Comment thread src/transformers/modeling_utils.py Resolved
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
@jiqing-feng

Copy link
Copy Markdown
Contributor Author

Hi @vasqu . I have fixed the comment and added the test, please review it. Thanks!

@jiqing-feng

Copy link
Copy Markdown
Contributor Author

The failed CI is not related to my changes.

@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: gpt_oss, openai_privacy_filter

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, I just changed it to raise an ValueError at the end instead. Having a broken registry shouldn't be the case and silently warning will be ignored so I'd rather be more strict here

@vasqu
vasqu enabled auto-merge May 7, 2026 12:21
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@vasqu
vasqu added this pull request to the merge queue May 7, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 7, 2026
@vasqu
vasqu added this pull request to the merge queue May 7, 2026
Merged via the queue into huggingface:main with commit ebac5e5 May 7, 2026
29 checks passed
louzongzhi pushed a commit to louzongzhi/transformers that referenced this pull request May 11, 2026
…ub` on `apply_rotary_pos_emb` (huggingface#45799)

* fix gpt-oss

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>

* fix format

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>

* fix copy

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>

* fix consistency

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>

* fix comment

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>

* change to error instead of warning

* fixup deregister on failure mode (as before) + more comments

---------

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Co-authored-by: vasqu <antonprogamer@gmail.com>
Co-authored-by: Anton Vlasjuk <73884904+vasqu@users.noreply.github.com>
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.

3 participants