-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Feature/add op test #2947
Feature/add op test #2947
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,10 @@ | |
namespace paddle { | ||
namespace framework { | ||
|
||
void PlainNet::CompleteAddOp() { | ||
void PlainNet::CompleteAddOp(bool calc) { | ||
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. 为什么需要 calc? 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. 因为其实继承产生的Net,是不需要重新计算哪些输入和输出的。 |
||
add_op_done_ = true; | ||
if (!calc) return; | ||
|
||
std::unordered_set<std::string> input_set; | ||
std::unordered_set<std::string> output_set; | ||
std::unordered_set<std::string> temp_output; | ||
|
@@ -52,7 +55,6 @@ void PlainNet::CompleteAddOp() { | |
} | ||
|
||
attrs_["temporary_index"] = tmp_index; | ||
add_op_done_ = true; | ||
} | ||
|
||
std::string PlainNet::DebugString() const { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* 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/framework/net.h" | ||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/framework/operator.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class FullyConnectedOp : public framework::PlainNet { | ||
public: | ||
void Init() override { | ||
AddOp(framework::OpRegistry::CreateOp("mul", | ||
{ | ||
Input("X"), Input("W"), | ||
}, | ||
{Output("before_act")}, | ||
{})); | ||
auto b = Input("b"); | ||
if (b != framework::OperatorBase::EMPTY_VAR_NAME()) { | ||
AddOp(framework::OpRegistry::CreateOp("rowwise_add", | ||
{Output("before_act"), Input("b")}, | ||
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. Is |
||
{Output("before_act")}, | ||
{})); | ||
} | ||
|
||
auto activation = GetAttr<std::string>("activation"); | ||
AddOp(framework::OpRegistry::CreateOp( | ||
activation, {Output("before_act")}, {Output("Y")}, {})); | ||
CompleteAddOp(false); | ||
} | ||
}; | ||
|
||
class FullyConnectedOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
FullyConnectedOpMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("X", "the input of fc operator"); | ||
AddInput("W", "the weight of fc operator"); | ||
AddInput("b", "the bias of fc operator"); | ||
|
||
AddOutput("Y", "the output of fc operator"); | ||
AddOutput( | ||
"before_act", "the before activation output of fc operator", true); | ||
AddAttr<std::string>("activation", "The activation key for fc layer") | ||
.SetDefault("sigmoid") | ||
.InEnum({"sigmoid", "softmax"}); | ||
|
||
//! TODO(yuyang18): Complete comment; | ||
AddComment("FullyConnected Operator"); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
|
||
USE_OP(mul); | ||
USE_OP(rowwise_add); | ||
USE_OP(sigmoid); | ||
USE_OP(softmax); | ||
|
||
REGISTER_OP(fc, | ||
paddle::operators::FullyConnectedOp, | ||
paddle::operators::FullyConnectedOpMaker); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python | ||
add_op mul_op rowwise_add_op sigmoid_op softmax_op) | ||
add_op fc_op) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ limitations under the License. */ | |
|
||
#include <Python.h> | ||
#include <paddle/framework/op_registry.h> | ||
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. #include "paddle/framework/op_registry.h" others same. 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. I will give another PR fix them all. |
||
#include <paddle/framework/operator.h> | ||
#include <paddle/framework/scope.h> | ||
#include <paddle/pybind/tensor_bind.h> | ||
#include <pybind11/numpy.h> | ||
|
@@ -26,10 +27,7 @@ namespace py = pybind11; | |
namespace pd = paddle::framework; | ||
|
||
USE_OP(add_two); | ||
USE_OP(softmax); | ||
USE_OP(mul); | ||
USE_OP(rowwise_add); | ||
USE_OP(sigmoid); | ||
USE_OP_WITHOUT_KERNEL(fc); | ||
|
||
PYBIND11_PLUGIN(core) { | ||
py::module m("core", "C++ core of Paddle Paddle"); | ||
|
@@ -53,7 +51,9 @@ PYBIND11_PLUGIN(core) { | |
self.mutable_data<int>(paddle::platform::CPUPlace()); | ||
}) | ||
.def("set", paddle::pybind::PyTensorSetFromArray<float>) | ||
.def("set", paddle::pybind::PyTensorSetFromArray<int>); | ||
.def("set", paddle::pybind::PyTensorSetFromArray<int>) | ||
.def("shape", | ||
[](pd::Tensor& self) { return pd::vectorize(self.dims()); }); | ||
|
||
py::class_<pd::Variable>(m, "Variable", R"DOC(Variable Class. | ||
|
||
|
@@ -83,15 +83,16 @@ All parameter, weight, gradient are variables in Paddle. | |
|
||
//! @note: Be careful! PyBind will return std::string as an unicode, not | ||
//! Python str. If you want a str object, you should cast them in Python. | ||
m.def("get_all_op_protos", []() -> std::vector<std::string> { | ||
m.def("get_all_op_protos", []() -> std::vector<py::bytes> { | ||
auto& protos = pd::OpRegistry::protos(); | ||
std::vector<std::string> ret_values; | ||
std::vector<py::bytes> ret_values; | ||
for (auto it = protos.begin(); it != protos.end(); ++it) { | ||
PADDLE_ENFORCE(it->second.IsInitialized(), | ||
"OpProto must all be initialized"); | ||
ret_values.emplace_back(); | ||
PADDLE_ENFORCE(it->second.SerializeToString(&ret_values.back()), | ||
std::string str; | ||
PADDLE_ENFORCE(it->second.SerializeToString(&str), | ||
"Serialize OpProto Error. This could be a bug of Paddle."); | ||
ret_values.push_back(py::bytes(str)); | ||
} | ||
return ret_values; | ||
}); | ||
|
@@ -101,17 +102,26 @@ All parameter, weight, gradient are variables in Paddle. | |
.def("empty", pd::OperatorBase::EMPTY_VAR_NAME) | ||
.def("temp", pd::OperatorBase::TMP_VAR_NAME); | ||
|
||
py::class_<paddle::platform::DeviceContext>(m, "DeviceContext") | ||
.def_static("cpu_context", []() -> paddle::platform::DeviceContext* { | ||
return new paddle::platform::CPUDeviceContext(); | ||
}); | ||
|
||
py::class_<pd::OperatorBase, pd::OperatorPtr>(m, "Operator") | ||
.def("__str__", &pd::OperatorBase::DebugString) | ||
.def_static("create", [](const std::string& protobin) { | ||
pd::OpDesc desc; | ||
PADDLE_ENFORCE(desc.ParsePartialFromString(protobin), | ||
"Cannot parse user input to OpDesc"); | ||
PADDLE_ENFORCE(desc.IsInitialized(), | ||
"User OpDesc is not initialized, reason %s", | ||
desc.InitializationErrorString()); | ||
return pd::OpRegistry::CreateOp(desc); | ||
}); | ||
.def_static("create", | ||
[](const std::string& protobin) { | ||
pd::OpDesc desc; | ||
PADDLE_ENFORCE(desc.ParsePartialFromString(protobin), | ||
"Cannot parse user input to OpDesc"); | ||
PADDLE_ENFORCE(desc.IsInitialized(), | ||
"User OpDesc is not initialized, reason %s", | ||
desc.InitializationErrorString()); | ||
return pd::OpRegistry::CreateOp(desc); | ||
}) | ||
.def("infer_shape", &pd::OperatorBase::InferShape) | ||
.def("run", &pd::OperatorBase::Run) | ||
.def("outputs", [](const pd::OperatorPtr& op) { return op->outputs_; }); | ||
|
||
return m.ptr(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,10 @@ def __impl__(*args, **kwargs): | |
return core.Operator.create(opdesc.SerializeToString()) | ||
|
||
__impl__.__doc__ = get_docstring_from_op_proto(op_proto) | ||
__impl__.all_input_args = [var.name for var in op_proto.inputs] | ||
__impl__.all_output_args = [var.name for var in op_proto.outputs] | ||
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. I think following is better all_input_names
all_output_names
all_attrs_names 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. It is not |
||
__impl__.all_attr_args = [attr.name for attr in op_proto.attrs] | ||
|
||
return __impl__ | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
add_python_test(test_framework test_protobuf.py test_scope.py | ||
test_default_scope_funcs.py test_op_creation_methods.py | ||
test_tensor.py) | ||
test_tensor.py test_fc_op.py test_add_two_op.py) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import paddle.v2.framework.core as core | ||
import unittest | ||
import numpy | ||
import paddle.v2.framework.create_op_creation_methods as creation | ||
|
||
|
||
class OpTestMeta(type): | ||
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. can you explain the Principle of this meta class? |
||
def __new__(cls, name, bases, attrs): | ||
obj = super(OpTestMeta, cls).__new__(cls, name, bases, attrs) | ||
|
||
def test_all(self): | ||
func = getattr(creation.op_creations, self.type, None) | ||
self.assertIsNotNone(func) | ||
|
||
scope = core.Scope(None) | ||
kwargs = dict() | ||
|
||
for in_name in func.all_input_args: | ||
if hasattr(self, in_name): | ||
kwargs[in_name] = in_name | ||
var = scope.create_var(in_name).get_tensor() | ||
arr = getattr(self, in_name) | ||
var.set_dims(arr.shape) | ||
var.set(arr) | ||
else: | ||
kwargs[in_name] = "@EMPTY@" | ||
|
||
for out_name in func.all_output_args: | ||
if hasattr(self, out_name): | ||
kwargs[out_name] = out_name | ||
scope.create_var(out_name).get_tensor() | ||
|
||
for attr_name in func.all_attr_args: | ||
if hasattr(self, attr_name): | ||
kwargs[attr_name] = getattr(self, attr_name) | ||
|
||
op = func(**kwargs) | ||
|
||
op.infer_shape(scope) | ||
|
||
ctx = core.DeviceContext.cpu_context() | ||
op.run(scope, ctx) | ||
|
||
for out_name in func.all_output_args: | ||
actual = numpy.array(scope.get_var(out_name).get_tensor()) | ||
expect = getattr(self, out_name) | ||
numpy.testing.assert_almost_equal(actual, expect) | ||
|
||
obj.test_all = test_all | ||
return obj |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import unittest | ||
from op_test_util import OpTestMeta | ||
import numpy | ||
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.
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. I think it doesn't matter. |
||
|
||
|
||
class TestAddOp(unittest.TestCase): | ||
__metaclass__ = OpTestMeta | ||
|
||
def setUp(self): | ||
self.type = "add_two" | ||
self.X = numpy.random.random((342, 345)).astype("float32") | ||
self.Y = numpy.random.random((342, 345)).astype("float32") | ||
self.Out = self.X + self.Y | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
this class is used as checker, so if EnumInContainerChecker a better name?
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.
If that names are really matter, I prefer add a sub namespace to do this.