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

Deprecate the "class" approach to the high-level kernels #492

Closed
rlouf opened this issue Feb 22, 2023 · 9 comments · Fixed by #501
Closed

Deprecate the "class" approach to the high-level kernels #492

rlouf opened this issue Feb 22, 2023 · 9 comments · Fixed by #501
Assignees
Labels
enhancement New feature or request refactoring Change that adds no functionality but improves code quality
Milestone

Comments

@rlouf
Copy link
Member

rlouf commented Feb 22, 2023

This was introduced to be able to access the low-level step and init functions at the higher level. With hindsight this was neither necessary nor a good design, and it also makes documenting these high-level constructs more complicated. We should use functions instead, like we do for window_adaptation.

@rlouf rlouf added enhancement New feature or request refactoring Change that adds no functionality but improves code quality labels Feb 22, 2023
@junpenglao
Copy link
Member

Breaking: French dev propose to demolish class.

Joke aside, what is the proposal for high level API? Factory function that returns callables (init and step)?

@rlouf
Copy link
Member Author

rlouf commented Feb 22, 2023

Yes. These classes currently serve no purpose other than proposing a shortcut to the lower-level step functions. Factory functions work just fine, and you can still import the lower level ones as e.g. blackjax.mcmc.hmc.step.

@albcab
Copy link
Member

albcab commented Feb 22, 2023

So the idea would be for the user to do

from blackjax import mcmc
state = mcmc.hmc.init(position)
step = mcmc.hmc.kernel()
new_state, info = step(rng_key, state, logdensity_fn, step_size, inverse_mass_matrix, num_integration_steps)

instead of

import blackjax
hmc = blackjax.hmc(logdensity_fn, step_size, inverse_mass_matrix, num_integration_steps)
state = hmc.init(position)
new_state, info = hmc.step(rng_key, state)

or do we want to introduce another function that does what classes do now? Maybe in blackjax.mcmc.hmc.py intead of kernels.py?

Also, this would work for VI, MCMC, SGMCMC, and SMC. But for adaptation algorithms we still need a higher level class or function that puts things together.

@rlouf
Copy link
Member Author

rlouf commented Feb 22, 2023

I wasn't clear, you would still do:

import blackjax

hmc = blackjax.hmc(logdensity_fn, step_size, inverse_mass_matrix, num_integration_steps)
state = hmc.init(position)
new_state, info = hmc.step(rng_key, state)

except blackjax.hmc would be a function instead of a class.

@junpenglao
Copy link
Member

So basically blackjax.hmc will just return a

class MCMCSamplingAlgorithm(NamedTuple):

@albcab
Copy link
Member

albcab commented Feb 23, 2023

I see how having a class with a __new__ method that returns an MCMCSamplingAlgorithm object is redundant, when we can use a function to directly return that object, and if it makes documenting easier then refactoring is necessary. If all agree I can do the refactoring tomorrow, before new kernels are implemented as a class.

@rlouf
Copy link
Member Author

rlouf commented Feb 24, 2023

Great, thanks! Then we can start moving these constructors to their respective algorithm-class folder like we discussed.

@albcab albcab self-assigned this Feb 26, 2023
@albcab
Copy link
Member

albcab commented Feb 27, 2023

Notice that by replacing classes with functions we loose this alias: blackjax.mcmc.hmc.kernel, alias blackjax.hmc.kernel.

@rlouf
Copy link
Member Author

rlouf commented Feb 27, 2023

I'm fine with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactoring Change that adds no functionality but improves code quality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants