11import functools
2- import inspect
3- from inspect import Parameter , Signature
2+ import warnings
43from pathlib import Path
54from types import ModuleType
6- from typing import TYPE_CHECKING , Callable , Protocol , Type
7-
8- from kernels .layer .repos import RepositoryProtocol
5+ from typing import TYPE_CHECKING , Protocol , Type
96
107from .._versions import select_revision_or_version
118from ..utils import (
1411 get_kernel ,
1512 get_local_kernel ,
1613)
14+ from .layer import _create_func_module , use_kernel_forward_from_hub
15+ from .repos import RepositoryProtocol
1716
1817if TYPE_CHECKING :
1918 from torch import nn
@@ -28,6 +27,10 @@ class FuncRepository:
2827 """
2928 Repository and name of a function for kernel mapping.
3029
30+ > [!WARNING]
31+ > `FuncRepository` is deprecated and will be removed in kernels 0.17.
32+ > Use [`~kernels.LayerRepository`] instead.
33+
3134 Args:
3235 repo_id (`str`):
3336 The Hub repository containing the layer.
@@ -68,6 +71,12 @@ def __init__(
6871 version : int | None = None ,
6972 trust_remote_code : bool | list [str ] = False ,
7073 ):
74+ warnings .warn (
75+ "FuncRepository is deprecated and will be removed in kernels 0.17. Use LayerRepository instead." ,
76+ DeprecationWarning ,
77+ stacklevel = 2 ,
78+ )
79+
7180 if revision is not None and version is not None :
7281 raise ValueError ("Either a revision or a version must be specified, not both." )
7382 if revision is None and version is None :
@@ -92,7 +101,9 @@ def _resolve_revision(self) -> str:
92101
93102 def load (self ) -> Type ["nn.Module" ]:
94103 kernel = get_kernel (
95- self ._repo_id , revision = self ._resolve_revision (), trust_remote_code = self ._trust_remote_code
104+ self ._repo_id ,
105+ revision = self ._resolve_revision (),
106+ trust_remote_code = self ._trust_remote_code ,
96107 )
97108 return _get_kernel_func (self , kernel )
98109
@@ -107,7 +118,15 @@ def __eq__(self, other):
107118 )
108119
109120 def __hash__ (self ):
110- return hash ((self .func_name , self ._repo_id , self ._revision , self ._version , self ._trust_remote_code ))
121+ return hash (
122+ (
123+ self .func_name ,
124+ self ._repo_id ,
125+ self ._revision ,
126+ self ._version ,
127+ self ._trust_remote_code ,
128+ )
129+ )
111130
112131 def __str__ (self ) -> str :
113132 return f"`{ self ._repo_id } ` (revision: { self ._resolve_revision ()} ), function `{ self .func_name } `"
@@ -117,6 +136,10 @@ class LocalFuncRepository:
117136 """
118137 Repository and function name from a local directory for kernel mapping.
119138
139+ > [!WARNING]
140+ > `LocalFuncRepository` is deprecated and will be removed in kernels 0.17.
141+ > Use [`~kernels.LocalLayerRepository`] instead.
142+
120143 Args:
121144 repo_path (`Path`):
122145 The local repository containing the layer.
@@ -143,6 +166,12 @@ def __init__(
143166 * ,
144167 func_name : str ,
145168 ):
169+ warnings .warn (
170+ "LocalFuncRepository is deprecated and will be removed in kernels 0.17. Use LocalLayerRepository instead." ,
171+ DeprecationWarning ,
172+ stacklevel = 2 ,
173+ )
174+
146175 self ._repo_path = repo_path
147176 self .func_name = func_name
148177
@@ -176,6 +205,10 @@ def use_kernel_func_from_hub(func_name: str):
176205 kernelized, it **must** be a member of another `torch.nn.Module` that is
177206 part of the model (see the example).
178207
208+ > [!WARNING]
209+ > `use_kernel_func_from_hub` is deprecated and will be removed in kernels 0.17.
210+ > Use [`~kernels.use_kernel_forward_from_hub`] instead.
211+
179212 Args:
180213 func_name (`str`):
181214 The name of the function name to use for kernel lookup in registered mappings.
@@ -210,13 +243,13 @@ def forward(self, x):
210243 # model = kernelize(model, mode=Mode.TRAINING | Mode.TORCH_COMPILE, device="cuda")
211244 ```
212245 """
246+ warnings .warn (
247+ "use_kernel_func_from_hub is deprecated and will be removed in kernels 0.17. Use [`use_kernel_forward_from_hub`] instead." ,
248+ DeprecationWarning ,
249+ stacklevel = 2 ,
250+ )
213251
214- def decorator (func ):
215- Func = _create_func_module (func )
216- Func .kernel_layer_name = func_name
217- return Func ()
218-
219- return decorator
252+ return use_kernel_forward_from_hub (func_name )
220253
221254
222255class LockedFuncRepository :
@@ -225,6 +258,18 @@ class LockedFuncRepository:
225258
226259 In contrast to `FuncRepository`, this class uses repositories that
227260 are locked inside a project.
261+
262+ > [!WARNING]
263+ > `LockedFuncRepository` is deprecated and will be removed in kernels 0.17.
264+ > Use [`~kernels.LockedLayerRepository`] instead.
265+
266+ Args:
267+ repo_id (`str`): The Hub repository containing the function.
268+ lockfile (`Path`, *optional*): Path to the lockfile. If not provided,
269+ the lockfile will be inferred from the caller's context.
270+ func_name (`str`): The name of the function within the kernel repository.
271+ trust_remote_code (`bool`, *optional*, defaults to `False`):
272+ Whether to allow loading kernels from untrusted organisations.
228273 """
229274
230275 def __init__ (
@@ -238,14 +283,13 @@ def __init__(
238283 """
239284 Construct a function repository.
240285
241- Args:
242- repo_id (`str`): The Hub repository containing the function.
243- lockfile (`Path`, *optional*): Path to the lockfile. If not provided,
244- the lockfile will be inferred from the caller's context.
245- func_name (`str`): The name of the function within the kernel repository.
246- trust_remote_code (`bool`, *optional*, defaults to `False`):
247- Whether to allow loading kernels from untrusted organisations.
248286 """
287+ warnings .warn (
288+ "LockedFuncRepository is deprecated and will be removed in kernels 0.17. Use LockedLayerRepository instead." ,
289+ DeprecationWarning ,
290+ stacklevel = 2 ,
291+ )
292+
249293 self ._repo_id = repo_id
250294 self ._lockfile = lockfile
251295 self .func_name = func_name
@@ -265,7 +309,11 @@ def _resolve_revision(self) -> str:
265309 return locked_sha
266310
267311 def load (self ) -> Type ["nn.Module" ]:
268- kernel = get_kernel (repo_id = self ._repo_id , revision = self ._revision , trust_remote_code = self ._trust_remote_code )
312+ kernel = get_kernel (
313+ repo_id = self ._repo_id ,
314+ revision = self ._revision ,
315+ trust_remote_code = self ._trust_remote_code ,
316+ )
269317 return _get_kernel_func (self , kernel )
270318
271319 def __eq__ (self , other ):
@@ -290,27 +338,3 @@ def _get_kernel_func(repo: FuncRepositoryProtocol, kernel: ModuleType) -> Type["
290338 raise ValueError (f"Function `{ repo .func_name } ` not found in `{ repo } `" )
291339
292340 return _create_func_module (func )
293-
294-
295- def _create_func_module (func : Callable ) -> Type ["nn.Module" ]:
296- from torch import nn
297-
298- class Func (nn .Module ):
299- # Same flags as possible with normal `nn.Module` objects that are exchanged
300- can_torch_compile = getattr (func , "can_torch_compile" , False )
301- has_backward = getattr (func , "has_backward" , True )
302-
303- def forward (self , * args , ** kwargs ):
304- return func (* args , ** kwargs )
305-
306- # Use function signature with args prepended by self to support
307- # module validation.
308- func_sig = inspect .signature (func )
309- new_args = [Parameter ("self" , Parameter .POSITIONAL_OR_KEYWORD )]
310- new_args .extend (func_sig .parameters .values ())
311- Func .forward .__signature__ = Signature ( # type: ignore[attr-defined]
312- parameters = new_args ,
313- return_annotation = func_sig .return_annotation ,
314- )
315-
316- return Func
0 commit comments