PaddlePaddle/PaddleOCR · error · Exception

invalid layer type {layer_type}

Error message

invalid layer type {layer_type}

What it means

Thrown while building the Transformer stack in the LaTeX-OCR recognition head. The constructor iterates over the layer_types string and maps each character to a module: 'a' = causal self-attention, 'c' = non-causal attention, 'f' = feed-forward. Any other character falls into the else branch and raises.

Source

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

                + ("f",) * sandwich_coef
            )
        else:
            layer_types = default_block * depth

        self.layer_types = layer_types
        self.num_attn_layers = len(list(filter(equals("a"), layer_types)))
        for layer_type in self.layer_types:
            if layer_type == "a":
                layer = Attention(
                    dim, heads=heads, causal=causal, is_export=is_export, **attn_kwargs
                )
            elif layer_type == "c":
                layer = Attention(dim, heads=heads, is_export=is_export, **attn_kwargs)
            elif layer_type == "f":
                layer = FeedForward(dim, **ff_kwargs)
                layer = layer if not macaron else Scale(0.5, layer)
            else:
                raise Exception(f"invalid layer type {layer_type}")
            if isinstance(layer, Attention) and exists(branch_fn):
                layer = branch_fn(layer)
            residual_fn = Residual()
            self.layers.append(nn.LayerList([norm_fn(), layer, residual_fn]))

    def forward(
        self,
        x,
        context=None,
        mask=None,
        context_mask=None,
        mems=None,
        seq_len=0,
        return_hiddens=False,
    ):
        assert not (
            self.cross_attend ^ exists(context)
        ), "context must be passed in if cross_attend is set to True"

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Fix the layer type string so it only contains 'a', 'c', 'f' (e.g. enc_layer_types: "aacfc", dec_layer_types: "cacfc")
  2. If you intended a new layer kind, subclass the Transformer wrapper in rec_latexocr_head.py and add an elif branch for your character before the else
  3. Verify the config actually loaded the values you expect (print the head args) to rule out YAML indentation mistakes that silently change the string

Example fix

# before (config yml)
Head:
  enc_layer_types: "aatfc"   # 't' is invalid
# after
Head:
  enc_layer_types: "aacfc"
Defensive patterns

Strategy: validation

Validate before calling

VALID = set('acf')
def check_layer_types(s):
    bad = set(str(s)) - VALID
    if bad:
        raise ValueError(f"invalid layer types {sorted(bad)}; allowed: a, c, f")
    return s
# before building the model:
# check_layer_types(cfg['Head']['enc_layer_types'])
# check_layer_types(cfg['Head']['dec_layer_types'])

Type guard

def is_layer_type_str(v) -> bool:
    return isinstance(v, str) and len(v) > 0 and set(v) <= {'a', 'c', 'f'}

Try / catch

try:
    model = build_model(cfg)
except Exception as e:
    if 'invalid layer type' in str(e):
        raise ValueError(f"config layer_types invalid: {e}") from e
    raise

Prevention

When it happens

Trigger: Setting Head.enc_layer_types or Head.dec_layer_types in the LaTeXOCR config to a string containing characters other than 'a', 'c', 'f' (e.g. "acafcX", "a-ttn", uppercase "A", or a typo like "g"). The exception fires during model construction, before any training/inference step.

Common situations: Editing a copied LaTeX-OCR yml config and mistyping the layer string; porting a config from another transformer implementation that uses letters like 't' or 'm'; accidentally including whitespace, separators, or uppercase letters in the layer string.

Related errors


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