tensor.py 1.72 KB
Newer Older
Xinchi Huang's avatar
Xinchi Huang committed
1
import torch
2
from lightx2v.utils.registry_factory import TENSOR_REGISTER
gushiqiao's avatar
gushiqiao committed
3
from lightx2v.utils.envs import *
4
5
6
7


@TENSOR_REGISTER("Default")
class DefaultTensor:
8
    def __init__(self, tensor_name, lazy_load=False, lazy_load_file=None):
9
        self.tensor_name = tensor_name
10
11
12
13
14
15
16
17
        self.lazy_load = lazy_load
        self.lazy_load_file = lazy_load_file

    def load_from_disk(self):
        if not torch._dynamo.is_compiling():
            self.tensor = self.lazy_load_file.get_tensor(self.tensor_name).to(torch.bfloat16).pin_memory()
        else:
            self.tensor = self.lazy_load_file.get_tensor(self.tensor_name).to(torch.bfloat16)
18
19

    def load(self, weight_dict):
20
        if not self.lazy_load:
gushiqiao's avatar
gushiqiao committed
21
            self.tensor = weight_dict[self.tensor_name]
22
23
24
            self.pinned_tensor = torch.empty(self.tensor.shape, pin_memory=True, dtype=self.tensor.dtype)

    def clear(self):
gushiqiao's avatar
FIX  
gushiqiao committed
25
        attrs = ["tensor", "pinned_tensor"]
gushiqiao's avatar
gushiqiao committed
26
27
28
29
        for attr in attrs:
            if hasattr(self, attr):
                delattr(self, attr)
                setattr(self, attr, None)
30
31
32

    def _calculate_size(self):
        return self.tensor.numel() * self.tensor.element_size()
33
34

    def to_cpu(self, non_blocking=False):
35
36
37
38
        if hasattr(self, "pinned_tensor"):
            self.tensor = self.pinned_tensor.copy_(self.tensor, non_blocking=non_blocking).cpu()
        else:
            self.tensor = self.tensor.to("cpu", non_blocking=non_blocking)
39
40
41

    def to_cuda(self, non_blocking=False):
        self.tensor = self.tensor.cuda(non_blocking=non_blocking)
42
43
44
45
46
47

    def state_dict(self, destination=None):
        if destination is None:
            destination = {}
        destination[self.tensor_name] = self.tensor.cpu().detach().clone()
        return destination