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

Add time values as sampler stats for NUTS #3986

Merged
merged 4 commits into from
Jul 1, 2020

Conversation

aseyboldt
Copy link
Member

This PR adds three more sampler stats for NUTS and HMC: "process_time_diff_ns" and "perf_counter_diff_ns" and "perf_counter_ns".
During debugging it can be useful to see how long it took to compute each sample, and for issues involving blas/openmp it can also be useful to see the difference between process time and wall time.
The names are taken from the python time module: https://docs.python.org/3/library/time.html#time.perf_counter

This can be used like this:

with pm.Model() as model:
    pm.Normal("a", shape=1000)
    
    tr = pm.sample(return_inferencedata=True)

stats = tr.sample_stats
for chain in stats.chain:
    sns.distplot(np.log(stats.process_time_diff_ns.sel(chain=chain)), label=chain.values)
plt.legend();

image

Copy link
Contributor

@AlexAndorra AlexAndorra left a comment

Choose a reason for hiding this comment

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

Neat, thanks @aseyboldt ! Left some comments below

pymc3/step_methods/hmc/nuts.py Outdated Show resolved Hide resolved
pymc3/step_methods/hmc/base_hmc.py Outdated Show resolved Hide resolved
@aseyboldt
Copy link
Member Author

I forgot to check when the time functions were added to the stdlib. The nanosecond versions are new in 3.7. We still want to support 3.6 for some time?
I guess we can just switch to the floating point versions then. The resolution should still be good enough I think.

pymc3/step_methods/hmc/base_hmc.py Outdated Show resolved Hide resolved
pymc3/step_methods/hmc/base_hmc.py Outdated Show resolved Hide resolved
Copy link
Contributor

@AlexAndorra AlexAndorra left a comment

Choose a reason for hiding this comment

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

Just a couple questions below before approving

"perf_counter_diff_ns": perf_end - perf_start,
"process_time_diff_ns": process_end - process_start,
"perf_counter_ns": perf_end,
"perf_counter_diff": perf_end - perf_start,
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a suggestion: why not call this perf_time_diff? I find it more explicit and it matches process_time_diff

Copy link
Member Author

Choose a reason for hiding this comment

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

I see why perf_time_diff might be nicer, I just followed the naming of the function in time, so that it's easier to see what clock is used exactly. But counter is a bit confusing...

"perf_counter_ns": perf_end,
"perf_counter_diff": perf_end - perf_start,
"process_time_diff": process_end - process_start,
"perf_counter_start": perf_start,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment on the name, and out of curiosity: why did switch from perf_end to perf_start?

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, I think the choice it kind of arbitrary. We can reconstruct the other one since we have the difference.
I just thought it might be a bit more intuitive to have the start of the draw as an absolute value.

@junpenglao
Copy link
Member

junpenglao commented Jul 1, 2020

Is it something we should considered push to top level api so that it is available for all step_methods?

https://github.com/pymc-devs/pymc3/blob/747db63948f8115e30d676089b77116791a028fa/pymc3/step_methods/arraystep.py#L145-L157

FWIW, I think it is fine for it to be a HMC only method, and change later if there are feature request - just want to bring up this point.

@twiecki
Copy link
Member

twiecki commented Jul 1, 2020

Also needs note in release notes.

@aseyboldt
Copy link
Member Author

@junpenglao That would be useful, but we don't have a way to easily add sampler stats to all samplers at once, since they have to be declared in the step method itself. If users implement their own step methods (does anyone?) that would be a breaking change. At least unless we change some code in the trace backends to allow for missing or undeclared stats.

@twiecki done

Copy link
Contributor

@AlexAndorra AlexAndorra left a comment

Choose a reason for hiding this comment

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

Sorry, just one last nitpick 😜

RELEASE-NOTES.md Outdated Show resolved Hide resolved
@twiecki
Copy link
Member

twiecki commented Jul 1, 2020

