model.py 5.11 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
2
3
4
5
6
7
8
import os
import torch
from lightx2v.text2v.models.networks.hunyuan.weights.pre_weights import HunyuanPreWeights
from lightx2v.text2v.models.networks.hunyuan.weights.post_weights import HunyuanPostWeights
from lightx2v.text2v.models.networks.hunyuan.weights.transformer_weights import HunyuanTransformerWeights
from lightx2v.text2v.models.networks.hunyuan.infer.pre_infer import HunyuanPreInfer
from lightx2v.text2v.models.networks.hunyuan.infer.post_infer import HunyuanPostInfer
from lightx2v.text2v.models.networks.hunyuan.infer.transformer_infer import HunyuanTransformerInfer
9
from lightx2v.text2v.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        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
        self.pre_weight.load_weights(weight_dict)
        self.post_weight.load_weights(weight_dict)
        self.transformer_weights.load_weights(weight_dict)

    def _init_infer(self):
        self.pre_infer = self.pre_infer_class()
        self.post_infer = self.post_infer_class()
        self.transformer_infer = self.transformer_infer_class(self.config)

    def set_scheduler(self, scheduler):
        self.scheduler = scheduler
        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()
    def infer(self, text_encoder_output, image_encoder_output, args):
gushiqiao's avatar
gushiqiao committed
92
93
94
        if self.config["cpu_offload"]:
            self.pre_weight.to_cuda()
            self.post_weight.to_cuda()
helloyongyang's avatar
helloyongyang committed
95
96
97
98
99
100
101
102
103
104
        pre_infer_out = self.pre_infer.infer(
            self.pre_weight,
            self.scheduler.latents,
            self.scheduler.timesteps[self.scheduler.step_index],
            text_encoder_output["text_encoder_1_text_states"],
            text_encoder_output["text_encoder_1_attention_mask"],
            text_encoder_output["text_encoder_2_text_states"],
            self.scheduler.freqs_cos,
            self.scheduler.freqs_sin,
            self.scheduler.guidance,
helloyongyang's avatar
helloyongyang committed
105
            img_latents=image_encoder_output["img_latents"] if "img_latents" in image_encoder_output else None,
helloyongyang's avatar
helloyongyang committed
106
        )
Dongz's avatar
Dongz committed
107
108
        img, vec = self.transformer_infer.infer(self.transformer_weights, *pre_infer_out)
        self.scheduler.noise_pred = self.post_infer.infer(self.post_weight, img, vec, self.scheduler.latents.shape)
gushiqiao's avatar
gushiqiao committed
109
110
        if self.config["cpu_offload"]:
            self.pre_weight.to_cpu()
gushiqiao's avatar
gushiqiao committed
111
            self.post_weight.to_cpu()
112
113
114
115
116

        if self.config["feature_caching"] == "Tea":
            self.scheduler.cnt += 1
            if self.scheduler.cnt == self.scheduler.num_steps:
                self.scheduler.cnt = 0