dgasmith / opt_einsum

⚡️Optimizing einsum functions in NumPy, Tensorflow, Dask, and more with contraction order optimization.
https://dgasmith.github.io/opt_einsum/
MIT License
848 stars 68 forks source link

Possibilities to utilize symmetry in opt_einsum #185

Closed Lancashire3001 closed 2 years ago

Lancashire3001 commented 2 years ago

I am wondering if there is any approach can adapt symmetry in tensor contraction in opt_einsum. E.g.,

A[a,b] B[b,c,d,e] = C[a,c,d,e], where B[:,:,d,e] = B[:,:,e,d], or various tensor contractions in the correlated methods (perturbation, coupled-cluster etc)

From my experience, ctf gets useful when there are many symmetries, but the efficiency is less than nested loops in Fortran https://github.com/cyclops-community/ctf/issues/136

If my observation is true, that would affect symtensor, which adopts ctf as the backend https://github.com/yangcal/symtensor

If so, and I use f2py etc to make a Fortran-python interface, how complicated to use it to the backend of opt_einsum? Or is there any available options already? (even I use ctf as the backend, presumably I still need to mark symmetries for the tensors used in opt_einsum)

dgasmith commented 2 years ago

We haven't attempted to add symmetry as, in practice, different kinds of symmetries have different performances as the binary product level. The lack of symmetry is fairly hard coded into the current code as we have optimized the tensor contractor cost and associated path finding tech for performance over flexibility.

Adding another backend to opt_einsum isn't enabled through functionality out of the box, but adding a new backend isn't particularly troublesome: https://github.com/dgasmith/opt_einsum/blob/master/opt_einsum/backends/dispatch.py