{"record":{"id":"9817a4945b469442","repo":"PaddlePaddle/PaddleOCR","slug":"the-norm-layer-must-be-str-or-paddle-nn-layer-laye","errorCode":null,"errorMessage":"The norm_layer must be str or paddle.nn.layer.Layer class","messagePattern":"The norm_layer must be str or paddle\\.nn\\.layer\\.Layer class","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/backbones/rec_vit_parseq.py","lineNumber":159,"sourceCode":"        dim,\n        num_heads,\n        mlp_ratio=4.0,\n        qkv_bias=False,\n        qk_scale=None,\n        drop=0.0,\n        attn_drop=0.0,\n        drop_path=0.0,\n        act_layer=nn.GELU,\n        norm_layer=\"nn.LayerNorm\",\n        epsilon=1e-5,\n    ):\n        super().__init__()\n        if isinstance(norm_layer, str):\n            self.norm1 = eval(norm_layer)(dim, epsilon=epsilon)\n        elif isinstance(norm_layer, Callable):\n            self.norm1 = norm_layer(dim)\n        else:\n            raise TypeError(\"The norm_layer must be str or paddle.nn.layer.Layer class\")\n        self.attn = Attention(\n            dim,\n            num_heads=num_heads,\n            qkv_bias=qkv_bias,\n            qk_scale=qk_scale,\n            attn_drop=attn_drop,\n            proj_drop=drop,\n        )\n        # NOTE: drop path for stochastic depth, we shall see if this is better than dropout here\n        self.drop_path = DropPath(drop_path) if drop_path > 0.0 else Identity()\n        if isinstance(norm_layer, str):\n            self.norm2 = eval(norm_layer)(dim, epsilon=epsilon)\n        elif isinstance(norm_layer, Callable):\n            self.norm2 = norm_layer(dim)\n        else:\n            raise TypeError(\"The norm_layer must be str or paddle.nn.layer.Layer class\")\n        mlp_hidden_dim = int(dim * mlp_ratio)\n        self.mlp = Mlp(","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/backbones/rec_vit_parseq.py#L141-L177","documentation":"Block in rec_vit_parseq accepts norm_layer either as a string (evaluated into a Paddle class, e.g. the default 'nn.LayerNorm') or a Callable (a layer class/lambda taking dim). The first check builds self.norm1; anything that is neither str nor Callable raises TypeError('The norm_layer must be str or paddle.nn.layer.Layer class').","triggerScenarios":"Passing norm_layer as an instance instead of a class (nn.LayerNorm(dim) rather than nn.LayerNorm), as None, or as some other object when constructing PARSeq ViT blocks.","commonSituations":"Config-driven construction where norm_layer is loaded as None for 'no norm'; passing a partial/instance because a different API expected an instantiated layer; typo like norm_layer='nn.Layernorm'.","solutions":["Pass the class or its string: norm_layer=nn.LayerNorm or keep the default norm_layer='nn.LayerNorm'","Do not pass an instance (no parentheses) and do not pass None"],"exampleFix":"# before\nBlock(dim, ..., norm_layer=nn.LayerNorm(dim))  # instance -> TypeError\nBlock(dim, ..., norm_layer=None)                # None -> TypeError\n\n# after\nBlock(dim, ..., norm_layer=nn.LayerNorm)        # class\nBlock(dim, ..., norm_layer='nn.LayerNorm')      # default string form","handlingStrategy":"type-guard","validationCode":"assert isinstance(norm_layer, (str, Callable)), 'norm_layer must be a str or Callable class, not an instance'","typeGuard":"from collections.abc import Callable\n\ndef is_valid_norm_layer(n) -> bool:\n    return isinstance(n, (str, Callable)) and not isinstance(n, nn.Layer)","tryCatchPattern":null,"preventionTips":["Pass classes ('nn.LayerNorm' string or nn.LayerNorm), never instances","Never use None to disable the norm in this Block; it is not supported","Watch out: the string form is eval'd, so it must be a valid Paddle layer expression"],"tags":["api-usage","transformer","parseq","type-error"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}