2016-05-13 21:43:48 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include "caffe2/core/operator.h"
|
|
|
|
|
#include "caffe2/operators/operator_fallback_gpu.h"
|
2017-03-28 07:38:28 +00:00
|
|
|
#include <gtest/gtest.h>
|
2016-05-13 21:43:48 +00:00
|
|
|
|
|
|
|
|
namespace caffe2 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IncrementByOneOp final : public Operator<CPUContext> {
|
|
|
|
|
public:
|
2019-03-06 21:47:27 +00:00
|
|
|
template <class... Args>
|
|
|
|
|
explicit IncrementByOneOp(Args&&... args)
|
|
|
|
|
: Operator<CPUContext>(std::forward<Args>(args)...) {}
|
2019-02-14 04:51:55 +00:00
|
|
|
bool RunOnDevice() override {
|
2016-05-13 21:43:48 +00:00
|
|
|
const auto& in = Input(0);
|
2018-12-13 20:42:58 +00:00
|
|
|
|
|
|
|
|
auto* out = Output(0, in.sizes(), at::dtype<float>());
|
2016-05-13 21:43:48 +00:00
|
|
|
const float* in_data = in.template data<float>();
|
|
|
|
|
float* out_data = out->template mutable_data<float>();
|
2018-10-26 22:27:23 +00:00
|
|
|
for (int i = 0; i < in.numel(); ++i) {
|
2016-05-13 21:43:48 +00:00
|
|
|
out_data[i] = in_data[i] + 1.f;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OPERATOR_SCHEMA(IncrementByOne)
|
|
|
|
|
.NumInputs(1).NumOutputs(1).AllowInplace({{0, 0}});
|
|
|
|
|
|
|
|
|
|
REGISTER_CPU_OPERATOR(IncrementByOne, IncrementByOneOp);
|
2018-09-06 22:01:07 +00:00
|
|
|
REGISTER_CUDA_OPERATOR(IncrementByOne, GPUFallbackOp);
|
2016-05-13 21:43:48 +00:00
|
|
|
|
|
|
|
|
TEST(OperatorFallbackTest, IncrementByOneOp) {
|
|
|
|
|
OperatorDef op_def = CreateOperatorDef(
|
|
|
|
|
"IncrementByOne", "", vector<string>{"X"},
|
|
|
|
|
vector<string>{"X"});
|
|
|
|
|
Workspace ws;
|
2018-09-23 01:07:38 +00:00
|
|
|
Tensor source_tensor(vector<int64_t>{2, 3}, CPU);
|
2016-05-13 21:43:48 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
|
source_tensor.mutable_data<float>()[i] = i;
|
|
|
|
|
}
|
2018-09-25 18:26:48 +00:00
|
|
|
BlobGetMutableTensor(ws.CreateBlob("X"), CPU)->CopyFrom(source_tensor);
|
2016-05-13 21:43:48 +00:00
|
|
|
unique_ptr<OperatorBase> op(CreateOperator(op_def, &ws));
|
|
|
|
|
EXPECT_TRUE(op.get() != nullptr);
|
|
|
|
|
EXPECT_TRUE(op->Run());
|
|
|
|
|
const TensorCPU& output = ws.GetBlob("X")->Get<TensorCPU>();
|
2018-11-07 15:27:47 +00:00
|
|
|
EXPECT_EQ(output.dim(), 2);
|
2018-11-05 15:23:35 +00:00
|
|
|
EXPECT_EQ(output.size(0), 2);
|
|
|
|
|
EXPECT_EQ(output.size(1), 3);
|
2016-05-13 21:43:48 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
|
EXPECT_EQ(output.data<float>()[i], i + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(OperatorFallbackTest, GPUIncrementByOneOp) {
|
2016-07-21 17:16:42 +00:00
|
|
|
if (!HasCudaGPU()) return;
|
2016-05-13 21:43:48 +00:00
|
|
|
OperatorDef op_def = CreateOperatorDef(
|
|
|
|
|
"IncrementByOne", "", vector<string>{"X"},
|
|
|
|
|
vector<string>{"X"});
|
caffe2::DeviceType -> at::DeviceType (#11254)
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
2018-09-05 23:13:54 +00:00
|
|
|
op_def.mutable_device_option()->set_device_type(PROTO_CUDA);
|
2016-05-13 21:43:48 +00:00
|
|
|
Workspace ws;
|
2018-09-23 01:07:38 +00:00
|
|
|
Tensor source_tensor(vector<int64_t>{2, 3}, CPU);
|
2016-05-13 21:43:48 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
|
source_tensor.mutable_data<float>()[i] = i;
|
|
|
|
|
}
|
2018-09-25 18:26:48 +00:00
|
|
|
BlobGetMutableTensor(ws.CreateBlob("X"), CUDA)->CopyFrom(source_tensor);
|
2016-05-13 21:43:48 +00:00
|
|
|
unique_ptr<OperatorBase> op(CreateOperator(op_def, &ws));
|
|
|
|
|
EXPECT_TRUE(op.get() != nullptr);
|
|
|
|
|
EXPECT_TRUE(op->Run());
|
|
|
|
|
const TensorCUDA& output = ws.GetBlob("X")->Get<TensorCUDA>();
|
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 17:50:54 +00:00
|
|
|
Tensor output_cpu(output, CPU);
|
2018-11-07 15:27:47 +00:00
|
|
|
EXPECT_EQ(output.dim(), 2);
|
2018-11-05 15:23:35 +00:00
|
|
|
EXPECT_EQ(output.size(0), 2);
|
|
|
|
|
EXPECT_EQ(output.size(1), 3);
|
2016-05-13 21:43:48 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
|
EXPECT_EQ(output_cpu.data<float>()[i], i + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace caffe2
|