pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Implement fallback for unimplemented Numba RandomVariables (#1245)

Open SwarnimShekhar opened this issue 9 months ago • 0 comments

Description

This PR updates the perform method in pytensor/tensor/random/op.py to gracefully handle unimplemented Numba RandomVariables by falling back to object mode. The changes include:

Fallback Mechanism: When a RandomVariable is not implemented in Numba, the code now falls back instead of raising a low-level error.

Improved Size Validation: A helper function _get_value (using get_underlying_scalar_constant_value) is introduced to evaluate symbolic constants in the size parameter. The validation now uses broadcasting rules (via zip_longest) to check that the provided size is compatible with the expected batch dimensions. If not, a ValueError is raised with the message: "Size length is incompatible with batched dimensions."

Known Issues: Two tests—test_RandomVariable_bcast_specify_shape and parts of test_RandomVariable_bcast—still fail due to output shape inference (e.g., the first dimension remains None instead of being concrete). This appears to be a separate type inference issue and is not directly related to the fallback mechanism. I plan to address this in a subsequent PR.

Related Issue

  • [ ] Closes #
  • [ ] Related to #

Checklist

Type of change

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

📚 Documentation preview 📚: https://pytensor--1269.org.readthedocs.build/en/1269/

SwarnimShekhar avatar Mar 05 '25 14:03 SwarnimShekhar