Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _accumulate_kv_cache(self, kv_cache, chunk_idx):
for block_id in range(self.num_model_blocks):
cur_kv_cache[block_id][2] = kv_cache[chunk_idx - 1][block_id][2]
cum_vk, cum_k_sum = None, None
start_chunk_idx = chunk_idx - self.num_cached_blocks if self.num_cached_blocks > 0 else 0
start_chunk_idx = max(chunk_idx - self.num_cached_blocks, 0) if self.num_cached_blocks > 0 else 0
for i in range(start_chunk_idx, chunk_idx):
prev = kv_cache[i][block_id]
if prev[0] is not None and prev[1] is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _accumulate_kv_cache(self, kv_cache, chunk_idx):
for block_id in range(self.num_model_blocks):
cur_kv_cache[block_id][2] = kv_cache[chunk_idx - 1][block_id][2]
cum_vk, cum_k_sum = None, None
start_chunk_idx = chunk_idx - self.num_cached_blocks if self.num_cached_blocks > 0 else 0
start_chunk_idx = max(chunk_idx - self.num_cached_blocks, 0) if self.num_cached_blocks > 0 else 0
for i in range(start_chunk_idx, chunk_idx):
prev = kv_cache[i][block_id]
if prev[0] is not None and prev[1] is not None:
Expand Down
2 changes: 1 addition & 1 deletion diffusion/longsana/pipeline/sana_inference_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def _accumulate_kv_cache(self, kv_cache, chunk_idx):
for block_id in range(self.num_model_blocks):
cur_kv_cache[block_id][2] = kv_cache[chunk_idx - 1][block_id][2]
cum_vk, cum_k_sum = None, None
start_chunk_idx = chunk_idx - self.num_cached_blocks if self.num_cached_blocks > 0 else 0
start_chunk_idx = max(chunk_idx - self.num_cached_blocks, 0) if self.num_cached_blocks > 0 else 0
for i in range(start_chunk_idx, chunk_idx):
prev = kv_cache[i][block_id]
if prev[0] is not None and prev[1] is not None:
Expand Down
2 changes: 1 addition & 1 deletion diffusion/longsana/pipeline/sana_training_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ def _accumulate_kv_cache(self, kv_cache, chunk_idx):
cum_vk, cum_k_sum = None, None
#
# accumulate the incremental of all historical blocks
start_chunk_idx = chunk_idx - self.num_cached_blocks if self.num_cached_blocks > 0 else 0
start_chunk_idx = max(chunk_idx - self.num_cached_blocks, 0) if self.num_cached_blocks > 0 else 0
for i in range(start_chunk_idx, chunk_idx):
prev = kv_cache[i][block_id]
if prev[0] is not None and prev[1] is not None:
Expand Down
2 changes: 1 addition & 1 deletion diffusion/scheduler/longlive_flow_euler_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def _accumulate_kv_cache(self, kv_cache, chunk_idx):
for block_id in range(self.num_model_blocks):
cur_kv_cache[block_id][2] = kv_cache[chunk_idx - 1][block_id][2]
cum_vk, cum_k_sum = None, None
start_chunk_idx = chunk_idx - self.num_cached_blocks if self.num_cached_blocks > 0 else 0
start_chunk_idx = max(chunk_idx - self.num_cached_blocks, 0) if self.num_cached_blocks > 0 else 0
for i in range(start_chunk_idx, chunk_idx):
prev = kv_cache[i][block_id]
if prev[0] is not None and prev[1] is not None:
Expand Down
83 changes: 83 additions & 0 deletions tests/test_longsana_cache_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2026 NVIDIA CORPORATION & AFFILIATES
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
import pytest
import torch

from diffusion.longsana.pipeline.sana_inference_interactive_pipeline import SanaInferenceInteractivePipeline
from diffusion.longsana.pipeline.sana_inference_interactive_pipeline_long_chunk import (
SanaInferenceInteractivePipelineLongChunk,
)
from diffusion.longsana.pipeline.sana_inference_pipeline import SanaInferencePipeline
from diffusion.longsana.pipeline.sana_training_pipeline import SanaTrainingPipeline
from diffusion.scheduler.longlive_flow_euler_sampler import LongLiveFlowEuler


@pytest.mark.parametrize(
"cache_type",
[
LongLiveFlowEuler,
SanaInferencePipeline,
SanaInferenceInteractivePipeline,
SanaInferenceInteractivePipelineLongChunk,
SanaTrainingPipeline,
],
ids=lambda cls: cls.__name__,
)
@pytest.mark.parametrize(
"num_chunks, chunk_idx, window",
[
(3, 1, 4), # Negative indices revisit the first chunk without clamping.
(3, 1, 5), # A still larger window indexes outside the cache list.
(4, 2, 8),
(4, 3, 1),
(4, 3, 2),
(4, 3, 4),
(4, 3, 0),
(4, 3, -1),
(1, 0, 4),
],
)
def test_cache_window_uses_available_history(cache_type, num_chunks, chunk_idx, window):
owner = cache_type.__new__(cache_type)
owner.num_model_blocks = 2
owner.num_cached_blocks = window
cache = [[[None, None, None] for _ in range(owner.num_model_blocks)] for _ in range(num_chunks)]
for i in range(chunk_idx):
for block in range(owner.num_model_blocks):
value = 10 * block + i + 1
cache[i][block] = [
torch.full((1, 2, 3, 3), float(value)),
torch.full((1, 2, 1, 3), float(value + 1)),
torch.tensor([float(value + 2)]),
]
history = [[tuple(t.clone() for t in block) for block in chunk] for chunk in cache[:chunk_idx]]

actual = owner._accumulate_kv_cache(cache, chunk_idx)

assert actual is cache[chunk_idx]
if chunk_idx == 0:
assert actual == [[None, None, None], [None, None, None]]
return
selected = history[-window:] if window > 0 else history
for block in range(owner.num_model_blocks):
for slot in (0, 1):
expected = torch.stack([chunk[block][slot] for chunk in selected]).sum(dim=0)
torch.testing.assert_close(actual[block][slot], expected, rtol=0, atol=0)
assert actual[block][2] is cache[chunk_idx - 1][block][2]
for before, after in zip(history, cache[:chunk_idx]):
for before_block, after_block in zip(before, after):
for expected, value in zip(before_block, after_block):
torch.testing.assert_close(value, expected, rtol=0, atol=0)
Loading