Commit 7dafddb
committed
[perf]: cache TimestepEmbedder sinusoidal frequency table per device
TimestepEmbedder rebuilt the sinusoidal frequency table on every forward
via a CPU `arange` + `exp` followed by an H2D copy. The table depends only
on (frequency_embedding_size, max_period, freq_dtype) — all instance-
constant — and the compute device, so it is identical across every
denoising step. Cache it on the module per device; only the timestep-
dependent product (`args = t * freqs`) runs each call.
`timestep_embedding` gains an optional precomputed `freqs` argument so the
free function's behavior is unchanged for any direct caller. Same cache-on-
self pattern as the Wan rotary precompute in this branch and matrixgame2
PR hao-ai-lab#1415. TimestepEmbedder is shared across the DiT stack (Wan,
HunyuanVideo/HV15, hunyuangamecraft, longcat, matrixgame2/3, hyworld), so
every model that uses it drops the per-forward recompute + H2D.
Bit-exact — the cached tensor is the same value the prior path recomputed
each call.1 parent dba71f2 commit 7dafddb
1 file changed
Lines changed: 30 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
128 | 146 | | |
129 | | - | |
| 147 | + | |
130 | 148 | | |
131 | 149 | | |
132 | 150 | | |
| |||
137 | 155 | | |
138 | 156 | | |
139 | 157 | | |
140 | | - | |
| 158 | + | |
| 159 | + | |
141 | 160 | | |
142 | 161 | | |
143 | | - | |
| 162 | + | |
144 | 163 | | |
145 | 164 | | |
146 | 165 | | |
147 | 166 | | |
148 | | - | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
149 | 173 | | |
150 | 174 | | |
151 | 175 | | |
152 | 176 | | |
153 | | - | |
| 177 | + | |
| 178 | + | |
154 | 179 | | |
155 | 180 | | |
156 | 181 | | |
| |||
0 commit comments