sgl-project/sglang · error · ValueError

Fitted quadratic coefficient a={fitted_a:.2e} is not positiv

Error message

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.

What it means

The fitted quadratic coefficient a<=0, but attention latency must grow quadratically (a>0). This means the warmup latency data is unphysical — noise, flat measurements, or lengths outside the quadratic regime.

Source

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

            )

        # 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

        logger.info(
            f"[ChunkSizePredictor] Fitted coefficients: a={fitted_a:.2e}, "
            f"b={fitted_b:.2e}, c={fitted_c:.2e}"

View on GitHub (pinned to 0132848349)

Solutions

  1. Re-run profiling on an idle GPU after full warmup
  2. Use larger/longer profiling sequence lengths so the quadratic term dominates
  3. Increase number of samples to average out noise
Defensive patterns

Strategy: retry

Validate before calling

assert all(t > 0 for t in latencies[1:]), 'non-positive latency sample'

Try / catch

try:
    predictor.fit(...)
except ValueError as e:
    if 'not positive' in str(e):
        reprofile_on_idle_gpu()
    raise

Prevention

When it happens

Trigger: Latency samples during profile_and_init_predictor are flat/noisy enough that lstsq fits a<=0 for the l² term (e.g. lengths too small, GPU not warmed up, noisy shared machine).

Common situations: Profiling on a busy/shared GPU, very short profiled lengths where quadratic term is negligible, throttling during warmup.

Related errors


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