.eval_results/mdpbench.yaml DELETED
@@ -1,207 +0,0 @@
1
- - dataset:
2
- id: Delores-Lin/MDPBench
3
- task_id: overall
4
- value: 78.3
5
- date: '2026-07-03'
6
- source:
7
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
8
- name: MDPBench leaderboard
9
- user: Delores-Lin
10
- - dataset:
11
- id: Delores-Lin/MDPBench
12
- task_id: digital
13
- value: 87.4
14
- date: '2026-07-03'
15
- source:
16
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
17
- name: MDPBench leaderboard
18
- user: Delores-Lin
19
- - dataset:
20
- id: Delores-Lin/MDPBench
21
- task_id: photographed
22
- value: 75.2
23
- date: '2026-07-03'
24
- source:
25
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
26
- name: MDPBench leaderboard
27
- user: Delores-Lin
28
- - dataset:
29
- id: Delores-Lin/MDPBench
30
- task_id: latin
31
- value: 81.2
32
- date: '2026-07-03'
33
- source:
34
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
35
- name: MDPBench leaderboard
36
- user: Delores-Lin
37
- - dataset:
38
- id: Delores-Lin/MDPBench
39
- task_id: de
40
- value: 84.8
41
- date: '2026-07-03'
42
- source:
43
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
44
- name: MDPBench leaderboard
45
- user: Delores-Lin
46
- - dataset:
47
- id: Delores-Lin/MDPBench
48
- task_id: en
49
- value: 83.0
50
- date: '2026-07-03'
51
- source:
52
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
53
- name: MDPBench leaderboard
54
- user: Delores-Lin
55
- - dataset:
56
- id: Delores-Lin/MDPBench
57
- task_id: es
58
- value: 75.7
59
- date: '2026-07-03'
60
- source:
61
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
62
- name: MDPBench leaderboard
63
- user: Delores-Lin
64
- - dataset:
65
- id: Delores-Lin/MDPBench
66
- task_id: fr
67
- value: 78.1
68
- date: '2026-07-03'
69
- source:
70
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
71
- name: MDPBench leaderboard
72
- user: Delores-Lin
73
- - dataset:
74
- id: Delores-Lin/MDPBench
75
- task_id: id
76
- value: 83.9
77
- date: '2026-07-03'
78
- source:
79
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
80
- name: MDPBench leaderboard
81
- user: Delores-Lin
82
- - dataset:
83
- id: Delores-Lin/MDPBench
84
- task_id: it
85
- value: 85.2
86
- date: '2026-07-03'
87
- source:
88
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
89
- name: MDPBench leaderboard
90
- user: Delores-Lin
91
- - dataset:
92
- id: Delores-Lin/MDPBench
93
- task_id: nl
94
- value: 80.6
95
- date: '2026-07-03'
96
- source:
97
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
98
- name: MDPBench leaderboard
99
- user: Delores-Lin
100
- - dataset:
101
- id: Delores-Lin/MDPBench
102
- task_id: pt
103
- value: 80.2
104
- date: '2026-07-03'
105
- source:
106
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
107
- name: MDPBench leaderboard
108
- user: Delores-Lin
109
- - dataset:
110
- id: Delores-Lin/MDPBench
111
- task_id: vi
112
- value: 78.9
113
- date: '2026-07-03'
114
- source:
115
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
116
- name: MDPBench leaderboard
117
- user: Delores-Lin
118
- - dataset:
119
- id: Delores-Lin/MDPBench
120
- task_id: non_latin
121
- value: 74.9
122
- date: '2026-07-03'
123
- source:
124
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
125
- name: MDPBench leaderboard
126
- user: Delores-Lin
127
- - dataset:
128
- id: Delores-Lin/MDPBench
129
- task_id: ar
130
- value: 71.3
131
- date: '2026-07-03'
132
- source:
133
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
134
- name: MDPBench leaderboard
135
- user: Delores-Lin
136
- - dataset:
137
- id: Delores-Lin/MDPBench
138
- task_id: hi
139
- value: 67.7
140
- date: '2026-07-03'
141
- source:
142
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
143
- name: MDPBench leaderboard
144
- user: Delores-Lin
145
- - dataset:
146
- id: Delores-Lin/MDPBench
147
- task_id: jp
148
- value: 69.5
149
- date: '2026-07-03'
150
- source:
151
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
152
- name: MDPBench leaderboard
153
- user: Delores-Lin
154
- - dataset:
155
- id: Delores-Lin/MDPBench
156
- task_id: ko
157
- value: 86.0
158
- date: '2026-07-03'
159
- source:
160
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
161
- name: MDPBench leaderboard
162
- user: Delores-Lin
163
- - dataset:
164
- id: Delores-Lin/MDPBench
165
- task_id: ru
166
- value: 76.0
167
- date: '2026-07-03'
168
- source:
169
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
170
- name: MDPBench leaderboard
171
- user: Delores-Lin
172
- - dataset:
173
- id: Delores-Lin/MDPBench
174
- task_id: th
175
- value: 68.4
176
- date: '2026-07-03'
177
- source:
178
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
179
- name: MDPBench leaderboard
180
- user: Delores-Lin
181
- - dataset:
182
- id: Delores-Lin/MDPBench
183
- task_id: zh
184
- value: 84.8
185
- date: '2026-07-03'
186
- source:
187
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
188
- name: MDPBench leaderboard
189
- user: Delores-Lin
190
- - dataset:
191
- id: Delores-Lin/MDPBench
192
- task_id: zh_t
193
- value: 75.7
194
- date: '2026-07-03'
195
- source:
196
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
197
- name: MDPBench leaderboard
198
- user: Delores-Lin
199
- - dataset:
200
- id: Delores-Lin/MDPBench
201
- task_id: private
202
- value: 80.7
203
- date: '2026-07-03'
204
- source:
205
- url: https://huggingface.co/datasets/Delores-Lin/MDPBench
206
- name: MDPBench leaderboard
207
- user: Delores-Lin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.eval_results/real5_omnidocbench.yaml DELETED
@@ -1,48 +0,0 @@
1
- - dataset:
2
- id: PaddlePaddle/Real5-OmniDocBench
3
- task_id: overall
4
- value: 92.05
5
- source:
6
- url: https://huggingface.co/datasets/PaddlePaddle/Real5-OmniDocBench
7
- name: Real5-OmniDocBench Leaderboard
8
- user: PaddlePaddle
9
- - dataset:
10
- id: PaddlePaddle/Real5-OmniDocBench
11
- task_id: scanning
12
- value: 93.43
13
- source:
14
- url: https://huggingface.co/datasets/PaddlePaddle/Real5-OmniDocBench
15
- name: Real5-OmniDocBench Leaderboard
16
- user: PaddlePaddle
17
- - dataset:
18
- id: PaddlePaddle/Real5-OmniDocBench
19
- task_id: warping
20
- value: 91.25
21
- source:
22
- url: https://huggingface.co/datasets/PaddlePaddle/Real5-OmniDocBench
23
- name: Real5-OmniDocBench Leaderboard
24
- user: PaddlePaddle
25
- - dataset:
26
- id: PaddlePaddle/Real5-OmniDocBench
27
- task_id: screen_photography
28
- value: 91.76
29
- source:
30
- url: https://huggingface.co/datasets/PaddlePaddle/Real5-OmniDocBench
31
- name: Real5-OmniDocBench Leaderboard
32
- user: PaddlePaddle
33
- - dataset:
34
- id: PaddlePaddle/Real5-OmniDocBench
35
- task_id: illumination
36
- value: 92.16
37
- source:
38
- url: https://huggingface.co/datasets/PaddlePaddle/Real5-OmniDocBench
39
- name: Real5-OmniDocBench Leaderboard
40
- user: PaddlePaddle
41
- - dataset:
42
- id: PaddlePaddle/Real5-OmniDocBench
43
- task_id: skew
44
- value: 91.66
45
- source:
46
- url: https://huggingface.co/datasets/PaddlePaddle/Real5-OmniDocBench
47
- name: Real5-OmniDocBench Leaderboard
48
- user: PaddlePaddle
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -14,12 +14,12 @@ tags:
14
  - chart
