Skip to content
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

[pt1][tensor] caffe2::DeviceType -> at::DeviceType #11254

Closed
wants to merge 3 commits into from

Conversation

jerryzh168
Copy link
Contributor

@jerryzh168 jerryzh168 commented Sep 5, 2018

[pt1][tensor] caffe2::DeviceType -> at::DeviceType

Previously we use DeviceType in caffe2.proto directly, but it's an enum and have implicit conversion to int, which does not have type safety, e.g. we have to explicitly check for a device type is valid in event.h:

template <int d>
struct EventCreateFunctionRegisterer {
  explicit EventCreateFunctionRegisterer(EventCreateFunction f) {
    static_assert(d < MaxDeviceTypes, "");
    Event::event_creator_[d] = f;
  }
};

at::DeviceType is an enum class, and it does not have implicit conversion to int, and provides better type safety guarantees. In this diff we have done the following refactor(taking CPU as an example):

1. caffe2::DeviceType → caffe2::DeviceTypeProto
2. caffe2::CPU → caffe2::PROTO_CPU
3. caffe2::DeviceType = at::DeviceType
4. caffe2::CPU = at::DeviceType::CPU

codemod -d caffe2/caffe2 --extensions h,cc,cpp 'device_type(), ' 'device_type(), PROTO_'

  • some manual changes

In short, after this diff, in c++, caffe2::CPU refers to the at::DeviceType::CPU and the old proto caffe2::CPU will be caffe2::PROTO_CPU.
In python side, we have a temporary workaround that alias caffe2_pb2.CPU = caffe2_pb2.PROOT_CPU to make the change easier to review and this will be removed later.

Differential Revision: D9545704

Differential Revision: D9545704
Differential Version: 56904177
Differential Revision: D9545704
Differential Version: 56924075
@@ -222,17 +222,18 @@ class CAFFE2_API TensorImpl : public c10::intrusive_ptr_target {
if (size() > 0) {
if (storage_.dtype().copy()) {
CAFFE_ENFORCE(
GetDeviceType() == CPU,
GetDeviceType() == DeviceType::CPU,

This comment was marked as off-topic.

This comment was marked as off-topic.

@@ -0,0 +1,12 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from caffe2.proto import caffe2_pb2
# TODO: refactor & remove the following alias

This comment was marked as off-topic.

Differential Revision: D9545704
Differential Version: 56965830
@jerryzh168 jerryzh168 changed the title [wip][pt1][tensor] caffe2::DeviceType -> at::DeviceType [pt1][tensor] caffe2::DeviceType -> at::DeviceType Sep 5, 2018
PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
Summary:
Pull Request resolved: pytorch#11254
Previously we use DeviceType in caffe2.proto directly, but it's an `enum` and have implicit conversion to int, which does not have type safety, e.g. we have to explicitly check for a device type is valid in event.h:
```
template <int d>
struct EventCreateFunctionRegisterer {
  explicit EventCreateFunctionRegisterer(EventCreateFunction f) {
    static_assert(d < MaxDeviceTypes, "");
    Event::event_creator_[d] = f;
  }
};
```
at::DeviceType is an `enum class`, and it does not have implicit conversion to int, and provides better type safety guarantees. In this diff we have done the following refactor(taking CPU as an example):

    1. caffe2::DeviceType → caffe2::DeviceTypeProto
    2. caffe2::CPU → caffe2::PROTO_CPU
    3. caffe2::DeviceType = at::DeviceType
    4. caffe2::CPU = at::DeviceType::CPU

codemod -d caffe2/caffe2 --extensions h,cc,cpp 'device_type\(\), ' 'device_type(), PROTO_'
+ some manual changes

In short, after this diff, in c++, caffe2::CPU refers to the at::DeviceType::CPU and the old proto caffe2::CPU will be caffe2::PROTO_CPU.
In python side, we have a temporary workaround that alias `caffe2_pb2.CPU = caffe2_pb2.PROOT_CPU` to make the change easier to review and this will be removed later.

Reviewed By: ezyang

Differential Revision: D9545704

fbshipit-source-id: 461a28a4ca74e616d3ee183a607078a717fd38a7
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants