Diff Coverage

Diff: origin/master...HEAD, staged and unstaged changes

Source File Diff Coverage (%) Missing Lines
hyper_parallel/components/losses/chunked_cross_entropy.py 100%  
hyper_parallel/distributed/context_parallel/attention.py 27.3% 240,242-245,265-266,274-276,280,304-305,308,311,315
hyper_parallel/distributed/context_parallel/attention.py
236
237
238
239
240
241
242
243
244
245
246
247
248
249

            @staticmethod
            def apply(*args: Any, **kwargs: Any) -> Any:
                """Apply the original KL loss with saved global sequence tensors."""
                arguments = _DSA_KL_SIGNATURE.bind(*args, **kwargs).arguments
                saved = _dsa_tensor_context.get()
                if saved is not None:
                    _dsa_tensor_context.set(None)
                    length = saved.query.size(1)
                    arguments.update(
                        query=saved.query.flatten(0, 1),
                        key=saved.key.flatten(0, 1),
                        query_rope=saved.q_pe.flatten(0, 1),
                        key_rope=saved.k_pe.flatten(0, 1),
261
262
263
264
265
266
267
268
269
270


def _head_tail_peer_rank(cp_mesh: Any) -> int:
    """Return the mirror rank in the CP mesh."""
    rank_list = list(cp_mesh.rank_list)
    return rank_list[cp_mesh.size() - 1 - rank_list.index(dist.get_rank())]


def _run_head_tail_half(
        attention_fn: Callable[[Tensor, Tensor, Tensor, dict[str, Any]], Any],
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
        attention_fn: Callable[[Tensor, Tensor, Tensor, dict[str, Any]], Any],
        query: Tensor, key: Tensor, value: Tensor,
        call_kwargs: dict[str, Any]) -> Tensor:
    """Run attention for one query half and check its output contract."""
    output = attention_fn(query, key, value, call_kwargs)
    if not isinstance(output, Tensor):
        raise TypeError(
            "Head-Tail load balance requires the attention callable to "
            f"return a Tensor, got {type(output).__name__}"
        )
    return output


def head_tail_load_balance_attention(
        attention_fn: Callable[[Tensor, Tensor, Tensor, dict[str, Any]], Any],
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
            f"length, got {local_q_len}; pad the global sequence to a "
            f"multiple of 2 * cp_size ({2 * cp_mesh.size()})"
        )

    peer_rank = _head_tail_peer_rank(cp_mesh)
    query_peer = _collectives.p2p_exchange(
        query.narrow(2, local_q_len // 2, local_q_len // 2), peer_rank)
    global_key, global_value = flex_cp_allgather(key, value, 2, cp_mesh)
    keep_output = _run_head_tail_half(
        attention_fn, query.narrow(2, 0, local_q_len // 2),
        global_key, global_value, attention_kwargs)
    peer_output = _run_head_tail_half(
        attention_fn, query_peer, global_key, global_value,
        attention_kwargs if peer_attention_kwargs is None else peer_attention_kwargs,
    )
    return torch.cat(
        [keep_output, _collectives.p2p_exchange(peer_output, peer_rank)], dim=2)


def _cp_offset_causal_mask(q_len: int, kv_len: int, lo: int,