invoke-ai/InvokeAI · error · ValueError

per_layer_weights must have exactly {_NUM_TEXT_LAYERS} value

Error message

per_layer_weights must have exactly {_NUM_TEXT_LAYERS} values, got {len(weights)}.

What it means

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.

Source

Thrown at invokeai/app/invocations/krea2_conditioning_rebalance.py:55

    )
    per_layer_weights: str = InputField(
        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",
        description=f"Comma-separated gains for the {_NUM_TEXT_LAYERS} tapped encoder layers (exactly "
        f"{_NUM_TEXT_LAYERS} values).",
    )
    multiplier: float = InputField(
        default=4.0,
        allow_inf_nan=False,
        description="Overall multiplier applied to the conditioning after per-layer weighting.",
    )

    def _parse_weights(self) -> list[float]:
        try:
            weights = [float(x.strip()) for x in self.per_layer_weights.split(",") if x.strip() != ""]
        except ValueError as e:
            raise ValueError(f"per_layer_weights must be comma-separated numbers: {e}") from e
        if len(weights) != _NUM_TEXT_LAYERS:
            raise ValueError(f"per_layer_weights must have exactly {_NUM_TEXT_LAYERS} values, got {len(weights)}.")
        if not all(math.isfinite(weight) for weight in weights):
            raise ValueError("per_layer_weights must contain only finite values.")
        return weights

    @torch.no_grad()
    def invoke(self, context: InvocationContext) -> Krea2ConditioningOutput:
        weights = self._parse_weights()

        cond_data = context.conditioning.load(self.conditioning.conditioning_name)
        assert len(cond_data.conditionings) == 1
        conditioning = cond_data.conditionings[0]
        assert isinstance(conditioning, Krea2ConditioningInfo)

        embeds = conditioning.prompt_embeds  # (B, seq, 12, hidden)
        gains = torch.tensor(weights, dtype=embeds.dtype, device=embeds.device).view(1, 1, _NUM_TEXT_LAYERS, 1)
        embeds = embeds * gains * self.multiplier

        new_data = ConditioningFieldData(

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Supply exactly 12 comma-separated numbers, e.g. ','.join(['1.0']*12).
  2. Count the values before invoking: len([x for x in s.split(',') if x.strip()]) must equal 12.
  3. Check the _NUM_TEXT_LAYERS constant / field description in krea2_conditioning_rebalance.py to confirm the required count.

Example fix

// before
per_layer_weights="1.0,0.8,1.2"  // 3 values -> error
// after
per_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
Defensive patterns

Strategy: validation

Validate before calling

def has_twelve_values(s: str) -> bool:
    return len([x for x in s.split(",") if x.strip() != ""]) == 12

Try / catch

try:
    out = invoke(node)
except ValueError as e:
    if "exactly 12" in str(e):
        node.per_layer_weights = ",".join(["1.0"] * 12)
        out = invoke(node)

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/3799b8937ea6e53b. Report an issue: GitHub.