Remove template parameter from Tensor#9125
Closed
jerryzh168 wants to merge 1 commit intopytorch:masterfrom
Closed
Conversation
3de1c30 to
fd8cad5
Compare
fd8cad5 to
abb67ea
Compare
abb67ea to
0cf6385
Compare
0cf6385 to
2b15f8d
Compare
2b15f8d to
e0cae40
Compare
e0cae40 to
dde3ef8
Compare
dde3ef8 to
4bb7cc7
Compare
4bb7cc7 to
4a9eab9
Compare
4a9eab9 to
79d12c8
Compare
79d12c8 to
0d923ff
Compare
0d923ff to
c591917
Compare
c591917 to
4c8056a
Compare
4c8056a to
5d842f8
Compare
5d842f8 to
26a9c6e
Compare
26a9c6e to
8c320fb
Compare
1b3070c to
84bcc2b
Compare
Contributor
Author
|
@pytorchbot retest this please |
84bcc2b to
c997c28
Compare
c997c28 to
44e2026
Compare
44e2026 to
1409f26
Compare
1409f26 to
99383ed
Compare
99383ed to
7f0b736
Compare
7f0b736 to
5290cdf
Compare
jerryzh168
added a commit
to jerryzh168/translate
that referenced
this pull request
Jul 23, 2018
Summary: Pull Request resolved: pytorch/pytorch#9125 Closes pytorch/pytorch#9125 Use inheritance for polymorphism, and remove template parameter This is to change the templating in call sites, the core implementations will change later - Added a `static_context_` field, which stores a pointer to the per-device singleton `StaticContext`, e.g. `CPUStaticContext`, `CUDAStaticContext` etc. - Default constructor is deleted. - All constructor of `Tensor` are pass in an extra argument `DeviceType` that indicates the type we want the tensor to have. except for `Tensor(const vector<TIndex>& dims, const vector<T>& values, BaseContext* context)`, where we use `context` to specify the `DeviceType` of the `Tensor`. - constructor `Tensor(const Tensor& src, ContextForCopy* context)` is removed, since with the templated being removed, we have no way of knowing what the type we want the target Tensor to have now, it is changed to `Tensor(const Tensor& src, DeviceType device_type)`, where `device_type` is the device type we want the target tensor to have. - Changed semantics of CopyFrom a bit, previously, it is `CopyFrom(const Tensor<SrcContext>& src, ContextForCopy* context)` the second argument is used for calling the templated Copy functions. This is changed to `CopyFrom(const Tensor& src, BaseContext* context = nullptr)`, when the second argument is not provided, we'll use the context from source tensor, when it is provided, we'll need to make sure it has the same type as source tensor. Also, in case that we are copying from a cpu tensor to a non-cpu tensor, we'll need to create a non-cpu context to perform the copy since CPU context don't know how to do the copy from CPU to non-CPU tensor. Note: Some changes are postponed just to keep this diff a bit smaller. Please see `TODO`s. Reviewed By: dzhulgakov Differential Revision: D8121878 fbshipit-source-id: 0964bde5b27a12f2e0c52c7040f1214f15cf3231
5290cdf to
395256e
Compare
jerryzh168
added a commit
to jerryzh168/translate
that referenced
this pull request
Jul 24, 2018
Summary: Pull Request resolved: https://github.com/facebookresearch/weakly-supervised-action-detection/pull/13 Pull Request resolved: pytorch#166 Pull Request resolved: pytorch/pytorch#9125 Closes pytorch/pytorch#9125 Use inheritance for polymorphism, and remove template parameter This is to change the templating in call sites, the core implementations will change later Before Caffe2 Tensor class was compile-time fixed to bind to a particular device/context. With this change, we're making it a runtime property (stored inside the tensor), but preserve the same semantics. For example, one has to specify device type in order to create a Tensor - there are no uninitialized tensors. More specifically the changes are: 1. We added an extra argument *DeviceType* to most of the constructors of the tensor, e.g. (Tensor(DeviceType type)), 2. Semantics of constructor Tensor(const Tensor<SrcContext>& src, ContextForCopy* context); is changed, in this constructor, the second context is passed in to enable us to call the templated Copy function, it could be in a different context as source and target previously, now we'll enforce that the context should have same device type as src, if it is provided. 3. To preserve 'get-or-construct' semantics of Blob, we added specialized getter Blob::GetMutableTensor that verifies both that Blob contains a Tensor and that it's of a correct type 4. Specifically, Tensor type is not default-constructible any more (as we don't have unknown device tensors) and thus some of the code handling STL containers needs to change Note: Some changes are postponed just to keep this diff a bit smaller. Please see `TODO`s. Reviewed By: dzhulgakov Differential Revision: D8121878 fbshipit-source-id: a0ee4bf360c49bf75e85796de3a4eb21a4f6fc5f
395256e to
9c5b4c4
Compare
jerryzh168
added a commit
to jerryzh168/translate
that referenced
this pull request
Jul 24, 2018
Summary: Pull Request resolved: https://github.com/facebookresearch/weakly-supervised-action-detection/pull/13 Pull Request resolved: pytorch#166 Pull Request resolved: pytorch/pytorch#9125 Closes pytorch/pytorch#9125 Use inheritance for polymorphism, and remove template parameter This is to change the templating in call sites, the core implementations will change later Before Caffe2 Tensor class was compile-time fixed to bind to a particular device/context. With this change, we're making it a runtime property (stored inside the tensor), but preserve the same semantics. For example, one has to specify device type in order to create a Tensor - there are no uninitialized tensors. More specifically the changes are: 1. We added an extra argument *DeviceType* to most of the constructors of the tensor, e.g. (Tensor(DeviceType type)), 2. Semantics of constructor Tensor(const Tensor<SrcContext>& src, ContextForCopy* context); is changed, in this constructor, the second context is passed in to enable us to call the templated Copy function, it could be in a different context as source and target previously, now we'll enforce that the context should have same device type as src, if it is provided. 3. To preserve 'get-or-construct' semantics of Blob, we added specialized getter Blob::GetMutableTensor that verifies both that Blob contains a Tensor and that it's of a correct type 4. Specifically, Tensor type is not default-constructible any more (as we don't have unknown device tensors) and thus some of the code handling STL containers needs to change Note: Some changes are postponed just to keep this diff a bit smaller. Please see `TODO`s. Reviewed By: dzhulgakov Differential Revision: D8121878 fbshipit-source-id: 88f93b92b8f3716fc43e01252fc64a1d0f8b1097
Contributor
|
@pytorchbot retest this please |
1 similar comment
Contributor
Author
|
@pytorchbot retest this please |
Summary: Pull Request resolved: https://github.com/facebookresearch/weakly-supervised-action-detection/pull/13 Pull Request resolved: pytorch/translate#166 Pull Request resolved: pytorch#9125 Closes pytorch#9125 Use inheritance for polymorphism, and remove template parameter This is to change the templating in call sites, the core implementations will change later Before Caffe2 Tensor class was compile-time fixed to bind to a particular device/context. With this change, we're making it a runtime property (stored inside the tensor), but preserve the same semantics. For example, one has to specify device type in order to create a Tensor - there are no uninitialized tensors. More specifically the changes are: 1. We added an extra argument *DeviceType* to most of the constructors of the tensor, e.g. (Tensor(DeviceType type)), 2. Semantics of constructor Tensor(const Tensor<SrcContext>& src, ContextForCopy* context); is changed, in this constructor, the second context is passed in to enable us to call the templated Copy function, it could be in a different context as source and target previously, now we'll enforce that the context should have same device type as src, if it is provided. 3. To preserve 'get-or-construct' semantics of Blob, we added specialized getter Blob::GetMutableTensor that verifies both that Blob contains a Tensor and that it's of a correct type 4. Specifically, Tensor type is not default-constructible any more (as we don't have unknown device tensors) and thus some of the code handling STL containers needs to change Note: Some changes are postponed just to keep this diff a bit smaller. Please see `TODO`s. Reviewed By: xw285cornell Differential Revision: D8121878 fbshipit-source-id: 5597b009f6a546bed6e474d3507e672f8ea3ffe5
This was referenced Jul 27, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Use inheritance for polymorphism, and remove template parameter
This is to change the templating in call sites, the core implementations will change later
We removed template parameter from Tensor as an effort to Pytorch and Caffe2 backend unification.
New caffe2::Tensor class
Before Caffe2 Tensor class was compile-time fixed to bind to a particular device/context. With this change, we're making it a runtime property (stored inside the tensor), but preserve the same semantics. For example, one has to specify device type in order to create a Tensor - there are no uninitialized tensors. More specifically the changes are:
For details about how the tensor class look like right now, please see https://github.com/pytorch/pytorch/blob/master/caffe2/core/tensor.h
We did a massive codemod of the fbsource and hopefully covered majority of usages. However, there are probably a few that slipped through the contbuild. You might need to refactor your custom code according the following guide:
Context and StaticContext
We virtualized the functions in Context in order to achieve runtime polymorphism. The static functions has been split into a StaticContext class, which has one to one correspondence with DeviceType (CPU, CUDA etc.) and it has a global singleton object. We'll store a pointer to the singleton in Tensor to indicate the context of Tensor.
Original Context class contained routines to copy between different contexts, however, most of the time the use case is between three types of copies: CopyToCPU, CopyFromCPU and CopySameDevice, therefore, in the new Context, while we still keep the one general templated function that can copy between arbitrary contexts (CopyBytes), we rewrote the other uses of the Copy into these three special variants of copies that do not rely on template.
Differential Revision: D8121878