lewisfogden / heavylight

A lightweight actuarial modelling framework for Python
https://lewisfogden.github.io/heavylight/
MIT License
5 stars 2 forks source link

source code generator compatible with AI compilers #31

Closed MatthewCaseres closed 5 months ago

MatthewCaseres commented 5 months ago

might not be a productive use of time to read this, be warned

motivation

memory bottlenexk

Because everything is elementwise and there is no big matmul the models are memory limited on GPU and not utilizing all the FLOPS. https://docs.nvidia.com/deeplearning/performance/dl-performance-gpu-background/index.html#element-op

use the AI compiler

To get around the bottleneck we can use the AI compiler

AI compiler doesn't work with memoization

Our models can't be compiled right now. Probably the logic related to checking if values are in the cache and stuff like that is causing what PyTorch people call a "graph break" as we need to execute python code between operations.

To avoid graph breaks, I believe we have to go around memoization/recursion and actually write a loop?

source code generation

But writing out the model as a loop would really suck. So we will have to generate the loop.

It would maybe be easier to just generate the unrolled loop, but 22 formulas across 277 timesteps would be like 6000 lines of code. And you can't really edit that by hand in any productive way, so we will probably have to actually write the loop just for ergonomics.

constraints

I don't want to deal with things that aren't single integer parameters, will probably enforce that.

implementation details

cache_graph.graph is currently unused. Probably use that sort of thing. Enforce that timesteps are never going back more than 1. Enforce that only args is ever timesteps.

algorithm:

MatthewCaseres commented 5 months ago

ok this is too complicated, rather write the loop by hand