pymc-devs / pymc-devs/pytensor

Directly use scipy lapack functions instead of scipy wrappers

Open
#1,468 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

beginner friendly help wanted linalg performance
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Description

We take small performance hits by using scipy.linalg functions rather than directly calling lapack functions in our perform methods. These wrappers include a lot of data validation checks that we don't need, and sometimes end up copying things that we don't want to copy.

Instead of using these wrappers, we can use scipy.linalg.get_lapack_funcs to get and use the relevant routine. For example, calling potrs instead of using linalg.cholesky.

Which routine to call is easy to figure out by looking at the scipy source code and checking which function the wrapper calls, and with which arguments.

Here's a list of Op that can be converted, and the LAPACK routines they call:

  • Cholesky (potrf)
  • CholeskySolve (potrs)
  • LUFactor (getrf)
  • LUSolve (getrs)
  • SolveTriangular (trtrs)
  • Solve (depends on the assume_a argument. They all do lu_factor + lu_solve, but the exact routines called depends on the structure of the A matrix)
    • "gen" - getrf + getrs
    • "sym" - sysv
    • "pos" - posv
    • "tridiagonal" - gttrf + gttrs
    • "banded" - gbsv
  • Eigvalsh (complicated -- depends on input arguments. Need to spend some time looking at the scipy wrapper)
  • SolveContinuousLyapunov - trsyl

Routines in tensor.nlinalg might also benefit, but it would require some testing. For example, GESVD and GESDD are LAPACK routines for SVD, which might be better than np.linalg.svd (and related functions, like np.linalg.pinv). As I type this, I can't say if calling these routines directly would be better or worse than calling the numpy function. The numpy code is somewhat harder to peer into than scipy.

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 reviewing the listed CholeskySolve, LUFactor, LUSolve, SolveTriangular, Solve, Eigvalsh, and SolveContinuousLyapunov Ops, then inspect the corresponding SciPy wrapper source and get_lapack_funcs documentation. Check tensor.nlinalg separately, since the issue notes that direct SVD routines need testing. Done means the applicable perform methods use the corresponding LAPACK routines without the SciPy wrappers while preserving behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.