{"record":{"id":"3b7bcb1458df79ba","repo":"sgl-project/sglang","slug":"failed-to-fit-f-l-al-2-bl-c-e","errorCode":null,"errorMessage":"Failed to fit f(l) = al^2 + bl + c: {e}","messagePattern":"Failed to fit f\\(l\\) = al\\^2 \\+ bl \\+ c: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/managers/scheduler_pp_mixin.py","lineNumber":1547,"sourceCode":"        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\n        self.linear_coeff_b = fitted_b\n        self.constant_coeff_c = fitted_c","sourceCodeStart":1529,"sourceCodeEnd":1565,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/managers/scheduler_pp_mixin.py#L1529-L1565","documentation":"Wrapper around np.linalg.LinAlgError raised when the least-squares fit of the latency polynomial fails outright (singular matrix in lstsq).","triggerScenarios":"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.","commonSituations":"Same as 4588: degenerate or duplicated profiling lengths, or NaN/zero sequence lengths from a broken profiling run.","solutions":["Sanitize profiling lengths (distinct, positive, non-NaN)","Re-run profiling with the default length schedule","If persistent, capture the seq_lens/latencies arrays and inspect for zeros/NaNs"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import numpy as np\nX = np.column_stack([np.array(l)**2, l, np.ones_like(l)])\nassert np.linalg.matrix_rank(X) == 3, 'degenerate design matrix'","typeGuard":null,"tryCatchPattern":"try:\n    predictor.fit(...)\nexcept ValueError as e:\n    if 'Failed to fit' in str(e):\n        log(seq_lens, latencies); reprofile()\n    raise","preventionTips":["Log raw profiling arrays when fits fail","Validate lengths are positive and finite pre-fit"],"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"}