Skip to content

Fix CogVideoX dynamic CFG to use loop index instead of timestep value#13172

Open
veeceey wants to merge 1 commit intohuggingface:mainfrom
veeceey:fix/issue-9641-cogvideox-dynamic-cfg
Open

Fix CogVideoX dynamic CFG to use loop index instead of timestep value#13172
veeceey wants to merge 1 commit intohuggingface:mainfrom
veeceey:fix/issue-9641-cogvideox-dynamic-cfg

Conversation

@veeceey
Copy link
Contributor

@veeceey veeceey commented Feb 23, 2026

The dynamic CFG calculation in CogVideoX pipelines was using t.item() (the raw timestep value, which ranges from ~1 to ~999) instead of the loop index i (which ranges from 0 to num_inference_steps - 1). This made ((num_inference_steps - t.item()) / num_inference_steps) go deeply negative, and after ** 5.0 + math.cos, the guidance scale oscillates wildly instead of following a smooth cosine annealing schedule.

Replaced t.item() with i in all four CogVideoX pipeline files:

  • pipeline_cogvideox.py
  • pipeline_cogvideox_image2video.py
  • pipeline_cogvideox_video2video.py
  • pipeline_cogvideox_fun_control.py

Quick sanity check with guidance_scale=4, num_inference_steps=50:

OLD (broken): 1.86, 2.03, 4.98, 4.61, 2.19, 3.18, 1.03, 3.11, 1.41, 4.99 ...
NEW (fixed):  5.00, 4.91, 4.67, 4.34, 3.96, 3.56, 3.17, 2.81, 2.49, 2.21 ...

Fixes #9641

The dynamic CFG calculation was using t.item() (timestep value, range
1-999) where it should use the loop index i (range 0 to
num_inference_steps-1). This caused the cosine schedule to produce
wildly oscillating guidance values instead of a smooth annealing
from guidance_scale+1 down to 1.

Fixes huggingface#9641
@veeceey
Copy link
Contributor Author

veeceey commented Feb 23, 2026

Manual Test Results

Verified the dynamic CFG schedule with guidance_scale=4, num_inference_steps=50:

Before fix (oscillating wildly due to t.item() being 1-999):

Step 0: t=999, cfg=1.8610
Step 1: t=979, cfg=2.0290
Step 2: t=959, cfg=4.9829
Step 3: t=939, cfg=4.6136
Step 4: t=919, cfg=2.1921
Step 5: t=899, cfg=3.1813
Step 6: t=879, cfg=1.0251
Step 7: t=859, cfg=3.1136
Step 8: t=839, cfg=1.4120
Step 9: t=819, cfg=4.9888

After fix (smooth cosine annealing from 5.0 down to 1.0):

Step 0: t=999, cfg=5.0000
Step 1: t=979, cfg=4.9096
Step 2: t=959, cfg=4.6729
Step 3: t=939, cfg=4.3409
Step 4: t=919, cfg=3.9584
Step 5: t=899, cfg=3.5609
Step 6: t=879, cfg=3.1740
Step 7: t=859, cfg=2.8145
Step 8: t=839, cfg=2.4917
Step 9: t=819, cfg=2.2100

Also fixed in pipeline_cogvideox_fun_control.py which had the same bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code With Strange Logic in CogVideoX's Dynamic CFG

1 participant