{"record":{"id":"929da5ec815cd9e0","repo":"invoke-ai/InvokeAI","slug":"per-layer-weights-must-contain-only-finite-values","errorCode":null,"errorMessage":"per_layer_weights must contain only finite values.","messagePattern":"per_layer_weights must contain only finite values\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/krea2_conditioning_rebalance.py","lineNumber":57,"sourceCode":"        default=\"1.0,1.0,1.0,1.0,1.0,1.0,1.0,2.5,5.0,1.1,4.0,1.0\",\n        description=f\"Comma-separated gains for the {_NUM_TEXT_LAYERS} tapped encoder layers (exactly \"\n        f\"{_NUM_TEXT_LAYERS} values).\",\n    )\n    multiplier: float = InputField(\n        default=4.0,\n        allow_inf_nan=False,\n        description=\"Overall multiplier applied to the conditioning after per-layer weighting.\",\n    )\n\n    def _parse_weights(self) -> list[float]:\n        try:\n            weights = [float(x.strip()) for x in self.per_layer_weights.split(\",\") if x.strip() != \"\"]\n        except ValueError as e:\n            raise ValueError(f\"per_layer_weights must be comma-separated numbers: {e}\") from e\n        if len(weights) != _NUM_TEXT_LAYERS:\n            raise ValueError(f\"per_layer_weights must have exactly {_NUM_TEXT_LAYERS} values, got {len(weights)}.\")\n        if not all(math.isfinite(weight) for weight in weights):\n            raise ValueError(\"per_layer_weights must contain only finite values.\")\n        return weights\n\n    @torch.no_grad()\n    def invoke(self, context: InvocationContext) -> Krea2ConditioningOutput:\n        weights = self._parse_weights()\n\n        cond_data = context.conditioning.load(self.conditioning.conditioning_name)\n        assert len(cond_data.conditionings) == 1\n        conditioning = cond_data.conditionings[0]\n        assert isinstance(conditioning, Krea2ConditioningInfo)\n\n        embeds = conditioning.prompt_embeds  # (B, seq, 12, hidden)\n        gains = torch.tensor(weights, dtype=embeds.dtype, device=embeds.device).view(1, 1, _NUM_TEXT_LAYERS, 1)\n        embeds = embeds * gains * self.multiplier\n\n        new_data = ConditioningFieldData(\n            conditionings=[\n                Krea2ConditioningInfo(prompt_embeds=embeds, prompt_embeds_mask=conditioning.prompt_embeds_mask)","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/krea2_conditioning_rebalance.py#L39-L75","documentation":"_parse_weights additionally requires every parsed weight to be finite (math.isfinite); NaN or ±Infinity values — which float() parses successfully but are meaningless as conditioning gains — raise this ValueError. This protects downstream tensor math from NaN/inf contamination.","triggerScenarios":"Invoking krea2_conditioning_rebalance with per_layer_weights containing 'nan', 'inf', '-inf', or 'Infinity' (all accepted by Python's float()).","commonSituations":"Programmatic generation of weights producing NaN (e.g. division by zero upstream) that gets stringified into the field; hand-typing 'inf' to try to disable a layer.","solutions":["Replace NaN/inf tokens with finite numbers; to effectively zero out a layer use 0.0 instead of inf.","Sanitize upstream computations that produce NaN before writing them into per_layer_weights.","Pre-validate with math.isfinite on each parsed value before invoking."],"exampleFix":"// before\nper_layer_weights=\"1.0,inf,0.5,...\"  // non-finite\n// after\nper_layer_weights=\"1.0,0.0,0.5,...\"","handlingStrategy":"validation","validationCode":"import math\ndef all_finite(s: str) -> bool:\n    try:\n        vals = [float(x) for x in s.split(\",\") if x.strip() != \"\"]\n    except ValueError:\n        return False\n    return all(math.isfinite(v) for v in vals)","typeGuard":null,"tryCatchPattern":"try:\n    out = invoke(node)\nexcept ValueError as e:\n    if \"only finite values\" in str(e):\n        node.per_layer_weights = \",\".join(\"0.0\" if not math.isfinite(float(x)) else x for x in node.per_layer_weights.split(\",\"))\n        out = invoke(node)","preventionTips":["Sanitize upstream numerics that can yield NaN/inf before stringifying weights.","Use 0.0 to disable a layer instead of inf.","Run math.isfinite over all computed weights before graph submission."],"tags":["validation","invokeai","krea2","nan"],"backgroundTag":"non-finite-value","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}