def dynamic_k_assign(cost, pair_wise_ious):
    """
    Assign grouth truths with priors dynamically.

    Args:
        cost: the assign cost.
        pair_wise_ious: iou of grouth truth and priors.

    Returns:
        prior_idx: the index of assigned prior.
        gt_idx: the corresponding ground truth index.
    """
    matching_matrix = paddle.zeros_like(cost)
    ious_matrix = pair_wise_ious
    ious_matrix[ious_matrix < 0] = 0.0
    n_candidate_k = 4
    topk_ious, _ = paddle.topk(ious_matrix, n_candidate_k, axis=0)
    dynamic_ks = paddle.clip(x=topk_ious.sum(0).cast("int32"), min=1)
    num_gt = cost.shape[1]

    for gt_idx in range(num_gt):
        _, pos_idx = paddle.topk(
            x=cost[:, gt_idx], k=dynamic_ks[gt_idx].item(), largest=False)
        matching_matrix[pos_idx, gt_idx] = 1.0
    del topk_ious, dynamic_ks, pos_idx
    matched_gt = matching_matrix.sum(axis=1)

    if (matched_gt > 1).sum() > 0:
        matched_gt_indices = paddle.nonzero(matched_gt > 1)[:, 0]
        cost_argmin = paddle.argmin(
            cost.index_select(matched_gt_indices), axis=1)
        matching_matrix[matched_gt_indices][0] *= 0.0
        matching_matrix[matched_gt_indices, cost_argmin] = 1.0

    prior_idx = matching_matrix.sum(axis=1).nonzero()
    gt_idx = matching_matrix[prior_idx].argmax(axis=-1)
    return prior_idx.flatten(), gt_idx.flatten()

    Assign grouth truths with priors dynamically.

    Args:
        cost: the assign cost.
        pair_wise_ious: iou of grouth truth and priors.

    Returns:
        prior_idx: the index of assigned prior.
        gt_idx: the corresponding ground truth index.
    
ppdet/modeling/assigners/clrnet_assigner.py