-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add merge_ids_op #11342
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
Merged
jacquesqiao
merged 12 commits into
PaddlePaddle:develop
from
jacquesqiao:add-merge-splited-ids
Jun 13, 2018
Merged
Add merge_ids_op #11342
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7cebec4
init merge_ids_op
jacquesqiao 509cb0b
add unit test, pass the unit test
jacquesqiao a941786
replace concat_op with merge_ids_op
jacquesqiao eccf0d5
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao 0485405
add more debug string
jacquesqiao 6dd3f3c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao f031555
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao d6c8d26
optimize code and comment
jacquesqiao cdd55db
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao 7ebef49
add row_size for selected rows in DebugStringEx
jacquesqiao 2e48ab6
add more detailed comment
jacquesqiao e6f54d5
update comment
jacquesqiao 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,128 @@ | ||
/* 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/merge_ids_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class MergeIdsOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
void Make() override { | ||
AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}"); | ||
AddInput( | ||
"X", | ||
"(LoDTensors) multi input tensor with shape{batch_num, N}, N is the " | ||
"size of embedding table") | ||
.AsDuplicable(); | ||
AddOutput("Out", "(LoDTensor) The merged outputs of the input tensors."); | ||
|
||
AddComment(R"DOC( | ||
Merge multi LoDTensor's into one according to Ids's shard num. | ||
|
||
|
||
split_ids_op -> prefetch_op -> merge_ids_op | ||
|
||
|
||
merge_ids_op should be used after split_ids_op and prefetch_op, split_ids_op | ||
will split input Ids into multiple tensors according to Id's shard number. | ||
prefetch_op will send them to parameter server to prefetch embedding value | ||
back. During split, the order of ids is disordered. In merge_ids_op we use | ||
the original Ids to restore the order of the fetched embedding value and | ||
also pass the lod information to the merged output. | ||
|
||
|
||
Example: | ||
|
||
Ids = [1,2,3,4,5,6] # 3 shared | ||
|
||
split_ids_op -> | ||
|
||
Id0 = [3, 6] # id % 3 == 0 | ||
Id1 = [1, 4] # id % 3 == 1 | ||
Id2 = [2, 5] # id % 3 == 2 | ||
|
||
prefetch_op -> | ||
|
||
X0 = [[0.3 0.3] # 3 | ||
[0.6 0.6]] # 6 | ||
X1 = [[0.1 0.1] # 1 | ||
[0.4 0.4]] # 4 | ||
X2 = [[0.2 0.2] # 2 | ||
[0.5 0.5]] # 5 | ||
|
||
merge_ids_op -> | ||
|
||
Out = [[0.1 0.1] # 1 | ||
[0.2 0.2] # 2 | ||
[0.3 0.3] # 3 | ||
[0.4 0.4] # 4 | ||
[0.5 0.5] # 5 | ||
[0.6 0.6]] # 6 | ||
)DOC"); | ||
} | ||
}; | ||
|
||
class MergeIdsOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
void InferShape(framework::InferShapeContext *ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("Ids"), "MergeIdsOp must has input Ids."); | ||
PADDLE_ENFORCE(ctx->HasInputs("X"), "MergeIdsOp must has input X."); | ||
PADDLE_ENFORCE(ctx->HasOutput("Out"), "MergeIdsOp must has output Out."); | ||
|
||
auto ids_var_type = ctx->GetInputsVarType("Ids").front(); | ||
auto ids_dims = ctx->GetInputDim("Ids"); | ||
if (ids_var_type == framework::proto::VarType::LOD_TENSOR) { | ||
PADDLE_ENFORCE_EQ(ids_dims.size(), 2); | ||
PADDLE_ENFORCE_EQ(ids_dims[1], 1); | ||
} | ||
auto x_var_type = ctx->GetInputsVarType("X"); | ||
for (auto &var_type : x_var_type) { | ||
PADDLE_ENFORCE_EQ(var_type, framework::proto::VarType::LOD_TENSOR, | ||
"input X only support lod tensors"); | ||
} | ||
ctx->ShareLoD("Ids", "Out"); | ||
} | ||
|
||
private: | ||
framework::OpKernelType GetExpectedKernelType( | ||
const framework::ExecutionContext &ctx) const override { | ||
return framework::OpKernelType( | ||
framework::ToDataType( | ||
ctx.MultiInput<framework::Tensor>("X").front()->type()), | ||
ctx.GetPlace()); | ||
} | ||
}; | ||
|
||
class MergeIdsOpInferVarType : public framework::VarTypeInference { | ||
public: | ||
void operator()(const framework::OpDesc &op_desc, | ||
framework::BlockDesc *block) const override { | ||
auto *input_var = block->Var(op_desc.Input("Ids")[0]); | ||
for (auto &out_var : op_desc.Output("Out")) { | ||
block->Var(out_var)->SetType(input_var->GetType()); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OPERATOR(merge_ids, ops::MergeIdsOp, ops::MergeIdsOpMaker, | ||
ops::MergeIdsOpInferVarType); | ||
REGISTER_OP_CPU_KERNEL( | ||
merge_ids, ops::MergeIdsOpKernel<paddle::platform::CPUPlace, float>); |
This file contains hidden or 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,92 @@ | ||
/* 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/framework/tensor_util.h" | ||
#include "paddle/fluid/operators/math/selected_rows_functor.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
template <typename DeviceContext, typename T> | ||
class MergeIdsOpKernel : 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("MergeIds do not support GPU kernel"); | ||
} | ||
VLOG(3) << "run in MergeIdsOpKernel"; | ||
|
||
const auto *ids_var = ctx.InputVar("Ids"); | ||
PADDLE_ENFORCE(ids_var->IsType<framework::LoDTensor>(), | ||
"only support to merge Ids of LoDTensor"); | ||
|
||
const auto &ids_tensor = ids_var->Get<framework::LoDTensor>(); | ||
const auto &ids_dims = ids_tensor.dims(); | ||
const int64_t *ids = ids_tensor.data<int64_t>(); | ||
|
||
auto x_tensors = ctx.MultiInput<framework::LoDTensor>("X"); | ||
|
||
auto *out = ctx.Output<framework::LoDTensor>("Out"); | ||
|
||
int batch_size = 0; | ||
int embedding_size = 0; | ||
for (auto &input : x_tensors) { | ||
if (framework::product(input->dims()) != 0) { | ||
if (embedding_size == 0) { | ||
embedding_size = input->dims()[1]; | ||
} | ||
PADDLE_ENFORCE_EQ(embedding_size, input->dims()[1], | ||
"embedding size of all input should be the same"); | ||
batch_size += input->dims()[0]; | ||
} | ||
} | ||
PADDLE_ENFORCE_EQ( | ||
batch_size, ids_dims[0], | ||
"the batch size of ids and merged embedding value should be the same"); | ||
|
||
const size_t shard_num = x_tensors.size(); | ||
|
||
if (shard_num == 1) { | ||
VLOG(3) << "only one shard, we can copy the data directly"; | ||
TensorCopy(*x_tensors[0], place, out); | ||
} else { | ||
std::vector<int> in_indexs(shard_num, 0); | ||
auto *out_data = out->mutable_data<T>( | ||
framework::make_ddim({batch_size, embedding_size}), place); | ||
// copy data from ins[shard_num] to out. | ||
for (int i = 0; i < ids_dims[0]; ++i) { | ||
int64_t id = ids[i]; | ||
size_t shard_id = static_cast<size_t>(id) % shard_num; | ||
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. Hash method is currently hardcoded, just a message for notice. 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. yes, will be an independent module in the future. |
||
int index = in_indexs[shard_id]; | ||
memcpy(out_data + embedding_size * i, | ||
x_tensors[shard_id]->data<T>() + index * embedding_size, | ||
sizeof(T) * embedding_size); | ||
in_indexs[shard_id] += 1; | ||
} | ||
|
||
for (size_t i = 0; i < shard_num; ++i) { | ||
PADDLE_ENFORCE_EQ(in_indexs[i], x_tensors[i]->dims()[0], | ||
"after merge, all data in x_tensor should be used"); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle |
This file contains hidden or 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,38 @@ | ||
# 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 TestMergeIdsOp(OpTest): | ||
def setUp(self): | ||
self.op_type = "merge_ids" | ||
ids = np.array([[0], [2], [2], [3], [5], [5], [6]]).astype('int64') | ||
x0 = np.array([[0.1, 0.2], [0.2, 0.3], [0.3, 0.4]]).astype('float32') | ||
x1 = np.array([]).astype('float32') | ||
x2 = np.array([[0.4, 0.5], [0.4, 0.5], [0.5, 0.6], | ||
[0.5, 0.6]]).astype('float32') | ||
out = np.array([[0.1, 0.2], [0.4, 0.5], [0.4, 0.5], [0.2, 0.3], | ||
[0.5, 0.6], [0.5, 0.6], [0.3, 0.4]]).astype('float32') | ||
self.inputs = {'Ids': ids, "X": [('x0', x0), ('x1', x1), ('x2', x2)]} | ||
self.outputs = {'Out': out} | ||
|
||
def test_check_output(self): | ||
self.check_output() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains hidden or 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
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.
Can we keep the log level before
op run
and after?