Skip to content

fix(longsana): clamp KV cache windows to available history - #577

Merged
lawrence-cj merged 1 commit into
NVlabs:mainfrom
yuhengcai1:codex/fix-longsana-cache-window
Nov 12, 2026
Merged

lawrence-cj merged 1 commit into
NVlabs:mainfrom
yuhengcai1:codex/fix-longsana-cache-window

Conversation

@yuhengcai1

Copy link
Copy Markdown
Contributor

A positive num_cached_blocks larger than the available history makes five legacy LongSANA cache accumulators start at a negative Python list index. For example, with three cache slots while processing chunk 1, a window of 4 counts chunk 0 twice; a window of 5 raises IndexError. Short videos can therefore produce incorrect history totals or fail with a larger configured cache window.

Clamp the window start to zero in the five accumulators, matching the existing streaming/self-forcing helpers. Add 45 parameterized regression cases across all five owners covering oversized windows, bounded windows, full-history settings (zero and negative), and the first chunk. The tests also check current-cache identity, previous convolution-state reuse, and that historical cache entries are not mutated.

Validation against main b3bad9563288b5fd0dc5734fc230827acf70f23e:

  • Local CPU, FP32 and BF16: before 60 passed / 30 failed; after 90 passed.
  • Colab NVIDIA A100-SXM4-40GB, FP32 and BF16: before 60 passed / 30 failed; after 90 passed, with PyTorch 2.11.0+cu130.
  • All applicable repository-pinned Python pre-commit hooks and git diff --check pass.

Validation scope: the standalone runner compiles the exact production method and submitted test function definitions while isolating unrelated model imports, then runs the 45 cases in each dtype. It does not construct pretrained models or validate the full Sana dependency/import setup or end-to-end video generation. Source hashes and per-case results were captured. The committed test uses normal project imports.

Related to #345: this bounds bug was found while tracing the cache path discussed there. The attention writer already correctly stores chunk-local statistics before adding history; this PR preserves that behavior and does not implement the proposed writer reordering or close that issue.

@lawrence-cj lawrence-cj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yuhengcai1, LGTM. This matches the existing clamp in the self-forcing sampler.

@lawrence-cj
lawrence-cj merged commit 45aece2 into NVlabs:main Nov 12, 2026
11 checks passed
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.

2 participants