Handle capacity-padded masks for cached full prompts
Browse filesFollow-up to the initial PR: merged [GPT-QModel #3571](https://github.com/ModelCloud/GPTQModel/pull/3571) exposed another `cache_position=None` case. An attention mask can be padded to a static cache's capacity, so `mask_length > input_length` alone does not prove that `input_ids` contains only new tokens. The updated code treats only `mask_length == past_length + input_length` as evidence for a new-only block. A regression test and the exact before/after reproduction are in [GPT-QModel #3574](https://github.com/ModelCloud/GPTQModel/pull/3574).
Validation against this updated upstream source: **34 passed** in the tiny Ripple suite (`tests/test_ricursive_support.py`); the extracted upstream generation method also passed all four prompt/block cases.
- modeling_ricursive.py +7 -3
|
@@ -910,13 +910,17 @@ class RicursiveForCausalLM(RicursivePreTrainedModel, GenerationMixin):
|
|
| 910 |
if cache_position is None:
|
| 911 |
# A full prompt can include cached tokens, while verification may pass
|
| 912 |
# only a new token block. A block no longer than the cache must be new;
|
| 913 |
-
# for a longer block, a mask
|
| 914 |
-
# input.
|
|
|
|
| 915 |
input_length = input_ids.shape[1]
|
| 916 |
full_prompt = (
|
| 917 |
past_length > 0
|
| 918 |
and input_length > past_length
|
| 919 |
-
and (
|
|
|
|
|
|
|
|
|
|
| 920 |
)
|
| 921 |
new_len = input_length - past_length if full_prompt else input_length
|
| 922 |
cache_position = torch.arange(
|
|
|
|
| 910 |
if cache_position is None:
|
| 911 |
# A full prompt can include cached tokens, while verification may pass
|
| 912 |
# only a new token block. A block no longer than the cache must be new;
|
| 913 |
+
# for a longer block, a mask with exactly the cached prefix plus new
|
| 914 |
+
# tokens identifies new-only input. A longer mask may be padded to a
|
| 915 |
+
# static cache's capacity, so it cannot identify a new-only block.
|
| 916 |
input_length = input_ids.shape[1]
|
| 917 |
full_prompt = (
|
| 918 |
past_length > 0
|
| 919 |
and input_length > past_length
|
| 920 |
+
and (
|
| 921 |
+
attention_mask is None
|
| 922 |
+
or attention_mask.shape[-1] != past_length + input_length
|
| 923 |
+
)
|
| 924 |
)
|
| 925 |
new_len = input_length - past_length if full_prompt else input_length
|
| 926 |
cache_position = torch.arange(
|