Fix BaseHMC default mass matrix ignoring requested dtype (GH #8213)#8345
Open
Mohit-Ak wants to merge 1 commit into
Open
Fix BaseHMC default mass matrix ignoring requested dtype (GH #8213)#8345Mohit-Ak wants to merge 1 commit into
Mohit-Ak wants to merge 1 commit into
Conversation
The default QuadPotentialDiagAdapt was built with floatX unconditionally, so passing an explicit dtype to NUTS/HamiltonianMC that differed from pytensor.config.floatX produced a potential whose dtype mismatched the logp/dlogp function, and CpuLeapfrogIntegrator raised "dtypes of potential ... and logp function ... don't match". Build the default mass matrix using the logp function's resolved dtype so the two always agree. Fixes pymc-devs#8213
|
|
Documentation build overview
159 files changed ·
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
BaseHMC.__init__accepts adtypeargument and forwards it to the parent so the compiled logp/dlogp function is built with that dtype. However, when it constructs the default mass matrix it ignored that dtype and usedfloatXunconditionally:So whenever the requested
dtypediffers frompytensor.config.floatX, the potential and the logp function end up with different dtypes, andCpuLeapfrogIntegrator.__init__bails out:That traceback matches the one in #8213 exactly (the crash originates from the
QuadPotentialDiagAdapt(...)line the reporter linked to).The fix builds the default mass matrix using the dtype the logp/dlogp function actually resolved to (
self._logp_dlogp_func.dtype) — which is the same value the integrator later compares against — instead offloatX. That keeps the two in sync in every case, including the common one wheredtype is None(both fall back tofloatX, so behavior is unchanged). The now-unusedfloatXimport is dropped.The
scaling-basedquad_potential(...)path already derives its dtype from the user-supplied array, so it isn't affected; this was the only default-construction site that hard-codedfloatX.How was it tested
Added
test_default_potential_honors_dtypeintests/step_methods/hmc/test_hmc.py. It builds a float64 model, then (insidepytensor.config.change_flags(floatX="float32")) constructs the step withdtype="float64"and asserts the default potential comes outfloat64and matches the integrator's expected dtype. Onmainthis test fails with thedon't matchValueError; with the fix it passes.Local run:
pytest tests/step_methods/hmc/→ 67 passed, 6 skipped (matplotlib-optional tests run once installed).ruff==0.11.13ruff checkandruff format --checkboth clean on the changed files.Related Issue
dtypeto QuadPotentialDiagAdapt#8213Checklist
Type of change