|
|
|
@ -18,7 +18,6 @@ namespace paddle {
|
|
|
|
|
namespace operators {
|
|
|
|
|
|
|
|
|
|
using Tensor = framework::Tensor;
|
|
|
|
|
using LoDTensor = framework::LoDTensor;
|
|
|
|
|
|
|
|
|
|
class MultiplexOp : public framework::OperatorWithKernel {
|
|
|
|
|
public:
|
|
|
|
@ -26,24 +25,31 @@ class MultiplexOp : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Ids"),
|
|
|
|
|
"Input(Ids) shouldn't be null.");
|
|
|
|
|
PADDLE_ENFORCE(!ctx.MultiInputVar("X").empty(),
|
|
|
|
|
"Input(X) should not be null");
|
|
|
|
|
"MultiInput(X) shouldn't be empty.");
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Out"),
|
|
|
|
|
"Output(Out) shouldn't be null.");
|
|
|
|
|
auto ids_dim = ctx.Input<Tensor>("Ids")->dims();
|
|
|
|
|
PADDLE_ENFORCE(
|
|
|
|
|
ids_dim.size() == 2 && ids_dim[1] == 1,
|
|
|
|
|
"The index tensor must be a vector with size batchSize x 1.");
|
|
|
|
|
|
|
|
|
|
auto ins = ctx.MultiInput<Tensor>("X");
|
|
|
|
|
auto *out = ctx.Output<LoDTensor>("Out");
|
|
|
|
|
auto *out = ctx.Output<Tensor>("Out");
|
|
|
|
|
auto num_ins = ins.size();
|
|
|
|
|
PADDLE_ENFORCE(num_ins > 2,
|
|
|
|
|
"multiplex operator should have more than 2 inputs.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(ins[0]->dims().size(), 1,
|
|
|
|
|
"The first input must be a index vector.");
|
|
|
|
|
auto in_dim = ins[1]->dims();
|
|
|
|
|
|
|
|
|
|
for (size_t i = 2; i < num_ins; i++) {
|
|
|
|
|
PADDLE_ENFORCE(num_ins > 1,
|
|
|
|
|
"multiplex operator should have more than "
|
|
|
|
|
"one candidate input tensors.");
|
|
|
|
|
|
|
|
|
|
auto in_dim = ins[0]->dims();
|
|
|
|
|
PADDLE_ENFORCE(in_dim.size() >= 2,
|
|
|
|
|
"The rank of candidate tensors must be not less than 2.");
|
|
|
|
|
for (size_t i = 1; i < num_ins; i++) {
|
|
|
|
|
auto dim = ins[i]->dims();
|
|
|
|
|
PADDLE_ENFORCE(
|
|
|
|
|
in_dim == dim,
|
|
|
|
|
"All the input tensors except the first one must have the same size");
|
|
|
|
|
PADDLE_ENFORCE(in_dim == dim,
|
|
|
|
|
"All the candidate tensors must have the same size.");
|
|
|
|
|
}
|
|
|
|
|
out->Resize(in_dim);
|
|
|
|
|
}
|
|
|
|
@ -54,25 +60,25 @@ class MultiplexOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
MultiplexOpMaker(framework::OpProto *proto,
|
|
|
|
|
framework::OpAttrChecker *op_checker)
|
|
|
|
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
|
|
|
|
AddInput("X", "The input tensors of multiplex operator.").AsDuplicable();
|
|
|
|
|
AddInput("Ids", "The index tensor of multiplex operator.");
|
|
|
|
|
AddInput("X", "The candidate tensors of multiplex operator.")
|
|
|
|
|
.AsDuplicable();
|
|
|
|
|
AddOutput("Out", "The output tensor of multiplex operator.");
|
|
|
|
|
AddComment(R"DOC(Multiplex operator
|
|
|
|
|
|
|
|
|
|
Multiplex multiple tensors according to the index provided by the first
|
|
|
|
|
input tensor.
|
|
|
|
|
Multiplex multiple tensors according to the index provided by the index tensor.
|
|
|
|
|
|
|
|
|
|
ins[0]: the index tensor.
|
|
|
|
|
ins[1:N]: the candidate output tensors.
|
|
|
|
|
Ids: the index tensor.
|
|
|
|
|
X[0 : N - 1]: the candidate tensors for output (N >= 2).
|
|
|
|
|
For each index i from 0 to batchSize - 1, the output is the i-th row of the
|
|
|
|
|
the (index[i] + 1)-th tensor.
|
|
|
|
|
the (Ids[i])-th tensor.
|
|
|
|
|
|
|
|
|
|
For i-th row of the output tensor:
|
|
|
|
|
|
|
|
|
|
y[i][j] = x_{k}[i][j], j = 0,1, ... , (x_{1}.width - 1)
|
|
|
|
|
y[i] = x_{k}[i]
|
|
|
|
|
|
|
|
|
|
where y is the output tensor. `x_{k}` is the k-th input tensor
|
|
|
|
|
and `k = x{0}[i] + 1`.
|
|
|
|
|
|
|
|
|
|
and `k = Ids[i]`.
|
|
|
|
|
)DOC");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -84,15 +90,15 @@ class MultiplexGradOp : public framework::OperatorWithKernel {
|
|
|
|
|
protected:
|
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(!ctx.MultiInputVar("X").empty(),
|
|
|
|
|
"Input(X) should not be null");
|
|
|
|
|
"Input(X) should not be null.");
|
|
|
|
|
PADDLE_ENFORCE(!ctx.MultiOutputVar(framework::GradVarName("X")).empty(),
|
|
|
|
|
"Output(X@Grad) should not be null");
|
|
|
|
|
"Output(X@Grad) should not be null.");
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
|
|
|
|
|
"Input(Out@GRAD) shouldn't be null.");
|
|
|
|
|
auto d_ins = ctx.MultiOutput<LoDTensor>(framework::GradVarName("X"));
|
|
|
|
|
"Input(Out@GRAD) should not be null.");
|
|
|
|
|
auto d_ins = ctx.MultiOutput<Tensor>(framework::GradVarName("X"));
|
|
|
|
|
auto ins = ctx.MultiInput<Tensor>("X");
|
|
|
|
|
// don't compute gradient for index (ins[0])
|
|
|
|
|
for (size_t i = 1; i < ins.size(); i++) {
|
|
|
|
|
// No need to compute gradient for Input(Ids)
|
|
|
|
|
for (size_t i = 0; i < ins.size(); i++) {
|
|
|
|
|
if (d_ins[i]) {
|
|
|
|
|
d_ins[i]->Resize(ins[i]->dims());
|
|
|
|
|
}
|
|
|
|
|