Repository navigation
Conjugate view tensor - #44799
Conjugate view tensor#44799ezyang wants to merge 10 commits into
Conversation
Based off of bdhirsch's prototype at #43702 [ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit 0c67830 (more details on the Dr. CI page):
🕵️ 30 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs Signed-off-by: Edward Z. Yang <[email protected]> [ghstack-poisoned]
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs Signed-off-by: Edward Z. Yang <[email protected]> [ghstack-poisoned]
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs ghstack-source-id: 726762d Pull Request resolved: #44799 Signed-off-by: Edward Z. Yang <[email protected]>
|
cc @boeddeker |
| // call it a day. | ||
| // 2. Inplace operation. Desugar x.add_(2) into x.conj_().add_(2).conj_(). | ||
| // Materialize other inputs as in (1). | ||
| // 3. Out-of-place operation. Desugar add(x, 2, out=y) into y.copy_(add(x, 2)) |
There was a problem hiding this comment.
nit: This is not really Out of place. Inplace via out? haha
|
|
||
| for (int64_t i = 0; i < num_arguments; ++i) { | ||
| auto& ivalue = (*stack)[stack_start + i]; | ||
| if (!ivalue.isTensor()) { |
There was a problem hiding this comment.
How are TensorList handled at this level? Are they already unpacked?
There was a problem hiding this comment.
Not handled, this is a bug.
| } | ||
| return at::_conj(self); | ||
| Tensor self_; | ||
| auto impl = c10::make_intrusive<TensorImpl>( |
There was a problem hiding this comment.
Is this expected to be different from self.variable_data() + setting conj?
| - name: view(Tensor(a) self, int[] size) -> Tensor(a) | ||
| self: grad.reshape(self.sizes()) | ||
|
|
||
| # When result1 is true, this is effectively a fused conj() and view_as_real |
|
Here, just some of my thoughts:
|
I don't know a good way to surface this. The semantics of
Only one is kept! A big benefit, thanks for telling us about it :)
Yes, between this and your comment about
Ah, this is a more general API problem.
Yes, conjugate is calculated two times, unless the operation supported fusion. |
|
Thank you for the answers.
Thanks for the explanation. Yes, letting the user know what happened is difficult. Warnings are maybe too verbose and difficult to track.
That is great.
I am not sure, where it would be used. Depending on this, it may have to be efficient.
Ah, now I get the intention for the name. It is probably difficult to find the right name for this.
So this is a small drawback of the current implementation. |
Yeah, this design explicitly duplicates work. One way this is observable, is that if you do an inplace operation on a conjugate view, the underlying base gets updated too. Lazy semantics wouldn't give you that. The lazy attribute is a valid alternative in the design space, but it is not too easy to implement correctly in a multithreaded setting. The problem is that |
I think my idea is slightly different from yours. My idea was slightly different, the python object can hold two data objects: class Tensor:
def __init__(self, data):
self.data = data
self._conj_data = None
@property
def conj_data(self):
if self._conj_data is None:
self._conj_data = self.data.conj()
return self._conj_dataIn python this is thread save, because the assignment is atomic (Thanks to the GIL) and the garbage collector takes care of a potential race condition (i.e. delete the object that was first assigned to
I was thinking about the "PostCheck" idea (Without knowing that name). |
Yep. Though, aren't you worried about memory usage? This suggests that when
I agree, the multithreaded situation is unlikely to happen. Most of my implementation difficulty is more to do with correctness: we must not segfault or data race when you use it from multiple threads. BTW I realized that https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.68.8868&rep=rep1&type=pdf is a better paper on the same subject as above (see Section 3). The primary challenge seems to be that we have arbitrarily many codepaths that may observe the Here is something that might work:
I must admit, while I believe this could work, and I believe that there may be other (non-conjugate) use cases that would benefit greatly from this model, I am unconvinced that this is the best choice for conjugate tensors. But we are kind of at a fork in the road regarding semantics: the semantics of this and conjugate views are different. In particular: For conjugate lazy tensors, you would expect Also, writing this example has made me realize that it is not sound to allow duplicate work if the user subsequently mutates the result of the conjugation, since two copies of the conjugate tensor is observable. So you really are going to have to proper locking when you materialize. |
Yes, I care about the memory, but mainly about the memory of the tensors hold in the backward graph. When I scrolled to some of my code and most of the time, the original data will be hold by another tensor. Another thing you may want to consider is, that the code should not have unpredictable side effects: y = x.conj()
if calculate_z:
z = y * x # Assume mul has no fusion of conj and mul
w = y @ x # Assume matmul has fusion of conj and matmul implementedDepending, whether For the following, I am not sure, what happens.
I agree, avoiding segfault or data race should have the highest priority.
The idea was the opposite. In the long term (maybe very long) no operation will see _conj_data and they use the non-conjugate data and apply the fused operation. The fused operation should always be faster (e.g. for cuda: one less kernel call). When I understand you right, your biggest problem is, that you have two possible realizations for the data: I would first do some investigations, if there is an improvement, to only keep 1 realization of the data. Furthermore, (at least at python level) just holding different realizations is simple and only need minimal locking (In Python the GIL can do this). At the moment, I am unsure, if this implicit implementation of conj is better than exposing a conjugate argument. |
This is good to know. To put it differently, if you have
If it's public, linking it from this discussion would be very helpful :)
In the scheme I described in my post, it would be materialized aka have overhead. In contrast, conjugate views wouldn't have this problem. I thought about your solution to repair this problem. I think this sentence sums it up nicely:
We can describe my two approaches in terms of how they make predictions about this exchange:
I think you are advancing the following third proposal:
In this third proposal, we also have to decide whether or not you have view semantics (mutating x.conj() affects x) or clone semantics (mutating x.conj() has no effect on x). Due to the materialized x.conj(), view semantics would be difficult, although still possible (using version counters). But it is still very tricky. For example, in your example, if
Alas, I need this to work in C++ only XD. This is one place where you probably don't care, but for the good of the framework we need to make sure this is handled correctly.
Yes. This is something that I am worried about too. Right now, I think it is worth it, but mostly because I think the mathematicians in the audience will get really irritated at having to translate all of their code into using explicit conjugate flags. Chances are they'll just make some wrapper object to handle it... and if they're going to do that, we might as well do it properly for them in framework. I do want to reiterate, that I feel that lazy conjugate views (which are implemented in this PR) are a lot simpler than lazy conjugate tensors. Their primary downside is duplicated work materializing conjugate, but as you say, if in the long term we add fusion to most operators, you will no longer have duplicated work. (Also, I suspect one could always add a cache on top later as a performance optimization; though we do have to make a call on clone or view semantics sooner rather than later.) What do you think? |
|
Thanks, your explanation for conjugate view tensors and conjugate lazy tensors helped me.
I thought about something like
Sure, I can share some pointers to our code, but it is numpy code and the use case is audio/speech:
I must say, I rarely think about inplace operations in NN code. Using no inplace operations worked for me until now.
Yes, I know. But I mainly write python code and I don't know much of the internals of PyTorch. So I can only guess how difficult it is to realize it in C++ and getting it working in a large framework (I try to make careful statements and I know not every python code can easily be translated, especially when threads and garbage collection is involved.).
At the moment, I must say, when I translate code to pytorch I have to do mayor changes. So explicit conjugate flags, would at least make it obvious, that the optimal implementation is used.
At the moment, I think using
When you add the cache, you arrive at my suggestion :), but with a better C++ implementation. |
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs Signed-off-by: Edward Z. Yang <[email protected]> [ghstack-poisoned]
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs ghstack-source-id: 9f12c93 Pull Request resolved: #44799 Signed-off-by: Edward Z. Yang <[email protected]>
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs Signed-off-by: Edward Z. Yang <[email protected]> [ghstack-poisoned]
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs ghstack-source-id: ccf7019 Pull Request resolved: #44799 Signed-off-by: Edward Z. Yang <[email protected]>
Based off of bdhirsch's prototype at #43702 Conjugate view tensor changes the semantics of x.conj() to return a view of the original tensor, rather than immediately performing the conjugation (aka, it implements a form of lazy evaluation for conjugation). This permits conjugations to be fused with later operations that occur; e.g., matrix multiply, where BLAS libraries typically all for a conjugate-transpose first argument. The implementation strategy of this PR: - Maintain conjugation status as a dispatch key in the dispatch key set (TODO: audit all propagation of key_set to ensure that we don't propagate conjugation status when we shouldn't) - Implement a few new functions for some other operations: - x.is_conj() tells you whether or not the conjugate bit is or not. If not x.is_conj(), what's in memory reflects the semantic content of the tensor; if x.is_conj(), the imaginary values are negated. This is implemented as a fastpath function similar to numel(); see #30774 - x.view_as_real_physical() lets you view the underlying memory of a possibly conjugated tensor as its actual stored representation. To interpret this result correctly, you will typically also have to query x.is_conj() - x.conj_physical_() will inplace flip the sign of the imaginary piece of the tensor. This is a fairly useful operation, especially when getting conjugations to line up correctly. TODO: Consider renaming this to just conj_() - x.resolve_conj() plays of contiguous() but for conjugate views: if x.is_conj(), it will return a new tensor with the conjugation done in memory. - Disable view_as_real() for conjugated tensors. This change is necessary because conjugation is a concept only for complex tensors; on real tensors, there is no way to say "view this memory but negate every Nth element". Most notably, this means x.imag no longer works on conjugated tensors; instead you should do x.resolve_conj().imag instead. - Implement a backend fallback on the conjugate dispatch key. This fallback works for all non-view operations. The general recipe is to take each tensor argument that is_conj() and resolve_conj() them. If an argument is a mutable inplace argument, we instead conj_physical_() inplace, set_conj(False) and then do our operation; on the way out, we then invert this operation. TODO: User-level docs Signed-off-by: Edward Z. Yang <[email protected]> [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) TODO: 1. conjugate view RFC [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) TODO: 1. conjugate view RFC [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Differential Revision: [D28227315](https://our.internmc.facebook.com/intern/diff/D28227315) [ghstack-poisoned]
Summary: Pull Request resolved: #54987 Based off of ezyang (#44799) and bdhirsh (#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Test Plan: Imported from OSS Reviewed By: VitalyFedyunin Differential Revision: D28227315 Pulled By: anjali411 fbshipit-source-id: acab9402b9d6a970c6d512809b627a290c8def5f
Summary: Pull Request resolved: pytorch#54987 Based off of ezyang (pytorch#44799) and bdhirsh (pytorch#43702) 's prototype: Here's a summary of the changes in this PR: This PR adds a new dispatch key called Conjugate. This enables us to make conjugate operation a view and leverage the specialized library functions that fast path with the hermitian operation (conj + transpose). 1. Conjugate operation will now return a view with conj bit (1) for complex tensors and returns self for non-complex tensors as before. This also means `torch.view_as_real` will no longer be a view on conjugated complex tensors and is hence disabled. To fill the gap, we have added `torch.view_as_real_physical` which would return the real tensor agnostic of the conjugate bit on the input complex tensor. The information about conjugation on the old tensor can be obtained by calling `.is_conj()` on the new tensor. 2. NEW API: a) `.conj()` -- now returning a view. b) `.conj_physical()` -- does the physical conjugate operation. If the conj bit for input was set, you'd get `self.clone()`, else you'll get a new tensor with conjugated value in its memory. c) `.conj_physical_()`, and `out=` variant d) `.resolve_conj()` -- materializes the conjugation. returns self if the conj bit is unset, else returns a new tensor with conjugated values and conj bit set to 0. e) `.resolve_conj_()` in-place version of (d) f) `view_as_real_physical` -- as described in (1), it's functionally same as `view_as_real`, just that it doesn't error out on conjugated tensors. g) `view_as_real` -- existing function, but now errors out on conjugated tensors. 3. Conjugate Fallback a) Vast majority of PyTorch functions would currently use this fallback when they are called on a conjugated tensor. b) This fallback is well equipped to handle the following cases: - functional operation e.g., `torch.sin(input)` - Mutable inputs and in-place operations e.g., `tensor.add_(2)` - out-of-place operation e.g., `torch.sin(input, out=out)` - Tensorlist input args - NOTE: Meta tensors don't work with conjugate fallback. 4. Autograd a) `resolve_conj()` is an identity function w.r.t. autograd b) Everything else works as expected. 5. Testing: a) All method_tests run with conjugate view tensors. b) OpInfo tests that run with conjugate views - test_variant_consistency_eager/jit - gradcheck, gradgradcheck - test_conj_views (that only run for `torch.cfloat` dtype) NOTE: functions like `empty_like`, `zero_like`, `randn_like`, `clone` don't propagate the conjugate bit. Follow up work: 1. conjugate view RFC 2. Add neg bit to re-enable view operation on conjugated tensors 3. Update linalg functions to call into specialized functions that fast path with the hermitian operation. Test Plan: Imported from OSS Reviewed By: VitalyFedyunin Differential Revision: D28227315 Pulled By: anjali411 fbshipit-source-id: acab9402b9d6a970c6d512809b627a290c8def5f
Stack from ghstack:
Based off of bdhirsch's prototype at #43702
Conjugate view tensor changes the semantics of x.conj() to return a view
of the original tensor, rather than immediately performing the
conjugation (aka, it implements a form of lazy evaluation for
conjugation). This permits conjugations to be fused with later
operations that occur; e.g., matrix multiply, where BLAS libraries
typically all for a conjugate-transpose first argument.
The implementation strategy of this PR:
Maintain conjugation status as a dispatch key in the dispatch
key set (TODO: audit all propagation of key_set to ensure that
we don't propagate conjugation status when we shouldn't)
Implement a few new functions for some other operations:
x.is_conj() tells you whether or not the conjugate bit is
or not. If not x.is_conj(), what's in memory reflects
the semantic content of the tensor; if x.is_conj(),
the imaginary values are negated. This is implemented
as a fastpath function similar to numel(); see
Natively Declarable Fast-path Functions #30774
x.view_as_real_physical() lets you view the underlying memory
of a possibly conjugated tensor as its actual stored
representation. To interpret this result correctly, you
will typically also have to query x.is_conj()
x.conj_physical_() will inplace flip the sign of the
imaginary piece of the tensor. This is a fairly useful
operation, especially when getting conjugations to line
up correctly. TODO: Consider renaming this to just conj_()
x.resolve_conj() plays of contiguous() but for conjugate views:
if x.is_conj(), it will return a new tensor with the conjugation
done in memory.
Disable view_as_real() for conjugated tensors. This change is
necessary because conjugation is a concept only for complex
tensors; on real tensors, there is no way to say "view this
memory but negate every Nth element". Most notably, this
means x.imag no longer works on conjugated tensors; instead
you should do x.resolve_conj().imag instead.
Implement a backend fallback on the conjugate dispatch key.
This fallback works for all non-view operations. The general
recipe is to take each tensor argument that is_conj() and
resolve_conj() them. If an argument is a mutable inplace argument,
we instead conj_physical_() inplace, set_conj(False) and then
do our operation; on the way out, we then invert this operation.
TODO: User-level docs
Signed-off-by: Edward Z. Yang [email protected]