Skip to content

Conversation

@drbh
Copy link
Collaborator

@drbh drbh commented Aug 7, 2025

This PR update the get_local_kernel function to handle more paths and infer the variant if needed.

These changes handle specifying the top level of the kernel repo or a specific variant directory as shown in the example below

git clone https://huggingface.co/kernels-community/activation
uv run get-local.py
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "numpy",
#     "torch==2.7.0",
#     "kernels",
# ]
#
# [tool.uv.sources]
# kernels = { git = "https://github.com/huggingface/kernels.git", branch = "improve-get-local-kernel" }
# ///
import torch
from kernels import get_local_kernel
from pathlib import Path

# set seed for reproducibility
torch.manual_seed(0)

activation = get_local_kernel(
    repo_path=Path("./activation"),
    # repo_path=Path("/home/ubuntu/.cache/huggingface/hub/models--kernels-community--activation/snapshots/2fafa6a3a38ccb57a1a98419047cf7816ecbc071/"),
    # repo_path=Path("activation/build/torch27-cxx11-cu126-x86_64-linux"),
    package_name="activation",
)

print(activation)

# Random tensor
x = torch.randn((10, 10), dtype=torch.float16, device="cuda")

# Run the kernel
y = torch.empty_like(x)
activation.gelu_fast(y, x)

# print(y)
sum_of_y = y.sum().item()
print(f"Sum of y: {sum_of_y}")

@HuggingFaceDocBuilderDev

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.

Copy link
Member

@pcuenca pcuenca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, haven't checked the details but looks good conceptually (and the example scripts looks quite complete).

Copy link
Collaborator

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for adding this 🤗

@drbh drbh merged commit 1caa4c1 into main Aug 8, 2025
13 checks passed
Comment on lines +254 to +266
# Presume we were given the top level path of the kernel repository.
for base_path in [repo_path, repo_path / "build"]:
# Prefer the universal variant if it exists.
for v in [universal_variant, variant]:
package_path = base_path / v / package_name / "__init__.py"
if package_path.exists():
return import_from_path(package_name, package_path)

# If we didn't find the package in the repo we may have a explicit
# package path.
package_path = repo_path / package_name / "__init__.py"
if package_path.exists():
return import_from_path(package_name, package_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add test cases for these? Not for universal vs. non-universal, but for the three different types of paths that are supported?

Probably best to change the fixture here:

def local_kernel():

to return the path and not the loaded kernel. Then the existing test could be extended to test these.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea no problem, totally agree that we should have tests for this. Just opened a PR here #134

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.

5 participants