Feature Request: Least Squares Solver
Nobody has claimed this yet.
- 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:
- Initial Implementation: As a proof of concept, an initial implementation could be made using the existing
linalg::qrorlinalg::svdprimitives to establish the API. Once created, we can tailor our efforts towards optimizing performance. - CPU Optimization: Transition the CPU backend to utilize LAPACK
gelsd. The current CPU SVD uses the LAPACK routines gesdd withjobz='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. Utilizinggelsdallows us to solve the system of linear equations without fully materializing the $U$ and $V$ matrix. - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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