With a capacity factor, each expert processes at most a fixed number of assignments per batch and the overflow is dropped: the dropped (token, expert) pair contributes nothing and the token keeps only its residual path for that expert. Implement the capacity rule with two drop policies, the drop statistics, and the layer output.
Inputs: idx and w are (T, k) arrays of selected experts and routing weights (a token never selects the same expert twice).
apply_capacity(idx, w, num_experts, capacity_factor, policy="probability")
capacity_factor is None the layer is dropless: nothing is dropped and the capacity is reported as None.policy="position": an expert keeps the assignments from the earliest tokens (lowest token index).policy="probability": an expert keeps the assignments with the largest routing weight, ties to the lower token index."capacity": (int) or None."kept": (T, k) nested list of 1 (kept) and 0 (dropped)."load": assignments per expert before dropping (list of ints)."kept_per_expert": assignments per expert after dropping."dropped_fraction": , rounded to 4 decimals."tokens_fully_dropped": number of tokens that lost all of their assignments."padding_share": if every expert's input is padded to rows, the share of those rows that are padding, rounded to 4 decimals (0.0 when dropless).capacity_forward(x, idx, w, kept, W1, W2)
W1 is (E, d, h), W2 is (E, h, d)).