MCPcopy Create free account
hub / github.com/modelscope/modelscope / _patch_kernels

Function _patch_kernels

modelscope/utils/hf_util/patcher.py:520–535  ·  view source on GitHub ↗

Monkey-patch the `kernels` library to route HF API calls to ModelScope. Only `kernels.utils._get_hf_api` is replaced; every download, file check and ref listing performed by `kernels` goes through `_MsKernelApi`, so the kernel loading/variant/lock logic stays untouched.

()

Source from the content-addressed store, hash-verified

518
519
520def _patch_kernels():
521 """Monkey-patch the `kernels` library to route HF API calls to ModelScope.
522
523 Only `kernels.utils._get_hf_api` is replaced; every download, file check
524 and ref listing performed by `kernels` goes through `_MsKernelApi`, so
525 the kernel loading/variant/lock logic stays untouched.
526 """
527 try:
528 from kernels import utils as kernels_utils
529 from kernels.utils import _get_hf_api
530 except ImportError:
531 return
532 if hasattr(kernels_utils, '_get_hf_api_origin'):
533 return
534 kernels_utils._get_hf_api_origin = kernels_utils._get_hf_api
535 kernels_utils._get_hf_api = lambda user_agent=None: _MsKernelApi()
536
537
538def _unpatch_kernels():

Callers 5

_patched_apiMethod · 0.90
_kernels_patch_scopeFunction · 0.85
patch_hubFunction · 0.85

Calls 1

_MsKernelApiClass · 0.85

Tested by 3

_patched_apiMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…