{"record":{"id":"3e387b458b8afccb","repo":"lllyasviel/Fooocus","slug":"normalize-should-be-true-if-scale-is-passed","errorCode":null,"errorMessage":"normalize should be True if scale is passed","messagePattern":"normalize should be True if scale is passed","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ldm_patched/pfn/architecture/face/codeformer.py","lineNumber":448,"sourceCode":"    )\n    return normalized_feat * style_std.expand(size) + style_mean.expand(size)\n\n\nclass PositionEmbeddingSine(nn.Module):\n    \"\"\"\n    This is a more standard version of the position embedding, very similar to the one\n    used by the Attention is all you need paper, generalized to work on images.\n    \"\"\"\n\n    def __init__(\n        self, num_pos_feats=64, temperature=10000, normalize=False, scale=None\n    ):\n        super().__init__()\n        self.num_pos_feats = num_pos_feats\n        self.temperature = temperature\n        self.normalize = normalize\n        if scale is not None and normalize is False:\n            raise ValueError(\"normalize should be True if scale is passed\")\n        if scale is None:\n            scale = 2 * math.pi\n        self.scale = scale\n\n    def forward(self, x, mask=None):\n        if mask is None:\n            mask = torch.zeros(\n                (x.size(0), x.size(2), x.size(3)), device=x.device, dtype=torch.bool\n            )\n        not_mask = ~mask  # pylint: disable=invalid-unary-operand-type\n        y_embed = not_mask.cumsum(1, dtype=torch.float32)\n        x_embed = not_mask.cumsum(2, dtype=torch.float32)\n        if self.normalize:\n            eps = 1e-6\n            y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale\n            x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale\n\n        dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device)","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/ldm_patched/pfn/architecture/face/codeformer.py#L430-L466","documentation":"PositionEmbeddingSine (DETR-style, used inside CodeFormer's transformer) validates its constructor arguments: passing a scale while normalize=False is contradictory - scale defines the normalization range for the cumulative position embeddings, so it is meaningless without normalization. ValueError fires immediately at construction.","triggerScenarios":"Instantiating PositionEmbeddingSine(num_pos_feats=..., scale=2*math.pi, normalize=False) - i.e. copying a config that sets scale but leaves normalize at its False default. Not a runtime/data error; purely a constructor argument inconsistency.","commonSituations":"Porting DETR/CodeFormer config dicts where scale was added experimentally; programmatic sweeps that vary scale but forget the normalize flag.","solutions":["Set normalize=True when you pass scale","Or omit scale entirely (it defaults to 2*pi) and keep normalize=False"],"exampleFix":"# before\nemb = PositionEmbeddingSine(num_pos_feats=64, scale=2 * math.pi, normalize=False)\n# -> ValueError: normalize should be True if scale is passed\n\n# after\nemb = PositionEmbeddingSine(num_pos_feats=64, scale=2 * math.pi, normalize=True)","handlingStrategy":"validation","validationCode":"def make_pos_embed(num_pos_feats=64, scale=None, normalize=False):\n    if scale is not None:\n        normalize = True  # scale requires normalized embeddings\n    from ldm_patched.pfn.architecture.face.codeformer import PositionEmbeddingSine\n    return PositionEmbeddingSine(num_pos_feats=num_pos_feats, normalize=normalize, scale=scale)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only pass scale together with normalize=True","If you do not need normalized position embeddings, omit scale entirely (defaults to 2*pi)"],"tags":["codeformer","position-embedding","detr","constructor","config"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}