sgl-project/sglang · error · ValueError

Failed to fit f(l) = al^2 + bl + c: {e}

Error message

Failed to fit f(l) = al^2 + bl + c: {e}

What it means

Wrapper around np.linalg.LinAlgError raised when the least-squares fit of the latency polynomial fails outright (singular matrix in lstsq).

Source

Thrown at python/sglang/srt/managers/scheduler_pp_mixin.py:1547

        if len(L) < 8:
            raise ValueError(
                f"Not enough data points for quadratic fitting ({len(L)} < 8). "
                "Need at least 8 samples with different sequence lengths."
            )

        # Build design matrix for f(l) = al^2 + bl + c
        X = np.column_stack([L * L, L, np.ones_like(L)])  # [l^2, l, 1]

        try:
            coeffs, residuals, rank, s = np.linalg.lstsq(X, T, rcond=None)
            if len(coeffs) >= 3:
                fitted_a = float(coeffs[0])  # quadratic coefficient
                fitted_b = float(coeffs[1])  # linear coefficient
                fitted_c = float(coeffs[2])  # constant coefficient
            else:
                raise ValueError("Failed to fit coefficients: insufficient rank")
        except np.linalg.LinAlgError as e:
            raise ValueError(f"Failed to fit f(l) = al^2 + bl + c: {e}")

        # Validate coefficients
        if fitted_a <= 0:
            raise ValueError(
                f"Fitted quadratic coefficient a={fitted_a:.2e} is not positive. "
                "Attention has O(n^2) complexity, so a must be positive. "
                "Check warmup data quality."
            )

        if fitted_b < 0:
            logger.warning(
                f"Fitted linear coefficient b={fitted_b:.2e} is negative. Setting b=0."
            )
            fitted_b = 0.0

        self.quadratic_coeff_a = fitted_a
        self.linear_coeff_b = fitted_b
        self.constant_coeff_c = fitted_c

View on GitHub (pinned to 0132848349)

Solutions

  1. Sanitize profiling lengths (distinct, positive, non-NaN)
  2. Re-run profiling with the default length schedule
  3. If persistent, capture the seq_lens/latencies arrays and inspect for zeros/NaNs
Defensive patterns

Strategy: try-catch

Validate before calling

import numpy as np
X = np.column_stack([np.array(l)**2, l, np.ones_like(l)])
assert np.linalg.matrix_rank(X) == 3, 'degenerate design matrix'

Try / catch

try:
    predictor.fit(...)
except ValueError as e:
    if 'Failed to fit' in str(e):
        log(seq_lens, latencies); reprofile()
    raise

Prevention

When it happens

Trigger: The design matrix X=[l², l, 1] built from profiled lengths is singular — degenerate sample lengths (e.g. all zeros or a single distinct value) during profile_and_init_predictor.

Common situations: Same as 4588: degenerate or duplicated profiling lengths, or NaN/zero sequence lengths from a broken profiling run.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/3b7bcb1458df79ba. Report an issue: GitHub.