tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
232 stars 17 forks source link

Numba integration #362

Open pckroon opened 1 year ago

pckroon commented 1 year ago

If numba is installed, we should @jit(nopython=True, fastmath=True) compile the model when we lambdify them. This should of course be optional. If numba is not installed everything should work out of the box, and if it is installed everything should work faster. In addition, there should be a flag/option somewhere to never jit the models, just in case there's issues. Finally, the arguments to the jit function should be user-providable.

tBuLi commented 1 year ago

I agree, this would be awesome. In principle the code we generate with sympy uses numpy, and thus should be compatible in most typical cases. I would propose to add a numba argument to either fit itself, or Fit.execute, which could be a boolean, or a dict of options which is passed to numba.njit.

pckroon commented 1 year ago

I'd argue passing it to Model makes more sense. There's more ways of calling a model besides Fit. e.g.

model_dict = {...}
model = Model(model_dict, numba=True)  # Default, use default numba.jit arguments we decide on
model = Model(model_dict, numba={})  # Use default numba.jit default arguments
model = Model(model_dict, numba=False)  # Don't jit at all
model = Model(model_dict, numba={'fastmath': False})  # Provide jit arguments explicitely. 

And note that anything we sympify should be jitted: not just the model itself, but also the jacobian

tBuLi commented 1 year ago

Good point, fair enough! Model does make more sense indeed.