Files
hao-ai-lab-FastVideo/fastvideo/api/request_resolution.py
Davids048andClaude Opus 5.5 38096b94a3 [refactor]: resolve request settings against the model's preset defaults
resolve_generation_request in fastvideo/api/resolution.py runs resolution
steps over the settings sections of a GenerationRequest (sampling, runtime,
output, stage_overrides) and freezes them as a ResolvedRequest. The explicit
paths are the ones the request tracked while it was parsed or built. Prompts,
inputs, state, plan, and extensions are request data and are left out, so
large tensors are not copied.

fastvideo/api/request_resolution.py adds resolve_request(request, model_path).
Its step, fill_sampling_defaults[preset <name>], sets each setting that the
request did not write to the model's preset default
(SamplingParam.from_pretrained). This is the same source that
request_to_sampling_param starts from, and a test checks that the two agree
field by field.

VideoGenerator attaches the ResolvedRequest to each GenerationResult as
resolved_request, so the source of every setting can be looked up. The golden
request snapshots record request_resolution_decisions; nothing else changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012M91wnVFmPEvJ9h39r5BH7
2026-10-02 20:23:05 +00:00

66 lines
3.0 KiB
Python

# SPDX-License-Identifier: Apache-2.0
"""Resolution of the settings of a ``GenerationRequest`` against the model's sampling defaults.
``resolve_request`` records, for every sampling, runtime, and output field, whether the request set it or the
model's preset defaults (``SamplingParam.from_pretrained``) decided it. ``request_to_sampling_param`` builds the
``SamplingParam`` from the same two sources: the preset defaults, then the fields that the request set.
"""
from __future__ import annotations
from copy import deepcopy
from dataclasses import fields
from typing import Any
from fastvideo.api.resolution import ResolutionStep, ResolutionView, ResolvedRequest, resolve_generation_request
from fastvideo.api.sampling_param import SamplingParam
from fastvideo.api.schema import GenerationRequest, OutputConfig, RequestRuntimeConfig, SamplingConfig
_DEFAULTED_SECTIONS = (("sampling", SamplingConfig), ("runtime", RequestRuntimeConfig), ("output", OutputConfig))
def sampling_defaults_step(sampling_param: SamplingParam, defaults_name: str) -> ResolutionStep:
"""Build the step that sets each request field the request did not set to the model's default.
A field is covered when ``sampling_param`` has an attribute of the same name; it is set when the request did
not write it and the default differs from the schema default. The step's source name is
``fill_sampling_defaults[<defaults_name>]``.
"""
def fill_sampling_defaults(view: ResolutionView) -> dict[str, Any]:
values: dict[str, Any] = {}
for section, config_type in _DEFAULTED_SECTIONS:
for config_field in fields(config_type):
path = f"{section}.{config_field.name}"
if view.is_explicit(path) or not hasattr(sampling_param, config_field.name):
continue
default = getattr(sampling_param, config_field.name)
if default != view.get(path):
values[path] = deepcopy(default)
return values
fill_sampling_defaults.__qualname__ = f"fill_sampling_defaults[{defaults_name}]"
return fill_sampling_defaults
def resolve_request(request: GenerationRequest, *, model_path: str) -> ResolvedRequest:
"""Resolve the settings of ``request`` against the sampling defaults of ``model_path``.
The defaults are the model's preset, as ``SamplingParam.from_pretrained`` builds them; the step's source name
is ``preset <name>``, or the ``SamplingParam`` class name for a model without a preset.
"""
from fastvideo.registry import get_preset_selection
sampling_param = SamplingParam.from_pretrained(model_path)
try:
preset_name, _ = get_preset_selection(model_path)
except (ValueError, RuntimeError):
preset_name = None
defaults_name = f"preset {preset_name}" if preset_name else type(sampling_param).__name__
return resolve_generation_request(request, [sampling_defaults_step(sampling_param, defaults_name)])
__all__ = [
"resolve_request",
"sampling_defaults_step",
]