wittjeff commited on
Commit
ac10f9c
·
verified ·
1 Parent(s): c0111ce

Guard `cache_position` against `None` in `prepare_inputs_for_generation`

Browse files

**Guard `cache_position` against `None` in `prepare_inputs_for_generation`**

`prepare_inputs_for_generation` subscripts `cache_position` unconditionally:

```python
if cache_position[0] == 0:
model_inputs["pixel_values"] = pixel_values
```

Recent `transformers` releases pass `cache_position=None` on some generation paths, which crashes with `TypeError: 'NoneType' object is not subscriptable` at this line. The failure is content-dependent and version-dependent, which makes it confusing to hit in practice:

- **transformers 5.14**: fails on a subset of inputs — in our document-OCR benchmarking, 4 of 25 OmniDocBench pages crashed while the rest processed normally.
- **transformers 5.12**: fails immediately on the first generation call.

Full traceback (5.12, but the failing frame is identical on 5.14):

```
File ".../transformers/generation/utils.py", line 3824, in _prefill
model_inputs = self.prepare_inputs_for_generation(
File ".../modeling_dots_ocr.py", line 128, in prepare_inputs_for_generation
if cache_position[0] == 0:
TypeError: 'NoneType' object is not subscriptable
```

This PR adds the standard guard used by in-tree transformers models:

```python
if cache_position is not None and cache_position[0] == 0:
```

Semantics are preserved: when `cache_position` is `None`, `pixel_values` is simply not re-attached for that step, matching the behavior of the equivalent guard in upstream transformers VLM implementations. Verified on the previously-crashing pages — they now generate and parse normally.

Files changed (1) hide show
  1. modeling_dots_ocr.py +1 -2
modeling_dots_ocr.py CHANGED
@@ -124,8 +124,7 @@ class DotsOCRForCausalLM(Qwen2ForCausalLM):
124
  num_logits_to_keep=num_logits_to_keep,
125
  **kwargs,
126
  )
127
-
128
- if cache_position[0] == 0:
129
  model_inputs["pixel_values"] = pixel_values
130
 
131
  return model_inputs
 
124
  num_logits_to_keep=num_logits_to_keep,
125
  **kwargs,
126
  )
127
+ if cache_position is not None and cache_position[0] == 0:
 
128
  model_inputs["pixel_values"] = pixel_values
129
 
130
  return model_inputs