15
  - seal
16
  - spotting
 
17
  language:
18
  - en
19
  - zh
20
  - multilingual
21
  library_name: PaddleOCR
22
- new_version: PaddlePaddle/PaddleOCR-VL-1.6
23
  ---
24
  <div align="center">
25
 
@@ -76,7 +76,6 @@ PaddleOCR-VL-1.5: Towards a Multi-Task 0.9B VLM for Robust In-the-Wild Document
76
 
77
  ## News
78
 
79
- * ```2026.03.06``` 🚀 Support llama.cpp inference for the VLM component in PaddleOCR-VL-1.5. Click [here](https://huggingface.co/PaddlePaddle/PaddleOCR-VL-1.5-GGUF) for details.
80
  * ```2026.01.29``` 🚀 We release [PaddleOCR-VL-1.5](https://huggingface.co/PaddlePaddle/PaddleOCR-VL-1.5), —a Multi-Task 0.9B VLM for Robust In-the-Wild Document Parsing.
81
 
82
  ## Usage
@@ -88,22 +87,25 @@ Install [PaddlePaddle](https://www.paddlepaddle.org.cn/install/quick) and [Paddl
88
  ```bash
89
  # The following command installs the PaddlePaddle version for CUDA 12.6. For other CUDA versions and the CPU version, please refer to https://www.paddlepaddle.org.cn/en/install/quick?docurl=/documentation/docs/en/develop/install/pip/linux-pip_en.html
90
  python -m pip install paddlepaddle-gpu==3.2.1 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
91
- python -m pip install -U "paddleocr[doc-parser]>=3.4.0"
92
  ```
93
 
 
 
 
94
  ### Basic Usage
95
 
96
  CLI usage:
97
 
98
  ```bash
99
- paddleocr doc_parser -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png --pipeline_version v1.5
100
  ```
101
 
102
  Python API usage:
103
 
104
  ```python
105
  from paddleocr import PaddleOCRVL
106
- pipeline = PaddleOCRVL(pipeline_version="v1.5")
107
  output = pipeline.predict("https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png")
108
  for res in output:
109
  res.print()
@@ -134,13 +136,12 @@ for res in output:
134
  ```bash
135
  paddleocr doc_parser \
136
  -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png \
137
- --pipeline_version v1.5 \
138
  --vl_rec_backend vllm-server \
139
  --vl_rec_server_url http://127.0.0.1:8080/v1
140
  ```
141
  ```python
142
  from paddleocr import PaddleOCRVL
143
- pipeline = PaddleOCRVL(pipeline_version="v1.5", vl_rec_backend="vllm-server", vl_rec_server_url="http://127.0.0.1:8080/v1")
144
  output = pipeline.predict("https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png")
145
  for res in output:
146
  res.print()
 
14
  - chart
15
  - seal
16
  - spotting
17
+ base_model: baidu/ERNIE-4.5-0.3B-Paddle
18
  language:
19
  - en
20
  - zh
21
  - multilingual
22
  library_name: PaddleOCR
 
23
  ---
24
  <div align="center">
25
 
 
76
 
77
  ## News
78
 
 
79
  * ```2026.01.29``` 🚀 We release [PaddleOCR-VL-1.5](https://huggingface.co/PaddlePaddle/PaddleOCR-VL-1.5), —a Multi-Task 0.9B VLM for Robust In-the-Wild Document Parsing.
80
 
81
  ## Usage
 
87
  ```bash
88
  # The following command installs the PaddlePaddle version for CUDA 12.6. For other CUDA versions and the CPU version, please refer to https://www.paddlepaddle.org.cn/en/install/quick?docurl=/documentation/docs/en/develop/install/pip/linux-pip_en.html
89
  python -m pip install paddlepaddle-gpu==3.2.1 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
90
+ python -m pip install -U "paddleocr[doc-parser]"
91
  ```
92
 
93
+ > **Please ensure that you install PaddlePaddle framework version 3.2.1 or above, along with the special version of safetensors.** For macOS users, please use Docker to set up the environment.
94
+
95
+
96
  ### Basic Usage
97
 
98
  CLI usage:
99
 
100
  ```bash
101
+ paddleocr doc_parser -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png
102
  ```
103
 
104
  Python API usage:
105
 
106
  ```python
107
  from paddleocr import PaddleOCRVL
108
+ pipeline = PaddleOCRVL()
109
  output = pipeline.predict("https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png")
110
  for res in output:
111
  res.print()
 
136
  ```bash
137
  paddleocr doc_parser \
138
  -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png \
 
139
  --vl_rec_backend vllm-server \
140
  --vl_rec_server_url http://127.0.0.1:8080/v1
141
  ```
142
  ```python
143
  from paddleocr import PaddleOCRVL
144
+ pipeline = PaddleOCRVL(vl_rec_backend="vllm-server", vl_rec_server_url="http://127.0.0.1:8080/v1")
145
  output = pipeline.predict("https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/paddleocr_vl_demo.png")
146
  for res in output:
147
  res.print()
image_processing_paddleocr_vl.py CHANGED
@@ -338,10 +338,6 @@ class PaddleOCRVLImageProcessor(BaseImageProcessor):
338
  """
339
  images = make_list_of_images(images)
340
 
341
- if input_data_format is None:
342
- # We assume that all images have the same channel dimension format.
343
- input_data_format = ChannelDimension.LAST if isinstance(images[0], Image.Image) else infer_channel_dimension_format(images[0])
344
-
345
  if do_convert_rgb:
346
  images = [convert_to_rgb(image) for image in images]
347
 
@@ -353,6 +349,9 @@ class PaddleOCRVLImageProcessor(BaseImageProcessor):
353
  "It looks like you are trying to rescale already rescaled images. If the input"
354
  " images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
355
  )
 
 
 
356
 
357
  height, width = get_image_size(images[0], channel_dim=input_data_format)
358
  resized_height, resized_width = height, width
 
338
  """
339
  images = make_list_of_images(images)
340
 
 
 
 
 
341
  if do_convert_rgb:
342
  images = [convert_to_rgb(image) for image in images]
343
 
 
349
  "It looks like you are trying to rescale already rescaled images. If the input"
350
  " images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again."
351
  )
352
+ if input_data_format is None:
353
+ # We assume that all images have the same channel dimension format.
354
+ input_data_format = infer_channel_dimension_format(images[0])
355
 
356
  height, width = get_image_size(images[0], channel_dim=input_data_format)
357
  resized_height, resized_width = height, width
modeling_paddleocr_vl.py CHANGED
@@ -27,8 +27,9 @@ from transformers.activations import ACT2FN, GELUActivation
27
  from transformers.cache_utils import (
28
  Cache,
29
  DynamicCache,
 
 
30
  )
31
- from transformers.masking_utils import create_causal_mask
32
  from transformers.generation import GenerationMixin
33
  from transformers.integrations import use_kernel_forward_from_hub
34
  from transformers.modeling_attn_mask_utils import AttentionMaskConverter
@@ -603,13 +604,12 @@ class Ernie4_5Model(Ernie4_5PreTrainedModel):
603
  elif position_ids.dim() == 2:
604
  position_ids = position_ids[None, ...].expand(3, position_ids.shape[0], -1)
605
 
606
- causal_mask = create_causal_mask(
607
- config=self.config,
608
- inputs_embeds=inputs_embeds,
609
- attention_mask=attention_mask,
610
- past_key_values=past_key_values,
611
- position_ids=position_ids,
612
- cache_position=cache_position,
613
  )
614
 
615
  hidden_states = inputs_embeds
@@ -632,6 +632,170 @@ class Ernie4_5Model(Ernie4_5PreTrainedModel):
632
  past_key_values=past_key_values,
633
  )
634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
 
636
  class Ernie4_5ForCausalLM(Ernie4_5PreTrainedModel, GenerationMixin):
637
  _tied_weights_keys = ["lm_head.weight"]
 
27
  from transformers.cache_utils import (
28
  Cache,
29
  DynamicCache,
30
+ SlidingWindowCache,
31
+ StaticCache,
32
  )
 
33
  from transformers.generation import GenerationMixin
34
  from transformers.integrations import use_kernel_forward_from_hub
35
  from transformers.modeling_attn_mask_utils import AttentionMaskConverter
 
604
  elif position_ids.dim() == 2:
605
  position_ids = position_ids[None, ...].expand(3, position_ids.shape[0], -1)
606
 
607
+ causal_mask = self._update_causal_mask(
608
+ attention_mask,
609
+ inputs_embeds,
610
+ cache_position,
611
+ past_key_values,
612
+ output_attentions,
 
613
  )
614
 
615
  hidden_states = inputs_embeds
 
632
  past_key_values=past_key_values,
633
  )
634
 
635
+ def _update_causal_mask(
636
+ self,
637
+ attention_mask: torch.Tensor,
638
+ input_tensor: torch.Tensor,
639
+ cache_position: torch.Tensor,
640
+ past_key_values: Cache,
641
+ output_attentions: bool = False,
642
+ ):
643
+ if self.config._attn_implementation == "flash_attention_2":
644
+ if attention_mask is not None and past_key_values is not None:
645
+ is_padding_right = (
646
+ attention_mask[:, -1].sum().item() != input_tensor.size()[0]
647
+ )
648
+ if is_padding_right:
649
+ raise ValueError
650
+ if attention_mask is not None and 0.0 in attention_mask:
651
+ return attention_mask
652
+ return None
653
+
654
+ # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in
655
+ # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail
656
+ # to infer the attention mask.
657
+ past_seen_tokens = (
658
+ past_key_values.get_seq_length() if past_key_values is not None else 0
659
+ )
660
+ using_static_cache = isinstance(past_key_values, StaticCache)
661
+ using_sliding_window_cache = isinstance(past_key_values, SlidingWindowCache)
662
+
663
+ # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward
664
+ if (
665
+ self.config._attn_implementation == "sdpa"
666
+ and not (using_static_cache or using_sliding_window_cache)
667
+ and not output_attentions
668
+ ):
669
+ if AttentionMaskConverter._ignore_causal_mask_sdpa(
670
+ attention_mask,
671
+ inputs_embeds=input_tensor,
672
+ past_key_values_length=past_seen_tokens,
673
+ sliding_window=self.config.sliding_window,
674
+ is_training=self.training,
675
+ ):
676
+ return None
677
+
678
+ dtype, device = input_tensor.dtype, input_tensor.device
679
+ min_dtype = torch.finfo(dtype).min
680
+ sequence_length = input_tensor.shape[1]
681
+ # SlidingWindowCache or StaticCache
682
+ if using_sliding_window_cache or using_static_cache:
683
+ target_length = past_key_values.get_max_cache_shape()
684
+ # DynamicCache or no cache
685
+ else:
686
+ target_length = (
687
+ attention_mask.shape[-1]
688
+ if isinstance(attention_mask, torch.Tensor)
689
+ else past_seen_tokens + sequence_length + 1
690
+ )
691
+
692
+ # In case the provided `attention` mask is 2D, we generate a causal mask here (4D).
693
+ causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position(
694
+ attention_mask,
695
+ sequence_length=sequence_length,
696
+ target_length=target_length,
697
+ dtype=dtype,
698
+ device=device,
699
+ cache_position=cache_position,
700
+ batch_size=input_tensor.shape[0],
701
+ config=self.config,
702
+ past_key_values=past_key_values,
703
+ )
704
+
705
+ if (
706
+ self.config._attn_implementation == "sdpa"
707
+ and attention_mask is not None
708
+ and attention_mask.device.type in ["cuda", "xpu"]
709
+ and not output_attentions
710
+ ):
711
+ # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when
712
+ # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path.
713
+ # Details: https://github.com/pytorch/pytorch/issues/110213
714
+ causal_mask = AttentionMaskConverter._unmask_unattended(
715
+ causal_mask, min_dtype
716
+ )
717
+
718
+ return causal_mask
719
+
720
+ @staticmethod
721
+ def _prepare_4d_causal_attention_mask_with_cache_position(
722
+ attention_mask: torch.Tensor,
723
+ sequence_length: int,
724
+ target_length: int,
725
+ dtype: torch.dtype,
726
+ device: torch.device,
727
+ cache_position: torch.Tensor,
728
+ batch_size: int,
729
+ config: PaddleOCRVLConfig,
730
+ past_key_values: Cache,
731
+ ):
732
+ """
733
+ Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
734
+ `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.
735
+
736
+ Args:
737
+ attention_mask (`torch.Tensor`):
738
+ A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`.
739
+ sequence_length (`int`):
740
+ The sequence length being processed.
741
+ target_length (`int`):
742
+ The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet.
743
+ dtype (`torch.dtype`):
744
+ The dtype to use for the 4D attention mask.
745
+ device (`torch.device`):
746
+ The device to place the 4D attention mask on.
747
+ cache_position (`torch.Tensor`):
748
+ Indices depicting the position of the input sequence tokens in the sequence.
749
+ batch_size (`torch.Tensor`):
750
+ Batch size.
751
+ config (`PaddleOCRVLConfig`):
752
+ The model's configuration class
753
+ past_key_values (`Cache`):
754
+ The cache class that is being used currently to generate
755
+ """
756
+ if attention_mask is not None and attention_mask.dim() == 4:
757
+ # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing.
758
+ causal_mask = attention_mask
759
+ else:
760
+ min_dtype = torch.finfo(dtype).min
761
+ causal_mask = torch.full(
762
+ (sequence_length, target_length),
763
+ fill_value=min_dtype,
764
+ dtype=dtype,
765
+ device=device,
766
+ )
767
+ diagonal_attend_mask = torch.arange(
768
+ target_length, device=device
769
+ ) > cache_position.reshape(-1, 1)
770
+ if config.sliding_window is not None:
771
+ # if we have sliding window, we should not attend to tokens beyond sliding window length, so we mask them out also
772
+ # the check is needed to verify is current checkpoint was trained with sliding window or not
773
+ if (
774
+ not isinstance(past_key_values, SlidingWindowCache)
775
+ or sequence_length > target_length
776
+ ):
777
+ sliding_attend_mask = torch.arange(
778
+ target_length, device=device
779
+ ) <= (cache_position.reshape(-1, 1) - config.sliding_window)
780
+ diagonal_attend_mask.bitwise_or_(sliding_attend_mask)
781
+ causal_mask *= diagonal_attend_mask
782
+ causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
783
+ if attention_mask is not None:
784
+ causal_mask = (
785
+ causal_mask.clone()
786
+ ) # copy to contiguous memory for in-place edit
787
+ if attention_mask.shape[-1] > target_length:
788
+ attention_mask = attention_mask[:, :target_length]
789
+ mask_length = attention_mask.shape[-1]
790
+ padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[
791
+ :, None, None, :
792
+ ].to(causal_mask.device)
793
+ padding_mask = padding_mask == 0
794
+ causal_mask[:, :, :, :mask_length] = causal_mask[
795
+ :, :, :, :mask_length
796
+ ].masked_fill(padding_mask, min_dtype)
797
+ return causal_mask
798
+
799
 
800
  class Ernie4_5ForCausalLM(Ernie4_5PreTrainedModel, GenerationMixin):
801
  _tied_weights_keys = ["lm_head.weight"]