pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Modify np.tri Op to use _iota instead

Open Nimish-4 opened this issue 9 months ago • 12 comments

Description

Related Issue

  • [ ] Closes #
  • [ ] Related to #1265

Checklist

Type of change

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

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

Nimish-4 avatar Mar 08 '25 13:03 Nimish-4

The circular import is because you are importing _iota from tensor.einsum inside tensor.basic, but inside tensor.einsum, tensor.basic is imported.

The solution is to move the _iota function to tensor.basic. I also suggest to remove the leading underscore, because there's no reason this function should be considered "hidden". I don't think it needs to be added to __all__ (because it's not a function that exists in numpy), but I also wouldn't object to it.

jessegrabowski avatar Mar 10 '25 04:03 jessegrabowski

Those Ops and a few more (arange, alloc, ...) should probably be in a tensor_creation file. basic is hosting too much

ricardoV94 avatar Mar 10 '25 05:03 ricardoV94

@jessegrabowski My bad for missing it, I'll make the change. @ricardoV94 If that is appropriate in scope for me, I'll be okay with working on it.

Nimish-4 avatar Mar 10 '25 16:03 Nimish-4

@ricardoV94 If that is appropriate in scope for me, I'll be okay with working on it.

That's up to you. It would be much appreciated!

ricardoV94 avatar Mar 10 '25 16:03 ricardoV94

@jessegrabowski Made the change. I have left the docstring of iota mostly the same, but I guess the example section may need to be removed now? Tests are pending, I'll try adding some and see how it goes.

Nimish-4 avatar Mar 10 '25 18:03 Nimish-4

@jessegrabowski The tests were passing for tri but now the tests for tril and triu are failing. The issue seems, in the test we are passing the matrix as a symbolic variable, and the call to Tri here also passes M and dtype as symbolic variables (because they are derived from the matrix?) which is not allowed according to the current structure. Any suggestions on how to move ahead? Still figuring out symbolic variables.

Nimish-4 avatar Mar 12 '25 03:03 Nimish-4

Tri doesn't need init at all, just make it an empty OpFromGraph, then make a function called tri that actually does the work. Have a look at how the kronecker product is implemented for a template:

jessegrabowski avatar Mar 12 '25 16:03 jessegrabowski

@jessegrabowski I made the change, the tests are passing now. I have removed 'complex64' dtype from the tests for now, because there seemed to be some compilation error which I could not remove.

Nimish-4 avatar Mar 13 '25 13:03 Nimish-4

Well what was the error? I don't want us removing tests.

jessegrabowski avatar Mar 13 '25 14:03 jessegrabowski

@jessegrabowski ~~The stack trace looks like (removed some parts)~~

Nimish-4 avatar Mar 13 '25 15:03 Nimish-4

Put the test back and i'll trigger a CI run so I can see the full output

jessegrabowski avatar Mar 13 '25 15:03 jessegrabowski

@jessegrabowski Also stuck at this test case; seems to be failing because we are passing concrete values instead of symbolic ones (I think). The same is tested in the test case below it.

Nimish-4 avatar Mar 14 '25 15:03 Nimish-4