conv3d.py 2.65 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
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
from lightx2v.utils.registry_factory import CONV3D_WEIGHT_REGISTER


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
31
@CONV3D_WEIGHT_REGISTER("Default")
helloyongyang's avatar
helloyongyang committed
32
33
34
35
36
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):
gushiqiao's avatar
gushiqiao committed
37
38
39
40
        self.weight = weight_dict[self.weight_name]
        self.bias = weight_dict[self.bias_name] if self.bias_name is not None else None
        self.pinned_weight = torch.empty(self.weight.shape, pin_memory=True, dtype=self.weight.dtype)
        self.pinned_bias = torch.empty(self.bias.shape, pin_memory=True, dtype=self.bias.dtype) if self.bias_name is not None else None
helloyongyang's avatar
helloyongyang committed
41
42

    def apply(self, input_tensor):
gushiqiao's avatar
Fix  
gushiqiao committed
43
44
45
46
47
48
49
50
51
        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
52
53
        return input_tensor

54
55
    def to_cpu(self, non_blocking=False):
        self.weight = self.weight.to("cpu", non_blocking=non_blocking)
helloyongyang's avatar
helloyongyang committed
56
        if self.bias is not None:
57
            self.bias = self.bias.to("cpu", non_blocking=non_blocking)
helloyongyang's avatar
helloyongyang committed
58

59
60
    def to_cuda(self, non_blocking=False):
        self.weight = self.weight.cuda(non_blocking=non_blocking)
helloyongyang's avatar
helloyongyang committed
61
        if self.bias is not None:
62
            self.bias = self.bias.cuda(non_blocking=non_blocking)
TorynCurtis's avatar
TorynCurtis committed
63

64
65
66
67
68
69
70
    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
gushiqiao's avatar
gushiqiao committed
71
72

    def clear(self):
gushiqiao's avatar
FIX  
gushiqiao committed
73
        attrs = ["weight", "bias", "pinned_weight", "pinned_bias"]
gushiqiao's avatar
gushiqiao committed
74
75
76
77
        for attr in attrs:
            if hasattr(self, attr):
                delattr(self, attr)
                setattr(self, attr, None)