{"record":{"id":"4bd846f269ade572","repo":"sgl-project/sglang","slug":"raw-latent-shape-must-be-divisible-by-patch-size-f","errorCode":null,"errorMessage":"raw_latent_shape must be divisible by patch_size for SAP attention","messagePattern":"raw_latent_shape must be divisible by patch_size for SAP attention","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/layers/attention/backends/sparse_video_gen_2_attn.py","lineNumber":156,"sourceCode":"        zero_step_kmeans_init: bool,\n        first_layers_fp: float,\n        first_times_fp: float,\n        context_length: int = 0,\n        prompt_length: int | None = None,\n        **kwargs: dict[str, Any],\n    ) -> SparseVideoGen2AttentionMetadata:\n        raw_shape = tuple(raw_latent_shape)\n        if len(raw_shape) == 5:\n            t, h, w = raw_shape[2:5]\n        elif len(raw_shape) == 3:\n            t, h, w = raw_shape\n        else:\n            raise ValueError(\n                \"raw_latent_shape must be (T, H, W) or (B, C, T, H, W) for SAP attention\"\n            )\n        pt, ph, pw = patch_size\n        if t % pt != 0 or h % ph != 0 or w % pw != 0:\n            raise ValueError(\n                \"raw_latent_shape must be divisible by patch_size for SAP attention\"\n            )\n\n        num_frame = t // pt\n        frame_size = (h // ph) * (w // pw)\n\n        return SparseVideoGen2AttentionMetadata(\n            current_timestep=current_timestep,\n            num_q_centroids=num_q_centroids,\n            num_k_centroids=num_k_centroids,\n            top_p_kmeans=top_p_kmeans,\n            min_kc_ratio=min_kc_ratio,\n            kmeans_iter_init=kmeans_iter_init,\n            kmeans_iter_step=kmeans_iter_step,\n            zero_step_kmeans_init=zero_step_kmeans_init,\n            first_layers_fp=first_layers_fp,\n            first_times_fp=first_times_fp,\n            context_length=context_length,","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/layers/attention/backends/sparse_video_gen_2_attn.py#L138-L174","documentation":"After parsing (T, H, W), the builder checks divisibility by patch_size (pt, ph, pw). If any spatial/temporal dim isn't evenly divisible by its patch component, it raises this ValueError, since patch-grid construction (num_frame = t // pt, frame_size = (h//ph)*(w//pw)) would otherwise be wrong.","triggerScenarios":"Passing e.g. raw_latent_shape=(25, 130, 256) with patch_size=(1, 2, 2) — 130 % 2 != 0 — so h // ph loses pixels and the check fires.","commonSituations":"Mixing VAE downsample factors and DiT patch sizes that don't align (e.g. 8x VAE with patchify 2 on an odd resolution); user-requested resolutions not on the model's supported grid; a temporal compression factor that doesn't divide the frame count.","solutions":["Choose resolution/frame counts divisible by patch_size per-axis (T % pt == 0, H % ph == 0, W % pw == 0).","Compute latent dims as raw_dims / vae_downsample, then round each to a multiple of the patch size before building metadata.","Check the model card for supported resolution lists (e.g. multiples of 32/64 px) and use only those."],"exampleFix":"# before\nraw_latent_shape = (T, H, W)  # H=130, patch_size=(1,2,2) -> 130 % 2 != 0\n\n# after\nH = (H // ph) * ph  # round down to patch multiple\nraw_latent_shape = (T, H, W)","handlingStrategy":"validation","validationCode":"def check_patch_divisible(thw, patch):\n    t, h, w = thw; pt, ph, pw = patch\n    assert t % pt == 0 and h % ph == 0 and w % pw == 0, (\n        f\"{(t,h,w)} not divisible by patch {patch}\")\n\ncheck_patch_divisible((T, H, W), patch_size)\nmeta = builder.build(kwargs={\"raw_latent_shape\": (T, H, W), \"patch_size\": patch_size, ...})","typeGuard":"def is_patch_divisible(thw, patch) -> bool:\n    return all(d % p == 0 for d, p in zip(thw, patch))","tryCatchPattern":null,"preventionTips":["Snap H, W (and T) down to patch multiples at request time for arbitrary resolutions.","Only expose resolutions from the model's supported grid in your API/UI.","Combine VAE downsample factor with patch size when computing valid multiples."],"tags":["shape-validation","patch-size","divisibility","video-generation"],"backgroundTag":"dimension-not-divisible","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}