Skip to content

vllm.multimodal.video_prune.evs

Functions:

compute_mrope_for_media(video_size_thw, spatial_merge_size, tokens_per_second=1.0, video_second_per_grid=1.0)

Computes the mrope for video embeddings based on the grid dimensions. Computed mrope positions match original qwen 2.5 implementation, but positions are built for media being the first element in sequence.

Parameters:

  • video_size_thw

    (LongTensor) –

    Media size (num frames, rows, cols)

  • spatial_merge_size

    (int) –

    Size reduction for rows & cols dimensions.

  • tokens_per_second

    (float, default: 1.0 ) –

    Number of tokens per second.

  • video_second_per_grid

    (float, default: 1.0 ) –

    Number of seconds per video.

Returns:

  • Tensor –

    Tensor of shape (T * H * W, 4) where last dimension

  • Tensor –

    represents mrope positions [0:3), while the last channel

  • Tensor –

    contains value of llm_grid_w repeated for all positions.

compute_retained_tokens_count(tokens_per_frame, num_frames, q)

Compute the number of retained tokens for a given video. Method ensures that we retain all the tokens from the first frame regardless of the pruning rate.

Parameters:

  • tokens_per_frame

    (int) –

    The number of tokens per frame.

  • num_frames

    (int) –

    The total number of frames.

  • q

    (float) –

    The pruning rate.

Returns:

  • int –

    The number of retained tokens.

compute_retention_mask(video_embeds, video_size_thw, spatial_merge_size, q)

Computes the retention mask for input video embeddings.

Parameters:

  • video_embeds

    (`torch.Tensor`) –

    The input video embeddings of shape (T * H * W // spatial_merge_size ^ 2, hidden_size)

  • video_size_thw

    (`torch.LongTensor` of shape `(3)`) –

    The temporal, height and width of video.

  • spatial_merge_size

    (int) –

    Size reduction for rows & cols dimensions.

  • q

    (float) –

    (float): Pruning rate factor [0,1)

Returns:

  • Tensor –

    torch.Tensor: The retention mask for the video embeddings of (T * H * W // spatial_merge_size ^ 2) shape.

recompute_mrope_positions(input_ids, multimodal_positions, mrope_positions, num_computed_tokens, vision_start_token_id, image_token_id, video_token_id)

Update part of input mrope positions. Original mrope_positions are computed incorrectly, so once we prune media tokens we should reflect this in the mrope positions for the LLM.

This method supports chunked prefill approach where multimodal_embeddings are passed to LLM in chunks, so input multimodal_embeddings may contain zero, some or even some part of all multimodal_embeddings for a given prompt.

Each multimodal_positions has 4 or 5 extra channels (first 3 channels correspond to the original 3 mrope positions; remaining channels vary by model — see below). Provided multimodal_positions do not reflect location of media position in sequence - they are computed like the media is in the 0-th position in the sequence.

Method works as follows: it recomputes mrope_positions starting from the num_computed_tokens for total_len_of_multimodal_embeddings and then shifts all text tokens that goes after total_len_of_multimodal_embeddings.

It also handles case when multimodal_embeddings is partial (e.g. one media is split into two prefill stages)

Parameters:

  • input_ids

    (LongTensor) –

    (N,) All input tokens of the prompt (entire sequence).

  • multimodal_positions

    (list[Tensor]) –

    List of mrope positions for each media. If a given element is of shape (4, N), it is assumed to only describe positions for video / image embeddings. This is the case of e.g. Qwen2.5 VL, where each multimodal input is a contiguous chunk of embeddings. The expected channels are [t, h, w, max_width]. If it is of shape (5, N), it is assumed to possibly describe positions for both video / image embeddings, as well as text embeddings. This is the case of e.g. Qwen3 VL, where each video inputs are comprised of individual frames' embeddings, interleaved with embeddings for timestamp tokens, and vision start / end tokens. The expected channels are [t, h, w, is_vision_start, is_vision].

  • mrope_positions

    (LongTensor) –

    Existing mrope positions (4, N) for entire sequence.

  • num_computed_tokens

    (int) –

    A number of computed tokens so far.

  • vision_start_token_id

    (int) –

    Token indicating start of vision media.

  • image_token_id

    (int) –

    Image token id

  • video_token_id

    (int) –

    Video token id

Returns:

  • tuple[LongTensor, int] –

    Tuple of (mrope_positions, mrope_position_delta).