scheduler.py 6.75 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
2
3
4
import torch
from ..scheduler import HunyuanScheduler


Dongz's avatar
Dongz committed
5
6
def cache_init(num_steps, model_kwargs=None):
    """
helloyongyang's avatar
helloyongyang committed
7
    Initialization for cache.
Dongz's avatar
Dongz committed
8
    """
helloyongyang's avatar
helloyongyang committed
9
10
11
    cache_dic = {}
    cache = {}
    cache_index = {}
Dongz's avatar
Dongz committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    cache[-1] = {}
    cache_index[-1] = {}
    cache_index["layer_index"] = {}
    cache_dic["attn_map"] = {}
    cache_dic["attn_map"][-1] = {}
    cache_dic["attn_map"][-1]["double_stream"] = {}
    cache_dic["attn_map"][-1]["single_stream"] = {}

    cache_dic["k-norm"] = {}
    cache_dic["k-norm"][-1] = {}
    cache_dic["k-norm"][-1]["double_stream"] = {}
    cache_dic["k-norm"][-1]["single_stream"] = {}

    cache_dic["v-norm"] = {}
    cache_dic["v-norm"][-1] = {}
    cache_dic["v-norm"][-1]["double_stream"] = {}
    cache_dic["v-norm"][-1]["single_stream"] = {}

    cache_dic["cross_attn_map"] = {}
    cache_dic["cross_attn_map"][-1] = {}
    cache[-1]["double_stream"] = {}
    cache[-1]["single_stream"] = {}
    cache_dic["cache_counter"] = 0
helloyongyang's avatar
helloyongyang committed
35
36

    for j in range(20):
Dongz's avatar
Dongz committed
37
        cache[-1]["double_stream"][j] = {}
helloyongyang's avatar
helloyongyang committed
38
        cache_index[-1][j] = {}
Dongz's avatar
Dongz committed
39
40
41
42
43
44
45
46
47
48
49
50
        cache_dic["attn_map"][-1]["double_stream"][j] = {}
        cache_dic["attn_map"][-1]["double_stream"][j]["total"] = {}
        cache_dic["attn_map"][-1]["double_stream"][j]["txt_mlp"] = {}
        cache_dic["attn_map"][-1]["double_stream"][j]["img_mlp"] = {}

        cache_dic["k-norm"][-1]["double_stream"][j] = {}
        cache_dic["k-norm"][-1]["double_stream"][j]["txt_mlp"] = {}
        cache_dic["k-norm"][-1]["double_stream"][j]["img_mlp"] = {}

        cache_dic["v-norm"][-1]["double_stream"][j] = {}
        cache_dic["v-norm"][-1]["double_stream"][j]["txt_mlp"] = {}
        cache_dic["v-norm"][-1]["double_stream"][j]["img_mlp"] = {}
helloyongyang's avatar
helloyongyang committed
51
52

    for j in range(40):
Dongz's avatar
Dongz committed
53
        cache[-1]["single_stream"][j] = {}
helloyongyang's avatar
helloyongyang committed
54
        cache_index[-1][j] = {}
Dongz's avatar
Dongz committed
55
56
57
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        cache_dic["attn_map"][-1]["single_stream"][j] = {}
        cache_dic["attn_map"][-1]["single_stream"][j]["total"] = {}

        cache_dic["k-norm"][-1]["single_stream"][j] = {}
        cache_dic["k-norm"][-1]["single_stream"][j]["total"] = {}

        cache_dic["v-norm"][-1]["single_stream"][j] = {}
        cache_dic["v-norm"][-1]["single_stream"][j]["total"] = {}

    cache_dic["taylor_cache"] = False
    cache_dic["duca"] = False
    cache_dic["test_FLOPs"] = False

    mode = "Taylor"
    if mode == "original":
        cache_dic["cache_type"] = "random"
        cache_dic["cache_index"] = cache_index
        cache_dic["cache"] = cache
        cache_dic["fresh_ratio_schedule"] = "ToCa"
        cache_dic["fresh_ratio"] = 0.0
        cache_dic["fresh_threshold"] = 1
        cache_dic["force_fresh"] = "global"
        cache_dic["soft_fresh_weight"] = 0.0
        cache_dic["max_order"] = 0
        cache_dic["first_enhance"] = 1

    elif mode == "ToCa":
        cache_dic["cache_type"] = "random"
        cache_dic["cache_index"] = cache_index
        cache_dic["cache"] = cache
        cache_dic["fresh_ratio_schedule"] = "ToCa"
        cache_dic["fresh_ratio"] = 0.10
        cache_dic["fresh_threshold"] = 5
        cache_dic["force_fresh"] = "global"
        cache_dic["soft_fresh_weight"] = 0.0
        cache_dic["max_order"] = 0
        cache_dic["first_enhance"] = 1
        cache_dic["duca"] = False

    elif mode == "DuCa":
        cache_dic["cache_type"] = "random"
        cache_dic["cache_index"] = cache_index
        cache_dic["cache"] = cache
        cache_dic["fresh_ratio_schedule"] = "ToCa"
        cache_dic["fresh_ratio"] = 0.10
        cache_dic["fresh_threshold"] = 5
        cache_dic["force_fresh"] = "global"
        cache_dic["soft_fresh_weight"] = 0.0
        cache_dic["max_order"] = 0
        cache_dic["first_enhance"] = 1
        cache_dic["duca"] = True

    elif mode == "Taylor":
        cache_dic["cache_type"] = "random"
        cache_dic["cache_index"] = cache_index
        cache_dic["cache"] = cache
        cache_dic["fresh_ratio_schedule"] = "ToCa"
        cache_dic["fresh_ratio"] = 0.0
        cache_dic["fresh_threshold"] = 5
        cache_dic["max_order"] = 1
        cache_dic["force_fresh"] = "global"
        cache_dic["soft_fresh_weight"] = 0.0
        cache_dic["taylor_cache"] = True
        cache_dic["first_enhance"] = 1
