File size: 15,372 Bytes
20962c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | """In-memory access to the prompt-sharded Self-Forcing predictor dataset."""
from __future__ import annotations
import time
from contextlib import ExitStack
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Iterable
import torch
from safetensors import safe_open
from wan.modules.causal_model import causal_rope_apply
TOKENS_PER_FRAME = 30 * 52
FRAMES_PER_CHUNK = 3
TOKENS_PER_CHUNK = TOKENS_PER_FRAME * FRAMES_PER_CHUNK
@dataclass
class PromptCommon:
hidden: list[list[torch.Tensor]]
noisy: dict[tuple[int, int], torch.Tensor]
flow: dict[tuple[int, int], torch.Tensor]
timestep: dict[tuple[int, int], torch.Tensor]
@dataclass
class LayerPromptCache:
history_k: torch.Tensor
history_v: torch.Tensor
cross_k: torch.Tensor
cross_v: torch.Tensor
class OfflinePredictorStore:
"""Keep common trajectories in RAM; load one block's KV cache at a time."""
def __init__(
self,
root: str | Path,
prompt_ids: Iterable[int],
num_chunks: int = 7,
max_history_chunks: int = 7,
) -> None:
self.root = Path(root).resolve()
self.prompt_ids = sorted(set(int(value) for value in prompt_ids))
self.num_chunks = int(num_chunks)
self.max_history_chunks = int(max_history_chunks)
if self.num_chunks < 2:
raise ValueError("num_chunks must be at least 2")
if self.max_history_chunks < 1:
raise ValueError("max_history_chunks must be positive")
self.common: dict[int, PromptCommon] = {}
self.layer_cache: dict[int, LayerPromptCache] = {}
self.layer_caches: dict[int, dict[int, LayerPromptCache]] = {}
self.layer_id: int | None = None
self._load_common()
def _load_common(self) -> None:
started = time.perf_counter()
for offset, prompt_id in enumerate(self.prompt_ids, start=1):
path = (
self.root
/ f"prompt_{prompt_id:04d}"
/ "trajectory.safetensors"
)
context_path = (
path.parent / "chunk0_context" / "trajectory.safetensors"
)
hidden: list[list[torch.Tensor]] = []
noisy: dict[tuple[int, int], torch.Tensor] = {}
flow: dict[tuple[int, int], torch.Tensor] = {}
timestep: dict[tuple[int, int], torch.Tensor] = {}
with ExitStack() as stack:
handle = stack.enter_context(
safe_open(path, framework="pt", device="cpu")
)
context_handle = (
stack.enter_context(
safe_open(context_path, framework="pt", device="cpu")
)
if context_path.exists()
else None
)
for chunk in range(self.num_chunks):
chunk_hidden = []
for step in range(4):
prefix = f"chunk_{chunk:02d}_step_{step:02d}"
source = (
context_handle
if chunk == 0 and context_handle is not None
else handle
)
chunk_hidden.append(
source.get_tensor(
f"{prefix}_final_hidden"
).squeeze(0)
)
if chunk >= 1 and step >= 1:
noisy[(chunk, step)] = handle.get_tensor(
f"{prefix}_noisy_latent"
).squeeze(0)
flow[(chunk, step)] = handle.get_tensor(
f"{prefix}_flow"
).squeeze(0)
timestep[(chunk, step)] = handle.get_tensor(
f"{prefix}_timestep"
).squeeze(0)
hidden.append(chunk_hidden)
self.common[prompt_id] = PromptCommon(
hidden=hidden,
noisy=noisy,
flow=flow,
timestep=timestep,
)
if offset % 10 == 0 or offset == len(self.prompt_ids):
elapsed = time.perf_counter() - started
print(
f"[data] common {offset}/{len(self.prompt_ids)} "
f"({elapsed:.1f}s)",
flush=True,
)
@torch.inference_mode()
def load_layer_cache(
self,
layer_id: int,
teacher_model: torch.nn.Module,
device: torch.device,
) -> None:
"""Project clean prefeatures once with frozen Teacher K/V weights."""
self.layer_caches = {}
self.layer_cache = {}
self.layer_id = int(layer_id)
teacher_block = teacher_model.blocks[layer_id]
heads = teacher_block.num_heads
head_dim = teacher_block.dim // heads
if teacher_model.freqs.device != device:
teacher_model.freqs = teacher_model.freqs.to(device)
started = time.perf_counter()
history_chunks = self.num_chunks - 1
grid_sizes = torch.tensor(
[[history_chunks * FRAMES_PER_CHUNK, 30, 52]], dtype=torch.long
)
for offset, prompt_id in enumerate(self.prompt_ids, start=1):
prompt_dir = self.root / f"prompt_{prompt_id:04d}"
prefeature_path = (
prompt_dir
/ "clean_prefeatures"
/ f"block_{layer_id:02d}.safetensors"
)
context_prefeature_path = (
prompt_dir
/ "chunk0_context"
/ "clean_prefeatures"
/ f"block_{layer_id:02d}.safetensors"
)
with ExitStack() as stack:
handle = stack.enter_context(
safe_open(prefeature_path, framework="pt", device="cpu")
)
context_handle = (
stack.enter_context(
safe_open(
context_prefeature_path,
framework="pt",
device="cpu",
)
)
if context_prefeature_path.exists()
else None
)
prefeature = torch.cat(
[
(
context_handle.get_tensor("chunk_00")
if chunk == 0 and context_handle is not None
else handle.get_tensor(f"chunk_{chunk:02d}")
)
for chunk in range(history_chunks)
],
dim=1,
)
prefeature = prefeature.to(
device=device,
dtype=torch.bfloat16,
non_blocking=False,
)
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
key = teacher_block.self_attn.norm_k(
teacher_block.self_attn.k(prefeature)
).view(1, -1, heads, head_dim)
value = teacher_block.self_attn.v(prefeature).view(
1, -1, heads, head_dim
)
key = causal_rope_apply(
key,
grid_sizes,
teacher_model.freqs,
start_frame=0,
)
key = (
key.reshape(
1, history_chunks, TOKENS_PER_CHUNK, heads, head_dim
)
.squeeze(0)
.to(device="cpu", dtype=torch.bfloat16)
.contiguous()
)
value = (
value.reshape(
1, history_chunks, TOKENS_PER_CHUNK, heads, head_dim
)
.squeeze(0)
.to(device="cpu", dtype=torch.bfloat16)
.contiguous()
)
cross_path = prompt_dir / "cross_attention.safetensors"
with safe_open(
cross_path, framework="pt", device="cpu"
) as handle:
cross_k = handle.get_tensor(
f"block_{layer_id:02d}_k"
).squeeze(0)
cross_v = handle.get_tensor(
f"block_{layer_id:02d}_v"
).squeeze(0)
self.layer_cache[prompt_id] = LayerPromptCache(
history_k=key,
history_v=value,
cross_k=cross_k,
cross_v=cross_v,
)
del prefeature, key, value
if offset % 10 == 0 or offset == len(self.prompt_ids):
elapsed = time.perf_counter() - started
print(
f"[data] block {layer_id:02d} cache "
f"{offset}/{len(self.prompt_ids)} ({elapsed:.1f}s)",
flush=True,
)
torch.cuda.empty_cache()
@torch.inference_mode()
def load_layer_caches(
self,
layer_ids: Iterable[int],
teacher_model: torch.nn.Module,
device: torch.device,
) -> None:
"""Load Teacher-layer caches, retaining overlap with the previous group."""
requested = list(dict.fromkeys(int(value) for value in layer_ids))
if not requested:
raise ValueError("At least one layer cache is required")
loaded: dict[int, dict[int, LayerPromptCache]] = {
layer_id: self.layer_caches[layer_id]
for layer_id in requested
if layer_id in self.layer_caches
}
reused = sorted(loaded)
# Drop layers that are no longer requested before projecting a new one.
# The dictionaries in ``loaded`` keep only the overlapping layers alive.
self.layer_caches = {}
self.layer_cache = {}
self.layer_id = None
if reused:
print(f"[data] reusing layer caches {reused}", flush=True)
for layer_id in requested:
if layer_id in loaded:
continue
self.load_layer_cache(layer_id, teacher_model, device)
loaded[layer_id] = self.layer_cache
self.layer_caches = loaded
def batch_layers(
self,
prompt_ids: list[int],
chunk: int,
target_step: int,
layer_ids: Iterable[int],
) -> dict[str, Any]:
"""Build one sample batch with independent K/V inputs for each block."""
requested = [int(value) for value in layer_ids]
if not requested:
raise ValueError("At least one layer ID is required")
missing = sorted(set(requested) - set(self.layer_caches))
if missing:
raise RuntimeError(f"Layer caches not loaded: {missing}")
output = self._base_batch(prompt_ids, chunk, target_step)
output["layer_ids"] = requested
for position, layer_id in enumerate(requested):
cache = [self.layer_caches[layer_id][prompt_id] for prompt_id in prompt_ids]
output[f"history_k_{position}"] = torch.stack(
[
item.history_k[:chunk].reshape(
chunk * TOKENS_PER_CHUNK,
item.history_k.shape[-2],
item.history_k.shape[-1],
)
for item in cache
]
)
output[f"history_v_{position}"] = torch.stack(
[
item.history_v[:chunk].reshape(
chunk * TOKENS_PER_CHUNK,
item.history_v.shape[-2],
item.history_v.shape[-1],
)
for item in cache
]
)
output[f"cross_k_{position}"] = torch.stack(
[item.cross_k for item in cache]
)
output[f"cross_v_{position}"] = torch.stack(
[item.cross_v for item in cache]
)
return output
def batch(
self,
prompt_ids: list[int],
chunk: int,
target_step: int,
) -> dict[str, Any]:
if self.layer_id is None or not self.layer_cache:
raise RuntimeError("load_layer_cache must be called first")
output = self._base_batch(prompt_ids, chunk, target_step)
cache = [self.layer_cache[prompt_id] for prompt_id in prompt_ids]
history_start = max(0, chunk - self.max_history_chunks)
history_chunks = chunk - history_start
output.update(
{
"history_k": torch.stack(
[
item.history_k[history_start:chunk].reshape(
history_chunks * TOKENS_PER_CHUNK,
item.history_k.shape[-2],
item.history_k.shape[-1],
)
for item in cache
]
),
"history_v": torch.stack(
[
item.history_v[history_start:chunk].reshape(
history_chunks * TOKENS_PER_CHUNK,
item.history_v.shape[-2],
item.history_v.shape[-1],
)
for item in cache
]
),
"cross_k": torch.stack([item.cross_k for item in cache]),
"cross_v": torch.stack([item.cross_v for item in cache]),
}
)
return output
def _base_batch(
self,
prompt_ids: list[int],
chunk: int,
target_step: int,
) -> dict[str, Any]:
if chunk < 1 or chunk >= self.num_chunks:
raise ValueError(
f"Trainable chunk must be 1..{self.num_chunks - 1}, got {chunk}"
)
if target_step < 1 or target_step > 3:
raise ValueError(
f"Target denoising step must be 1..3, got {target_step}"
)
anchor_step = target_step - 1
common = [self.common[prompt_id] for prompt_id in prompt_ids]
return {
"prompt_ids": prompt_ids,
"chunk": chunk,
"anchor_step": anchor_step,
"target_step": target_step,
"noisy_latent": torch.stack(
[item.noisy[(chunk, target_step)] for item in common]
),
"anchor_hidden": torch.stack(
[item.hidden[chunk][anchor_step] for item in common]
),
"previous_hidden": torch.stack(
[item.hidden[chunk - 1][target_step] for item in common]
),
"target_hidden": torch.stack(
[item.hidden[chunk][target_step] for item in common]
),
"target_flow": torch.stack(
[item.flow[(chunk, target_step)] for item in common]
),
"timestep": torch.stack(
[item.timestep[(chunk, target_step)] for item in common]
),
}
|