{"record":{"id":"a2b09b956982e7d6","repo":"Stability-AI/generative-models","slug":"initializing-actnorm-in-reverse-direction-is-disab","errorCode":null,"errorMessage":"Initializing ActNorm in reverse direction is disabled by default. Use allow_reverse_init=True to enable.","messagePattern":"Initializing ActNorm in reverse direction is disabled by default\\. Use allow_reverse_init=True to enable\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sgm/modules/autoencoding/lpips/util.py","lineNumber":110,"sourceCode":"            self.initialized.fill_(1)\n\n        h = self.scale * (input + self.loc)\n\n        if squeeze:\n            h = h.squeeze(-1).squeeze(-1)\n\n        if self.logdet:\n            log_abs = torch.log(torch.abs(self.scale))\n            logdet = height * width * torch.sum(log_abs)\n            logdet = logdet * torch.ones(input.shape[0]).to(input)\n            return h, logdet\n\n        return h\n\n    def reverse(self, output):\n        if self.training and self.initialized.item() == 0:\n            if not self.allow_reverse_init:\n                raise RuntimeError(\n                    \"Initializing ActNorm in reverse direction is \"\n                    \"disabled by default. Use allow_reverse_init=True to enable.\"\n                )\n            else:\n                self.initialize(output)\n                self.initialized.fill_(1)\n\n        if len(output.shape) == 2:\n            output = output[:, :, None, None]\n            squeeze = True\n        else:\n            squeeze = False\n\n        h = output / self.scale - self.loc\n\n        if squeeze:\n            h = h.squeeze(-1).squeeze(-1)\n        return h","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/Stability-AI/generative-models/blob/e8cd657656fa5d61688191730d0e03242bf4ed44/sgm/modules/autoencoding/lpips/util.py#L92-L128","documentation":"ActNorm layers can data-dependently initialize their scale/bias on the first forward pass. Initialization in the reverse (generative) direction is considered unsafe by default, so ActNorm2D.reverse raises this RuntimeError during training when the layer is uninitialized and allow_reverse_init is False (the default).","triggerScenarios":"Running a model flow that applies ActNorm in reverse mode while model.training is True and the layer's data has not yet been initialized (initialized==0), with ActNorm2D created without allow_reverse_init=True — e.g. computing a loss that requires the inverse pass first during training.","commonSituations":"Using LPIPS/flow utilities in a training loop that evaluates the reverse pass before any forward pass initialized the layers; swapping inference code (which initializes in forward) into training where the first call is reverse.","solutions":["Construct ActNorm2D with allow_reverse_init=True so reverse-direction initialization is permitted.","Run one forward pass (or call initialize manually) before invoking reverse in training.","If reverse should never initialize, restructure the code so normalization is applied in the forward direction first."],"exampleFix":"// before\nactnorm = ActNorm2D(num_features)\nloss = model.reverse(...)  # RuntimeError on first training step\n// after\nactnorm = ActNorm2D(num_features, allow_reverse_init=True)","handlingStrategy":"type-guard","validationCode":"if flow_layer.training and flow_layer.initialized.item() == 0 and not flow_layer.allow_reverse_init:\n    flow_layer.initialize(output)  # or run one forward pass before reverse","typeGuard":"def can_reverse_init(layer) -> bool:\n    return (not layer.training) or layer.initialized.item() == 1 or layer.allow_reverse_init","tryCatchPattern":"try:\n    out = actnorm.reverse(x)\nexcept RuntimeError as e:\n    if \"reverse direction\" in str(e):\n        actnorm.allow_reverse_init = True\n        out = actnorm.reverse(x)","preventionTips":["Instantiate ActNorm2D with allow_reverse_init=True whenever reverse passes occur during training.","Run a forward pass before the first reverse pass so layers are initialized."],"tags":["normalization","flow-models","training"],"backgroundTag":"reverse-init-disabled","analyzedSha":"e8cd657656fa5d61688191730d0e03242bf4ed44","analyzedAt":"2026-08-29T11:23:43.234Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}