Skip to content

Bring Lasso coordinate descent into a competitive performance range #209

Closed

Description

@godofecht

Parent: #202

Problem

The architecture audit identifies Lasso as one of the clearest sklearn-owned compiled-code gaps. sklearn uses mature Cython coordinate descent; Flow's current end-to-end canonical path is materially slower.

Work

Profile the Flow Lasso implementation at inner-loop level and compare against sklearn coordinate-descent semantics:

  • feature update order
  • residual maintenance strategy
  • convergence test / dual-gap calculation
  • memory layout and cache behavior
  • vectorization opportunities
  • allocation inside iterations
  • warm-start behavior

Match algorithmic semantics before claiming compiler/code-generation gains.

Add focused deterministic microbenchmarks around coordinate updates and residual maintenance in addition to the canonical Diabetes end-to-end row.

Acceptance criteria

The primary runtime bottleneck is identified with profiler/code-generation evidence.

The canonical Lasso implementation preserves or improves current numerical disparity while materially reducing the performance gap.

The native-hotspot audit records whether the remaining limitation is algorithmic, generated-code/vectorization related, or a Flow compiler/runtime blocker.

Activity

  1. godofecht commented on Aug 18, 2026

    @godofecht
    OwnerAuthor

    Triage: still open, no work has landed

    Verified against the frozen canonical evidence on main (c1e01df).

    • benchmarks/disparity_report.json, diabetes Lasso r2: runtime_ratio_sklearn_over_flow is 0.01763135584862311. Flow is about 57x slower than sklearn on this row, the worst runtime ratio in the entire canonical set. score_abs_diff is 1.1742e-05 and the row is "approximately equivalent" on both strict and final status.
    • No merged PR touches Lasso. The performance PRs on main cover GaussianNB (Roadmap #1: optimize GaussianNB.fit #210, Roadmap #3: optimize GaussianNB.predict #212, Roadmap #11: optimize GaussianNB.predict_proba #220), HashingVectorizer (Roadmap #80: optimize HashingVectorizer.transform #289), and AdditiveChi2Sampler (Optimize AdditiveChi2Sampler.transform hot loop #319). Nothing in the git log for Lasso.
    • No microbenchmarks exist. benchmarks/bench_flow_v2.flow has a single end-to-end Lasso row at lines 316-319 (lasso_fit(Xr_tr, sr.y_train, 0.1, 1000, 0.01) plus lasso_predict), with no instrumentation of coordinate updates or residual maintenance. benchmarks/bench_scaled.flow has no Lasso entry at all.
    • The architecture audit does back the premise. benchmarks/NATIVE_HOTSPOT_AUDIT.md line 141 lists Lasso.fit as mixed, sklearn-owned, already-equivalent, weight 33.1. benchmarks/OPTIMIZATION_ROADMAP.md lines 102-106 list the LassoCV/LassoLars family as python-bound rewrite-first at weight 46.5. Neither entry records a profiler-level attribution for the canonical Lasso.fit path, which is what the acceptance criteria ask for.

    Remaining work: profile the Flow Lasso inner loop and record whether the bottleneck is algorithmic, generated-code/vectorization related, or a Flow compiler/runtime blocker in benchmarks/native_hotspot_audit.json; compare feature update order, residual maintenance and the dual-gap convergence test against sklearn's Cython coordinate descent; add deterministic microbenchmarks around coordinate updates and residual maintenance alongside the end-to-end diabetes row, and add a Lasso entry to bench_scaled.flow so the gap is tracked as the problem scales.

  2. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    Profiling attribution for lasso_fit on the canonical diabetes row

    Measured before changing anything, on the exact canonical workload
    (benchmarks/split_diabetes.bin, StandardScaler-transformed train split,
    lasso_fit(X, y, 0.1, 1000, 0.01)), built the same way CI builds it
    (FLOW_OPT_LEVEL=0, which is what all three benchmark jobs in
    .github/workflows/flow.yml set).

    Shape: 353 train rows x 10 features.

    Baseline lasso_fit, median of 9 interleaved runs in one process:
    18.99 ms (min 18.52, max 20.60). That matches the committed
    headline_result_v2.json value of 19.2 ms.

    Phase breakdown from an instrumented clone of the function body:

    phase ms share
    mean centering pass 0.009 0.05%
    column squared norms 0.011 0.06%
    coordinate descent loop 19.66 99.9%

    And inside the coordinate descent loop:

    inner phase ms calls
    rho accumulation (sum_i xc_ij * r_i) 9.50 10000
    residual update (r_i += delta * xc_ij) 9.59 9976
    soft-threshold, bookkeeping, loop overhead ~0.6 -

    (The inner numbers carry the timer-call overhead, which is why they sum to
    slightly more than the uninstrumented 18.99 ms.)

    What this rules in and out

    Standardization and column norms are not the problem. Together they are
    0.02 ms, one tenth of one percent. ||x_j||^2 is already hoisted and cached
    outside the sweep, so item 2 on the usual checklist is already done.

    The residual is already maintained incrementally, not recomputed. Verified
    by reading the loop and by the update count: 9976 residual updates for 10000
    coordinate visits.

    There is no active set to exploit here. All 10 coefficients are nonzero at
    the solution (nonzero_w=1111111111), and the delta != 0 guard skips only
    24 of 10000 coordinate visits. Screening or a working set would buy nothing on
    this row.

    The convergence test costs nothing but never fires. The test is
    max_change < 1e-16 on the largest absolute coefficient change in a sweep. It
    is a scalar compare over 10 features, so it is free. It also never triggers:
    epochs_run = 1000, the full max_iter. The solver runs 1000 full sweeps
    regardless of how long ago the coefficients settled. sklearn's
    _cd_fast.pyx stops on a duality gap against tol * ||y||^2 with a default
    tol=1e-4 and stops far earlier.

    So the gap is two independent things

    1. Per-element cost in the inner loop. Each of the 7.06M inner iterations
      (1000 epochs x 10 features x 353 rows x 2 passes) currently does a
      matrix_at call, an i*cols+j index computation, an f32 load, a widen to
      f64, a subtraction of x_mean[j], then the multiply-accumulate. The
      centered value x_ij - mean_j is recomputed 2000 times per element over the
      fit and is never stored. Column access also strides by cols through a
      row-major buffer. sklearn runs ddot/daxpy over a contiguous
      Fortran-ordered column.

    2. Iteration count. 1000 sweeps against sklearn's early stop.

    Item 1 is the piece that can be made bit-identical: precomputing the centered
    matrix once, column-major, changes no arithmetic value or summation order, it
    only stops recomputing and re-striding. Item 2 changes where the solver stops,
    so it needs the parity argument made explicitly against the f32 coefficients
    that actually leave the function.

    Optimization work follows on perf/209-lasso-coordinate-descent.

  3. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    Result: 74x on the canonical row, coefficients bit-identical

    PR #427, branch perf/209-lasso-coordinate-descent.

    Following the attribution above, the whole cost was per-element work in the
    sweep, so that is what changed. Two things in lasso_fit:

    1. The centered design is materialized once, column-major, in f64. Worth 2.0x on
      its own, and bit-identical by construction: same values, same summation
      order, it just stops recomputing x_ij - mean_j 2000 times per element and
      stops striding by n_features through a row-major buffer.
    2. For modest feature counts the solver carries q = Xc^T r against a
      precomputed G = Xc^T Xc instead of carrying the residual. When w_j moves
      by delta the residual moves by delta * Xc_j, so q moves by
      delta * G_j, and rho_j = q_j + ||Xc_j||^2 * w_j is exactly what the
      residual formulation computes. One coordinate update goes from
      O(n_samples) to O(n_features): 706 operations down to 10 on this shape.
      Same trick as sklearn's enet_coordinate_descent_gram.

    Convergence semantics are untouched. As the attribution showed, there was no
    early-stopping win available on this design anyway.

    Paired and interleaved in one process, old and new alternating, median of 9,
    353x10, alpha 0.1, 1000 sweeps:

    run before after speedup
    load avg 12.6 20.081 ms 0.262 ms 76.65x
    load avg 25.7 32.820 ms 0.446 ms 73.59x

    Both ends and both fallback paths:

    shape sweeps path before after speedup
    100 x 8 1000 Gram 0.068 ms 0.015 ms 4.53x
    353 x 10 1000 Gram 20.081 ms 0.262 ms 76.65x
    10000 x 20 200 Gram 6.698 ms 5.090 ms 1.32x
    200 x 64 4 column-major residual 0.275 ms 0.183 ms 1.50x
    120 x 600 50 column-major residual 4.257 ms 2.305 ms 1.85x
    2900 x 2900 2 streaming, unchanged 122.21 ms 117.23 ms 1.04x

    No BLAS is involved, so no thread-dispatch threshold applies and there is no
    cross-backend reassociation question. The small end gains 4.5x, so there is no
    small-input regression.

    In the canonical benchmark itself, one fit per process, origin/main's
    linear.flow restored in place for the first line, both run back to back on the
    same machine:

    origin/main   RESULT|Lasso|diabetes|r2|0.455529690|28.841|0.005
    this branch   RESULT|Lasso|diabetes|r2|0.455529690| 0.333|0.004
    

    Applying the measured factor to the committed flow_fit_ms of 19.205 projects
    roughly 0.26 ms, which against sklearn_ms 0.4507 is a runtime ratio near 1.7
    rather than 0.0235, flipping the row from a sklearn win to a Flow win.
    benchmark-contract will produce the authoritative number.

    Parity

    Bit-identical, not merely close. A verbatim frozen copy of the pre-change
    implementation ran alongside the new one in the same process, compared
    element-wise on the f32 weights and intercept that leave the function:

    shape bit-identical max abs diff bias diff
    353 x 10 10 / 10 0 0
    100 x 8 8 / 8 0 0
    10000 x 20 20 / 20 0 0
    200 x 64 64 / 64 0 0
    120 x 600 600 / 600 0 0
    2900 x 2900 2900 / 2900 0 0

    All 19 canonical RESULT rows were captured twice on this machine, once with
    origin/main's linear.flow in place and once with the change. All 19 scores
    are identical to the last printed digit, linear.flow's other estimators
    included, which is the check compiler bug #469 requires on a file this size.
    Lasso is 0.455529690 both times, matching the committed flow_score.

    tests/test_opt_lasso_fit.flow is new and carries the reference implementation.
    16 cases covering every path plus near-collinear designs, column scales spanning
    eight orders of magnitude, constant columns, a single feature, p > n, and the
    streaming fallback; plus semantics a faster solver could silently break (a large
    penalty must give exactly zero, the unpenalised intercept must sit at the
    response mean).

    Scaled coverage

    bench_scaled.flow now has a Lasso row, with the matching row in
    bench_scaled_sklearn.py so the comparison resolves. This was not affordable
    before: 10000 x 32 at 1000 sweeps was 640M inner iterations on the old code,
    about 1.6 s per case before CI's three repeats. Measured now: 0.237 / 0.312 /
    1.417 ms at 8 features and 2.037 / 2.975 / 12.914 ms at 32 features, across
    100 / 1000 / 10000 rows.

    Audit disposition

    The remaining limitation on main was algorithmic, not a
    generated-code/vectorization problem and not a Flow compiler or runtime blocker:
    an O(n_samples) coordinate update where an O(n_features) one was available, plus
    per-element recentring that a materialized centered copy removes. With both
    addressed, what is left is ordinary generated-code quality at
    FLOW_OPT_LEVEL=0 on the Gram build, a small share of a sub-millisecond fit.

    benchmarks/native_hotspot_audit.json and NATIVE_HOTSPOT_AUDIT.md are
    CI-regenerated artifacts, so I have not hand-edited them; this comment and the
    PR body are the record.

  4. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    CI result: the row flips to a Flow win

    All checks green on fb8c135, including the two that matter here.

    Canonical benchmark contract pass (4m25s). Scaled benchmark report
    pass (2m14s), so the new Lasso row in the scaled matrix behaves and there
    is no small-input regression. Canonical 19/19 KMeans parity pass,
    FLOW syntax and imports pass, Run all tests and examples pass
    (39m16s), Python native package pass, both disparity-audit jobs
    pass, so #351's symmetric rule did not trip.

    Locally, python3 tools/run_all.py is 94/94 passed, 0 failed, including
    examples/regression_demo.flow.

    The authoritative measurement, from the headline-benchmark-32244505317
    artifact this run produced:

    committed baseline this run
    flow_score 0.45552969 0.45552969
    flow_fit_ms 19.205 0.835
    flow_ms 19.209 0.844
    sklearn_ms 0.451 0.905
    speedup 0.02346 1.0717
    classification sklearn win flow win
    parity_status approximately equivalent approximately equivalent

    The runtime ratio moves from 0.0235 to 1.0717, a 45.7x improvement, and the
    row changes from the worst sklearn win in the canonical set to a Flow win. The
    score is unchanged to the last digit.

    The ratio is the right figure to quote rather than the raw fit time: this
    runner is about twice as slow as the one that produced the committed row
    (sklearn_fit_ms 0.399 there against 0.799 here), so 19.205 -> 0.835 understates
    the change at 23x while the ratio, which cancels the machine, gives 45.7x. That
    is in line with the 74x measured on macOS/Accelerate where sklearn is not part
    of the comparison.

    parity unresolved on KMeans/digits in the same artifact is the known
    pre-existing flake; the Canonical 19/19 KMeans parity job itself passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions