Skip to content

Commit 7cebec4

Browse files
committed
init merge_ids_op
1 parent d896134 commit 7cebec4

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/operators/merge_ids_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class MergeIdsOpMaker : public framework::OpProtoAndCheckerMaker {
21+
public:
22+
void Make() override {
23+
AddInput("Ids", "(LoDTensor) the input ids with shape{batch_num, 1}");
24+
AddInput("X",
25+
"(LoDTensor) the input tensor with shape{batch_num, N}, N is the "
26+
"size of embedding table")
27+
.AsDuplicable();
28+
AddOutput("Out", "(LoDTensor) The merged outputs of the input tensors.");
29+
30+
AddComment(R"DOC(
31+
Merge multi LoDTensor's into one according to Ids's shard num.
32+
The values in the input LoDTensor are lookuped from the output of splite_ids_op
33+
Example:
34+
Input:
35+
Ids = [1,2,3,4,5,6]
36+
X0 = [[0.1 0.2] # 3
37+
[0.2 0.3]] # 6
38+
X1 = [[0.3 0.4] # 1
39+
[0.4 0.5]] # 4
40+
X2 = [[0.5 0.6] # 2
41+
[0.6 0.7]] # 5
42+
43+
Output:
44+
Out = [[0.3 0.4] # 1
45+
[0.5 0.6] # 2
46+
[0.1 0.2] # 3
47+
[0.4 0.5] # 4
48+
[0.6 0.7] # 5
49+
[0.2 0.3]] # 6
50+
)DOC");
51+
}
52+
};
53+
54+
class MergeIdsOp : public framework::OperatorWithKernel {
55+
public:
56+
using framework::OperatorWithKernel::OperatorWithKernel;
57+
58+
void InferShape(framework::InferShapeContext *ctx) const override {
59+
PADDLE_ENFORCE(ctx->HasInput("Ids"), "MergeIdsOp must has input Ids.");
60+
PADDLE_ENFORCE(ctx->HasInputs("X"), "MergeIdsOp must has input X.");
61+
PADDLE_ENFORCE(ctx->HasOutput("Out"), "MergeIdsOp must has output Out.");
62+
63+
auto ids_var_type = ctx->GetInputsVarType("Ids").front();
64+
auto ids_dims = ctx->GetInputDim("Ids");
65+
if (ids_var_type == framework::proto::VarType::LOD_TENSOR) {
66+
PADDLE_ENFORCE_EQ(ids_dims.size(), 2);
67+
PADDLE_ENFORCE_EQ(ids_dims[1], 1);
68+
}
69+
auto x_var_type = ctx->GetInputsVarType("X");
70+
for (auto &var_type : x_var_type) {
71+
PADDLE_ENFORCE_EQ(var_type, framework::proto::VarType::LOD_TENSOR,
72+
"input X only support lod tensors");
73+
}
74+
ctx->ShareLoD("Ids", "Out");
75+
}
76+
};
77+
78+
class MergeIdsOpInferVarType : public framework::VarTypeInference {
79+
public:
80+
void operator()(const framework::OpDesc &op_desc,
81+
framework::BlockDesc *block) const override {
82+
auto *input_var = block->Var(op_desc.Input("Ids")[0]);
83+
for (auto &out_var : op_desc.Output("Out")) {
84+
block->Var(out_var)->SetType(input_var->GetType());
85+
}
86+
}
87+
};
88+
89+
} // namespace operators
90+
} // namespace paddle
91+
92+
namespace ops = paddle::operators;
93+
REGISTER_OPERATOR(merge_ids, ops::MergeIdsOp, ops::MergeIdsOpMaker,
94+
ops::MergeIdsOpInferVarType);
95+
REGISTER_OP_CPU_KERNEL(
96+
merge_ids, ops::MergeIdsOpKernel<paddle::platform::CPUPlace, int64_t>,
97+
ops::MergeIdsOpKernel<paddle::platform::CPUPlace, float>);

paddle/fluid/operators/merge_ids_op.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <vector>
18+
#include "paddle/fluid/framework/op_registry.h"
19+
#include "paddle/fluid/framework/tensor_util.h"
20+
#include "paddle/fluid/operators/math/selected_rows_functor.h"
21+
22+
namespace paddle {
23+
namespace operators {
24+
25+
template <typename DeviceContext, typename T>
26+
class MergeIdsOpKernel : public framework::OpKernel<T> {
27+
public:
28+
void Compute(const framework::ExecutionContext &ctx) const override {
29+
auto place = ctx.GetPlace();
30+
if (!platform::is_cpu_place(place)) {
31+
PADDLE_THROW("MergeIds do not support GPU kernel");
32+
}
33+
34+
const auto *ids_var = ctx.InputVar("Ids");
35+
PADDLE_ENFORCE(ids_var->IsType<framework::LoDTensor>(),
36+
"only support to merge Ids of LoDTensor");
37+
38+
const auto &ids_tensor = ids_var->Get<framework::LoDTensor>();
39+
const auto &ids_dims = ids_tensor.dims();
40+
const T *ids = ids_tensor.data<T>();
41+
42+
auto x_tensors = ctx.MultiInput<framework::LoDTensor>("X");
43+
44+
auto *out = ctx.Output<framework::LoDTensor>("Out");
45+
46+
int batch_size = 0;
47+
int embedding_size = 0;
48+
for (auto &input : x_tensors) {
49+
if (embedding_size == 0) {
50+
embedding_size = input->dims()[1];
51+
}
52+
PADDLE_ENFORCE_EQ(embedding_size, input->dims()[1],
53+
"embedding size of all input should be the same");
54+
batch_size += input->dims()[0];
55+
}
56+
PADDLE_ENFORCE_EQ(
57+
batch_size, ids_dims[0],
58+
"the batch size of ids and embedding value should be the same");
59+
60+
const size_t shard_num = x_tensors.size();
61+
62+
if (shard_num == 1) {
63+
VLOG(3) << "only one shard, we can copy the data directly";
64+
TensorCopy(ids_tensor, place, out);
65+
} else {
66+
std::vector<int> in_indexs(shard_num, 0);
67+
auto *out_data = out->mutable_data<T>(ids_dims, place);
68+
// copy data from ins[shard_num] to out.
69+
for (int i = 0; i < ids_dims[0]; ++i) {
70+
T id = ids[i];
71+
size_t shard_id = static_cast<size_t>(id) % shard_num;
72+
int index = in_indexs[shard_id];
73+
memcpy(out_data + embedding_size * i,
74+
x_tensors[shard_id]->data<T>() + index * embedding_size,
75+
sizeof(T) * embedding_size);
76+
in_indexs[shard_id] += 1;
77+
}
78+
}
79+
}
80+
};
81+
82+
} // namespace operators
83+
} // namespace paddle

0 commit comments

Comments
 (0)