1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- """Qwen3-Omni-specific preprocessing utilities for multimodal features.
15+ """Qwen3-Omni-specific preprocessing utilities for multimodal features.
1616
1717Original implementation from HuggingFace: Qwen/Qwen3-Omni-30B-A3B-Instruct.
1818"""
@@ -177,9 +177,6 @@ def maybe_pad_video_values_to_max_grid(
177177
178178 temporal_patch_size = config .temporal_patch_size_for_vit
179179 patch_size = config .patch_size_for_vit
180- valid_t_px = actual_t * temporal_patch_size
181- valid_h_px = actual_h * patch_size
182- valid_w_px = actual_w * patch_size
183180 max_t_px = max_t * temporal_patch_size
184181 max_h_px = max_h * patch_size
185182 max_w_px = max_w * patch_size
@@ -188,12 +185,15 @@ def maybe_pad_video_values_to_max_grid(
188185 (video_values .shape [0 ], video_values .shape [1 ], max_t_px , max_h_px , max_w_px ),
189186 dtype = video_values .dtype ,
190187 )
191- padded_video_values [:, :, :valid_t_px , :valid_h_px , :valid_w_px ] = video_values [
192- :, :, :valid_t_px , :valid_h_px , :valid_w_px
193- ]
188+ patch_elements = video_values .shape [1 ] * temporal_patch_size * patch_size * patch_size
189+ valid_patches = actual_t * actual_h * actual_w
190+ padded_video_values .reshape ((video_values .shape [0 ], - 1 , patch_elements ))[:, :valid_patches ] = video_values .reshape (
191+ (video_values .shape [0 ], valid_patches , patch_elements )
192+ )
194193
195194 video_mask = np .zeros ((video_values .shape [0 ], 1 , max_t_px , max_h_px , max_w_px ), dtype = np .int32 )
196- video_mask [:, :, :valid_t_px , :valid_h_px , :valid_w_px ] = 1
195+ mask_patch_elements = temporal_patch_size * patch_size * patch_size
196+ video_mask .reshape ((video_values .shape [0 ], - 1 , mask_patch_elements ))[:, :valid_patches ] = 1
197197
198198 return padded_video_values , video_grid_thw , video_mask
199199
@@ -1136,7 +1136,9 @@ def get_rope_index(
11361136 # Process modality-specific content
11371137 # Audio Only
11381138 if min_ed == ed_audio_start :
1139- audio_len = _get_feat_extract_output_lengths (audio_lengths [audio_idx ]).item () # pyrefly: ignore[unsupported-operation]
1139+ audio_len = _get_feat_extract_output_lengths (
1140+ audio_lengths [audio_idx ]
1141+ ).item () # pyrefly: ignore[unsupported-operation]
11401142 audio_pos = np .arange (audio_len ).reshape (1 , - 1 ).repeat (3 , axis = 0 ) + st_idx
11411143 llm_pos_ids_list .append (audio_pos )
11421144
@@ -1151,10 +1153,14 @@ def get_rope_index(
11511153 grid_ws = image_grid_thw [:, 2 ] # pyrefly: ignore[unsupported-operation]
11521154 t_index = np .arange (grid_t , dtype = np .float32 ) * 1 * position_id_per_seconds
11531155
1154- image_pos = get_llm_pos_ids_for_vision (st_idx , image_idx , spatial_merge_size , t_index , grid_hs , grid_ws ) # pyrefly: ignore[bad-argument-type]
1156+ image_pos = get_llm_pos_ids_for_vision (
1157+ st_idx , image_idx , spatial_merge_size , t_index , grid_hs , grid_ws
1158+ ) # pyrefly: ignore[bad-argument-type]
11551159 llm_pos_ids_list .append (image_pos )
11561160
1157- image_len = int (np .prod (image_grid_thw [image_idx ]).item () // (spatial_merge_size ** 2 )) # pyrefly: ignore[unsupported-operation]
1161+ image_len = int (
1162+ np .prod (image_grid_thw [image_idx ]).item () // (spatial_merge_size ** 2 )
1163+ ) # pyrefly: ignore[unsupported-operation]
11581164 st += int (text_len + bos_len + image_len + eos_len )
11591165 image_idx += 1
11601166 remain_images -= 1
@@ -1164,27 +1170,39 @@ def get_rope_index(
11641170 grid_t = video_grid_thw [video_idx , 0 ].item () # pyrefly: ignore[unsupported-operation]
11651171 grid_hs = video_grid_thw [:, 1 ] # pyrefly: ignore[unsupported-operation]
11661172 grid_ws = video_grid_thw [:, 2 ] # pyrefly: ignore[unsupported-operation]
1167- t_index = np .arange (grid_t , dtype = np .float32 ) * second_per_grids [video_idx ].item () * position_id_per_seconds # pyrefly: ignore[unsupported-operation]
1173+ t_index = (
1174+ np .arange (grid_t , dtype = np .float32 ) * second_per_grids [video_idx ].item () * position_id_per_seconds
1175+ ) # pyrefly: ignore[unsupported-operation]
11681176
1169- video_pos = get_llm_pos_ids_for_vision (st_idx , video_idx , spatial_merge_size , t_index , grid_hs , grid_ws ) # pyrefly: ignore[bad-argument-type]
1177+ video_pos = get_llm_pos_ids_for_vision (
1178+ st_idx , video_idx , spatial_merge_size , t_index , grid_hs , grid_ws
1179+ ) # pyrefly: ignore[bad-argument-type]
11701180 llm_pos_ids_list .append (video_pos )
11711181
1172- video_len = int (np .prod (video_grid_thw [video_idx ]).item () // (spatial_merge_size ** 2 )) # pyrefly: ignore[unsupported-operation]
1182+ video_len = int (
1183+ np .prod (video_grid_thw [video_idx ]).item () // (spatial_merge_size ** 2 )
1184+ ) # pyrefly: ignore[unsupported-operation]
11731185 st += int (text_len + bos_len + video_len + eos_len )
11741186 video_idx += 1
11751187 remain_videos -= 1
11761188
11771189 # Audio in Video (interleaved)
11781190 elif min_ed == ed_vision_start and ed_vision_start + 1 == ed_audio_start :
1179- audio_len = _get_feat_extract_output_lengths (audio_lengths [audio_idx ]).item () # pyrefly: ignore[unsupported-operation]
1191+ audio_len = _get_feat_extract_output_lengths (
1192+ audio_lengths [audio_idx ]
1193+ ).item () # pyrefly: ignore[unsupported-operation]
11801194 audio_llm_pos_ids = np .arange (audio_len ).reshape (1 , - 1 ).repeat (3 , axis = 0 ) + st_idx
11811195
11821196 grid_t = video_grid_thw [video_idx , 0 ].item () # pyrefly: ignore[unsupported-operation]
11831197 grid_hs = video_grid_thw [:, 1 ] # pyrefly: ignore[unsupported-operation]
11841198 grid_ws = video_grid_thw [:, 2 ] # pyrefly: ignore[unsupported-operation]
1185- t_index = np .arange (grid_t , dtype = np .float32 ) * second_per_grids [video_idx ].item () * position_id_per_seconds # pyrefly: ignore[unsupported-operation]
1199+ t_index = (
1200+ np .arange (grid_t , dtype = np .float32 ) * second_per_grids [video_idx ].item () * position_id_per_seconds
1201+ ) # pyrefly: ignore[unsupported-operation]
11861202
1187- video_llm_pos_ids = get_llm_pos_ids_for_vision (st_idx , video_idx , spatial_merge_size , t_index , grid_hs , grid_ws ) # pyrefly: ignore[bad-argument-type]
1203+ video_llm_pos_ids = get_llm_pos_ids_for_vision (
1204+ st_idx , video_idx , spatial_merge_size , t_index , grid_hs , grid_ws
1205+ ) # pyrefly: ignore[bad-argument-type]
11881206
11891207 # Interleave audio and video based on temporal ordering
11901208 video_data_index = 0
@@ -1203,7 +1221,9 @@ def get_rope_index(
12031221 if audio_data_index < audio_llm_pos_ids .shape [1 ]:
12041222 llm_pos_ids_list .append (audio_llm_pos_ids [:, audio_data_index :])
12051223
1206- video_len = int (np .prod (video_grid_thw [video_idx ]).item () // (spatial_merge_size ** 2 )) # pyrefly: ignore[unsupported-operation]
1224+ video_len = int (
1225+ np .prod (video_grid_thw [video_idx ]).item () // (spatial_merge_size ** 2 )
1226+ ) # pyrefly: ignore[unsupported-operation]
12071227 st += int (text_len + bos_len + audio_len + video_len + eos_len )
12081228
12091229 audio_idx += 1
0 commit comments