conv2d.py 2.18 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
from abc import ABCMeta, abstractmethod
PengGao's avatar
PengGao committed
2
3
4

import torch

helloyongyang's avatar
helloyongyang committed
5
from lightx2v.utils.registry_factory import CONV2D_WEIGHT_REGISTER
6
from lightx2v_platform.base.global_var import AI_DEVICE
helloyongyang's avatar
helloyongyang committed
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


class Conv2dWeightTemplate(metaclass=ABCMeta):
    def __init__(self, weight_name, bias_name, stride, padding, dilation, groups):
        self.weight_name = weight_name
        self.bias_name = bias_name
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.groups = groups
        self.config = {}

    @abstractmethod
    def load(self, weight_dict):
        pass

    @abstractmethod
    def apply(self, input_tensor):
        pass

    def set_config(self, config=None):
        if config is not None:
            self.config = config


Dongz's avatar
Dongz committed
32
@CONV2D_WEIGHT_REGISTER("Default")
helloyongyang's avatar
helloyongyang committed
33
34
35
36
37
class Conv2dWeight(Conv2dWeightTemplate):
    def __init__(self, weight_name, bias_name, stride=1, padding=0, dilation=1, groups=1):
        super().__init__(weight_name, bias_name, stride, padding, dilation, groups)

    def load(self, weight_dict):
38
39
        self.weight = weight_dict[self.weight_name].to(AI_DEVICE)
        self.bias = weight_dict[self.bias_name].to(AI_DEVICE) if self.bias_name is not None else None
helloyongyang's avatar
helloyongyang committed
40
41

    def apply(self, input_tensor):
Dongz's avatar
Dongz committed
42
        input_tensor = torch.nn.functional.conv2d(input_tensor, weight=self.weight, bias=self.bias, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups)
helloyongyang's avatar
helloyongyang committed
43
44
        return input_tensor

Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
45
46
    def to_cpu(self, non_blocking=False):
        self.weight = self.weight.cpu(non_blocking=non_blocking)
helloyongyang's avatar
helloyongyang committed
47
        if self.bias is not None:
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
48
            self.bias = self.bias.cpu(non_blocking=non_blocking)
helloyongyang's avatar
helloyongyang committed
49

Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
50
    def to_cuda(self, non_blocking=False):
51
        self.weight = self.weight.to(AI_DEVICE, non_blocking=non_blocking)
helloyongyang's avatar
helloyongyang committed
52
        if self.bias is not None:
53
            self.bias = self.bias.to(AI_DEVICE, non_blocking=non_blocking)
54
55
56
57
58
59
60
61

    def state_dict(self, destination=None):
        if destination is None:
            destination = {}
        destination[self.weight_name] = self.weight.cpu().detach().clone()
        if self.bias is not None:
            destination[self.bias_name] = self.bias.cpu().detach().clone()
        return destination