Manuscript Offprint

arXiv:2610.35989

Layerwise Repacking: Variable-Length Hybrid-Attention Training without an Extra Redistribution Pass

Jiahao Zhang, Junwei Feng, Dongliang Huang, Xiaoling Zhou, Yiqiao Liao, Chengru Song

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Kling Infra Team

KlingAI Research

Beijing, China

Dongliang Huang ∗^{*}

huangdongliang@kuaishou.com

KlingAI Research

Beijing, China

Jiahao Zhang ∗^{*}

zhangjiahao09@kuaishou.com

KlingAI Research

Beijing, China

Xiaoling Zhou ∗^{*}

zhouxiaoling03@kuaishou.com

KlingAI Research

Beijing, China

Junwei Feng ∗^{*}

fengjunwei@kuaishou.com

KlingAI Research

Beijing, China

Yiqiao Liao †^{\dagger}

liaoyiqiao@kling.ai

KlingAI Research

Beijing, China

Chengru Song †^{\dagger}

songchengru@kuaishou.com

KlingAI Research

Beijing, China

Abstract

Variable-length training creates conflicting parallelism requirements across the layers of a hybrid-attention model. The longest samples require wide sequence-parallel (SP) groups of 16 or 32 ranks for full attention, whose cost grows quadratically with sequence length. Sparse-attention layers, dominated by near-linear MoE work, can instead balance most evaluated workloads within node-local SP8 groups. Reusing the wide groups for these layers pushes their all-to-all communication across nodes, where the observed effective rate is 7.0×7.0\times lower. Yet switching to smaller groups changes which ranks own each sample; a separate redistribution at every full-sparse boundary would introduce another data transfer.

LAYERWISE REPACKING keeps each SP16/32 parent for full attention and repacks its samples into node-local sparse children at SP{1, 2, 4, 8}, where hidden states persist between full layers. The layout change rides on the two existing full-layer Ulysses sites: at each, one variable-size all-to-all performs both the head exchange and the sample repacking, so no redistribution pass is added. Because the balance the children can reach depends on which samples share a parent, an auxiliary parent solver rebalances parent membership before the children are formed.

On all 22 steps with SP16/32 parents among the first 200 training steps of a 1F3S hybrid-attention model (one full-attention layer per three sparse-attention layers), repacking alone reduces the mean end-to-end time by 5.95% and raises model FLOPs utilization (MFU) by 3.75 percentage points over the baseline with repacking disabled; with the improved solver, end-to-end time falls by 7.61% and MFU rises by 4.83 percentage points.

1 Introduction

Full attention captures long-range dependencies, but its quadratic cost makes it expensive at long sequence lengths. Hybrid-attention models therefore interleave a small number of full-attention layers with cheaper alternatives, retaining periodic global information flow while reducing training cost. Examples include Gemma 2's alternating local and global layers [8], MiniMax-01's one softmax layer after every seven linear-attention layers [22], Jamba's interleaving of attention and Mamba layers [18], and Kimi Linear's three linear-attention layers per full-attention layer [14]. In the 1F3S pattern studied here, every full-attention layer is followed by three sparse-attention layers. As these cheaper layers become more efficient, an increasing fraction of the model no longer needs the degree of parallelism required by full attention.

Variable-length training creates distinct parallelism requirements for full-attention and sparse-attention layers. Here, each full or sparse layer denotes an entire Transformer block, including attention and MoE, classified by its attention type. In the evaluated steps, the longest sample is 9.8−175×9.8-175\times the length of the shortest (median: 23×23\times). Because full-attention computation scales quadratically with sequence length and its activations must be distributed, the longest samples require sequence-parallel (SP) groups of 16 or 32 ranks (SP16/32). The sparse layers of the profiled model, by contrast, are dominated by approximately length-linear MoE and other per-token work, so their share of the same samples' work balances within one eight-GPU node (Section 2.1).

