Code design: moving away from kwargs and towards abstracting each algorithm as a class with its methods and settings. #42
Replies: 5 comments 3 replies
-
Yeah we're open to some suggestions here. I get the need for some more transparency and control over what algorithm is ultimately run. I guess it would look like cola.linalg.inv(A, algorithm=Auto(tol=1e-5,max_iters=100)) and alternatives could be specified e.g. with cola.linalg.inv(A, algorithm=CG(tol=1e-5,max_iters=100))
cola.linalg.inv(A, algorithm=LU()) A challenge comes from situations created by dispatch. For example what happens if we have Dispatch splits the inverse into B⁻¹⊗C⁻¹. Using I suppose in these more complicated cases, the user could specify a mapping algorithm={cola.PSD: CG(...), other: GMRES(...)} or better as a function (since then it could also make the choices based on the sizes or types involved): algorithm=lambda A: CG(...) if A.isa(cola.PSD) else GMRES(...) |
Beta Was this translation helpful? Give feedback.
-
I do see this being especially helpful as we begin to have more algorithms for the same linear algebra operation |
Beta Was this translation helpful? Give feedback.
-
We've decided to go ahead and start working on this implementation. Algorithms will most likely be simple immutable dataclasses (pytrees as well) @dataclass_pytree
class Algorithm:
pass
class LU(Algorithm):
pass
class CG(Algorithm):
tol=1e-5
max_iters=1000
pbar=False
P = None
x0 = None The algorithm will most likely be specified also through dispatch: @dispatch
def inv(A: LinearOperator, alg: LU):
# implementation
@dispatch
def inv(A: LinearOperator, alg: CG):
# implementation but we will need to set the dispatch precedence so the |
Beta Was this translation helpful? Give feedback.
-
Nice work - this looks clean! I'm pleased to see you're adopting dataclasses for this (and I would suggest this for your LinearOperators too). Yeah immutability is super important here! A frozen dataclass would be the safest option. :) |
Beta Was this translation helpful? Give feedback.
-
Merged on main with #73 for the Algorithms implementation and assorted changes. For the pytree migration, we are hoping to use py🌲class, but some issues remain to be sorted out. So that is coming, but not ready yet. |
Beta Was this translation helpful? Give feedback.
-
The default approach is to use kwargs e.g.,
method="auto"
,max_iters
, etc., to define the algorithm or functions likecola.solve
,cola.logdet
, and others. It can make it challenging to work out what operations have been executed, especially in a library with extensive dispatch mechanisms. I strongly advocate for moving away from this design.A more robust approach would involve abstracting each algorithm and allowing them to be passed as arguments. This approach is effectively demonstrated in Lineax for structured matrix solves: Lineax Structured Matrices Documentation.
Beta Was this translation helpful? Give feedback.
All reactions