"test/gtest-1.11.0/googletest/samples/sample7_unittest.cc" did not exist on "e3f120b99de7bad9801b51c7e1fffea82d3c4f41"
conv3d.py 2.13 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
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
import torch
from abc import ABCMeta, abstractmethod
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
29
@CONV3D_WEIGHT_REGISTER("Default")
helloyongyang's avatar
helloyongyang committed
30
31
32
33
34
35
36
37
38
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):
        self.weight = weight_dict[self.weight_name].cuda()
        self.bias = weight_dict[self.bias_name].cuda() if self.bias_name is not None else None

    def apply(self, input_tensor):
Dongz's avatar
Dongz committed
39
        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
40
41
42
43
44
45
46
47
48
49
50
        return input_tensor

    def to_cpu(self):
        self.weight = self.weight.cpu()
        if self.bias is not None:
            self.bias = self.bias.cpu()

    def to_cuda(self):
        self.weight = self.weight.cuda()
        if self.bias is not None:
            self.bias = self.bias.cuda()
TorynCurtis's avatar
TorynCurtis committed
51
52


Dongz's avatar
Dongz committed
53
@CONV3D_WEIGHT_REGISTER("Defaultt-Force-BF16")
TorynCurtis's avatar
TorynCurtis committed
54
55
56
57
58
59
class Conv3dWeightForceBF16(Conv3dWeight):
    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):
        self.weight = weight_dict[self.weight_name].to(torch.bfloat16).cuda()
Dongz's avatar
Dongz committed
60
        self.bias = weight_dict[self.bias_name].to(torch.bfloat16).cuda() if self.bias_name is not None else None