Skip to content

Commit 4f7c6dd

Browse files
committed
Fix host slowdown regression in magcache_step by reverting to pure numpy
1 parent c2e0d1c commit 4f7c6dd

1 file changed

Lines changed: 29 additions & 89 deletions

File tree

src/maxdiffusion/pipelines/wan/wan_pipeline.py

Lines changed: 29 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,7 @@ def magcache_step(
13331333
skip_warmup: Warmup steps threshold.
13341334
use_magcache: Optional manual override boolean to enable/disable cache for this step.
13351335
"""
1336-
import jax.numpy as jnp
1337-
import jax
1336+
import numpy as np
13381337

13391338
(
13401339
accumulated_ratio_cond,
@@ -1349,97 +1348,38 @@ def magcache_step(
13491348
cur_mag_ratio_uncond = mag_ratios[step * 2 + 1]
13501349

13511350
if use_magcache is None:
1352-
use_magcache = step >= skip_warmup
1351+
use_magcache = True
1352+
if step < skip_warmup:
1353+
use_magcache = False
13531354

1354-
def compute_magcache_hit(args):
1355-
(
1356-
accumulated_ratio_cond,
1357-
accumulated_ratio_uncond,
1358-
accumulated_err_cond,
1359-
accumulated_err_uncond,
1360-
accumulated_steps_cond,
1361-
accumulated_steps_uncond,
1362-
cur_mag_ratio_cond,
1363-
cur_mag_ratio_uncond,
1364-
) = args
1355+
skip_blocks = False
1356+
if use_magcache:
13651357
new_ratio_cond = accumulated_ratio_cond * cur_mag_ratio_cond
13661358
new_ratio_uncond = accumulated_ratio_uncond * cur_mag_ratio_uncond
13671359

1368-
err_cond = jnp.abs(1.0 - new_ratio_cond)
1369-
err_uncond = jnp.abs(1.0 - new_ratio_uncond)
1370-
1371-
cond = jnp.logical_and(accumulated_err_cond + err_cond < magcache_thresh, accumulated_steps_cond < magcache_K)
1372-
cond = jnp.logical_and(cond, accumulated_err_uncond + err_uncond < magcache_thresh)
1373-
cond = jnp.logical_and(cond, accumulated_steps_uncond < magcache_K)
1374-
1375-
def hit_fn(_):
1376-
return (
1377-
True,
1378-
new_ratio_cond,
1379-
new_ratio_uncond,
1380-
accumulated_err_cond + err_cond,
1381-
accumulated_err_uncond + err_uncond,
1382-
accumulated_steps_cond + 1,
1383-
accumulated_steps_uncond + 1,
1384-
)
1385-
1386-
def miss_fn(_):
1387-
return (
1388-
False,
1389-
jnp.array(1.0, dtype=jnp.float32),
1390-
jnp.array(1.0, dtype=jnp.float32),
1391-
jnp.array(0.0, dtype=jnp.float32),
1392-
jnp.array(0.0, dtype=jnp.float32),
1393-
jnp.array(0, dtype=jnp.int32),
1394-
jnp.array(0, dtype=jnp.int32),
1395-
)
1396-
1397-
return jax.lax.cond(cond, hit_fn, miss_fn, None)
1398-
1399-
def skip_magcache(args):
1400-
(
1401-
accumulated_ratio_cond,
1402-
accumulated_ratio_uncond,
1403-
accumulated_err_cond,
1404-
accumulated_err_uncond,
1405-
accumulated_steps_cond,
1406-
accumulated_steps_uncond,
1407-
_,
1408-
_,
1409-
) = args
1410-
return (
1411-
False,
1412-
accumulated_ratio_cond,
1413-
accumulated_ratio_uncond,
1414-
accumulated_err_cond,
1415-
accumulated_err_uncond,
1416-
accumulated_steps_cond,
1417-
accumulated_steps_uncond,
1418-
)
1419-
1420-
(
1421-
skip_blocks,
1422-
accumulated_ratio_cond,
1423-
accumulated_ratio_uncond,
1424-
accumulated_err_cond,
1425-
accumulated_err_uncond,
1426-
accumulated_steps_cond,
1427-
accumulated_steps_uncond,
1428-
) = jax.lax.cond(
1429-
use_magcache,
1430-
compute_magcache_hit,
1431-
skip_magcache,
1432-
(
1433-
accumulated_ratio_cond,
1434-
accumulated_ratio_uncond,
1435-
accumulated_err_cond,
1436-
accumulated_err_uncond,
1437-
accumulated_steps_cond,
1438-
accumulated_steps_uncond,
1439-
cur_mag_ratio_cond,
1440-
cur_mag_ratio_uncond,
1441-
),
1442-
)
1360+
err_cond = np.abs(1.0 - new_ratio_cond)
1361+
err_uncond = np.abs(1.0 - new_ratio_uncond)
1362+
1363+
if (
1364+
accumulated_err_cond + err_cond < magcache_thresh
1365+
and accumulated_steps_cond < magcache_K
1366+
and accumulated_err_uncond + err_uncond < magcache_thresh
1367+
and accumulated_steps_uncond < magcache_K
1368+
):
1369+
skip_blocks = True
1370+
accumulated_ratio_cond = new_ratio_cond
1371+
accumulated_ratio_uncond = new_ratio_uncond
1372+
accumulated_err_cond += err_cond
1373+
accumulated_err_uncond += err_uncond
1374+
accumulated_steps_cond += 1
1375+
accumulated_steps_uncond += 1
1376+
else:
1377+
accumulated_ratio_cond = 1.0
1378+
accumulated_ratio_uncond = 1.0
1379+
accumulated_err_cond = 0.0
1380+
accumulated_err_uncond = 0.0
1381+
accumulated_steps_cond = 0
1382+
accumulated_steps_uncond = 0
14431383

14441384
new_state = (
14451385
accumulated_ratio_cond,

0 commit comments

Comments
 (0)