{"record":{"id":"f5ccd3ceb49b2a1e","repo":"Stability-AI/generative-models","slug":"decay-must-be-between-0-and-1","errorCode":null,"errorMessage":"Decay must be between 0 and 1","messagePattern":"Decay must be between 0 and 1","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sgm/modules/ema.py","lineNumber":9,"sourceCode":"import torch\nfrom torch import nn\n\n\nclass LitEma(nn.Module):\n    def __init__(self, model, decay=0.9999, use_num_upates=True):\n        super().__init__()\n        if decay < 0.0 or decay > 1.0:\n            raise ValueError(\"Decay must be between 0 and 1\")\n\n        self.m_name2s_name = {}\n        self.register_buffer(\"decay\", torch.tensor(decay, dtype=torch.float32))\n        self.register_buffer(\n            \"num_updates\",\n            torch.tensor(0, dtype=torch.int)\n            if use_num_upates\n            else torch.tensor(-1, dtype=torch.int),\n        )\n\n        for name, p in model.named_parameters():\n            if p.requires_grad:\n                # remove as '.'-character is not allowed in buffers\n                s_name = name.replace(\".\", \"\")\n                self.m_name2s_name.update({name: s_name})\n                self.register_buffer(s_name, p.clone().detach().data)\n\n        self.collected_params = []","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/Stability-AI/generative-models/blob/e8cd657656fa5d61688191730d0e03242bf4ed44/sgm/modules/ema.py#L1-L27","documentation":"LitEma stores exponential-moving-average shadow weights and requires a decay factor in [0, 1]; the constructor validates this up front and raises ValueError otherwise. Decay of 0 would make the EMA meaningless and negative or >1 values are mathematically invalid for an EMA.","triggerScenarios":"Instantiating LitEma(model, decay=...) with decay < 0.0 or decay > 1.0, e.g. passing a percentage like 99.9 instead of 0.999, or a typo such as 0.99999.9999.","commonSituations":"Config YAML files holding decay as 9999 or 0.9999e2; unit misinterpretation (percent vs fraction); loading an old config where decay was expressed differently.","solutions":["Pass decay as a fraction in [0,1], e.g. decay=0.9999","Fix the value in the model config dict/YAML that feeds LitEma","Clamp or validate the config value before constructing the model"],"exampleFix":"// before\nLitEma(model, decay=99.9)\n// after\nLitEma(model, decay=0.999)","handlingStrategy":"validation","validationCode":"decay = config.get('decay', 0.9999)\nif not isinstance(decay, (int, float)) or not (0.0 <= decay <= 1.0):\n    raise ValueError(f'decay must be in [0,1], got {decay}')\nema = LitEma(model, decay=float(decay))","typeGuard":null,"tryCatchPattern":"try:\n    ema = LitEma(model, decay=cfg.model.decay)\nexcept ValueError as e:\n    logging.error('bad EMA decay in config: %s', cfg.model.decay)\n    raise","preventionTips":["Store decay as a fraction (0.9999), never a percent, in configs","Add a config-schema check that clamps/validates decay before model build","Comment config values with their expected range"],"tags":["python","value-error","ema","hyperparameter"],"backgroundTag":"invalid-parameter-value","analyzedSha":"e8cd657656fa5d61688191730d0e03242bf4ed44","analyzedAt":"2026-08-29T11:23:43.234Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}