Placement systems for variable-length training choose one group per sample or packed micro-batch and use it for every layer [6, 16, 25]. A wide group sized for the longest samples' full attention is then carried through the three sparse layers that follow, moving their Ulysses all-to-all [12] from the intra-node fabric to inter-node links. In our workload, this lowers the observed effective Ulysses rate by 7.0×7.0\times relative to node-local SP8 groups, and all-to-all occupies 21.4% of a wide-parent step on wide ranks, against 4.3% on SP8 ranks (Section 2.2).

Assigning node-local groups to sparse layers requires more than changing their sequence-parallel degree. Because the full-and sparse-layer groups place each sample's tokens on different ranks, every transition between the two layer types requires an activation-layout conversion. Implemented as a separate redistribution phase, this conversion adds cross-node activation traffic on top of the full layer's existing exchange. In our workloads, such a

∗^{*}This work was conducted during the authors' internship at KlingAI.

†^{\dagger}Corresponding authors.

(a) Layer view: the sparse trunk stays in S

Figure

hidden states stay in S; sparse-layer A2A stays inside each node

existing full-layer Ulysses A2A = head transpose + sample repacking (no redistribution pass)

(b) Physical view: one SP32 full parent

(c) Planning view: per-parent decision chain

Figure

Figure

Figure 1: Overview of LAYERWISE REPACKING. (a) Hidden states use the node-local sparse layout S between full-attention layers; transitions to the wide layout F are fused with the existing Ulysses exchanges. (b) An SP32 full-attention parent spans four nodes, while its sparse-layer children remain node-local. (c) Planning assigns samples to parents, optionally rebalances parent membership, and then forms sparse-layer children; dashed arrows indicate fallback paths.

phase would offset approximately 30–60% of the communication savings from node-local sparse-layer execution, depending on the data redistributed (Section 2.3).

Accordingly, we propose LAYERWISE REPACKING to enable layer-specific parallel layouts without adding a dedicated activation-redistribution pass, as shown in Figure 1, where full-attention parents F\mathcal{F} keep SP{8, 16, 32}, while the samples of each SP16/32 parent are repacked into node-local sparse children S with SP{1, 2, 4, 8}. Hidden states persist in S, which we call the sparse trunk, and visit F\mathcal{F} only inside full attention. The key observation is that each full layer's mandatory Ulysses exchange already moves every token across the parent group: generalizing its two call sites to variable split sizes and a deterministic permutation lets the same all-to-all perform the standard sequence/head transpose and the sample repacking between S and F\mathcal{F}, so no redistribution pass or second payload transfer is added.

Child planning can redistribute work only within each parent, so its achievable load balance depends on parent membership. The baseline solver (round 1A) assigns samples to parents using weights calibrated to full-attention compute, as in schedulers for homogeneous Transformer stacks [6, 16, 25]. When full and sparse layers use different parallel groups, these weights do not capture sparse-layer costs and may place several expensive samples in the same parent; child planning cannot redistribute them across parents. We therefore introduce an auxiliary parent solver (round 1B) that rebalances membership using separate full- and sparse-layer cost components while preserving each parent's rank interval. The child planner (round 2) then partitions the revised parents (Section 5).

We evaluate LAYERWISE REPACKING on all 22 training steps with SP16/32 parents among the first 200 steps of a 1F3S hybrid-attention model. By separately enabling repacking and the auxiliary parent solver, we isolate their contributions: repacking alone reduces mean end-to-end time by 5.95%, while the complete system reduces it by 7.61% and increases model FLOPs utilization by 4.83 percentage points, relative to the baseline without repacking. These gains apply to steps that trigger wide parents rather than the full 200-step window. Repacking improves all 20 steps with a single SP16 parent, but slows the two remaining steps, where an extremely long sample limits the load balance achievable within node-local children.

Overall, our main contributions are as follows:

  • We propose LAYERWISE REPACKING, which assigns full- and sparse-attention layers different sequence-parallel layouts and fuses layout conversion into the existing Ulysses exchanges without an additional redistribution pass.

  • We design an auxiliary parent solver that accounts for both full- and sparse-layer costs when assigning samples to parent groups, improving the load balance achievable by node-local children.

  • We evaluate the framework on variable-length 1F3S training workloads, quantify the individual and combined gains

of repacking and parent rebalancing, and identify cases where node-local repacking does not improve performance.

References (15)