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

WIP: upd Monte-Carlo integration (vegasflow package) #407

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

mensigo
Copy link

@mensigo mensigo commented Jun 25, 2022

Fixes #

Proposed Changes

  • Add the 2nd MC method based on vegas algorithm (tf implementation – vegasflow).
  • Extra requirements: vegasflow, joblib.

Tests added

  • test_mc_vf_integration (3 simple functions, fully integrated)

Checklist

  • change approved
  • implementation finished
  • correct namespace imported
  • tests added
  • CHANGELOG updated
  • (if new public method/class) docs in rst file updated?

@mensigo mensigo marked this pull request as draft June 27, 2022 13:32
@mensigo mensigo marked this pull request as ready for review June 27, 2022 13:32
@mensigo mensigo marked this pull request as draft June 27, 2022 13:32
@mensigo mensigo changed the title Upd Monte-Carlo integration (vegasflow package) WIP: upd Monte-Carlo integration (vegasflow package) Jun 27, 2022
Copy link
Contributor

@jonas-eschle jonas-eschle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, that looks quite nice so far!

zfit/core/integration.py Outdated Show resolved Hide resolved
zfit/core/integration.py Outdated Show resolved Hide resolved
events_limit = vf_options.get("events_limit", int(1e6))
list_devices = vf_options.get("list_devices")
compilable = vf_options.get("compilable", True)
verbose = vf_options.get("verbose", False) # get_verbosity() >= 0 (?)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we can use the global zfit settings. What are the different verbosity levels in vegasflow? zfit goes from 0 to 10

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it's better to use vf's verbosity levels for convenience (4 levels) – added to docstring. However, it can be sync with zfit's levels in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that it's an additional effort to get this right. But it can also be called from outside, yeah

@@ -280,6 +312,60 @@ def test_mc_integration(chunksize, limits):
).numpy() == pytest.approx(integral3, rel=0.03)


def test_mc_vf_integration():
# simple examples
num_integral = zintegrate.mc_vf_integrate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There comes a crucial thing: can this run with a z.function wrapped? Because in the PDF, it usually will be.
You can also swap the default integration methods for PDFs and check if the tests still run, do they?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried wrapped version and faced some issues. The thing is, vf's run_integration is not intended to run under tf.function – there are some python control flow instructions (like "if" in print_iteration and numpy math ops when computing results from iterations) and tf.Variable creation (which causes "ValueError: tf.function-decorated function tried to create variables on non-first call"). I tried to use tf.py_function, but it did not help (anyway, i think it's not a relevant option in case of graph-like z.function application). I keep on thinking how to handle this issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yep. that's a bit of a problem. One thing you can try is to run it in a tf.init_scope or just make an issue with VF/ask them. That would be good to be solved somehow

func5_sigma_i = zfit.Parameter(f"sigma_{i}", func5_sigma)
func5_gauss_i = zfit.pdf.Gauss(obs=func5_obs_i, mu=func5_mu_i, sigma=func5_sigma_i)
func5_prod_gauss_lst.append(func5_gauss_i)
func5_prod_gauss = zfit.pdf.ProductPDF(pdfs=func5_prod_gauss_lst[::-1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move all of this inside a function

compilable: bool = True,
verbose: bool = False,
compilable: bool = False,
verbose: int | str = 3,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
verbose: int | str = 3,
verbose: int | str = 0,

Let the default be quiet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants