gpu sequence op step 1: clean headers

Summary:
@public

This has no functionality changes yet, only cleaning up the sequence_op file
so that the header is context-independent and I will implement the gpu parts
separately.

Reviewed By: pietern

Differential Revision: D4777140

fbshipit-source-id: 9b4aea6c36f06a64a53e235a125cd3477d54a045
This commit is contained in:
Yangqing Jia
2017-03-29 08:40:48 -07:00
committed by Facebook Github Bot
parent 58f7f2b441
commit 8efb762fcd
2 changed files with 384 additions and 352 deletions

View File

@ -1,37 +1,12 @@
#include "caffe2/operators/sequence_ops.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/tensor.h"
namespace caffe2 {
namespace {
class GatherPaddingOp final : public Operator<CPUContext> {
public:
GatherPaddingOp(const OperatorDef& operator_def, Workspace* ws)
: Operator(operator_def, ws),
startPaddingWidth_(
OperatorBase::GetSingleArgument<int>("padding_width", 1)),
endPaddingWidth_(
OperatorBase::GetSingleArgument<int>("end_padding_width", -1)) {
CAFFE_ENFORCE_GE(startPaddingWidth_, 0);
if (endPaddingWidth_ < 0) {
endPaddingWidth_ = startPaddingWidth_;
}
}
bool RunOnDevice() override {
if (startPaddingWidth_ == 0 && endPaddingWidth_ == 0) {
Output(0)->Resize(std::vector<TIndex>(0));
if (OutputSize() == 2) {
Output(1)->Resize(std::vector<TIndex>(0));
}
return true;
}
return DispatchHelper<TensorTypes<float, double, int, int64_t, bool>>::call(
this, Input(0));
}
template <typename T>
bool DoRunWithType() {
template <>
template <typename T>
bool GatherPaddingOp<CPUContext>::DoRunWithType() {
const auto& in = Input(0);
CAFFE_ENFORCE_GE(in.ndim(), 1);
const int32_t outer_size = in.dims()[0];
@ -51,18 +26,18 @@ class GatherPaddingOp final : public Operator<CPUContext> {
std::vector<TIndex> padShape(in.dims().begin() + 1, in.dims().end());
// output will contain accumulator over paddings
Output(0)->Resize(padShape);
T* padding_start_ptr = Output(0)->mutable_data<T>();
T* padding_start_ptr = Output(0)->template mutable_data<T>();
memset(padding_start_ptr, 0, sizeof(T) * block_size);
// if no end_padding is provided, assume it's the same as start_padding
T* padding_end_ptr = padding_start_ptr;
if (OutputSize() == 2) {
Output(1)->Resize(padShape);
padding_end_ptr = Output(1)->mutable_data<T>();
padding_end_ptr = Output(1)->template mutable_data<T>();
memset(padding_end_ptr, 0, sizeof(T) * block_size);
}
const auto* in_ptr = in.data<T>();
const auto* in_ptr = in.template data<T>();
int64_t total_length = 0;
for (int i = 0; i < lengths_size; ++i) {
// check total length consistency
@ -87,41 +62,11 @@ class GatherPaddingOp final : public Operator<CPUContext> {
}
}
return true;
}
}
private:
int startPaddingWidth_;
int endPaddingWidth_;
};
class RemovePaddingOp final : public Operator<CPUContext> {
public:
RemovePaddingOp(const OperatorDef& operator_def, Workspace* ws)
: Operator(operator_def, ws),
startPaddingWidth_(
OperatorBase::GetSingleArgument<int>("padding_width", 1)),
endPaddingWidth_(
OperatorBase::GetSingleArgument<int>("end_padding_width", -1)) {
CAFFE_ENFORCE_GE(startPaddingWidth_, 0);
if (endPaddingWidth_ < 0) {
endPaddingWidth_ = startPaddingWidth_;
}
}
bool RunOnDevice() override {
if (startPaddingWidth_ == 0 && endPaddingWidth_ == 0) {
Output(0)->CopyFrom(Input(0));
if (OutputSize() == 2) {
Output(1)->CopyFrom(Input(1));
}
return true;
}
return DispatchHelper<TensorTypes<float, double, int, int64_t, bool>>::call(
this, Input(0));
}
template <typename T>
bool DoRunWithType() {
template <>
template <typename T>
bool RemovePaddingOp<CPUContext>::DoRunWithType() {
const auto& in = Input(0);
CAFFE_ENFORCE_GE(in.ndim(), 1);
const int32_t outer_size = in.dims()[0];
@ -144,8 +89,8 @@ class RemovePaddingOp final : public Operator<CPUContext> {
out_dims[0] -= pad_width * lengths_size;
out->Resize(std::move(out_dims));
}
const auto* in_ptr = in.data<T>();
auto* out_ptr = out->mutable_data<T>();
const auto* in_ptr = in.template data<T>();
auto* out_ptr = out->template mutable_data<T>();
int64_t total_length = 0;
for (int i = 0; i < lengths_size; ++i) {
// check that total length is consistent
@ -170,41 +115,11 @@ class RemovePaddingOp final : public Operator<CPUContext> {
lengths_out->mutable_data<int32_t>(),
[pad_width](int32_t x) { return x - pad_width; });
return true;
}
}
private:
int startPaddingWidth_;
int endPaddingWidth_;
};
class AddPaddingOp final : public Operator<CPUContext> {
public:
AddPaddingOp(const OperatorDef& operator_def, Workspace* ws)
: Operator(operator_def, ws),
startPaddingWidth_(
OperatorBase::GetSingleArgument<int>("padding_width", 1)),
endPaddingWidth_(
OperatorBase::GetSingleArgument<int>("end_padding_width", -1)) {
CAFFE_ENFORCE_GE(startPaddingWidth_, 0);
if (endPaddingWidth_ < 0) {
endPaddingWidth_ = startPaddingWidth_;
}
}
bool RunOnDevice() override {
if (startPaddingWidth_ == 0 && endPaddingWidth_ == 0) {
Output(0)->CopyFrom(Input(0));
if (OutputSize() == 2) {
Output(1)->CopyFrom(Input(1));
}
return true;
}
return DispatchHelper<TensorTypes<float, double, int, int64_t, bool>>::call(
this, Input(0));
}
template <typename T>
bool DoRunWithType() {
template <>
template <typename T>
bool AddPaddingOp<CPUContext>::DoRunWithType() {
const auto& in = Input(0);
CAFFE_ENFORCE_GE(in.ndim(), 1);
const int32_t outer_size = in.dims()[0];
@ -229,12 +144,12 @@ class AddPaddingOp final : public Operator<CPUContext> {
if (InputSize() >= 3) {
auto& padding_start = Input(2);
CAFFE_ENFORCE_EQ(block_size, padding_start.size());
padding_start_ptr = padding_start.data<T>();
padding_start_ptr = padding_start.template data<T>();
}
if (InputSize() == 4) {
auto& padding_end = Input(3);
CAFFE_ENFORCE_EQ(block_size, padding_end.size());
padding_end_ptr = padding_end.data<T>();
padding_end_ptr = padding_end.template data<T>();
} else {
padding_end_ptr = padding_start_ptr;
}
@ -245,8 +160,8 @@ class AddPaddingOp final : public Operator<CPUContext> {
out_dims[0] += (startPaddingWidth_ + endPaddingWidth_) * lengths_size;
out->Resize(std::move(out_dims));
}
const auto* in_ptr = in.data<T>();
auto* out_ptr = out->mutable_data<T>();
const auto* in_ptr = in.template data<T>();
auto* out_ptr = out->template mutable_data<T>();
int64_t total_length = 0;
for (int i = 0; i < lengths_size; ++i) {
// check that total length is consistent
@ -291,23 +206,12 @@ class AddPaddingOp final : public Operator<CPUContext> {
lengths_out->mutable_data<int32_t>(),
[pad_width](int32_t x) { return x + pad_width; });
return true;
}
}
private:
int startPaddingWidth_;
int endPaddingWidth_;
};
using TLength = int32_t;
class PadEmptySamplesOp : public Operator<CPUContext> {
public:
PadEmptySamplesOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<CPUContext>(operator_def, ws) {}
bool RunOnDevice() override {
template <>
bool PadEmptySamplesOp<CPUContext>::RunOnDevice() {
auto& lengths = Input(0);
auto* lengthsPtr = lengths.template data<TLength>();
auto* lengthsPtr = lengths.template data<int32_t>();
CAFFE_ENFORCE(lengths.ndim() == 1, "LENGTH should be 1-D");
CAFFE_ENFORCE(InputSize() >= 1, "Input size must be no less than 1");
@ -322,7 +226,7 @@ class PadEmptySamplesOp : public Operator<CPUContext> {
}
out_lengths->Resize(lengths.size());
auto* outLengthsPtr = out_lengths->template mutable_data<TLength>();
auto* outLengthsPtr = out_lengths->template mutable_data<int32_t>();
for (int i = 0; i < lengths.size(); ++i) {
if (lengthsPtr[i] == 0) {
outLengthsPtr[i] = 1;
@ -373,13 +277,12 @@ class PadEmptySamplesOp : public Operator<CPUContext> {
}
}
return true;
}
};
}
REGISTER_CPU_OPERATOR(AddPadding, AddPaddingOp);
REGISTER_CPU_OPERATOR(RemovePadding, RemovePaddingOp);
REGISTER_CPU_OPERATOR(GatherPadding, GatherPaddingOp);
REGISTER_CPU_OPERATOR(PadEmptySamples, PadEmptySamplesOp);
REGISTER_CPU_OPERATOR(AddPadding, AddPaddingOp<CPUContext>);
REGISTER_CPU_OPERATOR(RemovePadding, RemovePaddingOp<CPUContext>);
REGISTER_CPU_OPERATOR(GatherPadding, GatherPaddingOp<CPUContext>);
REGISTER_CPU_OPERATOR(PadEmptySamples, PadEmptySamplesOp<CPUContext>);
struct GetAddPadingGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
@ -528,5 +431,5 @@ PadEmptySamples is thread safe.
0,
"out_lengths",
"Tensor containing lengths with empty sample padded.");
}
}
} // namespace caffe2

View File

@ -0,0 +1,129 @@
#ifndef CAFFE2_OPERATORS_SEQUENCE_OPS_H_
#define CAFFE2_OPERATORS_SEQUENCE_OPS_H_
#include "caffe2/core/operator.h"
#include "caffe2/core/tensor.h"
namespace caffe2 {
template <class Context>
class GatherPaddingOp final : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
GatherPaddingOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws),
startPaddingWidth_(
OperatorBase::GetSingleArgument<int>("padding_width", 1)),
endPaddingWidth_(
OperatorBase::GetSingleArgument<int>("end_padding_width", -1)) {
CAFFE_ENFORCE_GE(startPaddingWidth_, 0);
if (endPaddingWidth_ < 0) {
endPaddingWidth_ = startPaddingWidth_;
}
}
bool RunOnDevice() override {
if (startPaddingWidth_ == 0 && endPaddingWidth_ == 0) {
Output(0)->Resize(std::vector<TIndex>(0));
if (OutputSize() == 2) {
Output(1)->Resize(std::vector<TIndex>(0));
}
return true;
}
return DispatchHelper<TensorTypes<float, double, int, int64_t, bool>>::call(
this, Input(0));
}
template <typename T>
bool DoRunWithType();
private:
int startPaddingWidth_;
int endPaddingWidth_;
};
template <class Context>
class RemovePaddingOp final : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
RemovePaddingOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws),
startPaddingWidth_(
OperatorBase::GetSingleArgument<int>("padding_width", 1)),
endPaddingWidth_(
OperatorBase::GetSingleArgument<int>("end_padding_width", -1)) {
CAFFE_ENFORCE_GE(startPaddingWidth_, 0);
if (endPaddingWidth_ < 0) {
endPaddingWidth_ = startPaddingWidth_;
}
}
bool RunOnDevice() override {
if (startPaddingWidth_ == 0 && endPaddingWidth_ == 0) {
Output(0)->CopyFrom(Input(0), &context_);
if (OutputSize() == 2) {
Output(1)->CopyFrom(Input(1), &context_);
}
return true;
}
return DispatchHelper<TensorTypes<float, double, int, int64_t, bool>>::call(
this, Input(0));
}
template <typename T>
bool DoRunWithType();
private:
int startPaddingWidth_;
int endPaddingWidth_;
};
template <class Context>
class AddPaddingOp final : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
AddPaddingOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws),
startPaddingWidth_(
OperatorBase::GetSingleArgument<int>("padding_width", 1)),
endPaddingWidth_(
OperatorBase::GetSingleArgument<int>("end_padding_width", -1)) {
CAFFE_ENFORCE_GE(startPaddingWidth_, 0);
if (endPaddingWidth_ < 0) {
endPaddingWidth_ = startPaddingWidth_;
}
}
bool RunOnDevice() override {
if (startPaddingWidth_ == 0 && endPaddingWidth_ == 0) {
Output(0)->CopyFrom(Input(0), &context_);
if (OutputSize() == 2) {
Output(1)->CopyFrom(Input(1), &context_);
}
return true;
}
return DispatchHelper<TensorTypes<float, double, int, int64_t, bool>>::call(
this, Input(0));
}
template <typename T>
bool DoRunWithType();
private:
int startPaddingWidth_;
int endPaddingWidth_;
};
template <class Context>
class PadEmptySamplesOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
PadEmptySamplesOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws) {}
bool RunOnDevice() override;
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_SEQUENCE_OPS_H_