helloyongyang's avatar
helloyongyang committed
119
120

    current = {}
Dongz's avatar
Dongz committed
121
122
    current["num_steps"] = num_steps
    current["activated_steps"] = [0]
helloyongyang's avatar
helloyongyang committed
123
124
125
126
127

    return cache_dic, current


def force_scheduler(cache_dic, current):
Dongz's avatar
Dongz committed
128
    if cache_dic["fresh_ratio"] == 0:
helloyongyang's avatar
helloyongyang committed
129
130
        # FORA
        linear_step_weight = 0.0
Dongz's avatar
Dongz committed
131
    else:
helloyongyang's avatar
helloyongyang committed
132
133
        # TokenCache
        linear_step_weight = 0.0
Dongz's avatar
Dongz committed
134
135
    step_factor = torch.tensor(1 - linear_step_weight + 2 * linear_step_weight * current["step"] / current["num_steps"])
    threshold = torch.round(cache_dic["fresh_threshold"] / step_factor)
helloyongyang's avatar
helloyongyang committed
136
137
138

    # no force constrain for sensitive steps, cause the performance is good enough.
    # you may have a try.
Dongz's avatar
Dongz committed
139
140
141

    cache_dic["cal_threshold"] = threshold
    # return threshold
helloyongyang's avatar
helloyongyang committed
142
143
144


def cal_type(cache_dic, current):
Dongz's avatar
Dongz committed
145
    """
helloyongyang's avatar
helloyongyang committed
146
    Determine calculation type for this step
Dongz's avatar
Dongz committed
147
148
    """
    if (cache_dic["fresh_ratio"] == 0.0) and (not cache_dic["taylor_cache"]):
helloyongyang's avatar
helloyongyang committed
149
        # FORA:Uniform
Dongz's avatar
Dongz committed
150
        first_step = current["step"] == 0
helloyongyang's avatar
helloyongyang committed
151
152
    else:
        # ToCa: First enhanced
Dongz's avatar
Dongz committed
153
154
        first_step = current["step"] < cache_dic["first_enhance"]
        # first_step = (current['step'] <= 3)
helloyongyang's avatar
helloyongyang committed
155

Dongz's avatar
Dongz committed
156
    force_fresh = cache_dic["force_fresh"]
helloyongyang's avatar
helloyongyang committed
157
    if not first_step:
Dongz's avatar
Dongz committed
158
        fresh_interval = cache_dic["cal_threshold"]
helloyongyang's avatar
helloyongyang committed
159
    else:
Dongz's avatar
Dongz committed
160
        fresh_interval = cache_dic["fresh_threshold"]
helloyongyang's avatar
helloyongyang committed
161

Dongz's avatar
Dongz committed
162
163
164
165
166
    if (first_step) or (cache_dic["cache_counter"] == fresh_interval - 1):
        current["type"] = "full"
        cache_dic["cache_counter"] = 0
        current["activated_steps"].append(current["step"])
        # current['activated_times'].append(current['t'])
helloyongyang's avatar
helloyongyang committed
167
        force_scheduler(cache_dic, current)
Dongz's avatar
Dongz committed
168
169
170
171
172

    elif cache_dic["taylor_cache"]:
        cache_dic["cache_counter"] += 1
        current["type"] = "taylor_cache"

helloyongyang's avatar
helloyongyang committed
173
    else:
Dongz's avatar
Dongz committed
174
175
176
177
178
        cache_dic["cache_counter"] += 1
        if cache_dic["duca"]:
            if cache_dic["cache_counter"] % 2 == 1:  # 0: ToCa-Aggresive-ToCa, 1: Aggresive-ToCa-Aggresive
                current["type"] = "ToCa"
            # 'cache_noise' 'ToCa' 'FORA'
helloyongyang's avatar
helloyongyang committed
179
            else:
Dongz's avatar
Dongz committed
180
                current["type"] = "aggressive"
helloyongyang's avatar
helloyongyang committed
181
        else:
Dongz's avatar
Dongz committed
182
            current["type"] = "ToCa"
helloyongyang's avatar
helloyongyang committed
183

Dongz's avatar
Dongz committed
184
        # if current['step'] < 25:
helloyongyang's avatar
helloyongyang committed
185
        #    current['type'] = 'FORA'
Dongz's avatar
Dongz committed
186
        # else:
helloyongyang's avatar
helloyongyang committed
187
        #    current['type'] = 'aggressive'
Dongz's avatar
Dongz committed
188
189


helloyongyang's avatar
helloyongyang committed
190
######################################################################
Dongz's avatar
Dongz committed
191
192
# if (current['step'] in [3,2,1,0]):
#    current['type'] = 'full'
helloyongyang's avatar
helloyongyang committed
193
194
195
196
197
198
199
200
201


class HunyuanSchedulerFeatureCaching(HunyuanScheduler):
    def __init__(self, args):
        super().__init__(args)
        self.cache_dic, self.current = cache_init(self.infer_steps)

    def step_pre(self, step_index):
        super().step_pre(step_index)
Dongz's avatar
Dongz committed
202
        self.current["step"] = step_index
helloyongyang's avatar
helloyongyang committed
203
        cal_type(self.cache_dic, self.current)