Fix static-cache generate() with host-side inputs and under TP (#49317)
* fix(generation): move tensors nested in a dict input to the device
With inputs kept on another device (#46590), `prepare_inputs_for_generation`
moves only the top-level tensors of `model_inputs` onto the model device, so
a tensor nested in a dict stayed behind (example: causal masks for some models).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(generation): return early from the static cache init shape without TP
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(generation): size the early static cache by the heads each rank holds
Under tensor parallelism `_get_static_cache_init_shape` divided the KV
head count by `tp_size` whenever `tp_size > 1`, whether or not the plan
shards attention. With no plan (Qwen3-VL), a gathered key projection
(`colwise_gather_output`) or an expert-parallel plan, every rank keeps
all heads, so the chunked-prefill cache is allocated with too few and
the first `index_copy_` fails: "Incompatible shapes for broadcasting:
(1, 8, 512, 128) and requested shape (1, 4, 512, 128)" for
Qwen3-VL-8B-Instruct at tp=2.
Only divide when the key projection resolves to `colwise`, the style
that shards heads; skip early init when no key projection is found.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(generation): address review on nested inputs and TP KV heads
- Move tensors nested at any depth in dicts, lists and tuples to the device.
- Read the KV heads per rank from the TP plan instead of scanning parameters.
- Use a tiny random checkpoint in the nested inputs test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor: move the device helper into GenerationMixin and the key projections into the TP util
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* review: move the device helper to pytorch_utils
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>