{"record":{"id":"3799b8937ea6e53b","repo":"invoke-ai/InvokeAI","slug":"per-layer-weights-must-have-exactly-num-text-lay","errorCode":null,"errorMessage":"per_layer_weights must have exactly {_NUM_TEXT_LAYERS} values, got {len(weights)}.","messagePattern":"per_layer_weights must have exactly (.+?) values, got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/krea2_conditioning_rebalance.py","lineNumber":55,"sourceCode":"    )\n    per_layer_weights: str = InputField(\n        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(","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/krea2_conditioning_rebalance.py#L37-L73","documentation":"After parsing, _parse_weights enforces that exactly _NUM_TEXT_LAYERS (12) weights were supplied — one per tapped Krea2 text-encoder layer. A string that yields a different count (fewer or more comma-separated numbers) raises this ValueError with the actual count in the message.","triggerScenarios":"Invoking krea2_conditioning_rebalance with per_layer_weights like '1.0' or '1.0,0.5,...' having fewer/more than 12 comma-separated values (empty tokens between commas are skipped by the parser, so 'a,,b' counts only non-empty entries).","commonSituations":"Hand-editing the 12-value list and dropping a value; pasting a list sized for a different model; trailing commas (harmless) vs genuinely missing values.","solutions":["Supply exactly 12 comma-separated numbers, e.g. ','.join(['1.0']*12).","Count the values before invoking: len([x for x in s.split(',') if x.strip()]) must equal 12.","Check the _NUM_TEXT_LAYERS constant / field description in krea2_conditioning_rebalance.py to confirm the required count."],"exampleFix":"// before\nper_layer_weights=\"1.0,0.8,1.2\"  // 3 values -> error\n// after\nper_layer_weights=\"1.0,0.8,1.2,1.0,0.8,1.2,1.0,0.8,1.2,1.0,0.8,1.2\"  // 12 values","handlingStrategy":"validation","validationCode":"def has_twelve_values(s: str) -> bool:\n    return len([x for x in s.split(\",\") if x.strip() != \"\"]) == 12","typeGuard":null,"tryCatchPattern":"try:\n    out = invoke(node)\nexcept ValueError as e:\n    if \"exactly 12\" in str(e):\n        node.per_layer_weights = \",\".join([\"1.0\"] * 12)\n        out = invoke(node)","preventionTips":["Generate the 12-value list from code instead of typing it manually.","Count parsed tokens before invoking; empty tokens are skipped, trailing commas are safe but missing values are not.","Check the field description / _NUM_TEXT_LAYERS for the required arity."],"tags":["validation","invokeai","krea2","arity-mismatch"],"backgroundTag":"invalid-input-length","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}