I'm also fine with dropping Python 3.6, I know it's just a small thing here with an easy work-around but I think being progressive here is a good thing.

@ColCarroll
Copy link
Member

Good news about 1 week ago, then (via https://numpy.org/neps/nep-0029-deprecation_policy.html):

Date Python NumPy
Jan 07, 2020 3.6+ 1.15+
Jun 23, 2020 3.7+ 1.15+
Jul 23, 2020 3.7+ 1.16+
Jan 13, 2021 3.7+ 1.17+
Jul 26, 2021 3.7+ 1.18+
Dec 26, 2021 3.8+ 1.18+
Apr 14, 2023 3.9+ 1.18+

@junpenglao
Copy link
Member

@aseyboldt I see.

+1 to dropping py3.6

@twiecki
Copy link
Member

twiecki commented Jul 1, 2020 via email

Co-authored-by: Alexandre ANDORRA <andorra.alexandre@gmail.com>
@aseyboldt
Copy link
Member Author

Dropping 3.6 is fine, but maybe the float version is better after all. :-)
It has unit 'second' which is easier to work with, and we don't need good resolution in the ns range, we just aren't that fast.

Copy link
Contributor

@AlexAndorra AlexAndorra left a comment

Choose a reason for hiding this comment

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

All good now, thanks @aseyboldt ! I agree with you on the seconds vs. nanoseconds stuff

@AlexAndorra AlexAndorra dismissed michaelosthege’s stale review July 1, 2020 18:49

Problem is fixed now and this is blocking merge

