vllm.model_executor.models.llava_onevision2 ¶
Inference-only LLaVA-OneVision-2 (OV2) model for vLLM.
Architecture notes:
- LLM backbone is plain Qwen3-8B with 1-D position_ids (no M-RoPE).
- Vision tower removes the CLS token (no class_embedding/class_pos_emb).
- Vision RoPE is 3-D (T:H:W) with a 4:6:6 head_dim split and uses
patch_positionsinstead of grid_thw to compute per-token freqs. rotate_halfis interleaved ((::2, 1::2)) rather than split-half.- Vision attention uses windowed
cu_seqlens(frame_windows_sizein T-dim); two backends implemented (SDPA + flash_attn varlen). patch_positions: [total_patches, 3]is plumbed as a first-class MM kwarg alongsidepixel_values/image_grid_thw.- Video frame-backend and codec-backend both alias to the image path inside the HF processor, so the model implements a single visual code path.
Classes:
-
LlavaOnevision2ForConditionalGeneration–vLLM-side OV2 top-level model.
-
LlavaOnevision2VideoBackend–Frame-sampling backend for LLaVA-OneVision-2.
-
LlavaOnevision2VisionAttn–Vision self-attention with windowed cu_seqlens.
-
LlavaOnevision2VisionRotaryEmbedding–3-D rotary frequency constructor with 4:6:6 (T:H:W) split.
-
LlavaOnevision2VisionTower–OV2 vision tower (no CLS token, 3-D RoPE, windowed attention).
Functions:
-
prepare_codec_video_input–Wrap a video path for vLLM's MultiModalDataParser + OV2 codec backend.
LlavaOnevision2ForConditionalGeneration ¶
Bases: Module, SupportsMultiModal, SupportsPP
vLLM-side OV2 top-level model.
Weight name rewriting (HF checkpoint → vLLM module tree): Prefix rewrites only (longest match wins). Vision tower attribute names mirror HF names verbatim, so no substring rules are needed. Substring rules would otherwise collide with the Qwen3 text-path self_attn modules and break the language-model loader. model.language_model. → language_model.model. model.visual. → visual. lm_head. → language_model.lm_head. model. (fallback) → language_model.model.
Source code in vllm/model_executor/models/llava_onevision2.py
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 | |
LlavaOnevision2VideoBackend ¶
Bases: VideoBackend
Frame-sampling backend for LLaVA-OneVision-2.
Selected automatically for OV2 via the video_processor binding (video_processor_type == "LlavaOnevision2VideoProcessor" in the model's video_preprocessor_config.json). Decoding uses the inherited OpenCV / PyAV codecs; only the sampling index policy is overridden to match qwen.
Source code in vllm/model_executor/models/llava_onevision2.py
LlavaOnevision2VisionAttn ¶
Bases: Module
Vision self-attention with windowed cu_seqlens.
The HF checkpoint ships a fused qkv linear (self_attn.qkv), so we load directly into QKVParallelLinear with no stacked_params mapping. (Compare OV1.5, whose checkpoint had separate q/k/v.)
Source code in vllm/model_executor/models/llava_onevision2.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | |
_rotate_half_interleaved(x) staticmethod ¶
OV2-specific interleaved rotate_half.
Pairs adjacent dims: (x[::2], x[1::2]) -> (-x[1::2], x[::2]). NOT compatible with the split-half rotate used in OV1.5/LLaMA.
Source code in vllm/model_executor/models/llava_onevision2.py
LlavaOnevision2VisionRotaryEmbedding ¶
Bases: Module
3-D rotary frequency constructor with 4:6:6 (T:H:W) split.
Mirrors VisionRotaryEmbedding in the HF reference (modeling_llava_onevision2.py L79-L210). The three inv_freq_* buffers are non-persistent — they are not in the checkpoint and must be reconstructed at module init time (which we do here).
Public entry points used by the vision tower
forward_from_positions(patch_positions)— per-patch (t,h,w) positions → per-token freqs [N, half].
Methods:
-
forward_from_positions–[N, 3] (t,h,w) int → [N, half] float frequencies.
Source code in vllm/model_executor/models/llava_onevision2.py
forward_from_positions(patch_positions) ¶
[N, 3] (t,h,w) int → [N, half] float frequencies.
Source code in vllm/model_executor/models/llava_onevision2.py
LlavaOnevision2VisionTower ¶
Bases: Module
OV2 vision tower (no CLS token, 3-D RoPE, windowed attention).
Module attribute names mirror HF checkpoint names verbatim so the WeightsMapper only needs prefix rewrites (no substring rules, which would otherwise collide with the Qwen3 text-path self_attn modules): visual.embeddings.patch_embedding visual.layernorm_pre visual.encoder.layers.{i}.self_attn.{qkv,proj} visual.encoder.layers.{i}.layer_norm{1,2} visual.encoder.layers.{i}.mlp.fc{1,2} visual.merger.{ln_q, mlp.{0,2}} visual.rotary_pos_emb (non-persistent inv_freq buffers)
Source code in vllm/model_executor/models/llava_onevision2.py
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 | |
_build_window_cu_seqlens(grid_thw) ¶
Build cu_seqlens that chunk each sample's T-axis into windows of frame_windows_size frames.
Returns an int32 np.ndarray of shape [num_windows+1] (the canonical prefix-sum format). Backend-specific transforms are applied afterwards via MMEncoderAttention.maybe_recompute_cu_seqlens.
Source code in vllm/model_executor/models/llava_onevision2.py
_create_field_factory(spatial_merge_size) ¶
Build the per-batch field-config callback.
OV2-specific: also exposes patch_positions as a flat-from-sizes field, sized by the total per-image patch count (THW). The merger and the 3-D RoPE both consume it.
Source code in vllm/model_executor/models/llava_onevision2.py
_expand_video_markers_in_prompt(prompt, per_video_timestamps, *, timestamp_decimals) ¶
Replace each <|vision_start|><|video_pad|><|vision_end|> with a sequence of <{t:.Nf} seconds><|vision_start|><|image_pad|><|vision_end|> blocks -- one per frame -- matching vllm_hf_chat._build_prompt.
Replacement is positional: the i-th marker consumes per_video_timestamps[i].
Source code in vllm/model_executor/models/llava_onevision2.py
_frame_video_to_pil_and_timestamps(item) ¶
Convert a (frames_ndarray, metadata) video item into (pil_frames, timestamps_seconds).
Both real video_url inputs (decoded + sampled by the registered LlavaOnevision2VideoBackend) and dummy profiling videos arrive here as a (frames, metadata) tuple because the data parser runs with video_needs_metadata=True. frames is a (T, H, W, C) uint8 array; metadata carries frames_indices and the source fps.
Timestamps follow the qwen_vl_utils policy: frame_index / original_fps. The frame count is padded up to _TEMPORAL_MERGE_SIZE (repeating the last frame) because OV2's vision tower merges frames temporally in pairs.
Source code in vllm/model_executor/models/llava_onevision2.py
_ov2_smart_nframes(total_frames, video_fps, *, fps, min_frames, max_frames) ¶
Replicate qwen_vl_utils.smart_nframes (fps branch).
Returns an even frame count in [min_frames, min(max_frames, total)].
Source code in vllm/model_executor/models/llava_onevision2.py
_validate_video_source(path, model_config) ¶
Confine a codec video path to --allowed-local-media-path.
The codec backend keeps the raw path string alive past vLLM's MultiModalDataParser and hands it to the trust-remote-code codec module, which opens it directly via cv2.VideoCapture / ffmpeg. That bypasses both MediaConnector's access controls and its redirect handling (VLLM_MEDIA_URL_ALLOW_REDIRECTS), so we restrict the codec backend to local files only: remote http(s) / data URLs are rejected here and must instead go through the frame backend (a registered VIDEO_LOADER_REGISTRY loader), which rides vLLM's connector and its domain/redirect gates.
Returns the resolved absolute path so the codec module opens exactly the file that was validated, closing the validate-vs-open (symlink-retarget) window. Mirrors the confinement in MediaConnector._load_file_url.
Source code in vllm/model_executor/models/llava_onevision2.py
prepare_codec_video_input(video_path) ¶
Wrap a video path for vLLM's MultiModalDataParser + OV2 codec backend.
Returns (dummy_ndarray, metadata) where the ndarray satisfies the parser's 4-D shape check and the metadata carries the actual path to our _call_hf_processor. Use as::
multi_modal_data = {"video": prepare_codec_video_input("foo.mp4")}
The dummy ndarray bytes encode a hash of video_path so distinct codec videos get distinct mm_hashes: the parser drops the metadata dict before hashing (only the ndarray reaches MultiModalHasher), so without this variance every video after the first would collide and skip the encoder.