Once the router has chosen boundaries, the chunking layer keeps only those vectors. Each sequence in a batch keeps a different number, so the kept vectors have to be moved to the left and the batch padded. The released H-Net code does this with no Python loop over the batch, and it also has a packed mode for many documents laid end to end. Implement both.
compact_padded(x, p, b)
x float (B, L, D), p float (B, L), b bool (B, L). Every row of b has at least one True.M be the largest number of True values in any row.(x_next, P, mask):
x_next (B, M, D): for each row, the vectors at its True positions, in their original order, left-packed. Padding is 0.0.P (B, M): the matching entries of p, left-packed the same way. Padding is 0.0.mask bool (B, M): True on real entries, False on padding.B or L. Gradients must flow from x_next back to the kept entries of x.compact_packed(x, b, cu_seqlens)
x float (T, D): all documents concatenated. b bool (T,). cu_seqlens long (K+1,): cumulative document lengths with cu_seqlens[0] == 0 and cu_seqlens[K] == T. Document i occupies x[cu_seqlens[i]:cu_seqlens[i+1]].(x_next, next_cu_seqlens): x_next holds the kept vectors in order, and next_cu_seqlens long (K+1,) holds the cumulative kept counts per document.