Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into reshape_op_dev
commit
31cbb3432f
@ -0,0 +1,79 @@
|
||||
/* 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/concat_op.h"
|
||||
#include <vector>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
using framework::Tensor;
|
||||
|
||||
class ConcatOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
auto ins = ctx.MultiInput<framework::Tensor>("X");
|
||||
auto *out = ctx.Output<framework::Tensor>("Out");
|
||||
size_t axis = static_cast<size_t>(ctx.Attr<int>("axis"));
|
||||
size_t n = ins.size();
|
||||
|
||||
PADDLE_ENFORCE_GT(n, 1, "Input tensors count should > 1.");
|
||||
|
||||
auto out_dims = ins[0]->dims();
|
||||
size_t in_zero_dims_size = out_dims.size();
|
||||
for (size_t i = 1; i < n; i++) {
|
||||
for (size_t j = 0; j < in_zero_dims_size; j++) {
|
||||
if (j == axis) {
|
||||
out_dims[axis] += ins[i]->dims()[j];
|
||||
continue;
|
||||
}
|
||||
PADDLE_ENFORCE_EQ(out_dims[j], ins[i]->dims()[j],
|
||||
"Input tensors should have the same "
|
||||
"elements except the specify axis.")
|
||||
}
|
||||
}
|
||||
out->Resize(out_dims);
|
||||
}
|
||||
};
|
||||
|
||||
class ConcatOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
ConcatOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "the input tensors of concat operator.").AsDuplicable();
|
||||
AddOutput("Out", "the output tensor of concat operator.");
|
||||
AddComment(R"DOC(
|
||||
Join the input tensors along with the axis.
|
||||
Examples:
|
||||
Input[0] = [[1,2],[3,4]]
|
||||
Input[1] = [[5,6]]
|
||||
axis = 0
|
||||
Output = [[1,2],
|
||||
[3,4],
|
||||
[5,6]]
|
||||
)DOC");
|
||||
AddAttr<int>("axis", "The axis which the inputs will be joined with.")
|
||||
.SetDefault(0);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(concat, ops::ConcatOp, ops::ConcatOpMaker)
|
||||
REGISTER_OP_CPU_KERNEL(concat,
|
||||
ops::ConcatKernel<paddle::platform::CPUPlace, float>)
|
@ -0,0 +1,19 @@
|
||||
/* 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/concat_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
// TODO(Yancey1989) Add GPU kernel
|
@ -0,0 +1,64 @@
|
||||
/* 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 <vector>
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename Place, typename T>
|
||||
class ConcatKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto ins = ctx.MultiInput<framework::Tensor>("X");
|
||||
auto* out = ctx.Output<framework::Tensor>("Out");
|
||||
int64_t axis = static_cast<int64_t>(ctx.Attr<int>("axis"));
|
||||
size_t n = ins.size();
|
||||
size_t output_axis_dim = 0;
|
||||
size_t before = 1, after = 1;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
output_axis_dim += ins[i]->dims()[axis];
|
||||
}
|
||||
auto& input_zero = ins[0];
|
||||
for (int64_t i = 0; i < input_zero->dims().size(); i++) {
|
||||
if (i == axis) {
|
||||
continue;
|
||||
}
|
||||
if (i < axis) {
|
||||
before *= input_zero->dims()[i];
|
||||
} else {
|
||||
after *= input_zero->dims()[i];
|
||||
}
|
||||
}
|
||||
size_t output_offset = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
auto& in = ins[i];
|
||||
auto axis_dim = in->dims()[axis];
|
||||
for (size_t j = 0; j < before; j++) {
|
||||
size_t len = axis_dim * after * sizeof(T);
|
||||
const T* src = in->data<T>() + axis_dim * after * j;
|
||||
T* out_data = out->mutable_data<T>(platform::CPUPlace());
|
||||
T* dest = out_data + output_offset + output_axis_dim * after * j;
|
||||
memcpy(dest, src, len);
|
||||
}
|
||||
output_offset += axis_dim * after;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
File diff suppressed because it is too large
Load Diff
@ -1,72 +0,0 @@
|
||||
import numpy
|
||||
import paddle.v2.framework.core as core
|
||||
from paddle.v2.framework.op import Operator
|
||||
|
||||
|
||||
class OpTestMeta(type):
|
||||
"""
|
||||
Operator Test ClassMeta.
|
||||
|
||||
It injects `test_all` method into user's OperatorTest class, to make Python
|
||||
unittest module run that method.
|
||||
|
||||
The `test_all` read what value is stored in `self`. It use self's values to
|
||||
create and run a operator, and check whether that op is OK or not.
|
||||
|
||||
See `test_add_two_op` for example usage.
|
||||
"""
|
||||
|
||||
def __new__(cls, name, bases, attrs):
|
||||
obj = super(OpTestMeta, cls).__new__(cls, name, bases, attrs)
|
||||
|
||||
def test_all(self):
|
||||
scope = core.Scope()
|
||||
kwargs = dict()
|
||||
places = [core.CPUPlace()]
|
||||
if core.is_compile_gpu():
|
||||
places.append(core.GPUPlace(0))
|
||||
|
||||
for place in places:
|
||||
for in_name in Operator.get_op_input_names(self.type):
|
||||
if hasattr(self, "inputs") and in_name in self.inputs:
|
||||
kwargs[in_name] = in_name
|
||||
var = scope.new_var(in_name).get_tensor()
|
||||
arr = self.inputs[in_name]
|
||||
var.set_dims(arr.shape)
|
||||
var.set(arr, place)
|
||||
else:
|
||||
kwargs[in_name] = "@EMPTY@"
|
||||
|
||||
for out_name in Operator.get_op_output_names(self.type):
|
||||
if not hasattr(self, "outputs"):
|
||||
raise ValueError(
|
||||
"The test op must set self.outputs dict.")
|
||||
if out_name not in self.outputs:
|
||||
raise ValueError("The %s is not in self.outputs dict." %
|
||||
(out_name))
|
||||
kwargs[out_name] = out_name
|
||||
scope.new_var(out_name).get_tensor()
|
||||
|
||||
for attr_name in Operator.get_op_attr_names(self.type):
|
||||
if hasattr(self, "attrs") and attr_name in self.attrs:
|
||||
kwargs[attr_name] = self.attrs[attr_name]
|
||||
|
||||
op = Operator(self.type, **kwargs)
|
||||
if isinstance(place, core.GPUPlace) and not op.support_gpu():
|
||||
return
|
||||
|
||||
op.infer_shape(scope)
|
||||
|
||||
ctx = core.DeviceContext.create(place)
|
||||
op.run(scope, ctx)
|
||||
|
||||
for out_name in Operator.get_op_output_names(self.type):
|
||||
actual = numpy.array(scope.find_var(out_name).get_tensor())
|
||||
expect = self.outputs[out_name]
|
||||
self.assertTrue(
|
||||
numpy.allclose(
|
||||
actual, expect, atol=1e-05),
|
||||
"output name: " + out_name + " has diff")
|
||||
|
||||
obj.test_all = test_all
|
||||
return obj
|
@ -1,23 +1,20 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
import numpy
|
||||
import paddle.v2.framework.core as core
|
||||
from paddle.v2.framework.op import Operator
|
||||
|
||||
from op_test_util import OpTestMeta
|
||||
|
||||
|
||||
class TestAddOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestAddOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "add"
|
||||
self.op_type = "add"
|
||||
self.inputs = {
|
||||
'X': numpy.random.random((102, 105)).astype("float32"),
|
||||
'Y': numpy.random.random((102, 105)).astype("float32")
|
||||
'X': np.random.random((102, 105)).astype("float32"),
|
||||
'Y': np.random.random((102, 105)).astype("float32")
|
||||
}
|
||||
self.outputs = {'Out': self.inputs['X'] + self.inputs['Y']}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -0,0 +1,22 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestConcatOp(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "concat"
|
||||
x0 = np.random.random((2, 3, 2, 5)).astype('float32')
|
||||
x1 = np.random.random((2, 3, 3, 5)).astype('float32')
|
||||
x2 = np.random.random((2, 3, 4, 5)).astype('float32')
|
||||
axis = 2
|
||||
self.inputs = {'X': [('x0', x0), ('x1', x1), ('x2', x2)]}
|
||||
self.attrs = {'axis': axis}
|
||||
self.outputs = {'Out': np.concatenate((x0, x1, x2), axis=axis)}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -1,16 +1,17 @@
|
||||
import unittest
|
||||
from op_test_util import OpTestMeta
|
||||
import numpy
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestFillZerosLikeOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestFillZerosLikeOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "fill_zeros_like"
|
||||
self.inputs = {'Src': numpy.random.random((219, 232)).astype("float32")}
|
||||
self.outputs = {'Dst': numpy.zeros_like(self.inputs['Src'])}
|
||||
self.op_type = "fill_zeros_like"
|
||||
self.inputs = {'Src': np.random.random((219, 232)).astype("float32")}
|
||||
self.outputs = {'Dst': np.zeros_like(self.inputs["Src"])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,31 +1,22 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test_util import OpTestMeta
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestLookupTableOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestLookupTableOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = 'lookup_table'
|
||||
table = np.random.random((17, 31)).astype('float32')
|
||||
ids = np.random.randint(0, 17, 4).astype('int32')
|
||||
self.op_type = "lookup_table"
|
||||
table = np.random.random((17, 31)).astype("float32")
|
||||
ids = np.random.randint(0, 17, 4).astype("int32")
|
||||
self.inputs = {'W': table, 'Ids': ids}
|
||||
self.outputs = {'Out': table[ids]}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class TestLookupTableGradOp(GradientChecker):
|
||||
def test_grad(self):
|
||||
op = create_op('lookup_table')
|
||||
table = np.random.random((17, 31)).astype('float32')
|
||||
ids = np.random.randint(0, 17, 4).astype('int32')
|
||||
inputs = {'W': table, 'Ids': ids}
|
||||
# comapre gradients
|
||||
self.compare_grad(op, inputs, set(['Ids']))
|
||||
# check gradients
|
||||
self.check_grad(op, inputs, set('W'), 'Out')
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['W'], 'Out', no_grad_set=set('Ids'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue