RustQuant icon indicating copy to clipboard operation
RustQuant copied to clipboard

`autodiff`: re-structure to avoid lifetimes.

Open avhz opened this issue 2 years ago • 8 comments

The Variable struct has a reference to the Graph struct. This is causing a lot of issues, namely it prevents:

  • Adding support for ndarray and nalgebra.
  • Python bindings since #[pyclass] is incompatible with lifetimes.

It also makes it somewhat non-user-friendly since the user also has to annotate functions they want to differentiate with lifetimes.

Need to find a better solution than &'v Graph (basically re-structure the module).

Edit:

Ideally, I want to implement num-traits for the Variable type, and allow it to be 'static, since both ndarray and nalgebra require array/matrix elements to be:

  • ndarray:

    • pub trait LinalgScalar: 'static + Copy + Zero + One + Add + Sub + Mul + Div<Output = Self> { }
  • nalgebra:

    • T: Scalar + Zero + ClosedAdd + ClosedMul
    • pub trait Scalar: 'static + Clone + PartialEq + Debug { }

avhz avatar Jul 11 '23 19:07 avhz

The variable struct has a member Graph, maybe can be wrapped inside a Box?

SimonG85 avatar Jul 13 '23 09:07 SimonG85

I tried Rc but it means user needs to .clone() a lot.

avhz avatar Jul 13 '23 12:07 avhz

Hello. I discovered this project due to the latest TWIR and was looking through this issue.

Can't Variable just own the Graph? Specially since you have a graph function that returns a reference to this member.

EDIT: sorry, I went trough the other files and realized this won't do. Boxing and using an rc would be fine since you are only cloning the pointer so there are no performance losses.

laplus-sadness avatar Jul 13 '23 19:07 laplus-sadness

Hi thanks for your interest :)

Rc works (after a lot of refactoring), but it's very annoying for the user.

Long mathematical formulas become filled with .clone() which is annoying to write and doesn't look very good.

I think the actual logic of the module needs to change tbh.

avhz avatar Jul 13 '23 22:07 avhz

I am not familiar with automatic differentiation so I was reading the Wikipedia page about it and stumbled upon the cpp implementation of the reverse accumulation procedure, where they use a hashmap to store state.

So... I think that rewriting the Graph struct to own a vector, or a hashmap, of Variables, adding them through Graph, would do the things you are asking for.

laplus-sadness avatar Jul 14 '23 02:07 laplus-sadness

So store the Variables in the Vertexs ?

avhz avatar Jul 14 '23 07:07 avhz

Has this been addressed?

Autoparallel avatar Feb 25 '24 02:02 Autoparallel

Not yet, it's quite a task. But it would be nice to get a more flexible AD implementation that can be used to compute Greeks more easily, for example.

avhz avatar Feb 25 '24 05:02 avhz