def _transfer_input(self, tensor: torch.Tensor, *, is_query: bool) -> tuple[torch.Tensor, Any]:
"""Keep Q local for KV AG; otherwise exchange this ready operand along U."""
if self.ulysses_degree > 1:
if not is_query and self.kv_head_repeats > 1:
# Keep contiguous query groups mapped to their original KV head.
# Repeat only to lcm(KV heads, U), not Q heads; autograd sums replica gradients.
tensor = tensor.repeat_interleave(self.kv_head_repeats, dim=2)
if self.overlap:
return tensor, self.ulysses_context.launch_sequence_to_head(tensor)
return self.ulysses_context.sequence_to_head(tensor), None
if self.overlap and self.all_gather_degree > 1 and not is_query: