model.py 4.55 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
2
import os
import torch
3
4
5
6
7
8
9
from lightx2v.models.networks.hunyuan.weights.pre_weights import HunyuanPreWeights
from lightx2v.models.networks.hunyuan.weights.post_weights import HunyuanPostWeights
from lightx2v.models.networks.hunyuan.weights.transformer_weights import HunyuanTransformerWeights
from lightx2v.models.networks.hunyuan.infer.pre_infer import HunyuanPreInfer
from lightx2v.models.networks.hunyuan.infer.post_infer import HunyuanPostInfer
from lightx2v.models.networks.hunyuan.infer.transformer_infer import HunyuanTransformerInfer
from lightx2v.models.networks.hunyuan.infer.feature_caching.transformer_infer import HunyuanTransformerInferTaylorCaching, HunyuanTransformerInferTeaCaching
Dongz's avatar
Dongz committed
10

Xinchi Huang's avatar
Xinchi Huang committed
11
12
import lightx2v.attentions.distributed.ulysses.wrap as ulysses_dist_wrap
import lightx2v.attentions.distributed.ring.wrap as ring_dist_wrap
helloyongyang's avatar
helloyongyang committed
13
14
15
16
17
18
19


class HunyuanModel:
    pre_weight_class = HunyuanPreWeights
    post_weight_class = HunyuanPostWeights
    transformer_weight_class = HunyuanTransformerWeights

helloyongyang's avatar
helloyongyang committed
20
    def __init__(self, model_path, config, device, args):
helloyongyang's avatar
helloyongyang committed
21
22
        self.model_path = model_path
        self.config = config
gushiqiao's avatar
gushiqiao committed
23
        self.device = device
helloyongyang's avatar
helloyongyang committed
24
        self.args = args
helloyongyang's avatar
helloyongyang committed
25
26
27
28
        self._init_infer_class()
        self._init_weights()
        self._init_infer()

Xinchi Huang's avatar
Xinchi Huang committed
29
30
31
32
33
34
35
        if config["parallel_attn_type"]:
            if config["parallel_attn_type"] == "ulysses":
                ulysses_dist_wrap.parallelize_hunyuan(self)
            elif config["parallel_attn_type"] == "ring":
                ring_dist_wrap.parallelize_hunyuan(self)
            else:
                raise Exception(f"Unsuppotred parallel_attn_type")
Dongz's avatar
Dongz committed
36
37

        if self.config["cpu_offload"]:
helloyongyang's avatar
helloyongyang committed
38
39
40
41
42
            self.to_cpu()

    def _init_infer_class(self):
        self.pre_infer_class = HunyuanPreInfer
        self.post_infer_class = HunyuanPostInfer
Dongz's avatar
Dongz committed
43
        if self.config["feature_caching"] == "NoCaching":
helloyongyang's avatar
helloyongyang committed
44
            self.transformer_infer_class = HunyuanTransformerInfer
Dongz's avatar
Dongz committed
45
        elif self.config["feature_caching"] == "TaylorSeer":
46
47
48
            self.transformer_infer_class = HunyuanTransformerInferTaylorCaching
        elif self.config["feature_caching"] == "Tea":
            self.transformer_infer_class = HunyuanTransformerInferTeaCaching
helloyongyang's avatar
helloyongyang committed
49
50
51
52
        else:
            raise NotImplementedError(f"Unsupported feature_caching type: {self.config['feature_caching']}")

    def _load_ckpt(self):
helloyongyang's avatar
helloyongyang committed
53
54
55
56
        if self.args.task == "t2v":
            ckpt_path = os.path.join(self.model_path, "hunyuan-video-t2v-720p/transformers/mp_rank_00_model_states.pt")
        else:
            ckpt_path = os.path.join(self.model_path, "hunyuan-video-i2v-720p/transformers/mp_rank_00_model_states.pt")
gushiqiao's avatar
gushiqiao committed
57
        weight_dict = torch.load(ckpt_path, map_location=self.device, weights_only=True)["module"]
helloyongyang's avatar
helloyongyang committed
58
59
60
61
62
63
64
65
66
        return weight_dict

    def _init_weights(self):
        weight_dict = self._load_ckpt()
        # init weights
        self.pre_weight = self.pre_weight_class(self.config)
        self.post_weight = self.post_weight_class(self.config)
        self.transformer_weights = self.transformer_weight_class(self.config)
        # load weights
67
68
69
        self.pre_weight.load(weight_dict)
        self.post_weight.load(weight_dict)
        self.transformer_weights.load(weight_dict)
helloyongyang's avatar
helloyongyang committed
70
71

    def _init_infer(self):
72
73
        self.pre_infer = self.pre_infer_class(self.config)
        self.post_infer = self.post_infer_class(self.config)
helloyongyang's avatar
helloyongyang committed
74
75
76
77
        self.transformer_infer = self.transformer_infer_class(self.config)

    def set_scheduler(self, scheduler):
        self.scheduler = scheduler
78
79
        self.pre_infer.set_scheduler(scheduler)
        self.post_infer.set_scheduler(scheduler)
helloyongyang's avatar
helloyongyang committed
80
81
82
83
84
85
86
87
88
89
90
91
92
        self.transformer_infer.set_scheduler(scheduler)

    def to_cpu(self):
        self.pre_weight.to_cpu()
        self.post_weight.to_cpu()
        self.transformer_weights.to_cpu()

    def to_cuda(self):
        self.pre_weight.to_cuda()
        self.post_weight.to_cuda()
        self.transformer_weights.to_cuda()

    @torch.no_grad()
93
    def infer(self, inputs):
gushiqiao's avatar
gushiqiao committed
94
95
96
        if self.config["cpu_offload"]:
            self.pre_weight.to_cuda()
            self.post_weight.to_cuda()
97
98
99
100
101

        inputs = self.pre_infer.infer(self.pre_weight, inputs)
        inputs = self.transformer_infer.infer(self.transformer_weights, *inputs)
        self.scheduler.noise_pred = self.post_infer.infer(self.post_weight, *inputs)

gushiqiao's avatar
gushiqiao committed
102
103
        if self.config["cpu_offload"]:
            self.pre_weight.to_cpu()
gushiqiao's avatar
gushiqiao committed
104
            self.post_weight.to_cpu()
105
106
107
108
        if self.config["feature_caching"] == "Tea":
            self.scheduler.cnt += 1
            if self.scheduler.cnt == self.scheduler.num_steps:
                self.scheduler.cnt = 0