mirror of
https://github.com/huggingface/kernels.git
synced 2025-10-20 21:10:02 +08:00
hf-kernels -> kernels (#32)
* hf-kernels -> kernels * Set version to 0.1.7 * hf-kernels.lock -> kernels.lock
This commit is contained in:
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: Test hf-kernels
|
name: Test kernels
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
22
README.md
22
README.md
@ -1,4 +1,4 @@
|
|||||||
# hf-kernels
|
# kernels
|
||||||
|
|
||||||
The Kernel Hub allows Python libraries and applications to load compute
|
The Kernel Hub allows Python libraries and applications to load compute
|
||||||
kernels directly from the [Hub](https://hf.co/). To support this kind
|
kernels directly from the [Hub](https://hf.co/). To support this kind
|
||||||
@ -21,7 +21,7 @@ Here is how you would use the [activation](https://huggingface.co/kernels-commun
|
|||||||
```python
|
```python
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
from hf_kernels import get_kernel
|
from kernels import get_kernel
|
||||||
|
|
||||||
# Download optimized kernels from the Hugging Face hub
|
# Download optimized kernels from the Hugging Face hub
|
||||||
activation = get_kernel("kernels-community/activation")
|
activation = get_kernel("kernels-community/activation")
|
||||||
@ -44,10 +44,10 @@ please take a look at the following guide:
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
To install `hf-kernels`, we recommend installing from the pypi package:
|
To install `kernels`, we recommend installing from the pypi package:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install hf-kernels
|
pip install kernels
|
||||||
```
|
```
|
||||||
|
|
||||||
You should then be able to run the script above (also in [examples/basic.py](examples/basic.py)):
|
You should then be able to run the script above (also in [examples/basic.py](examples/basic.py)):
|
||||||
@ -68,33 +68,33 @@ docker run --gpus all -it --rm -e HF_TOKEN=$HF_TOKEN kernels-reference
|
|||||||
|
|
||||||
Projects that use `setuptools` can lock the kernel versions that should be
|
Projects that use `setuptools` can lock the kernel versions that should be
|
||||||
used. First specify the accepted versions in `pyproject.toml` and make
|
used. First specify the accepted versions in `pyproject.toml` and make
|
||||||
sure that `hf-kernels` is a build dependency:
|
sure that `kernels` is a build dependency:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["hf-kernels", "setuptools"]
|
requires = ["kernels", "setuptools"]
|
||||||
build-backend = "setuptools.build_meta"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[tool.kernels.dependencies]
|
[tool.kernels.dependencies]
|
||||||
"kernels-community/activation" = ">=0.0.1"
|
"kernels-community/activation" = ">=0.0.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
Then run `hf-kernel lock .` in the project directory. This generates a `kernels.lock` file with
|
Then run `kernel lock .` in the project directory. This generates a `kernels.lock` file with
|
||||||
the locked revisions. The locked revision will be used when loading a kernel with
|
the locked revisions. The locked revision will be used when loading a kernel with
|
||||||
`get_locked_kernel`:
|
`get_locked_kernel`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from hf_kernels import get_locked_kernel
|
from kernels import get_locked_kernel
|
||||||
|
|
||||||
activation = get_locked_kernel("kernels-community/activation")
|
activation = get_locked_kernel("kernels-community/activation")
|
||||||
```
|
```
|
||||||
|
|
||||||
**Note:** the lock file is included in the package metadata, so it will only be visible
|
**Note:** the lock file is included in the package metadata, so it will only be visible
|
||||||
to `hf-kernels` after doing an (editable or regular) installation of your project.
|
to `kernels` after doing an (editable or regular) installation of your project.
|
||||||
|
|
||||||
## Pre-downloading locked kernels
|
## Pre-downloading locked kernels
|
||||||
|
|
||||||
Locked kernels can be pre-downloaded by running `hf-kernel download .` in your
|
Locked kernels can be pre-downloaded by running `kernel download .` in your
|
||||||
project directory. This will download the kernels to your local Hugging Face
|
project directory. This will download the kernels to your local Hugging Face
|
||||||
Hub cache.
|
Hub cache.
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ want kernel loading to error when a kernel is not pre-downloaded, you can use
|
|||||||
the `load_kernel` function instead:
|
the `load_kernel` function instead:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from hf_kernels import load_kernel
|
from kernels import load_kernel
|
||||||
|
|
||||||
activation = load_kernel("kernels-community/activation")
|
activation = load_kernel("kernels-community/activation")
|
||||||
```
|
```
|
||||||
|
@ -31,13 +31,13 @@ WORKDIR /app/kernel-test
|
|||||||
# install python depdencies
|
# install python depdencies
|
||||||
RUN uv add torch==2.5.0 numpy
|
RUN uv add torch==2.5.0 numpy
|
||||||
|
|
||||||
# copy hf-kernels lib
|
# copy kernels lib
|
||||||
COPY src ./hf-kernels/src
|
COPY src ./kernels/src
|
||||||
COPY pyproject.toml ./hf-kernels/pyproject.toml
|
COPY pyproject.toml ./kernels/pyproject.toml
|
||||||
COPY README.md ./hf-kernels/README.md
|
COPY README.md ./kernels/README.md
|
||||||
|
|
||||||
# install library
|
# install library
|
||||||
RUN uv pip install -e hf-kernels
|
RUN uv pip install -e kernels
|
||||||
|
|
||||||
# copy examples
|
# copy examples
|
||||||
COPY examples ./examples
|
COPY examples ./examples
|
||||||
@ -48,4 +48,4 @@ ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility
|
|||||||
|
|
||||||
# command to run the script
|
# command to run the script
|
||||||
CMD ["uv", "run", "examples/basic.py"]
|
CMD ["uv", "run", "examples/basic.py"]
|
||||||
# CMD ["ls", "hf-kernels"]
|
# CMD ["ls", "kernels"]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import torch
|
import torch
|
||||||
|
|
||||||
from hf_kernels import get_kernel
|
from kernels import get_kernel
|
||||||
|
|
||||||
print("Starting examples/basic.py demo")
|
print("Starting examples/basic.py demo")
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "hf-kernels"
|
name = "kernels"
|
||||||
version = "0.1.6"
|
version = "0.1.7"
|
||||||
description = "Download cuda kernels"
|
description = "Download cuda kernels"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "OlivierDehaene", email = "olivier@huggingface.co" },
|
{ name = "OlivierDehaene", email = "olivier@huggingface.co" },
|
||||||
@ -29,12 +29,12 @@ dev = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
hf-kernels = "hf_kernels.cli:main"
|
kernels = "kernels.cli:main"
|
||||||
|
|
||||||
[project.entry-points."egg_info.writers"]
|
[project.entry-points."egg_info.writers"]
|
||||||
"hf-kernels.lock" = "hf_kernels.lockfile:write_egg_lockfile"
|
"kernels.lock" = "kernels.lockfile:write_egg_lockfile"
|
||||||
|
|
||||||
#[build-system]
|
#[build-system]
|
||||||
#requires = ["torch", "huggingface_hub", "numpy", "tomli;python_version<='3.10'"]
|
#requires = ["torch", "huggingface_hub", "numpy", "tomli;python_version<='3.10'"]
|
||||||
#build-backend = "hf_kernels.build"
|
#build-backend = "kernels.build"
|
||||||
#backend-path = ["src"]
|
#backend-path = ["src"]
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
from hf_kernels.utils import get_kernel, install_kernel, load_kernel, get_locked_kernel
|
|
||||||
|
|
||||||
__all__ = ["get_kernel", "get_locked_kernel", "load_kernel", "install_kernel"]
|
|
3
src/kernels/__init__.py
Normal file
3
src/kernels/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from kernels.utils import get_kernel, install_kernel, load_kernel, get_locked_kernel
|
||||||
|
|
||||||
|
__all__ = ["get_kernel", "get_locked_kernel", "load_kernel", "install_kernel"]
|
@ -16,7 +16,7 @@ don't require importing typing but then quote them so earlier Python version ign
|
|||||||
them while IDEs and type checker can see through the quotes.
|
them while IDEs and type checker can see through the quotes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from hf_kernels.compat import tomllib
|
from kernels.compat import tomllib
|
||||||
|
|
||||||
TYPE_CHECKING = False
|
TYPE_CHECKING = False
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -49,9 +49,9 @@ def call(
|
|||||||
data = tomllib.load(f)
|
data = tomllib.load(f)
|
||||||
|
|
||||||
for kernel, _ in (
|
for kernel, _ in (
|
||||||
data.get("tool", {}).get("hf-kernels", {}).get("dependencies", {}).items()
|
data.get("tool", {}).get("kernels", {}).get("dependencies", {}).items()
|
||||||
):
|
):
|
||||||
from hf_kernels.utils import install_kernel
|
from kernels.utils import install_kernel
|
||||||
|
|
||||||
install_kernel(kernel, revision="main")
|
install_kernel(kernel, revision="main")
|
||||||
uv_bin = shutil.which("uv")
|
uv_bin = shutil.which("uv")
|
@ -4,14 +4,14 @@ import json
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from hf_kernels.compat import tomllib
|
from kernels.compat import tomllib
|
||||||
from hf_kernels.lockfile import KernelLock, get_kernel_locks
|
from kernels.lockfile import KernelLock, get_kernel_locks
|
||||||
from hf_kernels.utils import build_variant, install_kernel, install_kernel_all_variants
|
from kernels.utils import build_variant, install_kernel, install_kernel_all_variants
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="hf-kernel", description="Manage compute kernels"
|
prog="kernel", description="Manage compute kernels"
|
||||||
)
|
)
|
||||||
subparsers = parser.add_subparsers(required=True)
|
subparsers = parser.add_subparsers(required=True)
|
||||||
|
|
||||||
@ -41,13 +41,13 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
def download_kernels(args):
|
def download_kernels(args):
|
||||||
lock_path = args.project_dir / "hf-kernels.lock"
|
lock_path = args.project_dir / "kernels.lock"
|
||||||
|
|
||||||
if not lock_path.exists():
|
if not lock_path.exists():
|
||||||
print(f"No hf-kernels.lock file found in: {args.project_dir}", file=sys.stderr)
|
print(f"No kernels.lock file found in: {args.project_dir}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
with open(args.project_dir / "hf-kernels.lock", "r") as f:
|
with open(args.project_dir / "kernels.lock", "r") as f:
|
||||||
lock_json = json.load(f)
|
lock_json = json.load(f)
|
||||||
|
|
||||||
all_successful = True
|
all_successful = True
|
||||||
@ -87,7 +87,7 @@ def lock_kernels(args):
|
|||||||
for kernel, version in kernel_versions.items():
|
for kernel, version in kernel_versions.items():
|
||||||
all_locks.append(get_kernel_locks(kernel, version))
|
all_locks.append(get_kernel_locks(kernel, version))
|
||||||
|
|
||||||
with open(args.project_dir / "hf-kernels.lock", "w") as f:
|
with open(args.project_dir / "kernels.lock", "w") as f:
|
||||||
json.dump(all_locks, f, cls=_JSONEncoder, indent=2)
|
json.dump(all_locks, f, cls=_JSONEncoder, indent=2)
|
||||||
|
|
||||||
|
|
@ -7,7 +7,7 @@ from huggingface_hub import HfApi
|
|||||||
from packaging.specifiers import SpecifierSet
|
from packaging.specifiers import SpecifierSet
|
||||||
from packaging.version import InvalidVersion, Version
|
from packaging.version import InvalidVersion, Version
|
||||||
|
|
||||||
from hf_kernels.compat import tomllib
|
from kernels.compat import tomllib
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -123,7 +123,7 @@ def write_egg_lockfile(cmd, basename, filename):
|
|||||||
if kernel_versions is None:
|
if kernel_versions is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
lock_path = cwd / "hf-kernels.lock"
|
lock_path = cwd / "kernels.lock"
|
||||||
if not lock_path.exists():
|
if not lock_path.exists():
|
||||||
logging.warning(f"Lock file {lock_path} does not exist")
|
logging.warning(f"Lock file {lock_path} does not exist")
|
||||||
# Ensure that the file gets deleted in editable installs.
|
# Ensure that the file gets deleted in editable installs.
|
@ -15,8 +15,8 @@ from typing import Dict, List, Optional, Tuple
|
|||||||
from huggingface_hub import hf_hub_download, snapshot_download
|
from huggingface_hub import hf_hub_download, snapshot_download
|
||||||
from packaging.version import parse
|
from packaging.version import parse
|
||||||
|
|
||||||
from hf_kernels.compat import tomllib
|
from kernels.compat import tomllib
|
||||||
from hf_kernels.lockfile import KernelLock, VariantLock
|
from kernels.lockfile import KernelLock, VariantLock
|
||||||
|
|
||||||
CACHE_DIR: Optional[str] = os.environ.get("HF_KERNELS_CACHE", None)
|
CACHE_DIR: Optional[str] = os.environ.get("HF_KERNELS_CACHE", None)
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ def get_locked_kernel(repo_id: str, local_files_only: bool = False):
|
|||||||
|
|
||||||
def _get_caller_locked_kernel(repo_id: str) -> Optional[str]:
|
def _get_caller_locked_kernel(repo_id: str) -> Optional[str]:
|
||||||
for dist in _get_caller_distributions():
|
for dist in _get_caller_distributions():
|
||||||
lock_json = dist.read_text("hf-kernels.lock")
|
lock_json = dist.read_text("kernels.lock")
|
||||||
if lock_json is not None:
|
if lock_json is not None:
|
||||||
for kernel_lock_json in json.loads(lock_json):
|
for kernel_lock_json in json.loads(lock_json):
|
||||||
kernel_lock = KernelLock.from_json(kernel_lock_json)
|
kernel_lock = KernelLock.from_json(kernel_lock_json)
|
@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import torch
|
import torch
|
||||||
from hf_kernels import get_kernel
|
from kernels import get_kernel
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import torch
|
import torch
|
||||||
from hf_kernels import get_kernel
|
from kernels import get_kernel
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from hf_kernels.cli import download_kernels
|
from kernels.cli import download_kernels
|
||||||
|
|
||||||
|
|
||||||
# Mock download arguments class.
|
# Mock download arguments class.
|
||||||
|
Reference in New Issue
Block a user