{"record":{"id":"d1caecaba60b91db","repo":"PaddlePaddle/PaddleOCR","slug":"the-filter-logits-fn-is-not-supported","errorCode":null,"errorMessage":"The filter_logits_fn is not supported ","messagePattern":"The filter_logits_fn is not supported ","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/heads/rec_latexocr_head.py","lineNumber":915,"sourceCode":"        b, t = start_tokens.shape\n\n        self.net.eval()\n        out = start_tokens\n        mask = kwargs.pop(\"mask\", None)\n\n        if mask is None:\n            mask = paddle.full_like(out, True, dtype=paddle.bool)\n\n        for _ in range(seq_len):\n            x = out[:, -self.max_seq_len :]\n            mask = mask[:, -self.max_seq_len :]\n            logits = self.net(x, mask=mask, **kwargs)[:, -1, :]\n            if filter_logits_fn in {top_k, top_p}:\n                filtered_logits = filter_logits_fn(logits, thres=filter_thres)\n\n                probs = F.softmax(filtered_logits / temperature, axis=-1)\n            else:\n                raise NotImplementedError(\"The filter_logits_fn is not supported \")\n\n            sample = paddle.multinomial(probs, 1)\n            out = paddle.concat((out, sample), axis=-1)\n            pad_mask = paddle.full(shape=[mask.shape[0], 1], fill_value=1, dtype=\"bool\")\n            mask = paddle.concat((mask, pad_mask), axis=1)\n            if (\n                eos_token is not None\n                and (\n                    paddle.cumsum((out == eos_token).cast(paddle.int64), 1)[:, -1] >= 1\n                ).all()\n            ):\n                break\n        out = out[:, t:]\n        if num_dims == 1:\n            out = out.squeeze(0)\n        return out\n\n    @paddle.no_grad()","sourceCodeStart":897,"sourceCodeEnd":933,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/heads/rec_latexocr_head.py#L897-L933","documentation":"Raised by the autoregressive sampling loop (generate) of the LaTeX-OCR head. Only two logit filters are accepted: top_k and top_p. The check is a membership test `filter_logits_fn in {top_k, top_p}`, so anything else (None, a lambda, a different function) raises NotImplementedError.","triggerScenarios":"Calling the head's sample/generate entry point with filter_logits_fn=None, filter_logits_fn set to a custom callable, or a function imported from somewhere other than the module where top_k/top_p are defined (identity comparison fails for look-alike functions).","commonSituations":"Adding temperature-only sampling by passing None for the filter; copying a sampling snippet from another codebase that uses its own top_k; passing the string \"top_k\" instead of the function object.","solutions":["Pass the exact functions exported by this module: from the head's namespace, filter_logits_fn=top_k (or top_p) with a suitable filter_thres","If you want unfiltered sampling, locally patch the loop to call F.softmax(logits / temperature) directly instead of passing None","If passing a custom filter, extend the membership set {top_k, top_p} in the loop to include your function"],"exampleFix":"# before\nfrom ppocr.modeling.heads.rec_latexocr_head import top_k\nlogits_fn = None  # or a custom fn\n# after\nfrom ppocr.modeling.heads.rec_latexocr_head import top_k\nlogits_fn = top_k  # exact function object defined in this module","handlingStrategy":"validation","validationCode":"from ppocr.modeling.heads.rec_latexocr_head import top_k, top_p\nALLOWED = (top_k, top_p)\ndef check_filter_fn(fn):\n    if fn not in ALLOWED:\n        raise ValueError('filter_logits_fn must be top_k or top_p from rec_latexocr_head')\n    return fn","typeGuard":"def is_supported_filter(fn, top_k, top_p) -> bool:\n    return fn is top_k or fn is top_p","tryCatchPattern":"try:\n    out = model.generate(..., filter_logits_fn=fn)\nexcept NotImplementedError as e:\n    if 'filter_logits_fn' in str(e):\n        fn = top_k  # fall back to a supported filter\n        out = model.generate(..., filter_logits_fn=fn)\n    else:\n        raise","preventionTips":["Import top_k/top_p from the same module the head uses (identity, not equality, is checked)","Never pass None or a string for filter_logits_fn","Wrap generation in a helper that whitelists the filter functions once"],"tags":["sampling","generation","latex-ocr","api-misuse"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}