Fix explicit position_ids generation on Transformers 4
#8
by ebrahim-ricursive - opened
- modeling_ricursive.py +19 -0
modeling_ricursive.py
CHANGED
|
@@ -858,6 +858,25 @@ class RicursiveForCausalLM(RicursivePreTrainedModel, GenerationMixin):
|
|
| 858 |
attentions=outputs.attentions,
|
| 859 |
)
|
| 860 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
def prepare_inputs_for_generation(
|
| 862 |
self,
|
| 863 |
input_ids: torch.LongTensor,
|
|
|
|
| 858 |
attentions=outputs.attentions,
|
| 859 |
)
|
| 860 |
|
| 861 |
+
def _update_model_kwargs_for_generation(
|
| 862 |
+
self, outputs, model_kwargs, is_encoder_decoder=False, num_new_tokens=1
|
| 863 |
+
):
|
| 864 |
+
"""Advance explicit positions when the generation backend leaves them unchanged."""
|
| 865 |
+
position_ids = model_kwargs.get("position_ids")
|
| 866 |
+
model_kwargs = super()._update_model_kwargs_for_generation(
|
| 867 |
+
outputs, model_kwargs, is_encoder_decoder=is_encoder_decoder,
|
| 868 |
+
num_new_tokens=num_new_tokens,
|
| 869 |
+
)
|
| 870 |
+
# Transformers 4 leaves positions unchanged; Transformers 5 already extends them.
|
| 871 |
+
if position_ids is not None and model_kwargs.get("position_ids") is position_ids:
|
| 872 |
+
offsets = torch.arange(
|
| 873 |
+
1, num_new_tokens + 1, device=position_ids.device, dtype=position_ids.dtype
|
| 874 |
+
)
|
| 875 |
+
model_kwargs["position_ids"] = torch.cat(
|
| 876 |
+
[position_ids, position_ids[:, -1:] + offsets], dim=-1
|
| 877 |
+
)
|
| 878 |
+
return model_kwargs
|
| 879 |
+
|
| 880 |
def prepare_inputs_for_generation(
|
| 881 |
self,
|
| 882 |
input_ids: torch.LongTensor,
|