Diff Coverage

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

Source File Diff Coverage (%) Missing Lines
hyper_parallel/components/modules/moe_utils.py 100%  
hyper_parallel/distributed/expert_parallel/collectives.py 33.3% 104,234
hyper_parallel/distributed/expert_parallel/experts.py 93.5% 352,552
hyper_parallel/distributed/expert_parallel/recipes.py 85.7% 252,261,264
hyper_parallel/distributed/expert_parallel/routing.py 0.0% 221,224-225,235
hyper_parallel/distributed/expert_parallel/collectives.py
100
101
102
103
104
105
106
107
108
        ctx.send_counts = send_counts
        ctx.recv_counts = recv_counts
        ctx.group = group
        out = x.new_empty((sum(recv_counts),) + tuple(x.shape[1:]))
        dist.all_to_all(list(out.split(recv_counts)), list(x.split(send_counts)), group=group)
        return out

    @staticmethod
    def backward(
230
231
232
233
234
        The exchanged rows, materialized lazily on the async path.
    """
    if not _backend_supports_uneven_a2a(group):
        return _EPAllToAllPadded.apply(x, send_counts, recv_counts, group)
    return differentiable_all_to_all_single_async(x, send_counts, recv_counts, group)
hyper_parallel/distributed/expert_parallel/experts.py
348
349
350
351
352
353
354
355
356
            token_count, experts_per_token, flattened_states.device)
    else:
        flat_keep = keep.reshape(-1)
        expert_indices = topk_indices.reshape(-1)[flat_keep]
        expert_weights = topk_weights.reshape(-1)[flat_keep].to(dtype=flattened_states.dtype)
        source_indices = _routed_slot_token_ids(
            token_count, experts_per_token, flattened_states.device)[flat_keep]
    use_fused_moe_token_permute_and_unpermute = (
        use_fused_moe_token_permute_and_unpermute
548
549
550
551
552
553
554
555
556
    topk_indices, topk_weights = router_fn(module, hidden_states)
    capacity_factor = _resolve_capacity_factor(module)
    keep = None
    if capacity_factor is not None:
        keep, _ = apply_capacity_limit(
            topk_indices, capacity_factor, global_expert_count, topk_weights,
        )
    return _prepare_ep_dispatch(
        hidden_states, topk_indices, topk_weights,
hyper_parallel/distributed/expert_parallel/recipes.py
248
249
250
251
252
253
254
255
256
        The local compute callable from the nested factory.
    """
    source = "ep_compute_with_dispatch_options.compute"
    if not _is_delayed_target(compute):
        raise TypeError(f"{source} requires a nested _target_ for an EP local compute factory")
    _require_ep_group(ep_mesh, source)
    _check_target_config_keys(compute, source)
    meta = require_injection_meta(compute.callable, LOCAL_COMPUTE, source=source)
    context = {"module": module, "mesh": mesh, "tp_mesh": tp_mesh, "cp_mesh": cp_mesh, "ep_mesh": ep_mesh}
257
258
259
260
261
262
263
264
265
266
267
    configured = compute._kwargs  # pylint: disable=protected-access
    compute_fn = compute.build(**fill_context_kwargs(meta, context, configured, source=source))
    experts = getattr(module, "experts", None)
    if not hasattr(experts, "_ep_use_grouped_gemm"):
        raise ValueError(f"{source} must bind the routed experts with bind_local_expert_forward")
    if capacity_factor is not None:
        if not math.isfinite(capacity_factor) or capacity_factor <= 0:
            raise ValueError("capacity_factor must be finite and positive")
        experts.capacity_factor = capacity_factor
    return compute_fn

hyper_parallel/distributed/expert_parallel/routing.py
217
218
219
220
221
222
223
224
225
226
227
228
229
    slots = flat_expert.numel()
    capacity = int(math.ceil(float(capacity_factor) * slots / float(num_experts)))
    keep_flat = torch.zeros_like(flat_expert, dtype=torch.bool)
    if capacity > 0:
        permutation = _slot_order(flat_expert, weights, num_experts)
        counts = torch.bincount(flat_expert, minlength=num_experts)
        starts = torch.cumsum(counts, dim=0) - counts
        ranks = torch.arange(slots, device=flat_expert.device) - starts[flat_expert[permutation]]
        keep_flat[permutation] = ranks < capacity
    return keep_flat.view_as(topk_idx), (~keep_flat).sum()


def _slot_order(
231
232
233
234
235
236
237
238
    flat_weights: Optional["torch.Tensor"],
    num_experts: int,
) -> "torch.Tensor":
    """Order slots by expert and by descending weight when available."""
    if flat_weights is None:
        return _expert_major_order(flat_expert, num_experts)
    return _expert_score_order(flat_expert, flat_weights, num_experts)