ml-explore / ml-explore/mlx

Feature Request: Least Squares Solver

Open
#3,773 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement low priority
Dominant language
C++
Stars
28.5k
Forks
2.3k
Avg merge
3d 8h
Merged PRs (30d)
62

Description

Motivation

In scientific computing, there are many situations where one has to solve an overdetermined or underdetermined linear system. Libraries like JAX and NumPy have a native least square solver via jax.numpy.linalg.lstsq and np.linalg.lstsq. mlx doesn't support this natively and I think adding this would be a good addition to the current library.

Roadmap

If mlx is interested in supporting this feature, a potential roadmap we can follow is the following:

  1. Initial Implementation: As a proof of concept, an initial implementation could be made using the existing linalg::qr or linalg::svd primitives to establish the API. Once created, we can tailor our efforts towards optimizing performance.
  2. CPU Optimization: Transition the CPU backend to utilize LAPACK gelsd. The current CPU SVD uses the LAPACK routines gesdd with jobz='A', which materializes the full $U$ and $V$ matrice. This is unideal in terms of memory usage when we have largely overdetermined or undetermined systems i.e. our coefficient matrix is a thin rectangle. Utilizing gelsd allows us to solve the system of linear equations without fully materializing the $U$ and $V$ matrix.
  3. Optional: An optional but another potential optimization we can make is to default to QR-decomposition based methods when the coefficient matrix is known/assumed to be near full rank.
API Parity

We should aim to emulate thenumpy API as much as possible.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing linalg::qr and linalg::svd entry points, then compare the requested behavior with the NumPy lstsq API. The roadmap also identifies LAPACK gelsd as a CPU optimization path; done means MLX exposes a least-squares solver with API behavior matching the agreed NumPy-compatible scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.