ops.py 1.63 KB
Newer Older
comfyanonymous's avatar
comfyanonymous committed
1
import torch
2
from contextlib import contextmanager
comfyanonymous's avatar
comfyanonymous committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class Linear(torch.nn.Module):
    def __init__(self, in_features: int, out_features: int, bias: bool = True,
                 device=None, dtype=None) -> None:
        factory_kwargs = {'device': device, 'dtype': dtype}
        super().__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = torch.nn.Parameter(torch.empty((out_features, in_features), **factory_kwargs))
        if bias:
            self.bias = torch.nn.Parameter(torch.empty(out_features, **factory_kwargs))
        else:
            self.register_parameter('bias', None)

    def forward(self, input):
        return torch.nn.functional.linear(input, self.weight, self.bias)
19
20
21
22

class Conv2d(torch.nn.Conv2d):
    def reset_parameters(self):
        return None
23

comfyanonymous's avatar
comfyanonymous committed
24
25
26
27
28
def conv_nd(dims, *args, **kwargs):
    if dims == 2:
        return Conv2d(*args, **kwargs)
    else:
        raise ValueError(f"unsupported dimensions: {dims}")
29
30

@contextmanager
31
def use_comfy_ops(device=None, dtype=None): # Kind of an ugly hack but I can't think of a better way
32
    old_torch_nn_linear = torch.nn.Linear
33
34
35
36
37
38
39
40
41
42
    force_device = device
    force_dtype = dtype
    def linear_with_dtype(in_features: int, out_features: int, bias: bool = True, device=None, dtype=None):
        if force_device is not None:
            device = force_device
        if force_dtype is not None:
            dtype = force_dtype
        return Linear(in_features, out_features, bias=bias, device=device, dtype=dtype)

    torch.nn.Linear = linear_with_dtype
43
44
45
46
    try:
        yield
    finally:
        torch.nn.Linear = old_torch_nn_linear