pytorch/caffe2/python/pybind_state_hip.cc
Jerry Zhang aebf3b47ae Remove template parameter from Tensor (#9939)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9939

Pull Request resolved: https://github.com/facebookresearch/weakly-supervised-action-detection/pull/13

Pull Request resolved: https://github.com/pytorch/translate/pull/166

Pull Request resolved: https://github.com/pytorch/pytorch/pull/9125

Closes https://github.com/pytorch/pytorch/pull/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: ezyang, houseroad

Differential Revision: D9024330

fbshipit-source-id: e0b8295d2dc6ebe2963383ded5af799ad17164ba
2018-07-27 10:56:39 -07:00

91 lines
2.8 KiB
C++

#define NO_IMPORT_ARRAY
#include "pybind_state.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "caffe2/core/hip/common_miopen.h"
#include "caffe2/core/hip/context_hip.h"
#include "caffe2/operators/hip/operator_fallback_hip.h"
namespace caffe2 {
namespace python {
REGISTER_HIP_OPERATOR(Python, GPUFallbackOp<PythonOp<CPUContext, false>>);
REGISTER_HIP_OPERATOR(
PythonGradient,
GPUFallbackOp<PythonGradientOp<CPUContext, false>>);
REGISTER_HIP_OPERATOR(PythonDLPack, PythonOp<HIPContext, true>);
REGISTER_HIP_OPERATOR(PythonDLPackGradient, PythonGradientOp<HIPContext, true>);
REGISTER_BLOB_FEEDER(HIP, TensorFeeder<HIPContext>);
namespace py = pybind11;
void addHIPGlobalMethods(py::module& m) {
m.def("num_hip_devices", &NumHipDevices);
m.def("set_default_gpu_id", &SetDefaultGPUID);
m.def("get_default_gpu_id", &GetDefaultGPUID);
m.def("get_hip_version", &HipVersion);
m.def("get_miopen_version", &miopenCompiledVersion);
m.def("get_hip_peer_access_pattern", []() {
std::vector<std::vector<bool>> pattern;
CAFFE_ENFORCE(caffe2::GetHipPeerAccessPattern(&pattern));
return pattern;
});
m.def("get_device_properties", [](int deviceid) {
auto& prop = GetDeviceProperty(deviceid);
std::map<std::string, py::object> obj;
obj["name"] = py::cast(prop.name);
obj["major"] = py::cast(prop.major);
obj["minor"] = py::cast(prop.minor);
return obj;
});
};
void addHIPObjectMethods(py::module& m) {
py::class_<DLPackWrapper<HIPContext>>(m, "DLPackTensorHIP")
.def_property_readonly(
"data",
[](DLPackWrapper<HIPContext>* t) -> py::object {
CAFFE_ENFORCE_EQ(
t->device_option.device_type(),
HIP,
"Expected HIP device option for HIP tensor");
return t->data();
},
"Return DLPack tensor with tensor's data.")
.def(
"feed",
[](DLPackWrapper<HIPContext>* t, py::object obj) {
CAFFE_ENFORCE_EQ(
t->device_option.device_type(),
HIP,
"Expected HIP device option for HIP tensor");
t->feed(obj);
},
"Copy data from given DLPack tensor into this tensor.")
.def_property_readonly(
"_shape",
[](const DLPackWrapper<HIPContext>& t) { return t.tensor->dims(); })
.def(
"_reshape",
[](DLPackWrapper<HIPContext>* t, std::vector<TIndex> dims) {
t->tensor->Resize(dims);
});
}
PYBIND11_MODULE(caffe2_pybind11_state_hip, m) {
m.doc() = "pybind11 stateful interface to Caffe2 workspaces - GPU edition";
addGlobalMethods(m);
addHIPGlobalMethods(m);
addObjectMethods(m);
addHIPObjectMethods(m);
}
} // namespace python
} // namespace caffe2