{"record":{"id":"2de9e3be60adfce1","repo":"PaddlePaddle/PaddleOCR","slug":"sliding-window-is-currently-only-implemented-for-c-2de9e3","errorCode":null,"errorMessage":"Sliding window is currently only implemented for causal masking","messagePattern":"Sliding window is currently only implemented for causal masking","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/heads/rec_unimernet_head.py","lineNumber":359,"sourceCode":"        input_shape = (attention_mask_2d.shape[0], query_length)\n        causal_4d_mask = None\n        if (input_shape[-1] > 1 or self.sliding_window is not None) and self.is_causal:\n            if key_value_length is None:\n                raise ValueError(\n                    \"This attention mask converter is causal. Make sure to pass `key_value_length` to correctly create a causal mask.\"\n                )\n\n            past_key_values_length = key_value_length - query_length\n\n            causal_4d_mask = self._make_causal_mask(\n                input_shape,\n                dtype,\n                past_key_values_length=past_key_values_length,\n                sliding_window=self.sliding_window,\n                is_export=is_export,\n            )\n        elif self.sliding_window is not None:\n            raise NotImplementedError(\n                \"Sliding window is currently only implemented for causal masking\"\n            )\n\n        expanded_attn_mask = self._expand_mask(\n            attention_mask_2d, dtype, tgt_len=input_shape[-1]\n        )\n\n        if causal_4d_mask is not None:\n            if is_export:\n                expanded_attn_mask = causal_4d_mask\n                return expanded_attn_mask\n            else:\n                expanded_attn_mask = causal_4d_mask.masked_fill_(\n                    expanded_attn_mask.cast(paddle.bool), paddle.finfo(dtype).min\n                )\n\n        expanded_4d_mask = expanded_attn_mask\n","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/heads/rec_unimernet_head.py#L341-L377","documentation":"This NotImplementedError is raised while building the 4D attention mask inside the UniMERNet recognition head (a port of HuggingFace transformer masking logic into PaddleOCR). A sliding window attention config (self.sliding_window) is only supported when the mask is built via the causal path (_make_causal_mask). If attention is not causal (no is_causal condition) but sliding_window is set, the library refuses to guess the correct windowed mask.","triggerScenarios":"Calling the UniMERnet head / its mask-preparation code with a config that sets sliding_window to a non-None value while the input is processed with a non-causal (bidirectional/padding-only) attention mask path, i.e. is_causal is falsy and a 2D attention_mask is supplied.","commonSituations":"Copying a HF config (e.g. Qwen2/ Mistral-style) that includes sliding_window into the UniMERNet head config; upgrading configs from a version that ignored sliding_window to one that validates it.","solutions":["Remove sliding_window (leave it None / unset) from the model config used by rec_unimernet_head","Keep sliding_window but ensure the causal path is taken (is_causal=True / causal input condition) so _make_causal_mask handles the window","If bidirectional sliding-window attention is genuinely required, implement a non-causal sliding-window mask and patch the elif branch in _update_causal_mask"],"exampleFix":"// before (config dict)\n{'Model': {'head': {'sliding_window': 512, ...}}}\n// after\n{'Model': {'head': {'sliding_window': null, ...}}}","handlingStrategy":"validation","validationCode":"cfg = head_config\nif cfg.get('sliding_window') is not None and not cfg.get('is_causal', False):\n    raise SystemExit('sliding_window requires causal masking; remove it or set is_causal')","typeGuard":"def supports_sliding_window(cfg: dict) -> bool:\n    return cfg.get('sliding_window') is None or bool(cfg.get('is_causal', False))","tryCatchPattern":"try:\n    out = model(...)\nexcept NotImplementedError as e:\n    if 'Sliding window' in str(e):\n        cfg['sliding_window'] = None  # retry without windowing\n        out = model(...)\n    else:\n        raise","preventionTips":["Keep sliding_window unset unless you know the head runs its causal path","Add a config lint step that rejects sliding_window for non-causal UniMERNet setups"],"tags":["paddle","attention-mask","sliding-window","config","not-implemented"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}