Repository: vllm-project/vllm
File: vllm/v1/core/sched/utils.py (Python)

Code before:
    if min_repetition_pattern_size <= 0:
        min_repetition_pattern_size = 1

    if (
        max_repetition_pattern_size <= 0
        or repetition_min_count < 2
        or min_repetition_pattern_size > max_repetition_pattern_size
    ):
        return False

    for pattern_len in range(
        min_repetition_pattern_size, max_repetition_pattern_size + 1
    ):
        # Check if the pattern is repeated at least min_repetitions times
        if pattern_len * repetition_min_count > len(token_ids):
            return False

        has_repetition = True
        for n in range(1, pattern_len + 1):
            for m in range(1, repetition_min_count):
                if token_ids[-(pattern_len * m + n)] != token_ids[-n]:
                    has_repetition = False
                    break

            if not has_repetition:
                break

        if has_repetition:
            return True

    return False


def remove_all(lst: list, items_to_remove: set) -> list:
    """Remove all items from a list that are in the items_to_remove set.

    This method optimizes for the common case of removing a single item,
    falling back to list comprehension for multiple items.

    Args:
        lst: The list to remove items from
        items_to_remove: Set of items to remove

    Returns:
        Either the modified original list (for single item removal) or
        a new list (for multiple item removal). Callers should use the
        returned value.

    Note:
        For single item removal, this modifies the original list in-place
        and returns it. For multiple items, it creates and returns a new list.

Review comment (suggestion):
Factor out the inner loops into a separate function so you can use `return` instead of `break`

Code after:
            if token_ids[-(pattern_len * m + n)] != target_token:
                return False
    return True


def check_sequence_repetition(
    token_ids: Sequence[int],
    params: RepetitionDetectionParams,
) -> bool:
    """Check if a sequence of token IDs has a repetition pattern.
    Args:
        token_ids: List of token IDs
        params: Repetition detection parameters.
    Returns:
        True if a repetition pattern is found, False otherwise.
    """
    max_pattern_size = params.max_pattern_size
    min_pattern_size = params.min_pattern_size
    min_count = params.min_count

    if min_pattern_size <= 0:
        min_pattern_size = 1

    if max_pattern_size <= 0 or min_count < 2 or min_pattern_size > max_pattern_size:
        return False

    for pattern_len in range(
        min_pattern_size,
        max_pattern_size + 1,
    ):
        if pattern_len * min_count > len(token_ids):
            return False

        if _has_repeating_pattern(token_ids, pattern_len, min_count):
            return True

    return False


def remove_all(lst: list, items_to_remove: set) -> list:
    """Remove all items from a list that are in the items_to_remove set.

    This method optimizes for the common case of removing a single item,
    falling back to list comprehension for multiple items.

    Args:
        lst: The list to remove items from
        items_to_remove: Set of items to remove

    Returns:
        Either the modified original list (for single item removal) or