Turn the prefix KV cache off when it would not fit next to the resident weights
Browse files57 unmodified lines585960616263164 unmodified lines22822923023123223323423521 unmodified lines25725825926026126226311 unmodified lines27527627727827928028157 unmodified lines}RESOLUTION_CHOICES = [("1K — fastest", 1024), ("1.5K", 1536), ("2K — model default", 2048)]AOTI_STEP_FACTOR = 0.78TRANSPARENCY_INFO = ("Describe what to generate, or how to edit the input images (refer to them as "164 unmodified linessteps, res, cfg = 40, 1024, 1.0n_images = len(input_images or [])tokens = (res / 16.0) ** 2per_step = 2.05e-5 * tokens**1.21 * (2.0 if cfg > 1.0 else 1.0)if AOTI_BLOCKS:per_step *= AOTI_STEP_FACTORfixed = 3.0 + 2.0 * n_images + 1.5e-6 * res * resreturn int(min(340, math.ceil((fixed + steps * per_step) * 1.12)))21 unmodified lineswidth, height = resolve_size(resolution, aspect_ratio)negative_prompt = (negative_prompt or "").strip()call_kwargs = {}if negative_prompt and float(true_cfg_scale) > 1.0:call_kwargs["negative_prompt"] = negative_promptcall_kwargs["true_cfg_scale"] = float(true_cfg_scale)11 unmodified lines).images[0]print(f"[generate] images={len(image_paths)} steps={num_inference_steps} "f"res={resolution} ratio={aspect_ratio} size={image.size} "f"elapsed={time.perf_counter() - started:.1f}s",flush=True,)57 unmodified lines58596061626364656667686970717273164 unmodified lines23823924024124224324424524624724821 unmodified lines27027127227327427527627711 unmodified lines28929029129229329429529657 unmodified lines}RESOLUTION_CHOICES = [("1K — fastest", 1024), ("1.5K", 1536), ("2K — model default", 2048)]AOTI_STEP_FACTOR = 0.78KV_CACHE_BYTES_PER_TOKEN = 32 * 2 * 4096 * 2KV_CACHE_BUDGET_GB = 10.0def prefix_tokens(n_images: int, resolution: int) -> int:return n_images * (int(resolution) // 16) ** 2def kv_cache_fits(n_images: int, resolution: int) -> bool:return prefix_tokens(n_images, resolution) * KV_CACHE_BYTES_PER_TOKEN <= KV_CACHE_BUDGET_GB * 1e9TRANSPARENCY_INFO = ("Describe what to generate, or how to edit the input images (refer to them as "164 unmodified linessteps, res, cfg = 40, 1024, 1.0n_images = len(input_images or [])tokens = (res / 16.0) ** 2prefix = prefix_tokens(n_images, res)cached = kv_cache_fits(n_images, res)tokens += 0.35 * prefix if cached else prefixper_step = 2.05e-5 * tokens**1.21 * (2.0 if cfg > 1.0 else 1.0)if AOTI_BLOCKS and cached:per_step *= AOTI_STEP_FACTORfixed = 3.0 + 2.0 * n_images + 1.5e-6 * res * resreturn int(min(340, math.ceil((fixed + steps * per_step) * 1.12)))21 unmodified lineswidth, height = resolve_size(resolution, aspect_ratio)negative_prompt = (negative_prompt or "").strip()use_kv_cache = kv_cache_fits(len(image_paths), int(resolution))call_kwargs = {"use_kv_cache": use_kv_cache}if negative_prompt and float(true_cfg_scale) > 1.0:call_kwargs["negative_prompt"] = negative_promptcall_kwargs["true_cfg_scale"] = float(true_cfg_scale)11 unmodified lines).images[0]print(f"[generate] images={len(image_paths)} steps={num_inference_steps} "f"res={resolution} ratio={aspect_ratio} size={image.size} kv_cache={use_kv_cache} "f"aoti={AOTI_LOADED} "f"elapsed={time.perf_counter() - started:.1f}s",flush=True,)
app.py+18-3