Emulate two-stage context-parallel compression of a packed KV sample. A block is m consecutive tokens of one sequence, compressed to the mean of its rows; a sequence's trailing tokens fewer than m are discarded. Implement two functions.
compress_blocks(kv, seq_ids, m)
kv is (n, c); seq_ids gives n integer labels, each sequence contiguous.(num_blocks, c), or (0, c) if nothing compresses.cp_two_stage(shards, seq_ids, m)
shards: cp_size arrays (s, c) in rank order, together the full sample; seq_ids covers all cp_size * s tokens; m divides s.r > 0 receives the last m rows of rank r - 1. Rank r owns every block whose last token has global position in [r*s, (r+1)*s), compresses them in order, and pads with zero rows to exactly s // m + 1 rows.cp_size * (s // m).{"compressed": (cp_size * s // m, c) array, "valid": bools (real rows first), "per_rank_counts": real blocks per rank}.The real rows must equal compress_blocks on the full sample; a token at offset off in its sequence ends a block iff (off + 1) % m == 0.