Skip to content

Conversation

@Utkarshkarki
Copy link

This PR makes bounds checking in PyMC transform-aware, allowing redundant runtime checks
(CheckParameterValue → Switch(condition, logp, -inf)) to be safely skipped when the associated
constraint is already enforced by a variable transform (e.g., HalfNormal with LogTransform).

The change introduces optional metadata on CheckParameterValue and a transform-aware rewrite
used during compile(). Checks are removed per parameter, never globally, and only when a
transform provably guarantees the constraint. Existing behavior is preserved when metadata
is absent.

Distribution logp implementations are intentionally left unchanged; guidance is requested on
the preferred way to attach RV context in a follow-up.

Related Issue

Checklist

  • Checked that the pre-commit linting/style checks pass
  • Included tests covering transformed and untransformed parameters
  • Added necessary documentation (not required for infrastructure change)
  • Each commit corresponds to a logical change

Type of change

  • Maintenance
  • New feature/enhancement
  • Bug fix
  • Documentation
  • Other (please specify):

@welcome
Copy link

welcome bot commented Jan 10, 2026

Thank You Banner]
💖 Thanks for opening this pull request! 💖 The PyMC community really appreciates your time and effort to contribute to the project. Please make sure you have read our Contributing Guidelines and filled in our pull request template to the best of your ability.

):
super().__init__(ParameterValueError, msg)
self.can_be_replaced_by_ninf = can_be_replaced_by_ninf
self.rv = rv
Copy link
Member

Choose a reason for hiding this comment

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

self.rv is not a good strategy. We don't want state at the Op level like this, if it's needed it should be in the graph as an extra input.

Example if we do model.clone() it will now reference the old RV, not the cloned one

rvs_to_transforms = {}

# Use transform-aware rewrite if we have transforms and check_bounds is enabled
if check_bounds and rvs_to_transforms:
Copy link
Member

Choose a reason for hiding this comment

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

logic on transforms / bounds should be moved away from pytensorf.compile and model.logp or model.fn (#8038)

@Utkarshkarki Utkarshkarki force-pushed the feature/transform-aware-bounds-checking branch from 43a31b3 to e707627 Compare January 11, 2026 05:50
Copy link
Author

@Utkarshkarki Utkarshkarki left a comment

Choose a reason for hiding this comment

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

Hi @ricardoV94,

Implemented transform-aware bounds checking (#8039) with your #8038 feedback:

  • ✅ Moved check_bounds logic from pytensorf.compile() to Model.compile_fn()
  • ✅ rv passed as graph input (compatible with model.copy())
  • ✅ Added create_transform_aware_check_rewrite() using rvs_to_transforms

Distribution logp methods are staticmethods without RV access. What's the preferred approach to pass RV context?

Thanks!

@Utkarshkarki Utkarshkarki force-pushed the feature/transform-aware-bounds-checking branch from ff64546 to 67bc3f6 Compare January 20, 2026 07:20
@Utkarshkarki
Copy link
Author

@ricardoV94, Can you please review my changes and tell me the next steps? I

@ricardoV94
Copy link
Member

ricardoV94 commented Jan 28, 2026

@Utkarshkarki I don't have capacity to guide in this PR, as it is rather technical. I suggest you try another issue if it's not obvious for you how to proceed alone (it is not obvious for me either until I think a bit more about it).

An easier starting point is to add rewrites like exp(x) > 0 -> True in PyTensor (with a nan_unsafe tag, that specifies this rewrite ignores nan edge cases). This sort of stuff will cover some of the useless checks in the logp for properly transformed variables, moving us closer towards the check_bounds being useless, without any changes in PyMC. We may find this sort of approach covers enough cases that we don't need to do anything in PyMC. Even if it doesn't, it's still widely useful.

@Utkarshkarki
Copy link
Author

i will look into it

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.

2 participants