Skip to content

Lazily build the default kernel mapping to decouple kernels from normal transformers usage - #46681

Merged
vasqu merged 6 commits into
huggingface:mainfrom
jiqing-feng:pipeline
Jun 19, 2026
Merged

vasqu merged 6 commits into
huggingface:mainfrom
jiqing-feng:pipeline

Conversation

@jiqing-feng

@jiqing-feng jiqing-feng commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Importing transformers.pipeline fails when an incompatible kernels version is installed:

from transformers import pipeline
File ".../kernels/layer/layer.py", line 76, in __init__
    raise ValueError("Either a revision or a version must be specified.")
ValueError: Either a revision or a version must be specified.

This happens even though the user never opts into kernels.

Root cause

integrations/hub_kernels.py builds the default _KERNEL_MAPPING dictionary as a module-level literal, so every LayerRepository(...) / FuncRepository(...) is instantiated eagerly at import time.

Because transformers uses lazy submodule loading, import transformers alone does not touch this module. But from transformers import pipeline forces the chain
pipelines → image_processing_utils → processing_utils → type_validators → activations → hub_kernels,
which eagerly constructs the mapping and trips kernels' new "version or revision required" validation.

The mapping is only ever consumed by register_kernel_mapping_transformers(), which is called exclusively when the user sets use_kernels=True. Building it at import time effectively couples the optional kernels library to all transformers usage.

Fix

Build the default kernel mapping lazily:

  • _build_kernel_mapping() constructs the repositories (unchanged content) only when invoked.
  • get_kernel_mapping_transformers() builds the mapping on first use and caches it.
  • register_kernel_mapping_transformers() now calls get_kernel_mapping_transformers() instead of referencing a pre-built global.

No mapping entries, behavior, or public APIs change.

Result

  • import transformers ✅
  • from transformers import pipeline ✅ (no LayerRepository is instantiated)
  • The kernel mapping is constructed only when the user explicitly enables use_kernels=True.

This decouples kernels from normal transformers usage: users who don't use kernels are no longer affected by kernels-version incompatibilities at import time.

…formers usage

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

Copy link
Copy Markdown
Contributor Author

The failing checks are unrelated to this change.

@Rocketknight1

Copy link
Copy Markdown
Member

Hmmn, not sure about this! If the user has kernels installed, we assume that they want kernels. What scenario are you imagining where the user has installed a version of kernels ahead of the one that's installed with transformers, but also they don't want kernels?

@jiqing-feng

Copy link
Copy Markdown
Contributor Author

Hmmn, not sure about this! If the user has kernels installed, we assume that they want kernels. What scenario are you imagining where the user has installed a version of kernels ahead of the one that's installed with transformers, but also they don't want kernels?

The problem isn't "kernels installed but unwanted" — even users who want kernels are broken, because the failure is unconditional and at import time.

The default mapping is a module-level literal, so every LayerRepository/FuncRepository is built the moment hub_kernels is imported (which from transformers import pipeline triggers). So anyone running a model that never touches these kernels still crashes on plain import pipeline, despite never reaching the use_kernels=True path that actually uses the mapping. (And kernels is often pulled in transitively, not opted into.)

This PR doesn't disable or hide kernels — it just builds the default mapping lazily, on first use (i.e. under use_kernels=True). Kernels users get identical behavior; everyone else just isn't broken at import by an unrelated kernels-version mismatch.

Even with kernels installed, many workflows never actually use it. My goal is simply to make sure those cases don't break on a kernels version mismatch — improving fault tolerance, without changing anything for users who do rely on kernels.

@github-actions

Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46681&sha=59673b

@vasqu

vasqu commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Valid, but will merge the sync PR first and then we can revisit this, wdyt? We should get it merged this week

@vasqu

vasqu commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Meaning #46039

@vasqu

vasqu commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Just merged the PR, feel free to sync + update and then ping me 🤗

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

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

@jiqing-feng

Copy link
Copy Markdown
Contributor Author

Hi @vasqu . I have rebased the PR, and the failed tests are not related to my changes. Please review it. Thanks!

@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.

Just one small nit but we can merge afterwards

# importing transformers (or `transformers.pipeline`) does not instantiate any `LayerRepository` /
# `FuncRepository`. This keeps the `kernels` library decoupled from normal transformers usage: the
# repositories are only constructed when the user explicitly opts in via `use_kernels=True`.
_KERNEL_MAPPING_CACHE: "dict | None" = None

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.

Suggested change
_KERNEL_MAPPING_CACHE: "dict | None" = None
_KERNEL_MAPPING_CACHE: dict | None = None

we can properly type no?

@vasqu
vasqu enabled auto-merge June 19, 2026 12:22
@vasqu
vasqu added this pull request to the merge queue Jun 19, 2026
@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.

Merged via the queue into huggingface:main with commit c2c1386 Jun 19, 2026
104 checks passed
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
…rmal transformers usage (huggingface#46681)

* Lazily build the default kernel mapping to decouple  from normal transformers usage

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

* update name

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

* fix typing

---------

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
Co-authored-by: vasqu <antonprogamer@gmail.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.

4 participants