-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinear.py
More file actions
41 lines (29 loc) · 846 Bytes
/
linear.py
File metadata and controls
41 lines (29 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from contextlib import contextmanager
import torch
import torch.nn as nn
import ops.ninetoothed.torch
class Linear(nn.Module):
bmm = None
def __init__(self, other):
super().__init__()
self.__dict__ = other.__dict__
def forward(self, input):
return type(self).bmm(
input, self.weight.T.unsqueeze(0).expand(input.shape[0], -1, -1)
)
@contextmanager
def bmm_backend(backend_name):
_prev_impl = Linear.bmm
if backend_name == "ninetoothed":
impl = ops.ninetoothed.torch.bmm
elif backend_name == "triton":
impl = ops.triton.torch.bmm
elif backend_name == "torch":
impl = torch.bmm
else:
raise ValueError(f"unknown backend: `{backend_name}`")
Linear.bmm = impl
try:
yield
finally:
Linear.bmm = _prev_impl