Skip to content

Conversation

@t-vi
Copy link
Collaborator

@t-vi t-vi commented Oct 8, 2018

Fixes: #12160

Copy link
Collaborator

@ssnl ssnl left a 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?

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.

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.

- 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.

This comment was marked as off-topic.

@t-vi
Copy link
Collaborator Author

t-vi commented Oct 8, 2018

On the naming: Indeed, I agree that matrix_diag is a not-so-nice name. I would have called that diagonal_tensor if I had to come up with my own name. As such I'll be more than happy to change the name.

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.
When I implemented diagonal, I thought it was strange to always put the diagonal at the end, but that is what numpy did. I haven't, however, heard anyone complain, so adding another parameter might be overthinking the applicability of this function. One could always add a "source dimension" keyword argument later (unless that does bad things to the C++ API). Again, I'll gladly add an argument (and would suggest to do the same in torch.diagonal).

@apaszke
Copy link
Contributor

apaszke commented Oct 8, 2018

Yeah, matrix_diag doesn't seem like a good name if this is supposed to be a general purpose function. Some names from me:

  • diagfill(...) - kind of like diagflat, except doesn't flatten the input?
  • as_diagonal(...) - might be confusing, because it looks a bit if it's making the input diagonal

Is there really no term for making a diagonal matrix out of a vector? 😕

@t-vi
Copy link
Collaborator Author

t-vi commented Oct 8, 2018

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 diagonal_embedding would sound like that to me.

@apaszke
Copy link
Contributor

apaszke commented Oct 8, 2018

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 diag_embed/embed_diag?

@ssnl
Copy link
Collaborator

ssnl commented Oct 8, 2018

One vote from me for embed_diag, although keeping the same diag_ prefix would be nice.

@apaszke
Copy link
Contributor

apaszke commented Oct 8, 2018

Yeah, diag_* is definitely good for discoverability, but it's a much worse method name 😕 Can't really say which one is better, but I think diag_embed is better. We can't expect people to go through 400 functions we provide.

@fmassa
Copy link
Member

fmassa commented Oct 8, 2018

About the naming: I suppose numpy doesn't have an equivalent function? FWIW matrix_diag is the name if the function in TF

@apaszke
Copy link
Contributor

apaszke commented Oct 8, 2018

matrix_diag is really not a good name... The function works with any tensors, and the name doesn't make it clear if it takes the diagonal, or fills the diagonal of a higher-dim tensor.

t-vi added 2 commits October 14, 2018 22:43
Also improve doc and example.

Thank you all for your input!
@t-vi t-vi changed the title Add matrix_diag to ATen and torch Add diag_embed to ATen and torch Oct 14, 2018
@t-vi
Copy link
Collaborator Author

t-vi commented Oct 14, 2018

So I renamed to diag_embed.

@bddppq
Copy link
Contributor

bddppq commented Oct 15, 2018

@pytorchbot retest this please

@t-vi
Copy link
Collaborator Author

t-vi commented Oct 15, 2018

I cannot quite understand the error on rocm:

22:36:15 test_ByteTensor_mode (__main__.TestCuda) ... python: symbol lookup error: /var/lib/jenkins/.local/lib/python2.7/site-packages/torch/lib/libcaffe2_hip.so: undefined symbol: THCudaByteTensor_mode

For the other error, I think I stayed clear of the networking code with diag_embed, but:

Oct 14 21:47:04     store = TCPStore(master_addr, master_port, start_daemon)
Oct 14 21:47:04 RuntimeError: Address already in use

I'd think the PR is OK unless you say it isn't.

Copy link
Collaborator

@ssnl ssnl left a 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.

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.

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.

@t-vi
Copy link
Collaborator Author

t-vi commented Oct 24, 2018

I think both CI failures are unrelated to the patch.

@t-vi
Copy link
Collaborator Author

t-vi commented Oct 30, 2018

@ssnl @apaszke anything I can do to move this forward?

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

Copy link
Collaborator

@ssnl ssnl left a 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....

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.

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.

Copy link
Collaborator

@ssnl ssnl left a 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!

@ssnl
Copy link
Collaborator

ssnl commented Nov 4, 2018

hmm could you rebase? It seems that the base commit is in bad state. Thanks!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Nov 5, 2018
Summary:
Fixes: #12160
Pull Request resolved: pytorch/pytorch#12447

Differential Revision: D12916234

Pulled By: SsnL

fbshipit-source-id: 512a04efb0c2e0a54295b857a61be66c3aae13da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature request] Batched (n-1)-D to n-D matrix_diag

8 participants