Skip to content

Commit 7687593

Browse files
committed
Fix host slowdown regression in SenCache by reverting to pure python control flow
1 parent 4f7c6dd commit 7687593

2 files changed

Lines changed: 26 additions & 46 deletions

File tree

src/maxdiffusion/pipelines/wan/wan_pipeline_2_2.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,19 @@ def run_inference_2_2(
374374
continue
375375

376376
# Accumulate deltas since last full compute
377-
dx_norm = jnp.sqrt(jnp.mean((latents - ref_latent) ** 2))
378-
dt = jnp.abs(t_float - ref_timestep)
377+
dx_norm = float(jnp.sqrt(jnp.mean((latents - ref_latent) ** 2)))
378+
dt = abs(t_float - ref_timestep)
379379
accum_dx += dx_norm
380380
accum_dt += dt
381381

382382
# Sensitivity score (Eq. 9)
383383
score = alpha_x * accum_dx + alpha_t * accum_dt
384384

385-
cond = jnp.logical_and(score <= sen_epsilon, reuse_count < max_reuse)
386-
387-
def hit_fn(args):
388-
ref_noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count, cache_count = args
389-
return ref_noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count + 1, cache_count + 1
390-
391-
def miss_fn(args):
392-
_, _, _, _, _, _, cache_count = args
385+
if score <= sen_epsilon and reuse_count < max_reuse:
386+
noise_pred = ref_noise_pred
387+
reuse_count += 1
388+
cache_count += 1
389+
else:
393390
latents_doubled = jnp.concatenate([latents] * 2)
394391
timestep = jnp.broadcast_to(t, bsz * 2)
395392
noise_pred, _, _ = transformer_forward_pass_full_cfg(
@@ -404,19 +401,12 @@ def miss_fn(args):
404401
rotary_emb=rotary_emb,
405402
encoder_attention_mask=encoder_attention_mask,
406403
)
407-
return (
408-
noise_pred,
409-
latents,
410-
jnp.array(t_float, dtype=jnp.float32),
411-
jnp.array(0.0, dtype=jnp.float32),
412-
jnp.array(0.0, dtype=jnp.float32),
413-
jnp.array(0, dtype=jnp.int32),
414-
cache_count,
415-
)
416-
417-
noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count, cache_count = jax.lax.cond(
418-
cond, hit_fn, miss_fn, (ref_noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count, cache_count)
419-
)
404+
ref_noise_pred = noise_pred
405+
ref_latent = latents
406+
ref_timestep = t_float
407+
accum_dx = 0.0
408+
accum_dt = 0.0
409+
reuse_count = 0
420410

421411
latents, scheduler_state = scheduler.step(scheduler_state, noise_pred, t, latents).to_tuple()
422412

src/maxdiffusion/pipelines/wan/wan_pipeline_i2v_2p2.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -491,21 +491,18 @@ def run_inference_2_2_i2v(
491491
latents, scheduler_state = scheduler.step(scheduler_state, noise_pred, t, latents).to_tuple()
492492
continue
493493

494-
dx_norm = jnp.sqrt(jnp.mean((latents - ref_latent) ** 2))
495-
dt = jnp.abs(t_float - ref_timestep)
494+
dx_norm = float(jnp.sqrt(jnp.mean((latents - ref_latent) ** 2)))
495+
dt = abs(t_float - ref_timestep)
496496
accum_dx += dx_norm
497497
accum_dt += dt
498498

499499
score = alpha_x * accum_dx + alpha_t * accum_dt
500500

501-
cond = jnp.logical_and(score <= sen_epsilon, reuse_count < max_reuse)
502-
503-
def hit_fn(args):
504-
ref_noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count, cache_count = args
505-
return ref_noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count + 1, cache_count + 1
506-
507-
def miss_fn(args):
508-
_, _, _, _, _, _, cache_count = args
501+
if score <= sen_epsilon and reuse_count < max_reuse:
502+
noise_pred = ref_noise_pred
503+
reuse_count += 1
504+
cache_count += 1
505+
else:
509506
latents_doubled = jnp.concatenate([latents, latents], axis=0)
510507
latents_doubled = jnp.transpose(latents_doubled, (0, 4, 1, 2, 3))
511508
latent_model_input = jnp.concatenate([latents_doubled, condition_doubled], axis=1)
@@ -524,19 +521,12 @@ def miss_fn(args):
524521
encoder_attention_mask=encoder_attention_mask,
525522
)
526523
noise_pred = jnp.transpose(noise_pred, (0, 2, 3, 4, 1))
527-
return (
528-
noise_pred,
529-
latents,
530-
jnp.array(t_float, dtype=jnp.float32),
531-
jnp.array(0.0, dtype=jnp.float32),
532-
jnp.array(0.0, dtype=jnp.float32),
533-
jnp.array(0, dtype=jnp.int32),
534-
cache_count,
535-
)
536-
537-
noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count, cache_count = jax.lax.cond(
538-
cond, hit_fn, miss_fn, (ref_noise_pred, ref_latent, ref_timestep, accum_dx, accum_dt, reuse_count, cache_count)
539-
)
524+
ref_noise_pred = noise_pred
525+
ref_latent = latents
526+
ref_timestep = t_float
527+
accum_dx = 0.0
528+
accum_dt = 0.0
529+
reuse_count = 0
540530

541531
latents, scheduler_state = scheduler.step(scheduler_state, noise_pred, t, latents).to_tuple()
542532

0 commit comments

Comments
 (0)