-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add a padding operator. #95
Changes from all commits
03fdff1
4064629
9e54e3a
1090710
1b17e2a
02ef71e
b2a51ba
c89fa42
fe44780
d818a5f
7689d04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
#include "smaug/core/backend.h" | ||
#include "smaug/operators/batch_norm_op.h" | ||
#include "smaug/operators/concat_op.h" | ||
#include "smaug/operators/control_flow_ops.h" | ||
#include "smaug/operators/convolution_op.h" | ||
#include "smaug/operators/data_op.h" | ||
#include "smaug/operators/depthwise_convolution_op.h" | ||
#include "smaug/operators/eltwise_add_op.h" | ||
#include "smaug/operators/eltwise_mul_op.h" | ||
#include "smaug/operators/less_op.h" | ||
#include "smaug/operators/greater_op.h" | ||
#include "smaug/operators/control_flow_ops.h" | ||
#include "smaug/operators/elu_op.h" | ||
#include "smaug/operators/greater_op.h" | ||
#include "smaug/operators/inner_product_op.h" | ||
#include "smaug/operators/less_op.h" | ||
#include "smaug/operators/padding_op.h" | ||
#include "smaug/operators/pooling_op.h" | ||
#include "smaug/operators/relu_op.h" | ||
#include "smaug/operators/reorder_op.h" | ||
#include "smaug/operators/concat_op.h" | ||
#include "smaug/operators/split_op.h" | ||
#include "smaug/operators/reshape_op.h" | ||
#include "smaug/operators/repeat_op.h" | ||
#include "smaug/operators/reshape_op.h" | ||
#include "smaug/operators/sigmoid_op.h" | ||
#include "smaug/operators/softmax_op.h" | ||
#include "smaug/operators/tanh_op.h" | ||
#include "smaug/operators/smv/smv_batch_norm_op.h" | ||
#include "smaug/operators/smv/smv_convolution_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_add_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_mul_op.h" | ||
#include "smaug/operators/smv/smv_elu_op.h" | ||
#include "smaug/operators/smv/smv_greater_op.h" | ||
#include "smaug/operators/smv/smv_inner_product_op.h" | ||
#include "smaug/operators/smv/smv_less_op.h" | ||
#include "smaug/operators/smv/smv_pooling_op.h" | ||
#include "smaug/operators/smv/smv_batch_norm_op.h" | ||
#include "smaug/operators/smv/smv_relu_op.h" | ||
#include "smaug/operators/smv/smv_elu_op.h" | ||
#include "smaug/operators/smv/smv_tanh_op.h" | ||
#include "smaug/operators/smv/smv_sigmoid_op.h" | ||
#include "smaug/operators/smv/smv_softmax_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_add_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_mul_op.h" | ||
#include "smaug/operators/smv/smv_less_op.h" | ||
#include "smaug/operators/smv/smv_greater_op.h" | ||
#include "smaug/operators/smv/smv_tanh_op.h" | ||
#include "smaug/operators/softmax_op.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Also, if this was meant to sort the header includes, these three should not be at the bottom :] ) |
||
#include "smaug/operators/split_op.h" | ||
#include "smaug/operators/tanh_op.h" | ||
|
||
namespace smaug { | ||
|
||
|
@@ -79,6 +80,7 @@ DEF_CREATE_OP(EluOp, ReferenceBackend) | |
DEF_CREATE_OP(SeluOp, ReferenceBackend) | ||
DEF_CREATE_OP(TanhOp, ReferenceBackend) | ||
DEF_CREATE_OP(HardTanhOp, ReferenceBackend) | ||
DEF_CREATE_OP(PaddingOp, ReferenceBackend) | ||
|
||
DEF_CREATE_SMV_OP(ConvolutionOp) | ||
DEF_CREATE_SMV_OP(InnerProductOp) | ||
|
@@ -108,7 +110,9 @@ DEF_CREATE_OP(RepeatOp, SmvBackend) | |
DEF_CREATE_OP(FlattenOp, SmvBackend) | ||
DEF_CREATE_OP(SwitchOp, SmvBackend) | ||
DEF_CREATE_OP(MergeOp, SmvBackend) | ||
DEF_CREATE_OP(PaddingOp, SmvBackend) | ||
|
||
// for simple tracing. | ||
namespace ref { | ||
const unsigned kConvolutionHw = 0x0001; | ||
const unsigned kInnerProductHw = 0x0002; | ||
|
@@ -140,5 +144,4 @@ float* spad1; | |
float* spad2; | ||
} // namespace smv | ||
|
||
|
||
} // namespace smaug |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,57 @@ | ||
#include <iostream> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, it'd better to put the code rearrangement into a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this. |
||
#include <fstream> | ||
#include <fcntl.h> | ||
#include <fstream> | ||
#include <iostream> | ||
|
||
#include <google/protobuf/text_format.h> | ||
#include <google/protobuf/io/zero_copy_stream_impl.h> | ||
#include <google/protobuf/text_format.h> | ||
|
||
#include "smaug/core/backend.h" | ||
#include "smaug/core/tensor.h" | ||
#include "smaug/core/graph.pb.h" | ||
#include "smaug/core/network.h" | ||
#include "smaug/core/network_builder.h" | ||
#include "smaug/core/workspace.h" | ||
#include "smaug/core/graph.pb.h" | ||
#include "smaug/core/node.pb.h" | ||
#include "smaug/core/tensor.h" | ||
#include "smaug/core/tensor.pb.h" | ||
#include "smaug/core/types.pb.h" | ||
#include "smaug/operators/common.h" | ||
#include "smaug/core/workspace.h" | ||
#include "smaug/operators/batch_norm_op.h" | ||
#include "smaug/operators/common.h" | ||
#include "smaug/operators/concat_op.h" | ||
#include "smaug/operators/control_flow_ops.h" | ||
#include "smaug/operators/convolution_op.h" | ||
#include "smaug/operators/data_op.h" | ||
#include "smaug/operators/depthwise_convolution_op.h" | ||
#include "smaug/operators/eltwise_add_op.h" | ||
#include "smaug/operators/eltwise_mul_op.h" | ||
#include "smaug/operators/less_op.h" | ||
#include "smaug/operators/greater_op.h" | ||
#include "smaug/operators/control_flow_ops.h" | ||
#include "smaug/operators/elu_op.h" | ||
#include "smaug/operators/greater_op.h" | ||
#include "smaug/operators/inner_product_op.h" | ||
#include "smaug/operators/less_op.h" | ||
#include "smaug/operators/padding_op.h" | ||
#include "smaug/operators/pooling_op.h" | ||
#include "smaug/operators/relu_op.h" | ||
#include "smaug/operators/reorder_op.h" | ||
#include "smaug/operators/concat_op.h" | ||
#include "smaug/operators/split_op.h" | ||
#include "smaug/operators/reshape_op.h" | ||
#include "smaug/operators/repeat_op.h" | ||
#include "smaug/operators/reshape_op.h" | ||
#include "smaug/operators/sigmoid_op.h" | ||
#include "smaug/operators/softmax_op.h" | ||
#include "smaug/operators/tanh_op.h" | ||
#include "smaug/operators/smv/smv_batch_norm_op.h" | ||
#include "smaug/operators/smv/smv_convolution_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_add_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_mul_op.h" | ||
#include "smaug/operators/smv/smv_elu_op.h" | ||
#include "smaug/operators/smv/smv_greater_op.h" | ||
#include "smaug/operators/smv/smv_inner_product_op.h" | ||
#include "smaug/operators/smv/smv_less_op.h" | ||
#include "smaug/operators/smv/smv_pooling_op.h" | ||
#include "smaug/operators/smv/smv_batch_norm_op.h" | ||
#include "smaug/operators/smv/smv_relu_op.h" | ||
#include "smaug/operators/smv/smv_elu_op.h" | ||
#include "smaug/operators/smv/smv_tanh_op.h" | ||
#include "smaug/operators/smv/smv_sigmoid_op.h" | ||
#include "smaug/operators/smv/smv_softmax_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_add_op.h" | ||
#include "smaug/operators/smv/smv_eltwise_mul_op.h" | ||
#include "smaug/operators/smv/smv_less_op.h" | ||
#include "smaug/operators/smv/smv_greater_op.h" | ||
#include "smaug/utility/utils.h" | ||
#include "smaug/operators/smv/smv_tanh_op.h" | ||
#include "smaug/operators/softmax_op.h" | ||
#include "smaug/operators/split_op.h" | ||
#include "smaug/operators/tanh_op.h" | ||
#include "smaug/utility/debug_stream.h" | ||
#include "smaug/utility/utils.h" | ||
|
||
using namespace smaug; | ||
using namespace std; | ||
|
@@ -263,6 +264,10 @@ static void createAndAddOperator(const NodeProto& node, | |
} else if (type == OpType::Tanh) { | ||
auto op = Backend::createTanhOp(name, workspace); | ||
network->addOperator(op); | ||
} else if (type == OpType::Padding) { | ||
auto op = Backend::createPaddingOp(name, workspace); | ||
op->setPaddingSize(node.params().padding_params().padding_size()); | ||
network->addOperator(op); | ||
} else if (type == OpType::HardTanh) { | ||
auto op = Backend::createHardTanhOp(name, workspace); | ||
network->addOperator(op); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#ifndef _OPERATORS_PADDING_OP_H_ | ||
#define _OPERATORS_PADDING_OP_H_ | ||
|
||
#include "smaug/core/backend.h" | ||
#include "smaug/core/operator.h" | ||
#include "smaug/core/tensor.h" | ||
#include "smaug/core/workspace.h" | ||
#include <google/protobuf/repeated_field.h> | ||
using namespace google::protobuf; | ||
|
||
namespace smaug { | ||
|
||
/** \ingroup Operators | ||
* \brief Pad a given tensor in any number of dimensions with arbitrary size. | ||
* | ||
* This has a software-based implementation. | ||
* | ||
* @tparam Backend The Backend that sets Alignment. | ||
*/ | ||
template <typename Backend> | ||
class PaddingOp : public Operator { | ||
public: | ||
PaddingOp(const std::string& name, Workspace* workspace) | ||
: Operator(name, OpType::Padding, workspace) { | ||
inputs.resize(kNumInputs, nullptr); | ||
outputs.resize(kNumOutputs, nullptr); | ||
} | ||
|
||
/** | ||
* Set the paddingSize of the Tensor along each dimension. | ||
* The paddingSize is orgainized as <{dim0_begin, dim0_end, dim1_begin, | ||
* dim1_end, ... > | ||
*/ | ||
void setPaddingSize(const RepeatedField<google::protobuf::int32>& val) { | ||
paddingSize.assign(val.begin(), val.end()); | ||
} | ||
|
||
void setPaddingSize(std::vector<int> const& val) { paddingSize = val; } | ||
|
||
const std::vector<int>& getPaddingSize() const { return paddingSize; } | ||
|
||
void run() override { | ||
Tensor* input = getInput(kInput); | ||
Tensor* output = getOutput(kOutput); | ||
int ndims = input->ndims(); | ||
const std::vector<int>& inputDims = input->getShape().dims(); | ||
const std::vector<int>& outputDims = output->getShape().dims(); | ||
int total_dim = 1; | ||
for (int i : outputDims) { | ||
total_dim *= i; | ||
} | ||
std::vector<float> vf(total_dim, 0); | ||
output->fillData(vf.data(), vf.size()); | ||
std::vector<int> paddingBegin, srcOrigin; | ||
for (int i = 0; i < ndims; i++) { | ||
paddingBegin.push_back(paddingSize.at(2 * i)); | ||
srcOrigin.push_back(0); | ||
} | ||
copyTensorRegion(output, input, paddingBegin, srcOrigin, inputDims); | ||
} | ||
|
||
// Optional override for testing purposes. | ||
void createAllTensors() override { | ||
Tensor* input = getInput(kInput); | ||
int ndims = input->ndims(); | ||
std::vector<int> dims = input->getShape().dims(); | ||
for (int i = 0; i < ndims; i++) { | ||
dims[i] += (paddingSize[2 * i] + paddingSize[2 * i + 1]); | ||
} | ||
TensorShape shape( | ||
dims, input->getShape().getLayout(), Backend::Alignment); | ||
Tensor* output = new Tensor(name, shape); | ||
workspace->addTensor(output); | ||
outputs.at(kOutput) = output; | ||
} | ||
|
||
// Optional but recommended function to verify operator parameters. | ||
bool validate() override { | ||
Tensor* input = getInput(kInput); | ||
int ndims = input->ndims(); | ||
if (paddingSize.size() != 2 * ndims) { | ||
return false; | ||
} | ||
return Operator::validate(); | ||
} | ||
|
||
enum { kInput, kNumInputs }; | ||
enum { kOutput, kNumOutputs }; | ||
|
||
private: | ||
std::vector<int> paddingSize = {}; | ||
}; | ||
|
||
} // namespace smaug | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code rearrangement generally is confusing :) Moving forward, please move this to a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely appreciate you sorting all the includes in alphabetical order! It's certainly cleaner than before. But yes, refactoring and code cleanups should be done separately if it's not related to the main PR purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this