"""Per-request qua_sdk DeployConfig selection (WHERE/HOW the SDK stages run). GPU path uses float16 and dynamic batching with compilation disabled: ZeroGPU forks per lease so the compile cache never amortizes, and dynamo crashes on the wav2vec2 mask path (torch 2.8 + transformers 5.0 ConstantVariable assertion). CPU path = the local-subprocess / worker / dev fallback: ``config.CPU_DTYPE`` (bfloat16 dodges the SDPA QK^T cache cliff), no compile, tighter 300s batch cap. The choice is made inside the stage functions because the same code body runs in three contexts: a ZeroGPU lease, a forced-CPU subprocess/worker (per-thread flag or no CUDA), and plain local dev. """ from __future__ import annotations from config import ZEROGPU_MAX_DURATION from qua_sdk.deploy.presets import BatchingConfig, DeployConfig CPU_MAX_BATCH_SECONDS = 300 def gpu_deploy() -> DeployConfig: cfg = DeployConfig( name="space_zerogpu", device="cuda", dtype="float16", torch_compile=False, zerogpu_max_duration_s=ZEROGPU_MAX_DURATION, ) # Boundary-head encoder windows per forward. The preset's 8 leaves the A10G # mostly idle on long audio (~145 x 20s windows for a 48-min clip); 32 is # content-neutral (batch dim only) and cuts segmentation wall time. cfg.segmentation_batch_size = 32 return cfg def cpu_deploy() -> DeployConfig: from config import CPU_DTYPE return DeployConfig( name="space_cpu", device="cpu", dtype=CPU_DTYPE, torch_compile=False, batching=BatchingConfig(max_batch_seconds=CPU_MAX_BATCH_SECONDS), ) def select_deploy() -> DeployConfig: """CPU when the request forced it or no CUDA is visible; GPU otherwise.""" from src.core.zero_gpu import is_user_forced_cpu if is_user_forced_cpu(): return cpu_deploy() try: import torch if not torch.cuda.is_available(): return cpu_deploy() except Exception: return cpu_deploy() return gpu_deploy()