FoundationAgents/MetaGPT · error · ValueError

Sum of exponential weights is 0, cannot normalize.

Error message

Sum of exponential weights is 0, cannot normalize.

What it means

Raised by _compute_probabilities when the sum of exponential weights equals exactly 0 after computing exp_weights = exp(alpha * (scores - max(scores))). Mathematically the maximum shifted score is 0 so exp(0)=1 and the sum is at least 1; this branch can only fire with non-finite scores (NaN or +/-inf), e.g. max being inf driving every exp to 0, or NaN propagating through np.sum.

Source

Thrown at metagpt/ext/aflow/scripts/optimizer_utils/data_utils.py:79

        return sorted_items[selected_index]

    def _compute_probabilities(self, scores, alpha=0.2, lambda_=0.3):
        scores = np.array(scores, dtype=np.float64)
        n = len(scores)

        if n == 0:
            raise ValueError("Score list is empty.")

        uniform_prob = np.full(n, 1.0 / n, dtype=np.float64)

        max_score = np.max(scores)
        shifted_scores = scores - max_score
        exp_weights = np.exp(alpha * shifted_scores)

        sum_exp_weights = np.sum(exp_weights)
        if sum_exp_weights == 0:
            raise ValueError("Sum of exponential weights is 0, cannot normalize.")

        score_prob = exp_weights / sum_exp_weights

        mixed_prob = lambda_ * uniform_prob + (1 - lambda_) * score_prob

        total_prob = np.sum(mixed_prob)
        if not np.isclose(total_prob, 1.0):
            mixed_prob = mixed_prob / total_prob

        return mixed_prob

    def load_log(self, cur_round, path=None, mode: str = "Graph"):
        if mode == "Graph":
            log_dir = os.path.join(self.root_path, "workflows", f"round_{cur_round}", "log.json")
        else:
            log_dir = path

        # 检查文件是否存在

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Inspect the experience/score files for inf or NaN entries and fix the failed round's score or remove that round
  2. Sanitize scores before calling: scores = [s for s in scores if np.isfinite(s)]
  3. Fix the evaluation step that produced a non-finite score so future rounds store finite values

Example fix

scores = [s for s in scores if np.isfinite(s)]
probs = du._compute_probabilities(scores)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
scores = [s for s in scores if np.isfinite(s)]
assert scores, "no finite scores left"

Type guard

def all_finite(scores) -> bool:
    import numpy as np
    return bool(np.all(np.isfinite(np.asarray(scores, dtype=np.float64))))

Prevention

When it happens

Trigger: Passing scores containing np.inf (max=inf => shifted=-inf => exp=0 for every element) or NaN round scores, e.g. a round whose validation score was recorded as inf/NaN.

Common situations: A scored round stored inf (e.g. 1/0 metric) or NaN (failed evaluation serialized as NaN) in the experience data; upstream parsing writing NaN for missing scores.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/f030259f0adb38ad. Report an issue: GitHub.