[Model] Gemma4: add bidirectional vision attention for sliding layers with window guard (#40534)
Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com> Signed-off-by: Luciano Martins <lucianomartins@google.com> Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Co-authored-by: Luciano Martins <lucianommartins@users.noreply.github.com> Co-authored-by: Isotr0py <2037008807@qq.com> Co-authored-by: Isotr0py <mozf@mail2.sysu.edu.cn>
This commit is contained in:
co-authored by
Luciano Martins
Isotr0py
Isotr0py
parent
4c34b2f6fc
commit
512f522192
@@ -2314,13 +2314,26 @@ class GPUModelRunner(
|
||||
|
||||
if self.is_mm_prefix_lm:
|
||||
req_doc_ranges = {}
|
||||
|
||||
# Gemma4 bidi: skip ranges that exceed the sliding
|
||||
# window. When image tokens > sliding_window, bidi causes
|
||||
# early image tokens to attend to the entire image
|
||||
# (e.g. 6 → 1092 targets), degrading spatial precision.
|
||||
# Per-range filtering keeps bidi for small images/video
|
||||
# frames while skipping oversized images.
|
||||
hf_text_config = self.model_config.hf_text_config
|
||||
_bidi_sw = getattr(hf_text_config, "sliding_window", None)
|
||||
|
||||
for req_id in self.input_batch.req_ids:
|
||||
image_doc_ranges = []
|
||||
req_state = self.requests[req_id]
|
||||
for mm_feature in req_state.mm_features:
|
||||
pos_info = mm_feature.mm_position
|
||||
img_doc_range = pos_info.extract_embeds_range()
|
||||
image_doc_ranges.extend(img_doc_range)
|
||||
for r in img_doc_range:
|
||||
if _bidi_sw is not None and (r[1] - r[0] + 1) > _bidi_sw:
|
||||
continue
|
||||
image_doc_ranges.append(r)
|
||||
req_idx = self.input_batch.req_id_to_index[req_id]
|
||||
req_doc_ranges[req_idx] = image_doc_ranges
|
||||
|
||||
|
||||
Reference in New Issue
Block a user