Skip to content

Make named tensor implementations more robust#26968

Closed
zou3519 wants to merge 1 commit intogh/zou3519/193/basefrom
gh/zou3519/193/head
Closed

Make named tensor implementations more robust#26968
zou3519 wants to merge 1 commit intogh/zou3519/193/basefrom
gh/zou3519/193/head

Conversation

@zou3519
Copy link
Copy Markdown
Contributor

@zou3519 zou3519 commented Sep 27, 2019

Stack from ghstack:

To make implementations of an operator more robust, we should have a
separate "named area" where name propagation happens and an "unnamed
area" where the implementation is. Right now, many functions are
implemented without an "unnamed area". The problem with that is that if
someone modifies the implementation, it is very easy to break
namedtensor support by using a helper function that does not propagate
names correctly. The test coverage for named tensors is also
insufficient to catch such breakages.

This PR modifies some named tensor implementations to have separate
"named area" and "unnamed area". The following implementations were
changed:

  • dropout, softmax, log_softmax, bernoulli
  • dot, mm, addmm, addmv, mv

Test Plan:

  • [namedtensor ci]

Differential Revision: D17627920

To make implementations of an operator more robust, we should have a
separate "named area" where name propagation happens and an "unnamed
area" where the implementation is. Right now, many functions are
implemented without an "unnamed area". The problem with that is that if
someone modifies the implementation, it is very easy to break
namedtensor support by using a helper function that does not propagate
names correctly. The test coverage for named tensors is also
insufficient to catch such breakages.

This PR modifies some named tensor implementations to have separate
"named area" and "unnamed area". The following implementations were
changed:
- dropout, softmax, log_softmax, bernoulli
- dot, mm, addmm, addmv, mv

Test Plan:
- [namedtensor ci]
@pytorchbot pytorchbot added module: cpu CPU specific problem (e.g., perf, algorithm) module: cublas Problem related to cublas support module: cuda Related to torch.cuda, and CUDA support in general module: operators labels Sep 27, 2019
zou3519 added a commit that referenced this pull request Sep 27, 2019
To make implementations of an operator more robust, we should have a
separate "named area" where name propagation happens and an "unnamed
area" where the implementation is. Right now, many functions are
implemented without an "unnamed area". The problem with that is that if
someone modifies the implementation, it is very easy to break
namedtensor support by using a helper function that does not propagate
names correctly. The test coverage for named tensors is also
insufficient to catch such breakages.

This PR modifies some named tensor implementations to have separate
"named area" and "unnamed area". The following implementations were
changed:
- dropout, softmax, log_softmax, bernoulli
- dot, mm, addmm, addmv, mv

Test Plan:
- [namedtensor ci]

ghstack-source-id: f758aba
Pull Request resolved: #26968
@zou3519 zou3519 requested a review from nairbv September 27, 2019 14:31
@zou3519 zou3519 added this to the 1.3 milestone Sep 27, 2019
}
}();
#ifdef BUILD_NAMEDTENSOR
namedinference::propagate_names(result, input_);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this adding named tensor support that didn't previously exist? from the description it sounds like the intent is just to refactor but I don't know where this is getting moved from.

Does at::_softmax have propagated names without the name guard? if so should we remove it since we're name guarding it anyway?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at::_softmax propagates names "coincidentally" because it calls empty_like under the hood. Named tensor support for softmax existed before this PR but still exists after this PR.

We need to explicitly propagate names now because the returned tensor from the NoNamesGuard block has no names.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I was expecting to see a propagate_names removed from _softmax but that makes sense

Tensor converted = dtype.has_value() ? input_.toType(dtype.value()) : input_;
return at::_softmax(converted, dim_, false);
}
}();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, does c++ have other ways to do this that don't require creating a function object? (e.g. in some other languages I could just have result = { whatever; }).

Are there cleaner options that only work in newer versions of C++?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of. The alternative is to create a new scope with { } and assign to variable outside the scope. This is a little less nice because sometimes there are multiple return statements in a block.

Copy link
Copy Markdown
Collaborator

@nairbv nairbv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to update the description if not just a refactor but also changing behavior for softmax / log_softmax, otherwise looks good.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Sep 27, 2019
Summary:
Pull Request resolved: pytorch/pytorch#26968

To make implementations of an operator more robust, we should have a
separate "named area" where name propagation happens and an "unnamed
area" where the implementation is. Right now, many functions are
implemented without an "unnamed area". The problem with that is that if
someone modifies the implementation, it is very easy to break
namedtensor support by using a helper function that does not propagate
names correctly. The test coverage for named tensors is also
insufficient to catch such breakages.

This PR modifies some named tensor implementations to have separate
"named area" and "unnamed area". The following implementations were
changed:
- dropout, softmax, log_softmax, bernoulli
- dot, mm, addmm, addmv, mv

Test Plan: - [namedtensor ci]

Differential Revision: D17627920

Pulled By: zou3519

fbshipit-source-id: 9300ac3962219b1fcd8c4c8705a2cea6f8c9d23d
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@zou3519 merged this pull request in 2cdfec6.

@facebook-github-bot facebook-github-bot deleted the gh/zou3519/193/head branch October 28, 2019 22:24
pdlive215 pushed a commit to pdlive215/pytorch that referenced this pull request Nov 27, 2019
Summary:
Pull Request resolved: pytorch#26968

To make implementations of an operator more robust, we should have a
separate "named area" where name propagation happens and an "unnamed
area" where the implementation is. Right now, many functions are
implemented without an "unnamed area". The problem with that is that if
someone modifies the implementation, it is very easy to break
namedtensor support by using a helper function that does not propagate
names correctly. The test coverage for named tensors is also
insufficient to catch such breakages.

This PR modifies some named tensor implementations to have separate
"named area" and "unnamed area". The following implementations were
changed:
- dropout, softmax, log_softmax, bernoulli
- dot, mm, addmm, addmv, mv

Test Plan: - [namedtensor ci]

Differential Revision: D17627920

Pulled By: zou3519

fbshipit-source-id: 9300ac3962219b1fcd8c4c8705a2cea6f8c9d23d
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#26968

To make implementations of an operator more robust, we should have a
separate "named area" where name propagation happens and an "unnamed
area" where the implementation is. Right now, many functions are
implemented without an "unnamed area". The problem with that is that if
someone modifies the implementation, it is very easy to break
namedtensor support by using a helper function that does not propagate
names correctly. The test coverage for named tensors is also
insufficient to catch such breakages.

This PR modifies some named tensor implementations to have separate
"named area" and "unnamed area". The following implementations were
changed:
- dropout, softmax, log_softmax, bernoulli
- dot, mm, addmm, addmv, mv

Test Plan: - [namedtensor ci]

Differential Revision: D17627920

Pulled By: zou3519

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

Labels

Merged module: cpu CPU specific problem (e.g., perf, algorithm) module: cublas Problem related to cublas support module: cuda Related to torch.cuda, and CUDA support in general

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants