pytensor
pytensor copied to clipboard
Fix complex support for BatchedDot in C and JAX backends
Description
This PR adds native support for complex64 and complex128 data types to the BatchedDot Op across both the C and JAX backends, resolving the NotImplementedError raised during graph compilation mentioned by @jessegrabowski in issue #1849
Key technical changes:
- Python Graph Layer: Updated
make_nodetype-checking inpytensor/tensor/blas.pyto explicitly allow complex types to be built into the graph. - C-Backend ABI Compatibility: Bypassed strict C++ complex struct type mismatches by defining
alphaandbetaas rawfloat[2]anddouble[2]arrays. This matches the exact memory layout expected by the underlying Fortran ABI forcgemm_andzgemm_. - Safe Pointer Arithmetic: Prevented stride-related memory corruption in the C-loop by casting
PyArray_DATAtochar*before applying NumPy byte strides. This guarantees safe memory traversal regardless of the underlying primitive byte size. - JAX Backend: Verified that once the PyTensor Python layer permits complex inputs, execution delegates seamlessly to JAX's native complex
matmulwithout requiring an explicit custom dispatch. - Testing: Added comprehensive complex execution tests (
test_batched_dot_complex) totests/tensor/test_blas.py. Implemented the modern@matrix multiplication operator in the tests to ensure zeroFutureWarningdeprecations are triggered in the CI pipeline.
Related Issue
- [x] Closes #1849
- [ ] Related to #
Checklist
- [x] Checked that the pre-commit linting/style checks pass
- [x] Included tests that prove the fix is effective or that the new feature works
- [ ] Added necessary documentation (docstrings and/or example notebooks)
- [x] If you are a pro: each commit corresponds to a relevant logical change
Type of change
- [x] New feature / enhancement
- [x] Bug fix
- [ ] Documentation
- [ ] Maintenance
- [ ] Other (please specify):
@jessegrabowski i would like to have your opinion on this pls check this PR out whenever you have a moment