{"record":{"id":"9b4d3aa875d57e28","repo":"PaddlePaddle/PaddleOCR","slug":"the-mask-name-should-be-specified-for-len-sel-9b4d3a","errorCode":null,"errorMessage":"The `{mask_name}` should be specified for {len(self.layers)} layers, but it is for {attn_mask.size()[0]}.","messagePattern":"The `(.+?)` should be specified for (.+?) layers, but it is for (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/heads/rec_unimernet_head.py","lineNumber":1788,"sourceCode":"                    \"`use_cache=True` is incompatible with gradient checkpointing`. Setting `use_cache=False`...\"\n                )\n                use_cache = False\n\n        # decoder layers\n        all_hidden_states = () if output_hidden_states else None\n        all_self_attns = () if output_attentions else None\n        all_cross_attentions = (\n            () if (output_attentions and encoder_hidden_states is not None) else None\n        )\n        next_decoder_cache = () if use_cache else None\n\n        # check if head_mask/cross_attn_head_mask has a correct number of layers specified if desired\n        for attn_mask, mask_name in zip(\n            [head_mask, cross_attn_head_mask], [\"head_mask\", \"cross_attn_head_mask\"]\n        ):\n            if attn_mask is not None:\n                if attn_mask.size()[0] != len(self.layers):\n                    raise ValueError(\n                        f\"The `{mask_name}` should be specified for {len(self.layers)} layers, but it is for\"\n                        f\" {attn_mask.size()[0]}.\"\n                    )\n\n        for idx, decoder_layer in enumerate(self.layers):\n            if output_hidden_states:\n                all_hidden_states += (hidden_states,)\n            if self.training:\n                dropout_probability = paddle.rand([])\n                if dropout_probability < self.layerdrop:\n                    continue\n\n            past_key_value = (\n                past_key_values[idx] if past_key_values is not None else None\n            )\n\n            if self.gradient_checkpointing and self.training:\n                layer_outputs = self._gradient_checkpointing_func(","sourceCodeStart":1770,"sourceCodeEnd":1806,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/heads/rec_unimernet_head.py#L1770-L1806","documentation":"The export decoder validates that head_mask / cross_attn_head_mask, when given, have a leading dimension equal to the number of decoder layers (attn_mask.size()[0] == len(self.layers)). This prevents per-layer mask indexing from failing mid-loop or silently masking the wrong layers.","triggerScenarios":"Passing head_mask sized for a different layer count during export or inference, e.g. [num_heads] instead of [num_layers, num_heads], or reusing a mask from a smaller/larger model.","commonSituations":"Switching UniMERNet config depth while keeping stale mask tensors; exporting with dummy inputs copied from another model variant.","solutions":["Pass head_mask=None and cross_attn_head_mask=None (default and sufficient for export)","Create masks with shape [len(decoder.layers), num_heads] derived from the live model object"],"exampleFix":"# before\nhead_mask = paddle.ones([8])  # wrong: heads only\n# after\nhead_mask = None","handlingStrategy":"validation","validationCode":"if head_mask is not None:\n    assert head_mask.shape[0] == len(decoder.layers), 'head_mask layer count mismatch'","typeGuard":"def layer_mask_ok(mask, n_layers: int) -> bool:\n    return mask is None or mask.size()[0] == n_layers","tryCatchPattern":null,"preventionTips":["Derive dummy head_mask shapes from len(model.decoder.layers) in export harnesses","Prefer None masks in exported graphs to keep them static"],"tags":["paddle","head-mask","export","shape-mismatch"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}