Fit the ZeroGPU booking to measurement (89s vs 125s for the default request) and guard the ceiling
Browse files211 unmodified lines21221321421521621721821922022122222322422522622722821 unmodified lines25025125225325425525625725825922 unmodified lines28228328428528628749 unmodified lines33733833934034134234348 unmodified lines3923933943953963973987 unmodified lines4064074084094104112 unmodified lines414415416417418419420211 unmodified linesreturn handle.get_tensor("prompt_embeds"), handle.get_tensor("text_token_tags"), metadata, plan# Seconds of GPU one request needs, from the packed video rows it is about to denoise: linear in the rows for the# matmuls, quadratic for the attention, against the AoTI block package this Space runs._DUR_B, _DUR_C = 1.1745e-4, 3.8396e-9# The two resident decoders, the post chain and the mux, which scale with the output rather than with the step# count. `_DEFAULT_CANVAS_PIXELS` is 960x544x124, the default request._DECODE_BASE, _DECODE_PER_DEFAULT_CANVAS, _DEFAULT_CANVAS_PIXELS = 15, 15, 960 * 544 * 124# FILM, per *emitted* intermediate frame at the default canvas. Re-measured against the live Space; a 2x pass over# 124 frames at 960x544 is 123 of them._FILM_PER_FRAME = 0.16# `pack` mode: only the ~10 GB of VAEs move on a cold worker._PLACEMENT_ALLOWANCE, _PAD = 12, 10def get_duration(21 unmodified linesdenoise = steps * (_DUR_B * rows + _DUR_C * rows**2)pixel_ratio = (height * width) / (960 * 544)decode = _DECODE_BASE + _DECODE_PER_DEFAULT_CANVAS * (height * width * num_frames) / _DEFAULT_CANVAS_PIXELSfilm = 0.0if multiplier > 1 and FILM is not None:film = (num_frames - 1) * (multiplier - 1) * _FILM_PER_FRAME * pixel_ratioreturn max(60, int(denoise + decode + film) + _PLACEMENT_ALLOWANCE + _PAD)@spaces.GPU(duration=get_duration, size=GPU_SIZE)22 unmodified linesglobal FILMif PLACEMENT == "lazy":PIPE.to("cuda")elif PLACEMENT == "pack":49 unmodified linespath = os.path.join(directory, f"pk-h3-{int(time.time() * 1000)}.mp4")encode_video(frames, fps=fps, output_path=path, audio=audio, audio_sample_rate=sampling_rate)return path, denoised, post_seconds, int(frames.shape[0]), fps, multiplierdef generate(48 unmodified linesreturn ImageOps.exif_transpose(Image.open(path)).convert("RGB") if path else Noneprogress(0.1, desc=f"Denoising {int(steps)} steps at {width}x{height}, {num_frames} frames ...")path, denoise_seconds, post_seconds, out_frames, fps, multiplier = _generate(prompt_embeds,text_token_tags,keyframe(first_frame),7 unmodified linesmultiplier,int(seed),)post = [f"RCAS {float(sharpen):.2f}" if float(sharpen) > 0 else "no sharpening"]post.append(f"FILM {multiplier}x -> {fps} fps" if multiplier > 1 else f"{fps} fps")2 unmodified linesf"{int(steps)} steps of `{schedule_key}` · {' · '.join(post)} · seed {int(seed)}\n\n"f"conditioner {condition_seconds:.0f}s ({plan['num_text_tokens']} tokens"f"{', upsampled' if refined else ''}) · denoise + decode {denoise_seconds:.0f}s "f"({denoise_seconds / max(1, int(steps)):.1f} s/step) · post {post_seconds:.0f}s")if refined:report += f"\n\n**Upsampled prompt**\n\n{refined}"211 unmodified lines21221321421521621721821922022122222322422522622722822923023123223323421 unmodified lines25625725825926026126226326426526626726826922 unmodified lines29229329429529629729849 unmodified lines34834935035135235335435548 unmodified lines4044054064074084094107 unmodified lines4184194204214224234244254264274284294304314322 unmodified lines435436437438439440441442211 unmodified linesreturn handle.get_tensor("prompt_embeds"), handle.get_tensor("text_token_tags"), metadata, plan# Seconds of GPU one request needs. Fitted to *this* Space, measured: booking a ceiling nobody reaches spends every# visitor's ZeroGPU quota on nothing and costs the demo queue priority, so each term below is a measurement.## The denoise loop, from the packed video rows it is about to run: linear in the rows for the matmuls, quadratic for# the attention, against the AoTI block package this Space loads. 3.6 s/step at the default canvas._DUR_B, _DUR_C = 1.1745e-4, 3.8396e-9# The two resident decoders, which scale with the output rather than with the step count. `_DEFAULT_CANVAS_PIXELS` is# 960x544x124, the default request, where the pair measures ~7 s._DECODE_BASE, _DECODE_PER_DEFAULT_CANVAS, _DEFAULT_CANVAS_PIXELS = 2, 5.5, 960 * 544 * 124# The workflow's post chain. RCAS is a handful of elementwise passes over the clip; FILM is per *emitted* intermediate# frame (a 2x pass over 124 frames is 123 of them); the h264 mux is per frame actually written._POST_BASE, _FILM_PER_FRAME, _MUX_PER_FRAME = 2.0, 0.025, 0.02# `pack` mode: only the ~10 GB of fp32 VAEs move, and only on a cold worker._PLACEMENT_ALLOWANCE, _MARGIN = 8, 1.15# The ZeroGPU per-call ceiling. A booking above it is refused with `ZeroGPU illegal duration` once the request is# already in flight, so `generate` checks it up front and says which knob to turn instead._MAX_BOOKING = int(os.environ.get("H3_MAX_BOOKING", "1500"))def get_duration(21 unmodified linesdenoise = steps * (_DUR_B * rows + _DUR_C * rows**2)pixel_ratio = (height * width) / (960 * 544)decode = _DECODE_BASE + _DECODE_PER_DEFAULT_CANVAS * (height * width * num_frames) / _DEFAULT_CANVAS_PIXELSif multiplier > 1 and FILM is None:multiplier = 1out_frames = (num_frames - 1) * multiplier + 1 if multiplier > 1 else num_framesfilm = (num_frames - 1) * (multiplier - 1) * _FILM_PER_FRAME * pixel_ratiopost = _POST_BASE + film + out_frames * _MUX_PER_FRAME * pixel_ratioreturn max(60, int((denoise + decode + post) * _MARGIN) + _PLACEMENT_ALLOWANCE)@spaces.GPU(duration=get_duration, size=GPU_SIZE)22 unmodified linesglobal FILMbooked = time.time()if PLACEMENT == "lazy":PIPE.to("cuda")elif PLACEMENT == "pack":49 unmodified linespath = os.path.join(directory, f"pk-h3-{int(time.time() * 1000)}.mp4")encode_video(frames, fps=fps, output_path=path, audio=audio, audio_sample_rate=sampling_rate)# `booked` to here is what `get_duration` had to predict, so it is what the report prints it against.return path, denoised, post_seconds, time.time() - booked, int(frames.shape[0]), fps, multiplierdef generate(48 unmodified linesreturn ImageOps.exif_transpose(Image.open(path)).convert("RGB") if path else Noneprogress(0.1, desc=f"Denoising {int(steps)} steps at {width}x{height}, {num_frames} frames ...")call = (prompt_embeds,text_token_tags,keyframe(first_frame),7 unmodified linesmultiplier,int(seed),)# The same call `spaces` will book the worker with, so the report can show the fit against the measurement.booked_seconds = get_duration(*call)if booked_seconds > _MAX_BOOKING:raise gr.Error(f"That would book {booked_seconds}s of GPU, over the {_MAX_BOOKING}s ZeroGPU ceiling. Shorten the "f"**duration**, drop the **steps**, or pick a smaller **target dimension** — the denoise loop is "f"quadratic in the canvas.")path, denoise_seconds, post_seconds, gpu_seconds, out_frames, fps, multiplier = _generate(*call)post = [f"RCAS {float(sharpen):.2f}" if float(sharpen) > 0 else "no sharpening"]post.append(f"FILM {multiplier}x -> {fps} fps" if multiplier > 1 else f"{fps} fps")2 unmodified linesf"{int(steps)} steps of `{schedule_key}` · {' · '.join(post)} · seed {int(seed)}\n\n"f"conditioner {condition_seconds:.0f}s ({plan['num_text_tokens']} tokens"f"{', upsampled' if refined else ''}) · denoise + decode {denoise_seconds:.0f}s "f"({denoise_seconds / max(1, int(steps)):.1f} s/step) · post {post_seconds:.0f}s · "f"GPU {gpu_seconds:.0f}s of {booked_seconds}s booked")if refined:report += f"\n\n**Upsampled prompt**\n\n{refined}"
app.py+39-17