Commit 8b807fa
Support functions as layers (#188)
This change adds two types of new functionality. First of all, it
introduces the `(Locked|Local)?FuncRepo` classes these can be used
to extend a layer with a kernel function. For instance, a layer
like
```
@use_kernel_forward_from_hub("SiluAndMul")
class SiluAndMul(nn.Module):
def forward(self, input: torch.Tensor) -> torch.Tensor:
d = input.shape[-1] // 2
return F.silu(input[..., :d]) * input[..., d:]
```
can now also be kernelized using a function `silu_and_mul` from the
Hub:
```
with use_kernel_mapping({
"SiluAndMul": {
"cuda": FuncRepository(
repo_id="kernels-community/activation",
func_name="silu_and_mul",
),
}
}):
kernelize(...)
```
This makes it easier to kernelize pure layers (layers that do not use
module state), since the Hub kernel does not have to provide a `layers`
Python module with wrappers.
Secondly, we introduce a decorator `use_kernel_func_from_hub` that turns
functions into layers that can be kernelized. For example:
```
@use_kernel_forward_from_hub("silu_and_mul")
def silu_and_mul(x: torch.Tensor) -> torch.Tensor:
d = x.shape[-1] // 2
return F.silu(x[..., :d]) * x[..., d:]
```
will implicitly create an instance of the following class:
```
class Func(nn.Module):
# We add some magic to preserve the function's signature.
def forward(self, *args, **kwargs):
return silu_and_mul(*args, **kwargs)
```
Due to the `__call__` implementation of `nn.Module`, the instance
still behaves as a function:
```
out = silu_and_mul(x)
```
However, when the function is used as a member of an `nn.Module`,
it will be kernelized:
```
class FeedForward(nn.Module):
def __init__(self, in_features: int, out_features: int):
self.linear = nn.Linear(in_features, out_features)
# Note: silu_and_mul is a Torch module.
self.silu_and_mul = silu_and_mul
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.silu_and_mul(self.linear(x))
```
Co-authored-by: Mohamed Mekkouri <[email protected]>1 parent 5b0067b commit 8b807fa
File tree
14 files changed
+672
-74
lines changed- docs/source
- api
- src/kernels
- layer
- tests
14 files changed
+672
-74
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
| |||
36 | 40 | | |
37 | 41 | | |
38 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
39 | 47 | | |
40 | 48 | | |
41 | 49 | | |
42 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
43 | 55 | | |
44 | 56 | | |
45 | 57 | | |
46 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
47 | 63 | | |
48 | 64 | | |
49 | 65 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
46 | 76 | | |
47 | 77 | | |
48 | 78 | | |
| |||
157 | 187 | | |
158 | 188 | | |
159 | 189 | | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
160 | 205 | | |
161 | 206 | | |
162 | 207 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
| 12 | + | |
11 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
12 | 17 | | |
13 | 18 | | |
| 19 | + | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
25 | 32 | | |
26 | 33 | | |
27 | 34 | | |
| 35 | + | |
28 | 36 | | |
| 37 | + | |
29 | 38 | | |
| 39 | + | |
30 | 40 | | |
31 | 41 | | |
32 | 42 | | |
| |||
38 | 48 | | |
39 | 49 | | |
40 | 50 | | |
41 | | - | |
42 | 51 | | |
43 | 52 | | |
44 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
2 | 8 | | |
3 | 9 | | |
4 | 10 | | |
| |||
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| 25 | + | |
19 | 26 | | |
| 27 | + | |
20 | 28 | | |
| 29 | + | |
21 | 30 | | |
22 | 31 | | |
23 | 32 | | |
24 | 33 | | |
25 | 34 | | |
26 | 35 | | |
| 36 | + | |
27 | 37 | | |
28 | 38 | | |
0 commit comments