invoke-ai/InvokeAI · error · ValueError

per_layer_weights must contain only finite values.

Error message

per_layer_weights must contain only finite values.

What it means

_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.

Source

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

        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(
            conditionings=[
                Krea2ConditioningInfo(prompt_embeds=embeds, prompt_embeds_mask=conditioning.prompt_embeds_mask)

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Replace NaN/inf tokens with finite numbers; to effectively zero out a layer use 0.0 instead of inf.
  2. Sanitize upstream computations that produce NaN before writing them into per_layer_weights.
  3. Pre-validate with math.isfinite on each parsed value before invoking.

Example fix

// before
per_layer_weights="1.0,inf,0.5,..."  // non-finite
// after
per_layer_weights="1.0,0.0,0.5,..."
Defensive patterns

Strategy: validation

Validate before calling

import math
def all_finite(s: str) -> bool:
    try:
        vals = [float(x) for x in s.split(",") if x.strip() != ""]
    except ValueError:
        return False
    return all(math.isfinite(v) for v in vals)

Try / catch

try:
    out = invoke(node)
except ValueError as e:
    if "only finite values" in str(e):
        node.per_layer_weights = ",".join("0.0" if not math.isfinite(float(x)) else x for x in node.per_layer_weights.split(","))
        out = invoke(node)

Prevention

When it happens

Trigger: Invoking krea2_conditioning_rebalance with per_layer_weights containing 'nan', 'inf', '-inf', or 'Infinity' (all accepted by Python's float()).

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

Related errors


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