-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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 split selected rows op #7604
Changes from 2 commits
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/operators/split_selected_rows_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class SplitSelectedRowsOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
SplitSelectedRowsOpMaker(OpProto *proto, OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("X", "The input SelectedRows."); | ||
AddOutput("Out", "The outputs of input SelectedRows.").AsDuplicable(); | ||
AddAttr<std::vector<int>>("rows_section", "Rows section for output.") | ||
.SetDefault(std::vector<int>({})); | ||
AddAttr<std::vector<int>>("height_section", | ||
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.
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. Done. |
||
"Height for each output SelectedRows.") | ||
.SetDefault(std::vector<int>({})); | ||
|
||
AddComment(R"DOC( | ||
Split a SelectedRows with a specified rows section. | ||
You could set height_section for specified the height for each output. | ||
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.
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. Done. |
||
|
||
Example: | ||
Input: | ||
X.rows = {0, 7, 5} | ||
X.height = 12 | ||
Attr: | ||
rows_section = {1, 2} | ||
height_section = {} | ||
Out: | ||
out0.rows = {0} | ||
out0.height = 12 | ||
out1.rows = {7, 5} | ||
out2.height = 12 | ||
|
||
)DOC"); | ||
} | ||
}; | ||
|
||
class SplitSelectedRowsOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
void InferShape(framework::InferShapeContext *ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("X"), "SplitSelectedRowsOp must has input X."); | ||
PADDLE_ENFORCE(ctx->HasOutputs("Out"), | ||
"SplitSelectedRowsOp must has output Out."); | ||
|
||
std::vector<int> height_section = | ||
ctx->Attrs().Get<std::vector<int>>("height_section"); | ||
std::vector<int> rows_section = | ||
ctx->Attrs().Get<std::vector<int>>("rows_section"); | ||
PADDLE_ENFORCE_EQ( | ||
rows_section.size(), ctx->Outputs("Out").size(), | ||
"The size of rows section should be the same with Outputs size."); | ||
int64_t n = ctx->Outputs("Out").size(); | ||
|
||
std::vector<framework::DDim> outs_dims; | ||
outs_dims.reserve(n); | ||
|
||
// make output dims | ||
for (int64_t i = 0; i < n; ++i) { | ||
auto dims = ctx->GetInputDim("X"); | ||
if (height_section.size()) { | ||
PADDLE_ENFORCE_EQ( | ||
height_section.size(), static_cast<size_t>(n), | ||
"The size of height section should be the same with height" | ||
" section size."); | ||
dims[0] = height_section[i]; | ||
} | ||
outs_dims.push_back(dims); | ||
} | ||
ctx->SetOutputsDim("Out", outs_dims); | ||
} | ||
}; | ||
|
||
class SplitSelectedRowsGradMaker : public framework::SingleGradOpDescMaker { | ||
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 didn't see a grad kernel implementation, did you missed that? 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. We reuse 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. Done. 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. Do you need to register the sum kernel as the grad kernel? 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. Maybe don't need, just inherit |
||
public: | ||
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; | ||
|
||
protected: | ||
std::unique_ptr<framework::OpDesc> Apply() const override { | ||
auto *grad_op = new framework::OpDesc(); | ||
grad_op->SetType("sum"); | ||
grad_op->SetInput("X", OutputGrad("Out")); | ||
grad_op->SetOutput("Out", InputGrad("X")); | ||
grad_op->SetAttrMap(Attrs()); | ||
return std::unique_ptr<framework::OpDesc>(grad_op); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OPERATOR(split_selected_rows, ops::SplitSelectedRowsOp, | ||
ops::SplitSelectedRowsOpMaker, | ||
ops::SplitSelectedRowsGradMaker); | ||
REGISTER_OP_CPU_KERNEL( | ||
split_selected_rows, | ||
ops::SplitSelectedRowsOpKernel<paddle::platform::CPUPlace, float>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
#include "paddle/framework/op_registry.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
template <typename DeviceContext, typename T> | ||
class SplitSelectedRowsOpKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
auto* x = ctx.Input<framework::SelectedRows>("X"); | ||
auto outs = ctx.MultiOutput<framework::SelectedRows>("Out"); | ||
|
||
auto rows_section = ctx.Attr<std::vector<int>>("rows_section"); | ||
auto height_section = ctx.Attr<std::vector<int>>("height_section"); | ||
|
||
int64_t n = outs.size(); | ||
int offset = 0; | ||
|
||
for (int64_t i = 0; i < n; ++i) { | ||
framework::Vector<int64_t> out_rows; | ||
for (int64_t j = 0; j < rows_section[i]; ++j) { | ||
out_rows.push_back(x->rows()[offset + j]); | ||
} | ||
|
||
auto& out = outs[i]; | ||
auto x_dims = x->GetCompleteDims(); | ||
x_dims[0] = rows_section[i]; | ||
out->mutable_value()->mutable_data<T>(x_dims, ctx.GetPlace()); | ||
framework::Copy(x->value().Slice(offset, rows_section[i] + offset), | ||
x->place(), ctx.device_context(), out->mutable_value()); | ||
outs[i]->set_rows(out_rows); | ||
if (height_section.size()) { | ||
outs[i]->set_height(height_section[i]); | ||
} | ||
offset += rows_section[i]; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. | ||
# | ||
#Licensed under the Apache License, Version 2.0 (the "License"); | ||
#you may not use this file except in compliance with the License. | ||
#You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
#Unless required by applicable law or agreed to in writing, software | ||
#distributed under the License is distributed on an "AS IS" BASIS, | ||
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
#See the License for the specific language governing permissions and | ||
#limitations under the License. | ||
import unittest | ||
import paddle.v2.fluid.core as core | ||
import numpy as np | ||
from paddle.v2.fluid.op import Operator | ||
|
||
|
||
class TestSpliteAndMergeSelectedRows(unittest.TestCase): | ||
def test_check_output(self): | ||
places = [core.CPUPlace()] | ||
if core.is_compile_gpu(): | ||
places.append(core.CUDAPlace(0)) | ||
for place in places: | ||
self.check_with_place(place) | ||
|
||
def check_with_place(self, place): | ||
scope = core.Scope() | ||
rows = [0, 5, 7, 4] | ||
height = 10 | ||
row_numel = 2 | ||
|
||
# initialize input variable X | ||
x = scope.var('X').get_selected_rows() | ||
x.set_rows(rows) | ||
x.set_height(height) | ||
np_array = np.ones((len(rows), row_numel)).astype("float32") | ||
np_array[0, 0] = 2.0 | ||
np_array[2, 1] = 4.0 | ||
x_tensor = x.get_tensor() | ||
x_tensor.set(np_array, place) | ||
|
||
rows_section = [2, 2] | ||
height_section = [] | ||
|
||
# initialize output variables [out0, out1] | ||
out0 = scope.var('out0').get_selected_rows() | ||
out1 = scope.var('out1').get_selected_rows() | ||
|
||
# expected output selected rows | ||
expected_out0_rows = [0, 5] | ||
expected_out1_rows = [7, 4] | ||
expected_height = height | ||
|
||
split_selected_rows = Operator( | ||
"split_selected_rows", | ||
X="X", | ||
Out=["out0", "out1"], | ||
rows_section=rows_section, | ||
height_section=height_section) | ||
|
||
split_selected_rows.run(scope, place) | ||
|
||
self.assertEqual(out0.rows(), expected_out0_rows) | ||
self.assertEqual(out1.rows(), expected_out1_rows) | ||
|
||
self.assertEqual(out0.height(), expected_height) | ||
self.assertEqual(out1.height(), expected_height) | ||
|
||
self.assertAlmostEqual(2.0, np.array(out0.get_tensor())[0, 0]) | ||
self.assertAlmostEqual(4.0, np.array(out1.get_tensor())[0, 1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
rows_section
=> `rows_sectionsThere 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.
Done.