PaddlePaddle/PaddleOCR · error · NotImplementedError

The filter_logits_fn is not supported

Error message

The filter_logits_fn is not supported 

What it means

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.

Source

Thrown at ppocr/modeling/heads/rec_latexocr_head.py:915

        b, t = start_tokens.shape

        self.net.eval()
        out = start_tokens
        mask = kwargs.pop("mask", None)

        if mask is None:
            mask = paddle.full_like(out, True, dtype=paddle.bool)

        for _ in range(seq_len):
            x = out[:, -self.max_seq_len :]
            mask = mask[:, -self.max_seq_len :]
            logits = self.net(x, mask=mask, **kwargs)[:, -1, :]
            if filter_logits_fn in {top_k, top_p}:
                filtered_logits = filter_logits_fn(logits, thres=filter_thres)

                probs = F.softmax(filtered_logits / temperature, axis=-1)
            else:
                raise NotImplementedError("The filter_logits_fn is not supported ")

            sample = paddle.multinomial(probs, 1)
            out = paddle.concat((out, sample), axis=-1)
            pad_mask = paddle.full(shape=[mask.shape[0], 1], fill_value=1, dtype="bool")
            mask = paddle.concat((mask, pad_mask), axis=1)
            if (
                eos_token is not None
                and (
                    paddle.cumsum((out == eos_token).cast(paddle.int64), 1)[:, -1] >= 1
                ).all()
            ):
                break
        out = out[:, t:]
        if num_dims == 1:
            out = out.squeeze(0)
        return out

    @paddle.no_grad()

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. 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
  2. If you want unfiltered sampling, locally patch the loop to call F.softmax(logits / temperature) directly instead of passing None
  3. If passing a custom filter, extend the membership set {top_k, top_p} in the loop to include your function

Example fix

# before
from ppocr.modeling.heads.rec_latexocr_head import top_k
logits_fn = None  # or a custom fn
# after
from ppocr.modeling.heads.rec_latexocr_head import top_k
logits_fn = top_k  # exact function object defined in this module
Defensive patterns

Strategy: validation

Validate before calling

from ppocr.modeling.heads.rec_latexocr_head import top_k, top_p
ALLOWED = (top_k, top_p)
def check_filter_fn(fn):
    if fn not in ALLOWED:
        raise ValueError('filter_logits_fn must be top_k or top_p from rec_latexocr_head')
    return fn

Type guard

def is_supported_filter(fn, top_k, top_p) -> bool:
    return fn is top_k or fn is top_p

Try / catch

try:
    out = model.generate(..., filter_logits_fn=fn)
except NotImplementedError as e:
    if 'filter_logits_fn' in str(e):
        fn = top_k  # fall back to a supported filter
        out = model.generate(..., filter_logits_fn=fn)
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/d1caecaba60b91db. Report an issue: GitHub.