{"id":5279,"library":"kernels","title":"Hugging Face Kernels","description":"The `kernels` Python library, currently at version 0.13.0, enables other Python libraries and applications to dynamically load optimized compute kernels directly from the Hugging Face Kernel Hub. These kernels are designed to be portable, unique (multiple versions can coexist), and compatible across various Python and PyTorch configurations. The library supports a rapid release cycle, with frequent updates to add new features and improve performance.","status":"active","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/huggingface/kernels","tags":["huggingface","machine-learning","compute-kernels","pytorch","cuda","gpu-acceleration"],"install":[{"cmd":"pip install kernels","lang":"bash","label":"Install with pip"},{"cmd":"pip install kernels torch>=2.5","lang":"bash","label":"Install with PyTorch (recommended for GPU kernels)"}],"dependencies":[{"reason":"Required for most compute kernels, especially those leveraging GPU acceleration (e.g., CUDA).","package":"torch","optional":false},{"reason":"Required for GPU-accelerated kernels. Not a Python package, but a system dependency.","package":"CUDA","optional":true}],"imports":[{"note":"This is the primary function for downloading and accessing kernels from the Hub.","symbol":"get_kernel","correct":"from kernels import get_kernel"}],"quickstart":{"code":"import torch\nfrom kernels import get_kernel\nimport os\n\n# NOTE: For this example to run, you need PyTorch installed with CUDA support\n# and a CUDA-enabled GPU. This example might fail on CPU-only setups.\n# Ensure 'torch' is installed: pip install torch\n\n# Download an optimized activation kernel from the Hugging Face Hub\n# Specifying the version is important to avoid future breaking changes (see warnings)\ntry:\n    activation = get_kernel(\"kernels-community/activation\", version=1)\n    print(\"Kernel downloaded successfully!\")\n\n    # Example usage: Generate a random tensor on a CUDA device\n    if torch.cuda.is_available():\n        x = torch.randn((10, 10), dtype=torch.float16, device=\"cuda\")\n        y = torch.empty_like(x)\n\n        # Run the kernel (e.g., gelu_fast)\n        activation.gelu_fast(y, x)\n        print(\"Kernel executed successfully on CUDA!\")\n        # print(y)\n    else:\n        print(\"CUDA is not available. Skipping kernel execution on GPU.\")\n\nexcept Exception as e:\n    print(f\"An error occurred during quickstart: {e}\")\n    print(\"Please ensure 'torch' is installed with CUDA and you have a compatible GPU.\")","lang":"python","description":"This quickstart demonstrates how to fetch a pre-optimized activation kernel from the Hugging Face Hub and execute it using PyTorch. It highlights the importance of specifying the kernel version for stability and requires a CUDA-enabled environment for GPU execution."},"warnings":[{"fix":"Always pass the `version` argument to `get_kernel()` (e.g., `get_kernel('org/name', version=1)`). Consult kernel documentation for available versions.","message":"Calling `get_kernel()` without specifying a `version` argument is deprecated in `kernels 0.12` and will become an error in `0.14` (except for local kernels).","severity":"breaking","affected_versions":">=0.12"},{"fix":"Kernel authors should migrate to building 'noarch kernels'. Users generally won't need to change their code, but should be aware of potential changes in kernel availability or performance if relying on older 'universal' builds.","message":"Support for 'universal kernels' is being phased out in favor of 'noarch kernels'. Future versions will emit deprecation warnings, eventually leading to removal.","severity":"deprecated","affected_versions":"Future releases (after 0.13)"},{"fix":"Ensure `torch` is installed with CUDA support (`pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118` or similar for your CUDA version) and that you have a functional GPU environment. Attempting to use GPU-only kernels on a CPU-only setup will result in errors.","message":"Most optimized kernels from the Hugging Face Hub (especially `kernels-community`) are designed for GPU acceleration and require a compatible PyTorch installation (e.g., `torch>=2.5` with CUDA).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}