mirror of
https://github.com/huggingface/kernels.git
synced 2025-10-21 21:38:52 +08:00
Compare commits
6 Commits
v0.9.0
...
type-kerne
Author | SHA1 | Date | |
---|---|---|---|
e7173d9a28 | |||
e487945cf6 | |||
338752c367 | |||
53723ea986 | |||
95d3a758a9 | |||
da701bf58a |
@ -265,6 +265,7 @@ Capabilities behave as follows:
|
||||
an existing kernel, the new kernel will replace the old kernel.
|
||||
- When there are multiple kernels that support a capability, the kernel
|
||||
with the smaller capability interval will be used. E.g. given:
|
||||
|
||||
- `KernelA` with `min_capability=80` and `max_capability=89`;
|
||||
- `KernelB` with `min_capability=75` and `max_capability=89`;
|
||||
- `kernelize` runs on a system with capability 8.6.
|
||||
@ -285,12 +286,11 @@ a kernel to a range of ROCm capabilities.
|
||||
The `LocalLayerRepository` class is provided to load a repository from
|
||||
a local directory. For example:
|
||||
|
||||
```
|
||||
```python
|
||||
with use_kernel_mapping(
|
||||
{
|
||||
"SiluAndMul": {
|
||||
"cuda": LocalLayerRepository(
|
||||
# install_kernel will give the fully-resolved path.
|
||||
repo_path="/home/daniel/kernels/activation",
|
||||
package_name="activation",
|
||||
layer_name="SiluAndMul",
|
||||
|
@ -71,6 +71,24 @@ def universal_build_variant() -> str:
|
||||
return "torch-universal"
|
||||
|
||||
|
||||
# Metaclass to allow overriding the `__repr__` method for kernel modules.
|
||||
class _KernelModuleMeta(type):
|
||||
def __repr__(self):
|
||||
return "<class 'kernel_module'>"
|
||||
|
||||
|
||||
# Custom module type to identify dynamically loaded kernel modules.
|
||||
# Using a subclass lets us distinguish these from regular imports.
|
||||
class _KernelModuleType(ModuleType, metaclass=_KernelModuleMeta):
|
||||
"""Marker class for modules loaded dynamically from a path."""
|
||||
|
||||
module_name: str
|
||||
is_kernel: bool = True
|
||||
|
||||
def __repr__(self):
|
||||
return f"<kernel_module '{self.module_name}' from '{self.__file__}'>"
|
||||
|
||||
|
||||
def import_from_path(module_name: str, file_path: Path) -> ModuleType:
|
||||
# We cannot use the module name as-is, after adding it to `sys.modules`,
|
||||
# it would also be used for other imports. So, we make a module name that
|
||||
@ -84,6 +102,9 @@ def import_from_path(module_name: str, file_path: Path) -> ModuleType:
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
if module is None:
|
||||
raise ImportError(f"Cannot load module {module_name} from spec")
|
||||
module.__class__ = _KernelModuleType
|
||||
assert isinstance(module, _KernelModuleType) # for mypy type checking
|
||||
module.module_name = module_name
|
||||
sys.modules[module_name] = module
|
||||
spec.loader.exec_module(module) # type: ignore
|
||||
return module
|
||||
|
Reference in New Issue
Block a user