-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 ids op #9370
Merged
Merged
add split ids op #9370
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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/fluid/operators/split_ids_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class SplitIdsOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
SplitIdsOpMaker(OpProto *proto, OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}"); | ||
AddOutput("Out", "(LoDTensor) The outputs of the input Ids.") | ||
.AsDuplicable(); | ||
|
||
AddComment(R"DOC( | ||
Split a LoDTensor of Ids into multi LoDTensors, the number is pserver's number | ||
Example: | ||
Input: | ||
X = [1,2,3,4,5,6] | ||
|
||
Out(3 output): | ||
out0 = [3, 6] | ||
out1 = [1, 4] | ||
out2 = [2, 5] | ||
)DOC"); | ||
} | ||
}; | ||
|
||
class SplitIdsOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
void InferShape(framework::InferShapeContext *ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("Ids"), "SplitIdsOp must has input Ids."); | ||
PADDLE_ENFORCE(ctx->HasOutputs("Out"), "SplitIdsOp must has output Out."); | ||
|
||
auto ids_var_type = ctx->GetInputsVarType("Ids").front(); | ||
PADDLE_ENFORCE_EQ(ids_var_type, framework::proto::VarType::LOD_TENSOR); | ||
|
||
auto ids_dims = ctx->GetInputDim("Ids"); | ||
PADDLE_ENFORCE_EQ(ids_dims.size(), 2); | ||
PADDLE_ENFORCE_EQ(ids_dims[1], 1); | ||
} | ||
}; | ||
|
||
class SplitIdsOpInferVarType : public framework::VarTypeInference { | ||
public: | ||
void operator()(const framework::OpDesc &op_desc, | ||
framework::BlockDesc *block) const override { | ||
for (auto &out_var : op_desc.Output("Out")) { | ||
block->Var(out_var)->SetType(framework::proto::VarType::LOD_TENSOR); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OPERATOR(split_ids, ops::SplitIdsOp, ops::SplitIdsOpMaker, | ||
ops::SplitIdsOpInferVarType); | ||
REGISTER_OP_CPU_KERNEL( | ||
split_ids, ops::SplitIdsOpKernel<paddle::platform::CPUPlace, int64_t>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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/fluid/framework/op_registry.h" | ||
#include "paddle/fluid/operators/math/selected_rows_functor.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
template <typename DeviceContext, typename T> | ||
class SplitIdsOpKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
auto place = ctx.GetPlace(); | ||
if (!platform::is_cpu_place(place)) { | ||
PADDLE_THROW("SplitIds do not support GPU kernel"); | ||
} | ||
|
||
const auto* ids_t = ctx.Input<framework::LoDTensor>("Ids"); | ||
auto& ids_dims = ids_t->dims(); | ||
auto outs = ctx.MultiOutput<framework::LoDTensor>("Out"); | ||
|
||
const T* ids = ids_t->data<T>(); | ||
|
||
const size_t shard_num = outs.size(); | ||
|
||
std::vector<std::vector<T>> out_ids; | ||
out_ids.resize(outs.size()); | ||
|
||
// split id by their shard_num. | ||
for (size_t i = 0; i < ids_dims[0]; ++i) { | ||
T id = ids[i]; | ||
size_t shard_id = static_cast<size_t>(id) % shard_num; | ||
out_ids[shard_id].push_back(id); | ||
} | ||
|
||
// create tensor for each shard and send to parameter server | ||
for (size_t i = 0; i < out_ids.size(); ++i) { | ||
auto* shard_t = outs[i]; | ||
std::vector<T> ids = out_ids[i]; | ||
auto* shard_data = shard_t->mutable_data<T>( | ||
framework::make_ddim({static_cast<int64_t>(ids.size()), 1}), place); | ||
for (size_t i = 0; i < ids.size(); ++i) { | ||
shard_data[i] = ids[i]; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# 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 numpy as np | ||
from op_test import OpTest | ||
|
||
|
||
class TestSplitIdsOp(OpTest): | ||
def setUp(self): | ||
self.op_type = "split_ids" | ||
ids = np.array([[0], [2], [2], [3], [5], [5], [6]]).astype('int64') | ||
out0 = np.array([[0], [3], [6]]).astype('int64') | ||
out1 = np.array([[]]).astype('int64') | ||
out2 = np.array([[2], [2], [5], [5]]).astype('int64') | ||
self.inputs = {'Ids': ids} | ||
self.outputs = {'Out': [('out0', out0), ('out1', out1), ('out2', out2)]} | ||
|
||
def test_check_output(self): | ||
self.check_output() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe can remove this temp vector and push data directly to
outs[i]
to reduce one for loop.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.
The problem is that we don't know the shape of outs[i], so we cannot call mutable_data to allocate memory for it. The first loop is used to get the shape of each outs.
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.
True.