-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3540 from zchen0211/develop
Gather_op with python op passed
- Loading branch information
Showing
9 changed files
with
187 additions
and
3 deletions.
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
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
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
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
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,72 @@ | ||
/* 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/gather_op.h" | ||
#include "paddle/framework/ddim.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class GatherOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(const framework::InferShapeContext &ctx) const override { | ||
int batch_size = ctx.Input<Tensor>("Index")->dims()[0]; | ||
PADDLE_ENFORCE_GE(batch_size, 0, "Batch size must be >0"); | ||
framework::DDim output_dims(ctx.Input<Tensor>("X")->dims()); | ||
output_dims[0] = batch_size; | ||
ctx.Output<Tensor>("Out")->Resize(output_dims); | ||
} | ||
}; | ||
|
||
class GatherGradOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(const framework::InferShapeContext &ctx) const override { | ||
auto X_grad = ctx.Output<Tensor>(framework::GradVarName("X")); | ||
auto X = ctx.Input<Tensor>("X"); | ||
|
||
X_grad->Resize(X->dims()); | ||
} | ||
}; | ||
|
||
class GatherOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
GatherOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("X", "The source input of gather op"); | ||
AddInput("Index", "The index input of gather op"); | ||
AddOutput("Out", "The output of add op"); | ||
AddComment(R"DOC( | ||
Gather Operator by selecting from the first axis, | ||
Out = X[Index] | ||
)DOC"); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP(gather, ops::GatherOp, ops::GatherOpMaker, gather_grad, | ||
ops::GatherGradOp); | ||
REGISTER_OP_CPU_KERNEL(gather, | ||
ops::GatherOpKernel<paddle::platform::CPUPlace, float>); | ||
REGISTER_OP_CPU_KERNEL( | ||
gather_grad, | ||
ops::GatherGradientOpKernel<paddle::platform::CPUPlace, float>); |
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,20 @@ | ||
/* 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. */ | ||
|
||
#define EIGEN_USE_GPU | ||
#include "paddle/operators/gather_op.h" | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP_GPU_KERNEL(gather, | ||
ops::GatherOpKernel<paddle::platform::GPUPlace, float>); |
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,53 @@ | ||
/* 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 "gather.h" | ||
#include "paddle/framework/eigen.h" | ||
#include "paddle/framework/op_registry.h" | ||
#include "scatter.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
using Tensor = framework::Tensor; | ||
|
||
template <typename Place, typename T> | ||
class GatherOpKernel : public framework::OpKernel { | ||
public: | ||
void Compute(const framework::ExecutionContext &ctx) const override { | ||
auto *X = ctx.Input<Tensor>("X"); | ||
auto *Index = ctx.Input<Tensor>("Index"); | ||
auto *Y = ctx.Output<Tensor>("Out"); | ||
|
||
Y->mutable_data<T>(ctx.GetPlace()); | ||
Gather<T>(ctx.GetPlace(), X, Index, Y); | ||
} | ||
}; | ||
|
||
template <typename Place, typename T> | ||
class GatherGradientOpKernel : public framework::OpKernel { | ||
public: | ||
void Compute(const framework::ExecutionContext &ctx) const override { | ||
auto *Index = ctx.Input<Tensor>("Index"); | ||
auto *dX = ctx.Output<Tensor>(framework::GradVarName("X")); | ||
auto *dO = ctx.Input<Tensor>(framework::GradVarName("Out")); | ||
|
||
dX->mutable_data<T>(ctx.GetPlace()); | ||
ScatterUpdate<T>(ctx.GetPlace(), dO, Index, dX); | ||
} | ||
}; | ||
|
||
} // 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
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,34 @@ | ||
import unittest | ||
from op_test_util import OpTestMeta | ||
from gradient_checker import GradientChecker, create_op | ||
import numpy | ||
import paddle.v2.framework.core as core | ||
from paddle.v2.framework.op import Operator | ||
|
||
|
||
class TestGatherOp(unittest.TestCase): | ||
__metaclass__ = OpTestMeta | ||
|
||
def setUp(self): | ||
self.type = "gather" | ||
xnp = numpy.random.random((10, 20)).astype("float32") | ||
self.inputs = { | ||
'X': xnp, | ||
'Index': numpy.array([1, 3, 5]).astype("int32") | ||
} | ||
self.outputs = {'Out': self.inputs['X'][self.inputs['Index']]} | ||
|
||
|
||
class TestGatherGradOp(GradientChecker): | ||
def test_gather_grad(self): | ||
print 'creating op' | ||
op = create_op("gather") | ||
print 'creating op done' | ||
xnp = numpy.random.random((10, 20)).astype("float32") | ||
inputs = {'X': xnp, 'Index': numpy.array([1, 3, 5]).astype("int32")} | ||
print 'correct before check gradient' | ||
self.check_grad(op, inputs, set("X"), "Out") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |