Repository navigation
Description
In #43270 (comment) I describe a design for storing the "conjugation" status of a tensor on the tensor itself. See the quoted comment for more details. This issue describes how you could go about prototyping this enhancement. This prototype is not expected to be committed, it is just to show the feasibility of this approach. This is bigger than a normal bootcamp task and it is OK not to get through all of this.
Step 1: Add the conjugate bit to TensorImpl in c10/core/TensorImpl.h. Add this boolean to the existing booleans so this doesn't increase the size of TensorImpl. You'll need to add a setter and getter for this bit. The semantics of this boolean are that when it is true, the meaning of the tensor is that the imaginary values in the tensor are negated.
Step 2: We need to add a new conjugation function that will set this bit, instead of actually doing the conjugation. Go to native_functions.yaml and make a new version of the conjugation operation:
- func: conj(Tensor self) -> Tensor
use_c10_dispatcher: full
variants: function, method
Copy paste this declaration and give it a new name (maybe conj_view), then find where conj is implemented, and copy the C++ signature. Inside the function declaration, you will implement it completely differently: you will simply allocate a new tensor which shares the same storage, size, etc. as the old tensor, but you'll invert the conjugate bit. Check out alias_with_sizes_and_strides for how you would go about it.
Step 3: So now we have a boolean, but nothing is using it. We're going to add a backend fallback to process this, but to do that, we need a dispatch key to attach that backend fallback. Add a new dispatch key Conjugate to DispatchKey.h; place it so that it runs after the autograd key (before, in source code). Modify your setter so that it also modifies the dispatch key set of the tensor, adding the Conjugate dispatch key when the boolean flag is true, and removing it when it's false.
Step 4: Finally, we will write a backend fallback. The general concept of this fallback is we will look through all of the tensor input arguments and find arguments which have the conjugate bit set. For these arguments, we'll run a normal conjugation operation (non conj_view) to transform it into a version with the conjugate bit not set, and then call the underlying operation. For more tips about how to write a backend fallback, see #41478
After you are done, you should be able to write code like x.conj_view() + y and show that the output is the same as x.conj() + y
Activity
anjali411 commentedon Aug 26, 2020
what would be the expected value of conj flag of x at the end of these operations?
ezyang commentedon Aug 26, 2020
To support x.imag as view we'll also need a negative flag. That's out of scope for the prototype, but I imagine negative flag should develop similarly.
There's also another question which is whether or not operations like multiplication preserve conjugate flag or not. One might see an analogy with memory format, where if something is in channels last, addition should preserve it. I'm not too sure, and hoping that the prototype can help answer some of these questions.