[diffusion] fix: skip zero blocks when hashing large conditioning inputs (#42748)
This commit is contained in:
+24
-3
@@ -90,6 +90,29 @@ def prefer_conditioning_cache():
|
||||
_prefer_cache.reset(token)
|
||||
|
||||
|
||||
# Image-to-video VAE inputs are one image followed by all-zero frames, so most
|
||||
# blocks of large CUDA inputs are zero. Hashing the nonzero-block bitmap and only
|
||||
# those blocks still identifies every byte, without copying the zeros to host.
|
||||
_SPARSE_HASH_BLOCK_BYTES = 64 * 1024
|
||||
_SPARSE_HASH_MIN_BYTES = 16 * 1024 * 1024
|
||||
|
||||
|
||||
def _tensor_digest(tensor):
|
||||
data = tensor.reshape(-1).view(torch.uint8)
|
||||
if not data.is_cuda or data.numel() < _SPARSE_HASH_MIN_BYTES:
|
||||
return hashlib.sha256(data.cpu().numpy()).digest()
|
||||
num_blocks = data.numel() // _SPARSE_HASH_BLOCK_BYTES
|
||||
split = num_blocks * _SPARSE_HASH_BLOCK_BYTES
|
||||
blocks = data[:split].view(num_blocks, _SPARSE_HASH_BLOCK_BYTES)
|
||||
nonzero = blocks.amax(dim=1).ne(0).cpu().numpy()
|
||||
digest = hashlib.sha256(nonzero.tobytes())
|
||||
edges = np.flatnonzero(np.diff(np.concatenate(([0], nonzero.view(np.int8), [0]))))
|
||||
for start, end in zip(edges[::2], edges[1::2]):
|
||||
digest.update(blocks[start:end].cpu().numpy())
|
||||
digest.update(data[split:].cpu().numpy())
|
||||
return digest.digest()
|
||||
|
||||
|
||||
def _fingerprint(value):
|
||||
if isinstance(value, torch.Tensor):
|
||||
if (
|
||||
@@ -98,15 +121,13 @@ def _fingerprint(value):
|
||||
or value.requires_grad
|
||||
):
|
||||
raise Uncacheable("only dense inference tensors can be cached")
|
||||
tensor = value.detach().cpu().contiguous()
|
||||
data = tensor.reshape(-1).view(torch.uint8).numpy()
|
||||
return (
|
||||
"tensor",
|
||||
str(value.dtype),
|
||||
str(value.device),
|
||||
tuple(value.shape),
|
||||
tuple(value.stride()),
|
||||
hashlib.sha256(data).digest(),
|
||||
_tensor_digest(value.detach().contiguous()),
|
||||
)
|
||||
if isinstance(value, Image.Image):
|
||||
return (
|
||||
|
||||
@@ -331,6 +331,29 @@ def test_cuda_snapshot_waits_for_producing_stream_before_restore():
|
||||
torch.testing.assert_close(actual.cpu(), reference, rtol=0, atol=0)
|
||||
|
||||
|
||||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA transfers")
|
||||
@torch.no_grad()
|
||||
def test_cuda_video_condition_key_covers_every_byte():
|
||||
cache = ConditioningCache(256 * 1024 * 1024)
|
||||
vae = VAE().eval()
|
||||
# image followed by zero frames, large enough for the sparse block digest
|
||||
video = torch.zeros(1, 3, 9, 512, 512, device="cuda")
|
||||
video[:, :, 0] = torch.rand(1, 3, 512, 512, device="cuda")
|
||||
negative_zero = video.clone()
|
||||
negative_zero[0, 1, 5, 7, 7] = -0.0
|
||||
last_byte = video.clone()
|
||||
last_byte.view(-1)[-1] = 1
|
||||
shifted = torch.roll(video, shifts=1, dims=2)
|
||||
with cache.scope():
|
||||
expected = vae.encode(video).latent_dist.mean
|
||||
hit = vae.encode(video.clone()).latent_dist.mean
|
||||
for changed in (negative_zero, last_byte, shifted):
|
||||
vae.encode(changed)
|
||||
torch.testing.assert_close(hit, expected, rtol=0, atol=0)
|
||||
assert vae.calls == 4
|
||||
assert cache.hits == 1
|
||||
|
||||
|
||||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA transfers")
|
||||
@torch.no_grad()
|
||||
def test_cuda_cache_hit_does_not_wait_for_unrelated_gpu_work():
|
||||
|
||||
Reference in New Issue
Block a user