Repository navigation
Lazily build the default kernel mapping to decouple kernels from normal transformers usage - #46681
Conversation
…formers usage Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
|
The failing checks are unrelated to this change. |
|
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 |
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 This PR doesn't disable or hide kernels — it just builds the default mapping lazily, on first use (i.e. under 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. |
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46681&sha=59673b |
|
Valid, but will merge the sync PR first and then we can revisit this, wdyt? We should get it merged this week |
|
Meaning #46039 |
|
Just merged the PR, feel free to sync + update and then ping me 🤗 |
Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
|
CI Dashboard: View test results in Grafana |
|
Hi @vasqu . I have rebased the PR, and the failed tests are not related to my changes. Please review it. Thanks! |
vasqu
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
| _KERNEL_MAPPING_CACHE: "dict | None" = None | |
| _KERNEL_MAPPING_CACHE: dict | None = None |
we can properly type no?
|
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. |
…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>
Problem
Importing
transformers.pipelinefails when an incompatiblekernelsversion is installed:This happens even though the user never opts into kernels.
Root cause
integrations/hub_kernels.pybuilds the default_KERNEL_MAPPINGdictionary as a module-level literal, so everyLayerRepository(...)/FuncRepository(...)is instantiated eagerly at import time.Because transformers uses lazy submodule loading,
import transformersalone does not touch this module. Butfrom transformers import pipelineforces the chainpipelines → 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 setsuse_kernels=True. Building it at import time effectively couples the optionalkernelslibrary 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 callsget_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✅ (noLayerRepositoryis instantiated)use_kernels=True.This decouples
kernelsfrom normal transformers usage: users who don't use kernels are no longer affected by kernels-version incompatibilities at import time.