conv3d.py 3.62 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 CONV3D_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 Conv3dWeightTemplate(metaclass=ABCMeta):
    def __init__(self, weight_name, bias_name, stride=1, padding=0, dilation=1, groups=1):
        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
@CONV3D_WEIGHT_REGISTER("Default")
helloyongyang's avatar
helloyongyang committed
33
34
35
36
37
class Conv3dWeight(Conv3dWeightTemplate):
    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
        device = weight_dict[self.weight_name].device
39
        if device.type == "cpu":
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
            weight_shape = weight_dict[self.weight_name].shape
            weight_dtype = weight_dict[self.weight_name].dtype
            self.pin_weight = torch.empty(weight_shape, pin_memory=True, dtype=weight_dtype)
            self.pin_weight.copy_(weight_dict[self.weight_name])

            if self.bias_name is not None:
                bias_shape = weight_dict[self.bias_name].shape
                bias_dtype = weight_dict[self.bias_name].dtype
                self.pin_bias = torch.empty(bias_shape, pin_memory=True, dtype=bias_dtype)
                self.pin_bias.copy_(weight_dict[self.bias_name])
            else:
                self.bias = None
                self.pin_bias = None
            del weight_dict[self.weight_name]
        else:
55
56
57
58
59
            self.weight = weight_dict[self.weight_name]
            if self.bias_name is not None:
                self.bias = weight_dict[self.bias_name]
            else:
                self.bias = None
helloyongyang's avatar
helloyongyang committed
60
61

    def apply(self, input_tensor):
gushiqiao's avatar
Fix  
gushiqiao committed
62
63
64
65
66
67
68
69
70
        input_tensor = torch.nn.functional.conv3d(
            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
71
72
        return input_tensor

73
    def to_cuda(self, non_blocking=False):
74
        self.weight = self.pin_weight.to(AI_DEVICE, non_blocking=non_blocking)
75
        if hasattr(self, "pin_bias") and self.pin_bias is not None:
76
            self.bias = self.pin_bias.to(AI_DEVICE, non_blocking=non_blocking)
77
78
79
80
81
82
83
84
85
86

    def to_cpu(self, non_blocking=False):
        if hasattr(self, "pin_weight"):
            self.weight = self.pin_weight.copy_(self.weight, non_blocking=non_blocking).cpu()
            if self.bias is not None:
                self.bias = self.pin_bias.copy_(self.bias, non_blocking=non_blocking).cpu()
        else:
            self.weight = self.weight.to("cpu", non_blocking=non_blocking)
            if hasattr(self, "bias") and self.bias is not None:
                self.bias = self.bias.to("cpu", non_blocking=non_blocking)
TorynCurtis's avatar
TorynCurtis committed
87

88
89
90
    def state_dict(self, destination=None):
        if destination is None:
            destination = {}
91
92
93
        destination[self.weight_name] = self.pin_weight if hasattr(self, "pin_weight") else self.weight  # .cpu().detach().clone().contiguous()
        if self.bias_name is not None:
            destination[self.bias_name] = self.pin_bias if hasattr(self, "pin_bias") else self.bias  # .cpu().detach().clone()
94
        return destination