mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-15 21:00:47 +00:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/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
91 lines
2.5 KiB
C++
91 lines
2.5 KiB
C++
#include <memory>
|
|
#include <vector>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "caffe2/core/context_gpu.h"
|
|
#include "caffe2/operators/batch_matmul_op.h"
|
|
|
|
namespace caffe2 {
|
|
namespace {
|
|
|
|
class BatchMatMulOpGPUTest : public testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
if (!HasCudaGPU()) {
|
|
return;
|
|
}
|
|
option_.set_device_type(PROTO_CUDA);
|
|
cuda_context_ = make_unique<CUDAContext>(option_);
|
|
def_.set_name("test");
|
|
def_.set_type("BatchMatMul");
|
|
def_.add_input("A");
|
|
def_.add_input("B");
|
|
def_.add_output("Y");
|
|
def_.mutable_device_option()->set_device_type(PROTO_CUDA);
|
|
}
|
|
|
|
void AddConstInput(
|
|
const std::vector<TIndex>& dims,
|
|
const float value,
|
|
const string& name) {
|
|
Blob* blob = ws_.CreateBlob(name);
|
|
auto* tensor = blob->GetMutableTensor(CUDA);
|
|
tensor->Resize(dims);
|
|
math::Set<float, CUDAContext>(
|
|
tensor->size(),
|
|
value,
|
|
tensor->template mutable_data<float>(),
|
|
cuda_context_.get());
|
|
}
|
|
|
|
void VerifyOutput(const std::vector<TIndex>& dims, const float value) const {
|
|
const Blob* Y_blob = ws_.GetBlob("Y");
|
|
ASSERT_NE(nullptr, Y_blob);
|
|
const auto& Y = Y_blob->Get<Tensor>();
|
|
Tensor Y_cpu(Y, CPU);
|
|
const auto& Y_dims = Y_cpu.dims();
|
|
ASSERT_EQ(dims.size(), Y_dims.size());
|
|
for (std::size_t i = 0; i < dims.size(); ++i) {
|
|
ASSERT_EQ(dims[i], Y_dims[i]);
|
|
}
|
|
for (int i = 0; i < Y_cpu.size(); ++i) {
|
|
EXPECT_FLOAT_EQ(value, Y_cpu.data<float>()[i]);
|
|
}
|
|
}
|
|
|
|
DeviceOption option_;
|
|
std::unique_ptr<CUDAContext> cuda_context_;
|
|
Workspace ws_;
|
|
OperatorDef def_;
|
|
};
|
|
|
|
TEST_F(BatchMatMulOpGPUTest, BatchMatMulOpGPUNormalTest) {
|
|
if (!HasCudaGPU()) {
|
|
return;
|
|
}
|
|
AddConstInput(std::vector<TIndex>{3, 5, 10}, 1.0f, "A");
|
|
AddConstInput(std::vector<TIndex>{3, 10, 6}, 1.0f, "B");
|
|
std::unique_ptr<OperatorBase> op(CreateOperator(def_, &ws_));
|
|
ASSERT_NE(nullptr, op);
|
|
ASSERT_TRUE(op->Run());
|
|
VerifyOutput(std::vector<TIndex>{3, 5, 6}, 10.0f);
|
|
}
|
|
|
|
TEST_F(BatchMatMulOpGPUTest, BatchMatMulOpGPUBroadcastTest) {
|
|
if (!HasCudaGPU()) {
|
|
return;
|
|
}
|
|
auto* arg = def_.add_arg();
|
|
arg->set_name("broadcast");
|
|
arg->set_i(1);
|
|
AddConstInput(std::vector<TIndex>{3, 5, 10}, 1.0f, "A");
|
|
AddConstInput(std::vector<TIndex>{2, 3, 10, 6}, 1.0f, "B");
|
|
std::unique_ptr<OperatorBase> op(CreateOperator(def_, &ws_));
|
|
ASSERT_NE(nullptr, op);
|
|
ASSERT_TRUE(op->Run());
|
|
VerifyOutput(std::vector<TIndex>{2, 3, 5, 6}, 10.0f);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace caffe2
|