{"record":{"id":"759de24c7b31f54e","repo":"sgl-project/sglang","slug":"failed-to-fit-coefficients-insufficient-rank","errorCode":null,"errorMessage":"Failed to fit coefficients: insufficient rank","messagePattern":"Failed to fit coefficients: insufficient rank","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/managers/scheduler_pp_mixin.py","lineNumber":1545,"sourceCode":"        T = np.array(latencies[1:], dtype=np.float64)\n\n        if len(L) < 8:\n            raise ValueError(\n                f\"Not enough data points for quadratic fitting ({len(L)} < 8). \"\n                \"Need at least 8 samples with different sequence lengths.\"\n            )\n\n        # Build design matrix for f(l) = al^2 + bl + c\n        X = np.column_stack([L * L, L, np.ones_like(L)])  # [l^2, l, 1]\n\n        try:\n            coeffs, residuals, rank, s = np.linalg.lstsq(X, T, rcond=None)\n            if len(coeffs) >= 3:\n                fitted_a = float(coeffs[0])  # quadratic coefficient\n                fitted_b = float(coeffs[1])  # linear coefficient\n                fitted_c = float(coeffs[2])  # constant coefficient\n            else:\n                raise ValueError(\"Failed to fit coefficients: insufficient rank\")\n        except np.linalg.LinAlgError as e:\n            raise ValueError(f\"Failed to fit f(l) = al^2 + bl + c: {e}\")\n\n        # Validate coefficients\n        if fitted_a <= 0:\n            raise ValueError(\n                f\"Fitted quadratic coefficient a={fitted_a:.2e} is not positive. \"\n                \"Attention has O(n^2) complexity, so a must be positive. \"\n                \"Check warmup data quality.\"\n            )\n\n        if fitted_b < 0:\n            logger.warning(\n                f\"Fitted linear coefficient b={fitted_b:.2e} is negative. Setting b=0.\"\n            )\n            fitted_b = 0.0\n\n        self.quadratic_coeff_a = fitted_a","sourceCodeStart":1527,"sourceCodeEnd":1563,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/managers/scheduler_pp_mixin.py#L1527-L1563","documentation":"np.linalg.lstsq returned fewer than 3 coefficients for the quadratic fit, i.e. the design matrix is rank-deficient — typically because all profiled sequence lengths are identical or nearly so.","triggerScenarios":"profile_and_init_predictor collects latency samples whose sequence lengths do not vary (duplicate lengths), so [l², l, 1] columns are linearly dependent.","commonSituations":"Profiling config that repeats the same sequence length, or a bug that passes a constant list into fit().","solutions":["Ensure profiling uses distinct, spread-out sequence lengths","Check the seq_lens list passed to fit for duplicates/constant values","Increase profiling length range (e.g. from min to max chunked prefill length)"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"assert len(set(seq_lens[1:])) >= 8 and len(set(seq_lens[1:])) >= 3, 'need distinct lengths for full-rank fit'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Deduplicate profiling lengths","Spread lengths across the intended chunk-size range"],"tags":["pipeline-parallel","profiling","linear-algebra"],"backgroundTag":"insufficient-data-for-fit","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}