-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Add diag_embed to ATen and torch #12447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ssnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still a bit unsure about the name and the API.
On name: it works on higher rank tensors than 2. So matrix is a bit misleading.
On API: Wouldn't it be nice if users can specify which dimension is used as the diagonal?
torch/_torch_docs.py
Outdated
| Example:: | ||
| >>> x = torch.randn(2, 3) | ||
| tensor([[[ 1.5410, 0.0000, 0.0000], |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_torch_docs.py
Outdated
| r""" | ||
| matrix_diag(input, offset=0, dim1=-2, dim2=-1) -> Tensor | ||
| Creates a diagonal matrix from :attr:`input` by using the last |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| - func: matmul_out(Tensor result, Tensor self, Tensor other) -> Tensor | ||
|
|
||
| - func: matrix_diag(Tensor self, int64_t offset=0, int64_t dim1=-2, int64_t dim2=-1) -> Tensor | ||
| variants: function, method |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
On the naming: Indeed, I agree that matrix_diag is a not-so-nice name. I would have called that On the API: I think that diagonal and this function should match, this is why I took the last dimension. I would expect this to cover the by far most common case. |
|
Yeah,
Is there really no term for making a diagonal matrix out of a vector? 😕 |
I'm sure the category theory-inclined will love diagonal_morphism or diagonal_embedding, but we would have to keep people searching for these from looking at Wikipedia. Actually |
|
Yeah, "diagonal embedding" seems to be a good term (the morphism could really go both ways, so it remains unclear). It's only a bit verbose. How about |
|
One vote from me for |
|
Yeah, |
|
About the naming: I suppose numpy doesn't have an equivalent function? FWIW |
|
|
Also improve doc and example. Thank you all for your input!
|
So I renamed to diag_embed. |
|
@pytorchbot retest this please |
|
I cannot quite understand the error on rocm: For the other error, I think I stayed clear of the networking code with diag_embed, but: I'd think the PR is OK unless you say it isn't. |
ssnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks clean, as always. However, I do have some comments on the doc.
torch/_torch_docs.py
Outdated
| The size of the new matrix will be calculated to make the specified diagonal | ||
| of the size of the last input dimension. | ||
| Note that for :attr:`offset` other than :math:`0`, the order of :attr:`dim1` | ||
| and :attr:`dim2` matters, exchaning them is equivalent to changing the |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_torch_docs.py
Outdated
| r""" | ||
| diag_embed(input, offset=0, dim1=-2, dim2=-1) -> Tensor | ||
| Creates a tensor with entries on a diagonal from :attr:`input` by |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Thank you, @ssnl, for the suggestions!
|
I think both CI failures are unrelated to the patch. |
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soumith has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
ssnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am so sorry. I had comments typed but forgot to hit "submit review" button....
torch/_torch_docs.py
Outdated
| The size of the new matrix will be calculated to make the specified diagonal | ||
| of the size of the last input dimension. | ||
| Note that for :attr:`offset` other than :math:`0`, the order of :attr:`dim1` | ||
| and :attr:`dim2` matters, exchanging them is equivalent to changing the |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_torch_docs.py
Outdated
| diag_embed(input, offset=0, dim1=-2, dim2=-1) -> Tensor | ||
| Creates a tensor whose diagonals of certain 2D planes (specified by | ||
| :attr:`dim1` and :attr:`dim2`) are filled by :attr:`input`. By default, the |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
ssnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much!
|
hmm could you rebase? It seems that the base commit is in bad state. Thanks! |
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soumith has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ssnl has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Summary: Fixes: #12160 Pull Request resolved: pytorch/pytorch#12447 Differential Revision: D12916234 Pulled By: SsnL fbshipit-source-id: 512a04efb0c2e0a54295b857a61be66c3aae13da
Fixes: #12160