Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop backend dispatch pattern #6

Open
snunez1 opened this issue Jul 9, 2023 · 3 comments
Open

Develop backend dispatch pattern #6

snunez1 opened this issue Jul 9, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@snunez1
Copy link

snunez1 commented Jul 9, 2023

One of the things we're going to need is a good pattern for dispatching to various backends. Here's one possible pattern that I used for a scatterplot smoother:

 ;; What about the case where both lla and linear-algebra are loaded?
		    beta #+lla (lla:solve A b)
			 #+linear-algebra (linear-algebra:solve A b)
		         #-(or lla linear-algebra) (solve-matrix (aops:stack-cols A b))

As I start to use it, I think it's more of an anti-pattern because:

  • It doesn't easily handle the case where I might want multiple back-ends in one application (is this an edge case?)
  • It is only evaluated at compile-time, so if I want to change I need to recompile
  • It sort of abuses *features* in a non-specific way I don't like

I know that MGL-MAT has a way to automatically dispatch methods depending on whether BLAS or CUDA are available. Generally his architecture seems similar to that of linear-algebra, with various 'kernels' (lisp, CUDA, BLAS) that are operated on by lisp code. It might be worth studying.

@snunez1 snunez1 added the enhancement New feature or request label Jul 9, 2023
@beberman
Copy link
Collaborator

beberman commented Jul 9, 2023

I think there are a handful of uses cases the libraries in lisp-stat should support:

  1. User who wants to do some linear algebra manipulation and doesn't want to load a pile of libraries to do it. So basic create vector, metric, solve them, products etc. So everything in pure Lisp. linear-algebra was designed for that.
  2. User who needs to scale out and do much more complex algorithms and needs the power of BLAS. LLA seems like that's its point
  3. User who needs to scale-up to really large matricies that can't be held in memory easily and needs to be very careful about moving data mgl-mat was designed for that.
  4. User who want t use GPU to do computation (i.e. most likely doing machine learning). So they need CUDA.

NumPy and CuPy solve this through the import trick. They use the same underlying function names and then just import it into the same name

import NumPy as np
or
import CuPy as np

equivalent I believe to (rename-package :cucl as :numcl) assuming the libraries in CL where named cucl and numcl.

Tensorflow does what you are suggesting. I checks if CUDA is installed and then dispatches to CUDA instead of the regular libraries.

I think the rename-package approach is much easier to maintain and extend.
So I would recommend designing a common function interface on top of these packages and then document how to rename.

@snunez1
Copy link
Author

snunez1 commented Jul 9, 2023

That's an interesting idea. The systems could remain separate, and yet be simply swapped out. Good thinking. I'd suggest using a package-nickname or import-from or for this, as it's closer to the Python import mechanism and causes less problems for other packages.

@snunez1
Copy link
Author

snunez1 commented Jul 28, 2023

This is now agreed and we can close it once we document the pattern and provide a few examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants