Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
e8304bd9
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e8304bd9
编写于
7月 19, 2017
作者:
Y
Yu Yang
提交者:
GitHub
7月 19, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2945 from reyoung/feature/grouped_ops
Skeleton Of fully connected operator
上级
b90780c3
0a0b4caa
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
195 addition
and
26 deletion
+195
-26
paddle/framework/attr_checker.h
paddle/framework/attr_checker.h
+35
-0
paddle/framework/net.cc
paddle/framework/net.cc
+4
-2
paddle/framework/net.h
paddle/framework/net.h
+2
-3
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+5
-1
paddle/operators/fc_op.cc
paddle/operators/fc_op.cc
+76
-0
paddle/pybind/CMakeLists.txt
paddle/pybind/CMakeLists.txt
+1
-1
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+28
-18
python/paddle/v2/framework/tests/CMakeLists.txt
python/paddle/v2/framework/tests/CMakeLists.txt
+1
-1
python/paddle/v2/framework/tests/test_fc_op.py
python/paddle/v2/framework/tests/test_fc_op.py
+43
-0
未找到文件。
paddle/framework/attr_checker.h
浏览文件 @
e8304bd9
...
...
@@ -4,6 +4,7 @@
#include <functional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "paddle/framework/enforce.h"
...
...
@@ -41,6 +42,35 @@ class DefaultValueSetter {
T
default_value_
;
};
template
<
typename
T
>
class
EnumInContainer
{
public:
explicit
EnumInContainer
(
const
std
::
unordered_set
<
T
>&
c
)
:
container_
(
c
)
{}
void
operator
()(
T
&
val
)
const
{
PADDLE_ENFORCE
(
container_
.
find
(
val
)
!=
container_
.
end
(),
"Value %s is not in enum container %s"
,
val
,
ContainerDebugString
());
}
private:
std
::
string
ContainerDebugString
()
const
{
std
::
ostringstream
sout
;
sout
<<
"["
;
size_t
cnt
=
0
;
for
(
auto
&
v
:
container_
)
{
sout
<<
v
;
++
cnt
;
if
(
cnt
!=
container_
.
size
())
{
sout
<<
" ,"
;
}
}
sout
<<
"]"
;
return
sout
.
str
();
}
std
::
unordered_set
<
T
>
container_
;
};
// check whether a certain attribute fit its limits
// an attribute can have more than one limits
template
<
typename
T
>
...
...
@@ -50,6 +80,11 @@ class TypedAttrChecker {
public:
TypedAttrChecker
(
const
std
::
string
&
attr_name
)
:
attr_name_
(
attr_name
)
{}
TypedAttrChecker
&
InEnum
(
const
std
::
unordered_set
<
T
>&
range
)
{
value_checkers_
.
push_back
(
EnumInContainer
<
T
>
(
range
));
return
*
this
;
}
TypedAttrChecker
&
LargerThan
(
const
T
&
lower_bound
)
{
value_checkers_
.
push_back
(
LargerThanChecker
<
T
>
(
lower_bound
));
return
*
this
;
...
...
paddle/framework/net.cc
浏览文件 @
e8304bd9
...
...
@@ -19,7 +19,10 @@
namespace
paddle
{
namespace
framework
{
void
PlainNet
::
CompleteAddOp
()
{
void
PlainNet
::
CompleteAddOp
(
bool
calc
)
{
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
{
...
...
paddle/framework/net.h
浏览文件 @
e8304bd9
...
...
@@ -16,7 +16,6 @@ limitations under the License. */
#include <paddle/framework/op_desc.pb.h>
#include <paddle/framework/operator.h>
#include "paddle/framework/net_proto.pb.h"
#include "paddle/framework/op_proto.pb.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/scope.h"
...
...
@@ -41,7 +40,7 @@ namespace framework {
class
Net
:
public
OperatorBase
{
public:
virtual
void
AddOp
(
const
OperatorPtr
&
op
)
=
0
;
virtual
void
CompleteAddOp
()
=
0
;
virtual
void
CompleteAddOp
(
bool
calc
)
=
0
;
};
using
NetPtr
=
std
::
shared_ptr
<
Net
>
;
...
...
@@ -86,7 +85,7 @@ class PlainNet : public Net {
ops_
.
push_back
(
op
);
}
void
CompleteAddOp
()
override
;
void
CompleteAddOp
(
bool
calculate
=
true
)
override
;
std
::
string
DebugString
()
const
override
;
...
...
paddle/operators/CMakeLists.txt
浏览文件 @
e8304bd9
...
...
@@ -27,7 +27,8 @@ function(op_library TARGET)
endif
()
list
(
LENGTH cu_srcs cu_srcs_len
)
if
(
${
cu_srcs_len
}
EQUAL 0
)
list
(
LENGTH op_library_DEPS dep_len
)
if
(
${
cu_srcs_len
}
EQUAL 0 AND
${
dep_len
}
EQUAL 0
)
message
(
WARNING
"The op library
${
TARGET
}
not support GPU!"
)
endif
()
...
...
@@ -47,3 +48,6 @@ op_library(mul_op SRCS mul_op.cc mul_op.cu)
op_library
(
rowwise_add_op SRCS rowwise_add_op.cu rowwise_add_op.cc
)
op_library
(
sigmoid_op SRCS sigmoid_op.cu sigmoid_op.cc
)
op_library
(
softmax_op SRCS softmax_op.cc softmax_op.cu
)
op_library
(
fc_op SRCS fc_op.cc DEPS mul_op rowwise_add_op sigmoid_op
softmax_op net
)
paddle/operators/fc_op.cc
0 → 100644
浏览文件 @
e8304bd9
/* 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"
)},
{
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
);
paddle/pybind/CMakeLists.txt
浏览文件 @
e8304bd9
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
)
paddle/pybind/pybind.cc
浏览文件 @
e8304bd9
...
...
@@ -14,6 +14,7 @@ limitations under the License. */
#include <Python.h>
#include <paddle/framework/op_registry.h>
#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"
,
[](
py
::
bytes
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
();
}
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
e8304bd9
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
)
python/paddle/v2/framework/tests/test_fc_op.py
0 → 100644
浏览文件 @
e8304bd9
import
paddle.v2.framework.core
as
core
import
unittest
import
numpy
import
paddle.v2.framework.create_op_creation_methods
as
creation
class
TestFc
(
unittest
.
TestCase
):
def
test_fc
(
self
):
scope
=
core
.
Scope
(
None
)
x
=
scope
.
create_var
(
"X"
)
x_tensor
=
x
.
get_tensor
()
x_tensor
.
set_dims
([
1000
,
784
])
x_tensor
.
alloc_float
()
w
=
scope
.
create_var
(
"W"
)
w_tensor
=
w
.
get_tensor
()
w_tensor
.
set_dims
([
784
,
100
])
w_tensor
.
alloc_float
()
w_tensor
.
set
(
numpy
.
random
.
random
((
784
,
100
)).
astype
(
"float32"
))
# Set a real numpy array here.
# x_tensor.set(numpy.array([]))
op
=
creation
.
op_creations
.
fc
(
X
=
"X"
,
Y
=
"Y"
,
W
=
"W"
)
for
out
in
op
.
outputs
():
if
scope
.
get_var
(
out
)
is
None
:
scope
.
create_var
(
out
).
get_tensor
()
tensor
=
scope
.
get_var
(
"Y"
).
get_tensor
()
op
.
infer_shape
(
scope
)
self
.
assertEqual
([
1000
,
100
],
tensor
.
shape
())
ctx
=
core
.
DeviceContext
.
cpu_context
()
op
.
run
(
scope
,
ctx
)
# After complete all ops, check Y is expect or not.
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录