pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Fix complex support for BatchedDot in C and JAX backends

Open ayushkumrdev opened this issue 9 months ago • 1 comments

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_node type-checking in pytensor/tensor/blas.py to explicitly allow complex types to be built into the graph.
  • C-Backend ABI Compatibility: Bypassed strict C++ complex struct type mismatches by defining alpha and beta as raw float[2] and double[2] arrays. This matches the exact memory layout expected by the underlying Fortran ABI for cgemm_ and zgemm_.
  • Safe Pointer Arithmetic: Prevented stride-related memory corruption in the C-loop by casting PyArray_DATA to char* 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 matmul without requiring an explicit custom dispatch.
  • Testing: Added comprehensive complex execution tests (test_batched_dot_complex) to tests/tensor/test_blas.py. Implemented the modern @ matrix multiplication operator in the tests to ensure zero FutureWarning deprecations are triggered in the CI pipeline.

Related Issue

  • [x] Closes #1849
  • [ ] Related to #

Checklist

Type of change

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

ayushkumrdev avatar Feb 22 '26 14:02 ayushkumrdev

@jessegrabowski i would like to have your opinion on this pls check this PR out whenever you have a moment

ayushkumrdev avatar Mar 04 '26 08:03 ayushkumrdev