@AlexAndorra AlexAndorra merged commit 7842072 into pymc-devs:master Jul 1, 2020
@aseyboldt aseyboldt mentioned this pull request Jul 2, 2020
gmingas added a commit to alan-turing-institute/pymc3 that referenced this pull request Jul 22, 2020
* Update GP NBs to use standard notebook style (pymc-devs#3978)

* update gp-latent nb to use arviz

* rerun, run black

* rerun after fixes from comments

* rerun black

* rewrite radon notebook using ArviZ and xarray (pymc-devs#3963)

* rewrite radon notebook using ArviZ and xarray

Roughly half notebook has been updated

* add comments on xarray usage

* rewrite 2n half of notebook

* minor fix

* rerun notebook and minor changes

* rerun notebook on pymc3.9.2 and ArviZ 0.9.0

* remove unused import

* add change to release notes

* SMC: refactor, speed-up and run multiple chains in parallel for diagnostics (pymc-devs#3981)

* first attempt to vectorize smc kernel

* add ess, remove multiprocessing

* run multiple chains

* remove unused imports

* add more info to report

* minor fix

* test log

* fix type_num error

* remove unused imports update BF notebook

* update notebook with diagnostics

* update notebooks

* update notebook

* update notebook

* Honor discard_tuned_samples during KeyboardInterrupt (pymc-devs#3785)

* Honor discard_tuned_samples during KeyboardInterrupt

* Do not compute convergence checks without samples

* Add time values as sampler stats for NUTS (pymc-devs#3986)

* Add time values as sampler stats for NUTS

* Use float time counters for nuts stats

* Add timing sampler stats to release notes

* Improve doc of time related sampler stats

Co-authored-by: Alexandre ANDORRA <andorra.alexandre@gmail.com>

Co-authored-by: Alexandre ANDORRA <andorra.alexandre@gmail.com>

* Drop support for py3.6 (pymc-devs#3992)

* Drop support for py3.6

* Update RELEASE-NOTES.md

Co-authored-by: Colin <ColCarroll@users.noreply.github.com>

Co-authored-by: Colin <ColCarroll@users.noreply.github.com>

* Fix Mixture distribution mode computation and logp dimensions

Closes pymc-devs#3994.

* Add more info to divergence warnings (pymc-devs#3990)

* Add more info to divergence warnings

* Add dataclasses as requirement for py3.6

* Fix tests for extra divergence info

* Remove py3.6 requirements

* follow-up of py36 drop (pymc-devs#3998)

* Revert "Drop support for py3.6 (pymc-devs#3992)"

This reverts commit 1bf867e.

* Update README.rst

* Update setup.py

* Update requirements.txt

* Update requirements.txt

Co-authored-by: Adrian Seyboldt <aseyboldt@users.noreply.github.com>

* Show pickling issues in notebook on windows (pymc-devs#3991)

* Merge close remote connection

* Manually pickle step method in multiprocess sampling

* Fix tests for extra divergence info

* Add test for remote process crash

* Better formatting in test_parallel_sampling

Co-authored-by: Junpeng Lao <junpenglao@gmail.com>

* Use mp_ctx forkserver on MacOS

* Add test for pickle with dill

Co-authored-by: Junpeng Lao <junpenglao@gmail.com>

* Fix keep_size for arviz structures. (pymc-devs#4006)

* Fix posterior pred. sampling keep_size w/ arviz input.

Previously posterior predictive sampling functions did not properly
handle the `keep_size` keyword argument when getting an xarray Dataset
as parameter.

Also extended these functions to accept InferenceData object as input.

* Reformatting.

* Check type errors.

Make errors consistent across sample_posterior_predictive and fast_sample_posterior_predictive, and add 2 tests.

* Add changelog entry.

Co-authored-by: Robert P. Goldman <rpgoldman@sift.net>

* SMC-ABC add distance, refactor and update notebook (pymc-devs#3996)

* update notebook

* move dist functions out of simulator class

* fix docstring

* add warning and test for automatic selection of sort sum_stat when using wassertein and energy distances

* update release notes

* fix typo

* add sim_data test

* update and add tests

* update and add tests

* add docs for interpretation of length scales in periodic kernel (pymc-devs#3989)

* fix the expression of periodic kernel

* revert change and add doc

* FIXUP: add suggested doc string

* FIXUP: revertchanges in .gitignore

* Fix Matplotlib type error for tests (pymc-devs#4023)

* Fix for issue 4022.

Check for support for `warn` argument in `matplotlib.use()` call. Drop it if it causes an error.

* Alternative fix.

* Switch from pm.DensityDist to pm.Potential to describe the likelihood in MLDA notebooks and script examples. This is done because of the bug described in arviz-devs/arviz#1279. The commit also changes a few parameters in the MLDA .py example to match the ones in the equivalent notebook.

* Remove Dirichlet distribution type restrictions (pymc-devs#4000)

* Remove Dirichlet distribution type restrictions

Closes pymc-devs#3999.

* Add missing Dirichlet shape parameters to tests

* Remove Dirichlet positive concentration parameter constructor tests

This test can't be performed in the constructor if we're allowing Theano-type
distribution parameters.

* Add a hack to statically infer Dirichlet argument shapes

Co-authored-by: Brandon T. Willard <brandonwillard@users.noreply.github.com>

Co-authored-by: Bill Engels <w.j.engels@gmail.com>
Co-authored-by: Oriol Abril-Pla <oriol.abril.pla@gmail.com>
Co-authored-by: Osvaldo Martin <aloctavodia@gmail.com>
Co-authored-by: Adrian Seyboldt <aseyboldt@users.noreply.github.com>
Co-authored-by: Alexandre ANDORRA <andorra.alexandre@gmail.com>
Co-authored-by: Colin <ColCarroll@users.noreply.github.com>
Co-authored-by: Brandon T. Willard <brandonwillard@users.noreply.github.com>
Co-authored-by: Junpeng Lao <junpenglao@gmail.com>
Co-authored-by: rpgoldman <rpgoldman@goldman-tribe.org>
Co-authored-by: Robert P. Goldman <rpgoldman@sift.net>
Co-authored-by: Tirth Patel <tirthasheshpatel@gmail.com>
Co-authored-by: Brandon T. Willard <971601+brandonwillard@users.noreply.github.com>
@kyleabeauchamp kyleabeauchamp added this to the 3.9.3 milestone Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants