commit
0b0b3ba16a
@ -0,0 +1,6 @@
|
||||
if(WITH_GPU)
|
||||
nv_library(add_op SRCS add_op.cc add_op.cu DEPS operator op_registry glog ddim)
|
||||
else()
|
||||
cc_library(add_op SRCS add_op.cc DEPS operator op_registry glog ddim)
|
||||
endif()
|
||||
cc_test(add_op_test SRCS add_op_test.cc DEPS add_op)
|
@ -0,0 +1,44 @@
|
||||
#include <paddle/framework/op_registry.h>
|
||||
#include <paddle/framework/tensor.h>
|
||||
#include <paddle/operators/add_op.h>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class AddOp : public framework::OperatorWithKernel {
|
||||
protected:
|
||||
void InferShape(
|
||||
const std::vector<const framework::Tensor *> &inputs,
|
||||
const std::vector<framework::Tensor *> &outputs) const override {
|
||||
PADDLE_ENFORCE(inputs.size() == 2, "Input size of AddOp must be two");
|
||||
PADDLE_ENFORCE(outputs.size() == 1, "Output size of AddOp must be one");
|
||||
PADDLE_ENFORCE(
|
||||
inputs[0] != nullptr && inputs[1] != nullptr && outputs[0] != nullptr,
|
||||
"Inputs/Outputs of AddOp must all be set");
|
||||
PADDLE_ENFORCE(inputs[0]->dims() == inputs[1]->dims(),
|
||||
"Two input of Add Op's dimension must be same.");
|
||||
// Need set dims in Tensor
|
||||
// outputs[0]->set_dims(inputs[0]->dims())
|
||||
}
|
||||
};
|
||||
|
||||
class AddOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
AddOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "The first input of add op");
|
||||
AddInput("Y", "The second input of add op");
|
||||
AddOutput("Out", "The output of add op");
|
||||
AddComment(R"DOC(
|
||||
Two Element Add Operator.
|
||||
|
||||
The equation is: Out = X + Y
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace op
|
||||
} // namespace paddle
|
||||
|
||||
REGISTER_OP(add_two, paddle::operators::AddOp, paddle::operators::AddOpMaker);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
add_two, ::paddle::operators::AddKernel<::paddle::platform::CPUPlace>);
|
@ -0,0 +1,5 @@
|
||||
#include <paddle/operators/add_op.h>
|
||||
#include <paddle/framework/op_registry.h>
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(add_two,
|
||||
paddle::operators::AddKernel<paddle::platform::GPUPlace>);
|
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <glog/logging.h>
|
||||
#include <paddle/framework/operator.h>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename Place>
|
||||
class AddKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const KernelContext &context) const override {
|
||||
LOG(INFO) << "Add kernel in " << typeid(Place).name();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace op
|
||||
} // namespace paddle
|
@ -0,0 +1,9 @@
|
||||
#include <gtest/gtest.h>
|
||||
#define private public
|
||||
#include <paddle/framework/op_registry.h>
|
||||
USE_OP(add_two);
|
||||
TEST(AddOp, GetOpProto) {
|
||||
auto& protos = paddle::framework::OpRegistry::protos();
|
||||
auto it = protos.find("add_two");
|
||||
ASSERT_NE(it, protos.end());
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
"""
|
||||
Default scope function.
|
||||
|
||||
`Paddle` manages Scope as programming language's scope. It just a
|
||||
thread-local stack of Scope. Top of that stack is current scope, the bottom
|
||||
of that stack is all scopes' parent.
|
||||
|
||||
Invoking `create_var/get_var` can `create/get` variable in current scope.
|
||||
Invoking `enter_local_scope/leave_local_scope` can create or destroy local
|
||||
scope.
|
||||
|
||||
A `scoped_function` will take a `function` as input. That function will be
|
||||
invoked in a new local scope.
|
||||
"""
|
||||
|
||||
import paddle.v2.framework.core
|
||||
import threading
|
||||
|
||||
__tl_scope__ = threading.local()
|
||||
|
||||
__all__ = [
|
||||
'get_cur_scope', 'enter_local_scope', 'leave_local_scope', 'create_var',
|
||||
'get_var', 'scoped_function'
|
||||
]
|
||||
|
||||
|
||||
def get_cur_scope():
|
||||
"""
|
||||
Get current scope.
|
||||
:rtype: paddle.v2.framework.core.Scope
|
||||
"""
|
||||
cur_scope_stack = getattr(__tl_scope__, 'cur_scope', None)
|
||||
if cur_scope_stack is None:
|
||||
__tl_scope__.cur_scope = list()
|
||||
if len(__tl_scope__.cur_scope) == 0:
|
||||
__tl_scope__.cur_scope.append(paddle.v2.framework.core.Scope(None))
|
||||
return __tl_scope__.cur_scope[-1]
|
||||
|
||||
|
||||
def enter_local_scope():
|
||||
"""
|
||||
Enter a new local scope
|
||||
"""
|
||||
cur_scope = get_cur_scope()
|
||||
new_scope = paddle.v2.framework.core.Scope(cur_scope)
|
||||
__tl_scope__.cur_scope.append(new_scope)
|
||||
|
||||
|
||||
def leave_local_scope():
|
||||
"""
|
||||
Leave local scope
|
||||
"""
|
||||
__tl_scope__.cur_scope.pop()
|
||||
|
||||
|
||||
def create_var(name):
|
||||
"""
|
||||
create variable in current scope.
|
||||
"""
|
||||
return get_cur_scope().create_var(name)
|
||||
|
||||
|
||||
def get_var(name):
|
||||
"""
|
||||
get variable in current scope.
|
||||
"""
|
||||
return get_cur_scope().get_var(name)
|
||||
|
||||
|
||||
def scoped_function(func):
|
||||
"""
|
||||
invoke `func` in new scope.
|
||||
|
||||
:param func: a callable function that will be run in new scope.
|
||||
:type func: callable
|
||||
"""
|
||||
enter_local_scope()
|
||||
try:
|
||||
func()
|
||||
except:
|
||||
raise
|
||||
finally:
|
||||
leave_local_scope()
|
@ -1 +1,2 @@
|
||||
add_python_test(test_framework test_protobuf.py test_scope.py)
|
||||
add_python_test(test_framework test_protobuf.py test_scope.py
|
||||
test_default_scope_funcs.py)
|
||||
|
@ -0,0 +1,33 @@
|
||||
from paddle.v2.framework.default_scope_funcs import *
|
||||
import unittest
|
||||
|
||||
|
||||
class TestDefaultScopeFuncs(unittest.TestCase):
|
||||
def test_cur_scope(self):
|
||||
self.assertIsNotNone(get_cur_scope())
|
||||
|
||||
def test_none_variable(self):
|
||||
self.assertIsNone(get_var("test"))
|
||||
|
||||
def test_create_var_get_var(self):
|
||||
var_a = create_var("var_a")
|
||||
self.assertIsNotNone(var_a)
|
||||
self.assertIsNotNone(get_cur_scope().get_var('var_a'))
|
||||
enter_local_scope()
|
||||
self.assertIsNotNone(get_cur_scope().get_var('var_a'))
|
||||
leave_local_scope()
|
||||
|
||||
def test_var_get_int(self):
|
||||
def __new_scope__():
|
||||
i = create_var("var_i")
|
||||
self.assertFalse(i.is_int())
|
||||
i.set_int(10)
|
||||
self.assertTrue(i.is_int())
|
||||
self.assertEqual(10, i.get_int())
|
||||
|
||||
for _ in xrange(10):
|
||||
scoped_function(__new_scope__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue