Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
47e5978f
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
338
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
47e5978f
编写于
5月 18, 2018
作者:
朔-望
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add clang-format clang-tidy hook
上级
ab5a35c2
变更
76
显示空白变更内容
内联
并排
Showing
76 changed file
with
17667 addition
and
18947 deletion
+17667
-18947
.pre-commit-config.yaml
.pre-commit-config.yaml
+10
-1
src/common/log.h
src/common/log.h
+106
-107
src/common/type_define.h
src/common/type_define.h
+11
-12
src/common/types.h
src/common/types.h
+26
-26
src/common/variant.h
src/common/variant.h
+56
-56
src/framework/attribute.h
src/framework/attribute.h
+85
-87
src/framework/block_desc.cpp
src/framework/block_desc.cpp
+16
-16
src/framework/block_desc.h
src/framework/block_desc.h
+25
-28
src/framework/data_layout.h
src/framework/data_layout.h
+30
-27
src/framework/data_transform.cpp
src/framework/data_transform.cpp
+54
-54
src/framework/data_transform.h
src/framework/data_transform.h
+7
-7
src/framework/data_type.h
src/framework/data_type.h
+18
-18
src/framework/ddim.cc
src/framework/ddim.cc
+310
-312
src/framework/ddim.h
src/framework/ddim.h
+132
-135
src/framework/dim.h
src/framework/dim.h
+350
-368
src/framework/executor.cpp
src/framework/executor.cpp
+56
-59
src/framework/executor.h
src/framework/executor.h
+15
-15
src/framework/framework.pb.cpp
src/framework/framework.pb.cpp
+7986
-8584
src/framework/framework.pb.h
src/framework/framework.pb.h
+4928
-5426
src/framework/lod_tensor.cc
src/framework/lod_tensor.cc
+275
-284
src/framework/lod_tensor.h
src/framework/lod_tensor.h
+181
-185
src/framework/op_desc.cpp
src/framework/op_desc.cpp
+45
-48
src/framework/op_desc.h
src/framework/op_desc.h
+18
-20
src/framework/op_info.h
src/framework/op_info.h
+69
-70
src/framework/op_kernel_type.h
src/framework/op_kernel_type.h
+31
-38
src/framework/op_proto_maker.h
src/framework/op_proto_maker.h
+4
-4
src/framework/operator.cpp
src/framework/operator.cpp
+16
-16
src/framework/operator.h
src/framework/operator.h
+51
-55
src/framework/paddle_mobile_object.h
src/framework/paddle_mobile_object.h
+9
-9
src/framework/program.cpp
src/framework/program.cpp
+1
-1
src/framework/program.h
src/framework/program.h
+10
-10
src/framework/program_desc.cpp
src/framework/program_desc.cpp
+11
-11
src/framework/program_desc.h
src/framework/program_desc.h
+11
-13
src/framework/scope.cc
src/framework/scope.cc
+98
-98
src/framework/scope.h
src/framework/scope.h
+38
-38
src/framework/selected_rows.h
src/framework/selected_rows.h
+41
-42
src/framework/tensor.h
src/framework/tensor.h
+303
-309
src/framework/tensor_util.cc
src/framework/tensor_util.cc
+183
-185
src/framework/tensor_util.h
src/framework/tensor_util.h
+31
-31
src/framework/var_desc.cpp
src/framework/var_desc.cpp
+3
-3
src/framework/var_desc.h
src/framework/var_desc.h
+52
-53
src/framework/var_type.h
src/framework/var_type.h
+12
-12
src/framework/variable.h
src/framework/variable.h
+66
-66
src/io.cpp
src/io.cpp
+349
-361
src/io.h
src/io.h
+7
-8
src/memory/t_malloc.cc
src/memory/t_malloc.cc
+22
-22
src/memory/t_malloc.h
src/memory/t_malloc.h
+32
-32
src/operators/conv_op.cpp
src/operators/conv_op.cpp
+27
-27
src/operators/conv_op.h
src/operators/conv_op.h
+22
-23
src/operators/elementwise_add_op.cpp
src/operators/elementwise_add_op.cpp
+5
-5
src/operators/elementwise_add_op.h
src/operators/elementwise_add_op.h
+21
-24
src/operators/kernel/arm/conv_kernel.cpp
src/operators/kernel/arm/conv_kernel.cpp
+119
-128
src/operators/kernel/arm/elementwise_add_kernel.cpp
src/operators/kernel/arm/elementwise_add_kernel.cpp
+11
-11
src/operators/kernel/arm/mul_kernel.cpp
src/operators/kernel/arm/mul_kernel.cpp
+24
-26
src/operators/kernel/conv_kernel.h
src/operators/kernel/conv_kernel.h
+6
-7
src/operators/kernel/elementwise_add_kernel.h
src/operators/kernel/elementwise_add_kernel.h
+5
-5
src/operators/kernel/fpga/conv_kernel.cpp
src/operators/kernel/fpga/conv_kernel.cpp
+1
-1
src/operators/kernel/mul_kernel.h
src/operators/kernel/mul_kernel.h
+5
-5
src/operators/math/elementwise_op_function.h
src/operators/math/elementwise_op_function.h
+144
-148
src/operators/math/im2col.cc
src/operators/math/im2col.cc
+237
-270
src/operators/math/im2col.h
src/operators/math/im2col.h
+13
-15
src/operators/math/math_function.cc
src/operators/math/math_function.cc
+85
-100
src/operators/math/math_function.h
src/operators/math/math_function.h
+13
-15
src/operators/math/transform.h
src/operators/math/transform.h
+15
-17
src/operators/math/vol2col.cc
src/operators/math/vol2col.cc
+165
-177
src/operators/math/vol2col.h
src/operators/math/vol2col.h
+11
-15
src/operators/mul_op.cpp
src/operators/mul_op.cpp
+24
-25
src/operators/mul_op.h
src/operators/mul_op.h
+20
-21
src/operators/op_param.cpp
src/operators/op_param.cpp
+19
-19
src/operators/op_param.h
src/operators/op_param.h
+169
-179
test/common/test_log.cpp
test/common/test_log.cpp
+3
-2
test/elementwise_add_op_test.h
test/elementwise_add_op_test.h
+136
-141
test/mul_op_test.h
test/mul_op_test.h
+161
-173
test/test_include.h
test/test_include.h
+0
-1
tools/pre-commit.hooks/.clang-format.hook
tools/pre-commit.hooks/.clang-format.hook
+15
-0
tools/pre-commit.hooks/.clang-tidy.hook
tools/pre-commit.hooks/.clang-tidy.hook
+1
-5
未找到文件。
.pre-commit-config.yaml
浏览文件 @
47e5978f
...
...
@@ -20,11 +20,20 @@ repos:
-
id
:
trailing-whitespace
files
:
(src).*\.(md|py|mm|swift|java|c|cc|cxx|cpp|cu|h|hpp|hxx)$
-
repo
:
local
hooks
:
-
id
:
clang-format
name
:
clang-format
description
:
Format files with ClangFormat.
entry
:
bash ./tools/pre-commit.hooks/.clang-format.hook -i
language
:
system
files
:
\.(c|cc|cxx|cpp|h|hpp|hxx)$
-
repo
:
local
hooks
:
-
id
:
clang-tidy
name
:
clang-tidy
description
:
Format files with
tidy.
description
:
Check C++ code style using clang-
tidy.
entry
:
bash ./tools/pre-commit.hooks/.clang-tidy.hook -i
language
:
system
files
:
(src).*\.(c|cc|cxx|cpp|h|hpp|hxx)$
...
...
src/common/log.h
浏览文件 @
47e5978f
...
...
@@ -27,7 +27,7 @@ SOFTWARE.
namespace
paddle_mobile
{
enum
LogLevel
{
enum
LogLevel
{
kNO_LOG
,
kLOG_ERROR
,
kLOG_WARNING
,
...
...
@@ -37,18 +37,18 @@ namespace paddle_mobile {
kLOG_DEBUG2
,
kLOG_DEBUG3
,
kLOG_DEBUG4
};
};
// log level
static
LogLevel
log_level
=
kLOG_DEBUG4
;
// log level
static
LogLevel
log_level
=
kLOG_DEBUG4
;
static
std
::
vector
<
std
::
string
>
logs
{
"NO"
,
"ERROR "
,
"WARNING"
,
static
std
::
vector
<
std
::
string
>
logs
{
"NO"
,
"ERROR "
,
"WARNING"
,
"INFO "
,
"DEBUG "
,
"DEBUG1 "
,
"DEBUG2 "
,
"DEBUG3 "
,
"DEBUG4 "
};
struct
ToLog
;
struct
Print
;
struct
ToLog
;
struct
Print
;
struct
Print
{
struct
Print
{
friend
struct
ToLog
;
template
<
typename
T
>
Print
&
operator
<<
(
T
const
&
value
)
{
...
...
@@ -66,9 +66,9 @@ namespace paddle_mobile {
}
}
std
::
ostringstream
buffer_
;
};
};
struct
ToLog
{
struct
ToLog
{
ToLog
(
LogLevel
level
=
kLOG_DEBUG
,
const
std
::
string
&
info
=
""
)
:
level_
(
level
)
{
unsigned
blanks
=
...
...
@@ -87,16 +87,16 @@ namespace paddle_mobile {
private:
LogLevel
level_
;
Print
printer_
;
};
};
#define LOG(level) \
if (level > paddle_mobile::log_level) { \
} else \
paddle_mobile::ToLog( \
level, \
(std::stringstream() \
level, (std::stringstream() \
<< "[file: " \
<< (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \
<< (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) \
: __FILE__) \
<< "] [line: " << __LINE__ << "] ") \
.str())
...
...
@@ -107,10 +107,11 @@ namespace paddle_mobile {
paddle_mobile::kLOG_DEBUG, \
(std::stringstream() \
<< "[file: " \
<< (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \
<< (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) \
: __FILE__) \
<< "] [line: " << __LINE__ << "] ") \
.str())
}
}
// namespace paddle_mobile
#define LOGF(level, format, ...) \
if (level > paddle_mobile::log_level) { \
...
...
@@ -126,7 +127,7 @@ namespace paddle_mobile {
namespace
paddle_mobile
{
enum
LogLevel
{
enum
LogLevel
{
kNO_LOG
,
kLOG_ERROR
,
kLOG_WARNING
,
...
...
@@ -136,23 +137,21 @@ namespace paddle_mobile {
kLOG_DEBUG2
,
kLOG_DEBUG3
,
kLOG_DEBUG4
};
};
struct
ToLog
;
struct
Print
{
struct
ToLog
;
struct
Print
{
friend
struct
ToLog
;
template
<
typename
T
>
Print
&
operator
<<
(
T
const
&
value
)
{}
private:
};
};
struct
ToLog
{
struct
ToLog
{
ToLog
(
LogLevel
level
)
{}
template
<
typename
T
>
ToLog
&
operator
<<
(
T
const
&
value
)
{
return
*
this
;
}
};
template
<
typename
T
>
ToLog
&
operator
<<
(
T
const
&
value
)
{
return
*
this
;
}
};
#define LOG(level) \
if (true) { \
...
...
@@ -167,6 +166,6 @@ namespace paddle_mobile {
#define LOGF(level, format, ...)
#define DLOGF(format, ...)
}
}
// namespace paddle_mobile
#endif
src/common/type_define.h
浏览文件 @
47e5978f
...
...
@@ -24,30 +24,29 @@ SOFTWARE.
namespace
paddle_mobile
{
namespace
framework
{
template
<
typename
Dtype
>
class
OperatorBase
;
template
<
typename
Dtype
>
class
OperatorBase
;
class
OpDesc
;
class
BlockDesc
;
class
InferShapeContext
;
}
}
// namespace framework
using
VariableNameMap
=
std
::
map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
;
template
<
typename
Dtype
>
template
<
typename
Dtype
>
using
OpCreator
=
std
::
function
<
framework
::
OperatorBase
<
Dtype
>
*
(
const
std
::
string
&
/*type*/
,
const
VariableNameMap
&
/*inputs*/
,
const
VariableNameMap
&
/*outputs*/
,
const
framework
::
AttributeMap
&
/*attrs*/
)
>
;
using
GradOpMakerFN
=
std
::
function
<
std
::
vector
<
std
::
unique_ptr
<
framework
::
OpDesc
>>
(
std
::
function
<
std
::
vector
<
std
::
unique_ptr
<
framework
::
OpDesc
>>
(
const
framework
::
OpDesc
&
,
const
std
::
unordered_set
<
std
::
string
>
&
/*no_grad_set*/
,
std
::
unordered_map
<
std
::
string
,
std
::
string
>
*
/*grad_to_var*/
,
const
std
::
vector
<
framework
::
BlockDesc
*>
&
grad_block
)
>
;
using
InferVarTypeFN
=
std
::
function
<
void
(
const
framework
::
OpDesc
&
/*op_desc*/
,
using
InferVarTypeFN
=
std
::
function
<
void
(
const
framework
::
OpDesc
&
/*op_desc*/
,
framework
::
BlockDesc
*
/*block*/
)
>
;
using
InferShapeFN
=
std
::
function
<
void
(
framework
::
InferShapeContext
*
)
>
;
};
};
// namespace paddle_mobile
src/common/types.h
浏览文件 @
47e5978f
...
...
@@ -24,7 +24,7 @@ enum class Precision : int { FP32 = 0 };
//! device type
enum
DeviceTypeEnum
{
kINVALID
=
-
1
,
kCPU
=
0
,
kFPGA
=
1
,
kGPU_MALI
=
2
};
template
<
DeviceTypeEnum
T
>
struct
DeviceType
{};
template
<
DeviceTypeEnum
T
>
struct
DeviceType
{};
typedef
DeviceType
<
kCPU
>
CPU
;
typedef
DeviceType
<
kFPGA
>
FPGA
;
...
...
@@ -60,4 +60,4 @@ enum PMStatus {
PMUnImplError
=
0x07
,
/*!< Unimplement error. */
PMWrongDevice
=
0x08
/*!< un-correct device. */
};
}
}
// namespace paddle_mobile
src/common/variant.h
浏览文件 @
47e5978f
...
...
@@ -21,9 +21,9 @@ SOFTWARE.
#pragma once
namespace
paddle_mobile
{
template
<
int
ID
,
typename
Type
>
struct
IDToType
{
typedef
Type
type_t
;
};
template
<
int
ID
,
typename
Type
>
struct
IDToType
{
typedef
Type
type_t
;
};
template
<
typename
F
,
typename
...
Ts
>
struct
VariantHelper
{
template
<
typename
F
,
typename
...
Ts
>
struct
VariantHelper
{
static
const
size_t
size
=
sizeof
(
F
)
>
VariantHelper
<
Ts
...
>::
size
?
sizeof
(
F
)
:
VariantHelper
<
Ts
...
>::
size
;
...
...
@@ -37,7 +37,7 @@ template<typename F, typename... Ts> struct VariantHelper {
}
};
template
<
typename
F
>
struct
VariantHelper
<
F
>
{
template
<
typename
F
>
struct
VariantHelper
<
F
>
{
static
const
size_t
size
=
sizeof
(
F
);
inline
static
void
Destroy
(
size_t
id
,
void
*
data
)
{
if
(
id
==
typeid
(
F
).
hash_code
())
{
...
...
@@ -48,8 +48,8 @@ template<typename F> struct VariantHelper<F> {
}
};
template
<
size_t
size
>
class
RawData
{
public:
template
<
size_t
size
>
class
RawData
{
public:
char
data
[
size
];
RawData
()
{}
RawData
(
const
RawData
&
raw_data
)
{
strcpy
(
data
,
raw_data
.
data
);
}
...
...
@@ -58,7 +58,7 @@ public:
// }
};
template
<
typename
...
Ts
>
struct
Variant
{
template
<
typename
...
Ts
>
struct
Variant
{
Variant
(
const
Variant
&
variant
)
{
// std::cout << " 赋值构造函数 " << std::endl;
type_id
=
variant
.
type_id
;
...
...
@@ -70,13 +70,13 @@ template<typename... Ts> struct Variant {
// helper::Destroy(type_id, &data);
}
template
<
typename
T
,
typename
...
Args
>
void
Set
(
Args
&&
...
args
)
{
template
<
typename
T
,
typename
...
Args
>
void
Set
(
Args
&&
...
args
)
{
helper
::
Destroy
(
type_id
,
&
data
);
new
(
&
data
)
T
(
std
::
forward
<
Args
>
(
args
)...);
new
(
&
data
)
T
(
std
::
forward
<
Args
>
(
args
)...);
type_id
=
typeid
(
T
).
hash_code
();
}
template
<
typename
T
>
T
&
Get
()
const
{
template
<
typename
T
>
T
&
Get
()
const
{
if
(
type_id
==
typeid
(
T
).
hash_code
())
{
return
*
const_cast
<
T
*>
(
reinterpret_cast
<
const
T
*>
(
&
data
));
}
else
{
...
...
@@ -87,13 +87,13 @@ template<typename... Ts> struct Variant {
size_t
TypeId
()
const
{
return
type_id
;
}
private:
private:
static
inline
size_t
invalid_type
()
{
return
typeid
(
void
).
hash_code
();
}
typedef
VariantHelper
<
Ts
...
>
helper
;
size_t
type_id
;
RawData
<
helper
::
size
>
data
;
};
template
<
typename
T
>
struct
Vistor
{
typedef
T
type_t
;
};
template
<
typename
T
>
struct
Vistor
{
typedef
T
type_t
;
};
}
// namespace paddle_mobile
src/framework/attribute.h
浏览文件 @
47e5978f
...
...
@@ -27,9 +27,8 @@ namespace framework {
class
BlockDesc
;
class
Attribute
{
public:
static
Attribute
GetAttrValue
(
const
proto
::
OpDesc
::
Attr
&
attr_desc
)
{
public:
static
Attribute
GetAttrValue
(
const
proto
::
OpDesc
::
Attr
&
attr_desc
)
{
// std::cout << "begin get attr value" << std::endl;
Attribute
attr
;
switch
(
attr_desc
.
type
())
{
...
...
@@ -94,36 +93,35 @@ public:
}
Attribute
()
{}
template
<
typename
T
,
typename
...
Args
>
Attribute
&
Set
(
Args
&&
...
args
)
{
template
<
typename
T
,
typename
...
Args
>
Attribute
&
Set
(
Args
&&
...
args
)
{
variant_
.
Set
<
T
>
(
args
...);
return
*
this
;
}
template
<
typename
T
>
T
&
Get
()
const
{
return
variant_
.
Get
<
T
>
();
}
template
<
typename
T
>
T
&
Get
()
const
{
return
variant_
.
Get
<
T
>
();
}
private:
Variant
<
int
,
float
,
std
::
string
,
std
::
vector
<
in
t
>
,
std
::
vector
<
float
>
,
std
::
vector
<
std
::
string
>
,
bool
,
std
::
vector
<
bool
>
,
BlockDesc
*
,
int64_t
>
private:
Variant
<
int
,
float
,
std
::
string
,
std
::
vector
<
int
>
,
std
::
vector
<
floa
t
>
,
std
::
vector
<
std
::
string
>
,
bool
,
std
::
vector
<
bool
>
,
BlockDesc
*
,
int64_t
>
variant_
;
};
using
AttributeMap
=
std
::
unordered_map
<
std
::
string
,
Attribute
>
;
class
AttrReader
{
public:
public:
explicit
AttrReader
(
const
AttributeMap
&
attrs
)
:
attrs_
(
attrs
)
{}
template
<
typename
T
>
inline
T
Get
(
const
std
::
string
&
name
)
const
{
template
<
typename
T
>
inline
T
Get
(
const
std
::
string
&
name
)
const
{
// PADDLE_ENFORCE(attrs_.count(name) != 0, "%s should
// be in
// AttributeMap",
// name);
return
((
Attribute
)
attrs_
.
at
(
name
)).
Get
<
T
>
();
return
((
Attribute
)
attrs_
.
at
(
name
)).
Get
<
T
>
();
}
private:
private:
const
AttributeMap
&
attrs_
;
};
...
...
src/framework/block_desc.cpp
浏览文件 @
47e5978f
src/framework/block_desc.h
浏览文件 @
47e5978f
...
...
@@ -27,29 +27,26 @@ namespace paddle_mobile {
namespace
framework
{
class
BlockDesc
:
PaddleMobileObject
{
public:
public:
BlockDesc
(
const
proto
::
BlockDesc
&
desc
);
const
int
&
ID
()
const
{
return
desc_
.
idx
();
}
const
int
&
Parent
()
const
{
return
desc_
.
parent_idx
();
}
bool
operator
==
(
const
paddle_mobile
::
framework
::
BlockDesc
&
in_block
)
const
{
bool
operator
==
(
const
paddle_mobile
::
framework
::
BlockDesc
&
in_block
)
const
{
return
this
->
ID
()
==
in_block
.
ID
()
&&
this
->
Parent
()
==
in_block
.
Parent
();
}
bool
operator
<
(
const
paddle_mobile
::
framework
::
BlockDesc
&
in_block
)
const
{
return
this
->
ID
()
<
in_block
.
ID
()
&&
this
->
Parent
()
<
in_block
.
Parent
();
bool
operator
<
(
const
paddle_mobile
::
framework
::
BlockDesc
&
in_block
)
const
{
return
this
->
ID
()
<
in_block
.
ID
()
&&
this
->
Parent
()
<
in_block
.
Parent
();
}
std
::
vector
<
std
::
shared_ptr
<
VarDesc
>>
Vars
()
const
;
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
Ops
()
const
;
private:
private:
proto
::
BlockDesc
desc_
;
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops_
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
VarDesc
>>
vars_
;
...
...
@@ -60,7 +57,7 @@ private:
namespace
std
{
template
<
>
struct
hash
<
paddle_mobile
::
framework
::
BlockDesc
>
{
template
<
>
struct
hash
<
paddle_mobile
::
framework
::
BlockDesc
>
{
typedef
paddle_mobile
::
framework
::
BlockDesc
argument_type
;
typedef
std
::
size_t
result_type
;
result_type
operator
()(
argument_type
const
&
s
)
const
noexcept
{
...
...
src/framework/data_layout.h
浏览文件 @
47e5978f
...
...
@@ -46,16 +46,19 @@ inline DataLayout StringToDataLayout(const std::string &str) {
inline
std
::
string
DataLayoutToString
(
const
DataLayout
&
data_layout
)
{
switch
(
data_layout
)
{
case
DataLayout
::
kNHWC
:
return
"NHWC"
;
case
DataLayout
::
kNCHW
:
return
"NCHW"
;
case
DataLayout
::
kAnyLayout
:
return
"ANY_LAYOUT"
;
default:
break
;
case
DataLayout
::
kNHWC
:
return
"NHWC"
;
case
DataLayout
::
kNCHW
:
return
"NCHW"
;
case
DataLayout
::
kAnyLayout
:
return
"ANY_LAYOUT"
;
default:
break
;
// std::cout << "unknown DataLayou %d", data_layout;
}
}
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
DataLayout
&
l
)
{
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
DataLayout
&
l
)
{
out
<<
DataLayoutToString
(
l
);
return
out
;
}
...
...
src/framework/data_transform.cpp
浏览文件 @
47e5978f
...
...
@@ -68,8 +68,8 @@ void DataTransform(const OpKernelType &expected_kernel_type,
output_tensor
->
ShareDataWith
(
in
);
}
void
CopyVariableWithTensor
(
const
Variable
&
in_var
,
const
Tensor
&
tensor
,
Variable
&
out_var
)
{
void
CopyVariableWithTensor
(
const
Variable
&
in_var
,
const
Tensor
&
tensor
,
Variable
&
out_var
)
{
// if (in_var.IsType<LoDTensor>()) {
// auto& in_lod_tensor = in_var.Get<LoDTensor>();
// auto* tran_lod_tensor = out_var.GetMutable<LoDTensor>();
...
...
src/framework/data_transform.h
浏览文件 @
47e5978f
...
...
@@ -28,14 +28,14 @@ SOFTWARE.
#include "variable.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
void
DataTransform
(
const
OpKernelType
&
expected_kernel_type
,
void
DataTransform
(
const
OpKernelType
&
expected_kernel_type
,
const
OpKernelType
&
kernel_type_for_var
,
const
Tensor
&
input_tensor
,
Tensor
*
out
);
void
CopyVariableWithTensor
(
const
Variable
&
in_va
r
,
const
Tensor
&
tensor
,
Variable
&
out_var
);
void
CopyVariableWithTensor
(
const
Variable
&
in_var
,
const
Tensor
&
tenso
r
,
Variable
&
out_var
);
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/data_type.h
浏览文件 @
47e5978f
...
...
@@ -21,23 +21,23 @@ SOFTWARE.
#include "framework.pb.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
// inline proto::VarType::Type ToDataType(std::type_index type) {
// using namespace paddle_mobile::framework::proto;
// if (typeid(float).hash_code() == type.hash_code()) {
// return proto::VarType::FP32;
// } else if (typeid(double).hash_code() == type.hash_code()) {
// return proto::VarType::FP64;
// } else if (typeid(int).hash_code() == type.hash_code()) {
// return proto::VarType::INT32;
// } else if (typeid(int64_t).hash_code() == type.hash_code()) {
// return proto::VarType::INT64;
// } else if (typeid(bool).hash_code() == type.hash_code()) {
// return proto::VarType::BOOL;
// } else {
//// PADDLE_THROW("Not supported");
// }
// }
}
// inline proto::VarType::Type ToDataType(std::type_index type) {
// using namespace paddle_mobile::framework::proto;
// if (typeid(float).hash_code() == type.hash_code()) {
// return proto::VarType::FP32;
// } else if (typeid(double).hash_code() == type.hash_code()) {
// return proto::VarType::FP64;
// } else if (typeid(int).hash_code() == type.hash_code()) {
// return proto::VarType::INT32;
// } else if (typeid(int64_t).hash_code() == type.hash_code()) {
// return proto::VarType::INT64;
// } else if (typeid(bool).hash_code() == type.hash_code()) {
// return proto::VarType::BOOL;
// } else {
//// PADDLE_THROW("Not supported");
// }
// }
}
}
// namespace paddle_mobile
src/framework/ddim.cc
浏览文件 @
47e5978f
...
...
@@ -15,17 +15,17 @@ limitations under the License. */
#include "ddim.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
/// @cond HIDDEN
/// @cond HIDDEN
template
<
int
i
>
Dim
<
i
>
make_dim
(
const
int64_t
*
d
)
{
template
<
int
i
>
Dim
<
i
>
make_dim
(
const
int64_t
*
d
)
{
return
Dim
<
i
>
(
*
d
,
make_dim
<
i
-
1
>
(
d
+
1
));
}
}
template
<
>
Dim
<
0
>
make_dim
<
0
>
(
const
int64_t
*
d
)
{
return
Dim
<
0
>
(
*
d
);
}
template
<
>
Dim
<
0
>
make_dim
<
0
>
(
const
int64_t
*
d
)
{
return
Dim
<
0
>
(
*
d
);
}
void
make_ddim
(
DDim
&
ddim
,
const
int64_t
*
dims
,
int
n
)
{
void
make_ddim
(
DDim
&
ddim
,
const
int64_t
*
dims
,
int
n
)
{
switch
(
n
)
{
case
0
:
ddim
=
make_dim
<
0
>
(
dims
);
...
...
@@ -63,33 +63,33 @@ namespace paddle_mobile {
// dimensions.";
break
;
}
}
}
/// @endcond
/// @endcond
DDim
make_ddim
(
std
::
initializer_list
<
int64_t
>
dims
)
{
DDim
make_ddim
(
std
::
initializer_list
<
int64_t
>
dims
)
{
DDim
result
(
make_dim
(
0
));
make_ddim
(
result
,
dims
.
begin
(),
dims
.
size
());
return
result
;
}
}
DDim
make_ddim
(
const
std
::
vector
<
int64_t
>
&
dims
)
{
DDim
make_ddim
(
const
std
::
vector
<
int64_t
>
&
dims
)
{
DDim
result
(
make_dim
(
0
));
make_ddim
(
result
,
&
dims
[
0
],
dims
.
size
());
return
result
;
}
}
DDim
make_ddim
(
const
std
::
vector
<
int
>
&
dims
)
{
DDim
make_ddim
(
const
std
::
vector
<
int
>
&
dims
)
{
std
::
vector
<
int64_t
>
res
(
dims
.
size
());
std
::
transform
(
dims
.
begin
(),
dims
.
end
(),
res
.
begin
(),
[](
int
d
)
{
return
static_cast
<
int64_t
>
(
d
);
});
return
make_ddim
(
res
);
}
}
/// @cond HIDDEN
// XXX For some reason, putting this in an anonymous namespace causes
// errors
struct
DynamicMutableIndexer
:
Vistor
<
int64_t
&>
{
/// @cond HIDDEN
// XXX For some reason, putting this in an anonymous namespace causes
// errors
struct
DynamicMutableIndexer
:
Vistor
<
int64_t
&>
{
public:
explicit
DynamicMutableIndexer
(
int
idx
)
:
idx_
(
idx
)
{}
...
...
@@ -99,9 +99,9 @@ namespace paddle_mobile {
private:
int
idx_
;
};
};
struct
DynamicConstIndexer
:
public
Vistor
<
int64_t
>
{
struct
DynamicConstIndexer
:
public
Vistor
<
int64_t
>
{
public:
explicit
DynamicConstIndexer
(
int
idx
)
:
idx_
(
idx
)
{}
...
...
@@ -111,21 +111,21 @@ namespace paddle_mobile {
private:
int
idx_
;
};
};
/// @endcond
/// @endcond
int64_t
&
DDim
::
operator
[](
int
idx
)
{
int64_t
&
DDim
::
operator
[](
int
idx
)
{
return
DDim
::
ApplyVistor
(
DynamicMutableIndexer
(
idx
),
*
this
);
}
}
int64_t
DDim
::
operator
[](
int
idx
)
const
{
int64_t
DDim
::
operator
[](
int
idx
)
const
{
return
DDim
::
ApplyVistor
(
DynamicConstIndexer
(
idx
),
*
this
);
}
}
int
DDim
::
size
()
const
{
return
arity
(
*
this
);
}
int
DDim
::
size
()
const
{
return
arity
(
*
this
);
}
bool
DDim
::
operator
==
(
DDim
d
)
const
{
bool
DDim
::
operator
==
(
DDim
d
)
const
{
// if (var.which() != d.getVar().which()) {
// return false;
// } else {
...
...
@@ -140,11 +140,11 @@ namespace paddle_mobile {
return
true
;
// }
}
}
bool
DDim
::
operator
!=
(
DDim
d
)
const
{
return
!
(
*
this
==
d
);
}
bool
DDim
::
operator
!=
(
DDim
d
)
const
{
return
!
(
*
this
==
d
);
}
DDim
DDim
::
operator
+
(
DDim
d
)
const
{
DDim
DDim
::
operator
+
(
DDim
d
)
const
{
std
::
vector
<
int64_t
>
v1
=
vectorize
(
*
this
);
std
::
vector
<
int64_t
>
v2
=
vectorize
(
d
);
...
...
@@ -157,9 +157,9 @@ namespace paddle_mobile {
}
return
make_ddim
(
v3
);
}
}
DDim
DDim
::
operator
*
(
DDim
d
)
const
{
DDim
DDim
::
operator
*
(
DDim
d
)
const
{
std
::
vector
<
int64_t
>
v1
=
vectorize
(
*
this
);
std
::
vector
<
int64_t
>
v2
=
vectorize
(
d
);
...
...
@@ -172,14 +172,14 @@ namespace paddle_mobile {
}
return
make_ddim
(
v3
);
}
}
int64_t
get
(
const
DDim
&
ddim
,
int
idx
)
{
return
ddim
[
idx
];
}
int64_t
get
(
const
DDim
&
ddim
,
int
idx
)
{
return
ddim
[
idx
];
}
void
set
(
DDim
&
ddim
,
int
idx
,
int
value
)
{
ddim
[
idx
]
=
value
;
}
void
set
(
DDim
&
ddim
,
int
idx
,
int
value
)
{
ddim
[
idx
]
=
value
;
}
/// @cond HIDDEN
struct
VectorizeVisitor
:
Vistor
<
void
>
{
/// @cond HIDDEN
struct
VectorizeVisitor
:
Vistor
<
void
>
{
std
::
vector
<
int64_t
>
&
vector
;
explicit
VectorizeVisitor
(
std
::
vector
<
int64_t
>
&
v
)
:
vector
(
v
)
{}
...
...
@@ -190,36 +190,36 @@ namespace paddle_mobile {
}
void
operator
()(
const
Dim
<
0
>
&
t
)
{}
};
/// @endcond
};
/// @endcond
std
::
vector
<
int64_t
>
vectorize
(
const
DDim
&
ddim
)
{
std
::
vector
<
int64_t
>
vectorize
(
const
DDim
&
ddim
)
{
std
::
vector
<
int64_t
>
result
;
VectorizeVisitor
visitor
(
result
);
DDim
::
ApplyVistor
(
visitor
,
ddim
);
return
result
;
}
}
// NOTE: framework::vectorize converts to type int64_t
// which does not fit cudnn inputs.
std
::
vector
<
int
>
vectorize2int
(
const
DDim
&
ddim
)
{
// NOTE: framework::vectorize converts to type int64_t
// which does not fit cudnn inputs.
std
::
vector
<
int
>
vectorize2int
(
const
DDim
&
ddim
)
{
std
::
vector
<
int64_t
>
temp
=
vectorize
(
ddim
);
std
::
vector
<
int
>
result
(
temp
.
begin
(),
temp
.
end
());
return
result
;
}
}
struct
ProductVisitor
:
Vistor
<
int64_t
>
{
struct
ProductVisitor
:
Vistor
<
int64_t
>
{
template
<
int
D
>
int64_t
operator
()(
const
Dim
<
D
>
&
dim
)
{
return
product
(
dim
);
}
};
};
int64_t
product
(
const
DDim
&
ddim
)
{
int64_t
product
(
const
DDim
&
ddim
)
{
ProductVisitor
visitor
;
return
DDim
::
ApplyVistor
(
visitor
,
ddim
);
}
}
struct
SliceVectorizeVisitor
:
Vistor
<
void
>
{
struct
SliceVectorizeVisitor
:
Vistor
<
void
>
{
std
::
vector
<
int64_t
>
&
vector
;
int
begin
;
int
end
;
...
...
@@ -251,9 +251,9 @@ namespace paddle_mobile {
// PADDLE_ENFORCE(end == 0, "End index in ddim slice is out
// of bound.");
}
};
};
DDim
slice_ddim
(
const
DDim
&
ddim
,
int
begin
,
int
end
)
{
DDim
slice_ddim
(
const
DDim
&
ddim
,
int
begin
,
int
end
)
{
std
::
vector
<
int64_t
>
vec
;
vec
.
reserve
(
end
-
begin
);
SliceVectorizeVisitor
visitor
(
vec
,
begin
,
end
);
...
...
@@ -261,27 +261,27 @@ namespace paddle_mobile {
DDim
::
ApplyVistor
(
visitor
,
ddim
);
// visitor(ddim.var.Get<Dim<4>>());
return
make_ddim
(
vec
);
}
}
/// \cond HIDDEN
/// \cond HIDDEN
struct
ArityVisitor
:
Vistor
<
int
>
{
struct
ArityVisitor
:
Vistor
<
int
>
{
template
<
int
D
>
int
operator
()(
Dim
<
D
>
)
const
{
return
D
;
}
};
};
/// \endcond
/// \endcond
int
arity
(
const
DDim
&
d
)
{
int
arity
(
const
DDim
&
d
)
{
ArityVisitor
arityVisitor
=
ArityVisitor
();
return
DDim
::
ApplyVistor
(
arityVisitor
,
d
);
// return arityVisitor(d.var.Get<Dim<4>>());
// return boost::apply_visitor(ArityVisitor(), d); }
}
/// \cond HIDDEN
}
/// \cond HIDDEN
/// \endcond
/// \endcond
struct
OSVistor
:
Vistor
<
std
::
ostream
&>
{
struct
OSVistor
:
Vistor
<
std
::
ostream
&>
{
OSVistor
(
std
::
ostream
&
os
)
:
os_
(
os
)
{}
template
<
int
D
>
std
::
ostream
&
operator
()(
Dim
<
D
>
dim
)
const
{
...
...
@@ -290,45 +290,43 @@ namespace paddle_mobile {
private:
std
::
ostream
&
os_
;
};
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
DDim
&
ddim
)
{
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
DDim
&
ddim
)
{
auto
vistor
=
OSVistor
(
os
);
DDim
::
ApplyVistor
(
vistor
,
ddim
);
return
os
;
}
}
DDim
::
DDim
(
std
::
initializer_list
<
int64_t
>
init_list
)
{
DDim
::
DDim
(
std
::
initializer_list
<
int64_t
>
init_list
)
{
*
this
=
make_ddim
(
init_list
);
}
}
DDim
flatten_to_2d
(
const
DDim
&
src
,
int
num_col_dims
)
{
DDim
flatten_to_2d
(
const
DDim
&
src
,
int
num_col_dims
)
{
int
rank
=
src
.
size
();
return
make_ddim
({
product
(
slice_ddim
(
src
,
0
,
num_col_dims
)),
product
(
slice_ddim
(
src
,
num_col_dims
,
rank
))});
}
}
DDim
flatten_to_1d
(
const
DDim
&
src
)
{
return
make_ddim
({
product
(
src
)});
}
DDim
flatten_to_1d
(
const
DDim
&
src
)
{
return
make_ddim
({
product
(
src
)});
}
DDim
stride
(
const
DDim
&
ddim
)
{
DDim
stride
(
const
DDim
&
ddim
)
{
std
::
vector
<
int64_t
>
strides
(
ddim
.
size
());
strides
[
ddim
.
size
()
-
1
]
=
1
;
for
(
int
i
=
ddim
.
size
()
-
2
;
i
>=
0
;
--
i
)
{
strides
[
i
]
=
strides
[
i
+
1
]
*
ddim
[
i
+
1
];
}
return
framework
::
make_ddim
(
strides
);
}
}
DDim
stride_numel
(
const
framework
::
DDim
&
ddim
)
{
DDim
stride_numel
(
const
framework
::
DDim
&
ddim
)
{
std
::
vector
<
int64_t
>
strides
(
ddim
.
size
());
strides
[
ddim
.
size
()
-
1
]
=
ddim
[
ddim
.
size
()
-
1
];
for
(
int
i
=
ddim
.
size
()
-
2
;
i
>=
0
;
--
i
)
{
strides
[
i
]
=
strides
[
i
+
1
]
*
ddim
[
i
];
}
return
framework
::
make_ddim
(
strides
);
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/ddim.h
浏览文件 @
47e5978f
...
...
@@ -22,22 +22,21 @@ limitations under the License. */
#include <vector>
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
/**
/**
* \brief A dynamically sized dimension.
*
* The number of dimensions must be between [1, 9].
*/
struct
DDim
{
typedef
Variant
<
Dim
<
0
>
,
Dim
<
1
>
,
Dim
<
2
>
,
Dim
<
3
>
,
Dim
<
4
>
,
Dim
<
5
>
,
Dim
<
6
>
,
Dim
<
7
>
,
Dim
<
8
>
,
Dim
<
9
>>
struct
DDim
{
typedef
Variant
<
Dim
<
0
>
,
Dim
<
1
>
,
Dim
<
2
>
,
Dim
<
3
>
,
Dim
<
4
>
,
Dim
<
5
>
,
Dim
<
6
>
,
Dim
<
7
>
,
Dim
<
8
>
,
Dim
<
9
>>
DDimVar
;
DDimVar
var
;
template
<
typename
Vistor
>
static
typename
Vistor
::
type_t
ApplyVistor
(
Vistor
vistor
,
const
DDim
&
d
)
{
static
typename
Vistor
::
type_t
ApplyVistor
(
Vistor
vistor
,
const
DDim
&
d
)
{
if
(
d
.
var
.
TypeId
()
==
typeid
(
Dim
<
0
>
).
hash_code
())
{
return
vistor
(
d
.
var
.
Get
<
Dim
<
0
>>
());
}
else
if
(
d
.
var
.
TypeId
()
==
typeid
(
Dim
<
1
>
).
hash_code
())
{
...
...
@@ -67,9 +66,7 @@ namespace paddle_mobile {
DDim
()
{
var
.
Set
<
Dim
<
1
>>
(
Dim
<
1
>
());
}
template
<
int
D
>
explicit
DDim
(
const
Dim
<
D
>
&
in
)
{
var
.
Set
<
Dim
<
D
>>
(
in
);
}
template
<
int
D
>
explicit
DDim
(
const
Dim
<
D
>
&
in
)
{
var
.
Set
<
Dim
<
D
>>
(
in
);
}
/*implicit*/
DDim
(
std
::
initializer_list
<
int64_t
>
init_list
);
...
...
@@ -104,63 +101,63 @@ namespace paddle_mobile {
DDim
operator
*
(
DDim
d
)
const
;
int
size
()
const
;
};
};
/**
/**
* \brief Make a DDim from std::vector<int64_t>
*
* \param dims An vector of ints. Must be sized between [1, 9]
*/
DDim
make_ddim
(
const
std
::
vector
<
int64_t
>
&
dims
);
DDim
make_ddim
(
const
std
::
vector
<
int64_t
>
&
dims
);
DDim
make_ddim
(
const
std
::
vector
<
int
>
&
dims
);
DDim
make_ddim
(
const
std
::
vector
<
int
>
&
dims
);
/**
/**
* \brief Make a DDim from an initializer list
*
* \param dims An initializer list of ints. Must be sized between [1, 9]
*
*/
DDim
make_ddim
(
std
::
initializer_list
<
int64_t
>
dims
);
DDim
make_ddim
(
std
::
initializer_list
<
int64_t
>
dims
);
int64_t
get
(
const
DDim
&
dim
,
int
idx
);
int64_t
get
(
const
DDim
&
dim
,
int
idx
);
void
set
(
DDim
&
dim
,
int
idx
,
int
val
);
void
set
(
DDim
&
dim
,
int
idx
,
int
val
);
std
::
vector
<
int64_t
>
vectorize
(
const
DDim
&
ddim
);
std
::
vector
<
int64_t
>
vectorize
(
const
DDim
&
ddim
);
std
::
vector
<
int
>
vectorize2int
(
const
DDim
&
ddim
);
std
::
vector
<
int
>
vectorize2int
(
const
DDim
&
ddim
);
int64_t
product
(
const
DDim
&
ddim
);
int64_t
product
(
const
DDim
&
ddim
);
/**
/**
* \brief Slice a ddim
*
* Slice dim with [begin, end).
* e.g. DDim d = make_ddim({1,2,3,4,5});
* slice_ddim(d, 1, 3); ====> {2,3}
*/
DDim
slice_ddim
(
const
DDim
&
dim
,
int
begin
,
int
end
);
DDim
slice_ddim
(
const
DDim
&
dim
,
int
begin
,
int
end
);
/**
/**
* \brief What is the length of this dimension?
*
* \param Dynamic dimension to inspect
*/
int
arity
(
const
DDim
&
ddim
);
int
arity
(
const
DDim
&
ddim
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
,
const
DDim
&
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
,
const
DDim
&
);
// Reshape a tensor to a matrix. The matrix's first dimension(column
// length)
// will be the product of tensor's first `num_col_dims` dimensions.
DDim
flatten_to_2d
(
const
DDim
&
src
,
int
num_col_dims
);
// Reshape a tensor to a matrix. The matrix's first dimension(column
// length)
// will be the product of tensor's first `num_col_dims` dimensions.
DDim
flatten_to_2d
(
const
DDim
&
src
,
int
num_col_dims
);
DDim
flatten_to_1d
(
const
DDim
&
src
);
DDim
flatten_to_1d
(
const
DDim
&
src
);
DDim
stride
(
const
DDim
&
ddim
);
DDim
stride
(
const
DDim
&
ddim
);
DDim
stride_numel
(
const
DDim
&
ddim
);
}
// namespace framework
DDim
stride_numel
(
const
DDim
&
ddim
);
}
// namespace framework
}
// namespace paddle_mobile
src/framework/dim.h
浏览文件 @
47e5978f
...
...
@@ -21,23 +21,20 @@
#include "platform/hostdevice.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
// Statically sized, statically indexed dimension
template
<
int
i
>
struct
Dim
{
// Statically sized, statically indexed dimension
template
<
int
i
>
struct
Dim
{
static
constexpr
int
dimensions
=
i
;
template
<
typename
...
Args
>
HOSTDEVICE
Dim
(
int64_t
_head
,
Args
...
_tail
)
:
head
(
_head
),
tail
(
_tail
...)
{
static_assert
(
sizeof
...(
_tail
)
==
i
-
1
,
HOSTDEVICE
Dim
(
int64_t
_head
,
Args
...
_tail
)
:
head
(
_head
),
tail
(
_tail
...)
{
static_assert
(
sizeof
...(
_tail
)
==
i
-
1
,
"Dim initialized with the wrong number of parameters"
);
}
HOSTDEVICE
Dim
(
int64_t
_head
,
const
Dim
<
i
-
1
>
&
_tail
)
:
head
(
_head
),
tail
(
_tail
)
{}
Dim
(
int64_t
_head
,
const
Dim
<
i
-
1
>
&
_tail
)
:
head
(
_head
),
tail
(
_tail
)
{}
HOSTDEVICE
Dim
()
:
head
(
0
),
tail
()
{}
...
...
@@ -70,10 +67,10 @@ namespace paddle_mobile {
int64_t
head
;
Dim
<
i
-
1
>
tail
;
};
};
// Base case specialization
template
<
>
struct
Dim
<
0
>
{
// Base case specialization
template
<
>
struct
Dim
<
0
>
{
static
constexpr
int
dimensions
=
0
;
HOSTDEVICE
...
...
@@ -103,41 +100,38 @@ namespace paddle_mobile {
int64_t
&
operator
[](
int
idx
);
HOSTDEVICE
int64_t
operator
[](
int
idx
)
const
;
};
};
namespace
{
namespace
{
// Helper for accessing Dim classes
template
<
int
i
>
struct
DimGetter
{
// Helper for accessing Dim classes
template
<
int
i
>
struct
DimGetter
{
// Return a copy if Dim is const
template
<
typename
D
>
HOSTDEVICE
static
int64_t
impl
(
const
D
&
d
)
{
template
<
typename
D
>
HOSTDEVICE
static
int64_t
impl
(
const
D
&
d
)
{
return
DimGetter
<
i
-
1
>::
impl
(
d
.
tail
);
}
// Return a reference if Dim is mutable
template
<
typename
D
>
HOSTDEVICE
static
int64_t
&
impl
(
D
&
d
)
{
return
DimGetter
<
i
-
1
>::
impl
(
d
.
tail
);
}
};
};
// Eureka! We found the element!
template
<
>
struct
DimGetter
<
0
>
{
// Eureka! We found the element!
template
<
>
struct
DimGetter
<
0
>
{
// Return a copy if Dim is const
template
<
typename
D
>
HOSTDEVICE
static
int64_t
impl
(
const
D
&
d
)
{
template
<
typename
D
>
HOSTDEVICE
static
int64_t
impl
(
const
D
&
d
)
{
return
d
.
head
;
}
// Return a reference if Dim is mutable
template
<
typename
D
>
HOSTDEVICE
static
int64_t
&
impl
(
D
&
d
)
{
return
d
.
head
;
}
};
};
template
<
int
D
>
HOSTDEVICE
int64_t
&
indexer
(
Dim
<
D
>
&
dim
,
int
idx
)
{
template
<
int
D
>
HOSTDEVICE
int64_t
&
indexer
(
Dim
<
D
>
&
dim
,
int
idx
)
{
#ifndef __CUDA_ARCH__
if
(
idx
<
0
)
{
throw
std
::
invalid_argument
(
"Tried to access a negative dimension"
);
throw
std
::
invalid_argument
(
"Tried to access a negative dimension"
);
}
#else
PADDLE_ASSERT
(
idx
>=
0
);
...
...
@@ -146,9 +140,9 @@ namespace paddle_mobile {
return
dim
.
head
;
}
return
indexer
(
dim
.
tail
,
idx
-
1
);
}
}
template
<
>
HOSTDEVICE
int64_t
&
indexer
<
0
>
(
Dim
<
0
>
&
dim
,
int
idx
)
{
template
<
>
HOSTDEVICE
int64_t
&
indexer
<
0
>
(
Dim
<
0
>
&
dim
,
int
idx
)
{
#ifndef __CUDA_ARCH__
throw
std
::
invalid_argument
(
"Invalid index"
);
#else
...
...
@@ -162,14 +156,12 @@ namespace paddle_mobile {
#endif
return
head
;
#endif
}
}
template
<
int
D
>
HOSTDEVICE
int64_t
indexer
(
const
Dim
<
D
>
&
dim
,
int
idx
)
{
template
<
int
D
>
HOSTDEVICE
int64_t
indexer
(
const
Dim
<
D
>
&
dim
,
int
idx
)
{
#ifndef __CUDA_ARCH__
if
(
idx
<
0
)
{
throw
std
::
invalid_argument
(
"Tried to access a negative dimension"
);
throw
std
::
invalid_argument
(
"Tried to access a negative dimension"
);
}
#else
PADDLE_ASSERT
(
idx
>=
0
);
...
...
@@ -178,10 +170,9 @@ namespace paddle_mobile {
return
dim
.
head
;
}
return
indexer
(
dim
.
tail
,
idx
-
1
);
}
}
template
<
>
HOSTDEVICE
int64_t
indexer
<
0
>
(
const
Dim
<
0
>
&
dim
,
int
idx
)
{
template
<
>
HOSTDEVICE
int64_t
indexer
<
0
>
(
const
Dim
<
0
>
&
dim
,
int
idx
)
{
#ifndef __CUDA_ARCH__
throw
std
::
invalid_argument
(
"Invalid index"
);
#else
...
...
@@ -195,152 +186,146 @@ namespace paddle_mobile {
#endif
return
head
;
#endif
}
}
}
// namespace
// Static access to constant Dim
template
<
int
i
,
int
l
>
HOSTDEVICE
int64_t
get
(
const
Dim
<
l
>
&
d
)
{
}
// namespace
// Static access to constant Dim
template
<
int
i
,
int
l
>
HOSTDEVICE
int64_t
get
(
const
Dim
<
l
>
&
d
)
{
return
DimGetter
<
i
>::
impl
(
d
);
}
}
// Static access to mutable Dim
template
<
int
i
,
int
l
>
HOSTDEVICE
int64_t
&
get
(
Dim
<
l
>
&
d
)
{
// Static access to mutable Dim
template
<
int
i
,
int
l
>
HOSTDEVICE
int64_t
&
get
(
Dim
<
l
>
&
d
)
{
return
DimGetter
<
i
>::
impl
(
d
);
}
}
// Dynamic access to constant Dim
template
<
int
l
>
HOSTDEVICE
int64_t
Dim
<
l
>::
operator
[](
int
i
)
const
{
// Dynamic access to constant Dim
template
<
int
l
>
HOSTDEVICE
int64_t
Dim
<
l
>::
operator
[](
int
i
)
const
{
// std::cout << "l: " << l << std::endl;
return
indexer
(
*
this
,
i
);
}
}
// Dynamic access to mutable Dim
template
<
int
l
>
HOSTDEVICE
int64_t
&
Dim
<
l
>::
operator
[](
int
i
)
{
// Dynamic access to mutable Dim
template
<
int
l
>
HOSTDEVICE
int64_t
&
Dim
<
l
>::
operator
[](
int
i
)
{
return
indexer
(
*
this
,
i
);
}
}
// Dynamic access to constant Dim
inline
HOSTDEVICE
int64_t
Dim
<
0
>::
operator
[](
int
i
)
const
{
// Dynamic access to constant Dim
inline
HOSTDEVICE
int64_t
Dim
<
0
>::
operator
[](
int
i
)
const
{
return
indexer
(
*
this
,
i
);
}
}
// Dynamic access to mutable Dim
inline
HOSTDEVICE
int64_t
&
Dim
<
0
>::
operator
[](
int
i
)
{
// Dynamic access to mutable Dim
inline
HOSTDEVICE
int64_t
&
Dim
<
0
>::
operator
[](
int
i
)
{
return
indexer
(
*
this
,
i
);
}
}
// Dynamic access to constant Dim
// without std::enable_if will try to instantiate this on get<0>(d)
template
<
int
l
>
HOSTDEVICE
typename
std
::
enable_if
<
(
l
>
0
),
int64_t
>::
type
get
(
const
Dim
<
l
>
&
d
,
int
i
)
{
// Dynamic access to constant Dim
// without std::enable_if will try to instantiate this on get<0>(d)
template
<
int
l
>
HOSTDEVICE
typename
std
::
enable_if
<
(
l
>
0
),
int64_t
>::
type
get
(
const
Dim
<
l
>
&
d
,
int
i
)
{
return
d
[
i
];
}
}
// Dynamic access to mutable Dim
template
<
int
l
>
HOSTDEVICE
typename
std
::
enable_if
<
(
l
>
0
),
int64_t
&>::
type
get
(
Dim
<
l
>
&
d
,
int
i
)
{
// Dynamic access to mutable Dim
template
<
int
l
>
HOSTDEVICE
typename
std
::
enable_if
<
(
l
>
0
),
int64_t
&>::
type
get
(
Dim
<
l
>
&
d
,
int
i
)
{
return
d
[
i
];
}
}
// Dot product of two dims
template
<
int
i
>
HOSTDEVICE
int64_t
linearize
(
const
Dim
<
i
>
&
a
,
const
Dim
<
i
>
&
b
)
{
// Dot product of two dims
template
<
int
i
>
HOSTDEVICE
int64_t
linearize
(
const
Dim
<
i
>
&
a
,
const
Dim
<
i
>
&
b
)
{
return
a
.
head
*
b
.
head
+
linearize
(
a
.
tail
,
b
.
tail
);
}
}
// Base case dot product of two Dims
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
int64_t
linearize
(
const
Dim
<
0
>
&
a
,
const
Dim
<
0
>
&
b
)
{
// Base case dot product of two Dims
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
int64_t
linearize
(
const
Dim
<
0
>
&
a
,
const
Dim
<
0
>
&
b
)
{
return
0
;
}
}
// Product of a Dim
template
<
int
i
>
HOSTDEVICE
int64_t
product
(
const
Dim
<
i
>
&
a
,
int
prod
=
1
)
{
// Product of a Dim
template
<
int
i
>
HOSTDEVICE
int64_t
product
(
const
Dim
<
i
>
&
a
,
int
prod
=
1
)
{
return
prod
*
a
.
head
*
product
(
a
.
tail
);
}
}
// Base case product of a Dim
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
int64_t
product
(
const
Dim
<
0
>
&
a
,
int
prod
)
{
// Base case product of a Dim
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
int64_t
product
(
const
Dim
<
0
>
&
a
,
int
prod
)
{
return
prod
;
}
}
// Is 0 <= idx_i < size_i for all i?
template
<
int
i
>
HOSTDEVICE
bool
contained
(
const
Dim
<
i
>
&
idx
,
const
Dim
<
i
>
&
size
)
{
// Is 0 <= idx_i < size_i for all i?
template
<
int
i
>
HOSTDEVICE
bool
contained
(
const
Dim
<
i
>
&
idx
,
const
Dim
<
i
>
&
size
)
{
return
((
0
<=
idx
.
head
)
&&
(
idx
.
head
<
size
.
head
)
&&
contained
(
idx
.
tail
,
size
.
tail
));
}
}
// Base case of is 0 <= idx_i < size_i ?
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
bool
contained
(
const
Dim
<
0
>
&
idx
,
const
Dim
<
0
>
&
size
)
{
// Base case of is 0 <= idx_i < size_i ?
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
bool
contained
(
const
Dim
<
0
>
&
idx
,
const
Dim
<
0
>
&
size
)
{
return
true
;
}
}
/**
/**
* \brief Compute exclusive prefix-multiply of a Dim.
*/
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
ex_prefix_mul
(
const
Dim
<
i
>
&
src
,
int
mul
=
1
)
{
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
ex_prefix_mul
(
const
Dim
<
i
>
&
src
,
int
mul
=
1
)
{
return
Dim
<
i
>
(
mul
,
ex_prefix_mul
(
src
.
tail
,
mul
*
src
.
head
));
}
}
///\cond HIDDEN
// Base case of ex_prefix_mul
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
ex_prefix_mul
(
const
Dim
<
0
>
&
src
,
int
mul
)
{
///\cond HIDDEN
// Base case of ex_prefix_mul
// Notice it is inline because it is no longer a template
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
ex_prefix_mul
(
const
Dim
<
0
>
&
src
,
int
mul
)
{
return
Dim
<
0
>
();
}
///\endcond
}
///\endcond
/**
/**
* Add two dimensions together
*/
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
dim_plus
(
const
Dim
<
i
>
&
a
,
const
Dim
<
i
>
&
b
)
{
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
dim_plus
(
const
Dim
<
i
>
&
a
,
const
Dim
<
i
>
&
b
)
{
return
Dim
<
i
>
(
a
.
head
+
b
.
head
,
dim_plus
(
a
.
tail
,
b
.
tail
));
}
}
// Base case
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
dim_plus
(
const
Dim
<
0
>
&
a
,
const
Dim
<
0
>
&
b
)
{
// Base case
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
dim_plus
(
const
Dim
<
0
>
&
a
,
const
Dim
<
0
>
&
b
)
{
return
Dim
<
0
>
();
}
}
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
operator
+
(
const
Dim
<
i
>
&
lhs
,
const
Dim
<
i
>
&
rhs
)
{
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
operator
+
(
const
Dim
<
i
>
&
lhs
,
const
Dim
<
i
>
&
rhs
)
{
return
dim_plus
(
lhs
,
rhs
);
}
}
/**
/**
* Multiply two dimensions together
*/
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
dim_mult
(
const
Dim
<
i
>
&
a
,
const
Dim
<
i
>
&
b
)
{
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
dim_mult
(
const
Dim
<
i
>
&
a
,
const
Dim
<
i
>
&
b
)
{
return
Dim
<
i
>
(
a
.
head
*
b
.
head
,
dim_mult
(
a
.
tail
,
b
.
tail
));
}
}
// Base case
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
dim_mult
(
const
Dim
<
0
>
&
a
,
const
Dim
<
0
>
&
b
)
{
// Base case
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
dim_mult
(
const
Dim
<
0
>
&
a
,
const
Dim
<
0
>
&
b
)
{
return
Dim
<
0
>
();
}
}
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
operator
*
(
const
Dim
<
i
>
&
lhs
,
const
Dim
<
i
>
&
rhs
)
{
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
operator
*
(
const
Dim
<
i
>
&
lhs
,
const
Dim
<
i
>
&
rhs
)
{
return
dim_mult
(
lhs
,
rhs
);
}
}
/**
/**
* \brief Normalize strides to ensure any dimension with extent 1
* has stride 0.
*
...
...
@@ -350,25 +335,23 @@ namespace paddle_mobile {
*
*/
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
normalize_strides
(
const
Dim
<
i
>
&
size
,
const
Dim
<
i
>
&
stride
)
{
template
<
int
i
>
HOSTDEVICE
Dim
<
i
>
normalize_strides
(
const
Dim
<
i
>
&
size
,
const
Dim
<
i
>
&
stride
)
{
int
norm_stride
=
size
.
head
==
1
?
0
:
stride
.
head
;
return
Dim
<
i
>
(
norm_stride
,
normalize_strides
(
size
.
tail
,
stride
.
tail
));
}
return
Dim
<
i
>
(
norm_stride
,
normalize_strides
(
size
.
tail
,
stride
.
tail
));
}
///\cond HIDDEN
///\cond HIDDEN
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
normalize_strides
(
const
Dim
<
0
>
&
size
,
template
<
>
HOSTDEVICE
inline
Dim
<
0
>
normalize_strides
(
const
Dim
<
0
>
&
size
,
const
Dim
<
0
>
&
stride
)
{
return
Dim
<
0
>
();
}
}
///\endcond
///\endcond
/**
/**
* Helper function to create a Dim
*
* \param idxes The type of Dim constructed depends on the number of
...
...
@@ -376,44 +359,43 @@ namespace paddle_mobile {
*
*/
template
<
typename
...
Args
>
HOSTDEVICE
Dim
<
sizeof
...(
Args
)
>
make_dim
(
Args
...
idxes
)
{
template
<
typename
...
Args
>
HOSTDEVICE
Dim
<
sizeof
...(
Args
)
>
make_dim
(
Args
...
idxes
)
{
return
Dim
<
sizeof
...(
Args
)
>
(
idxes
...);
}
}
// Allows us to output a Dim
// XXX For some reason, overloading fails to resolve this correctly
template
<
int
i
>
typename
std
::
enable_if
<
(
i
>
1
),
std
::
ostream
&>::
type
operator
<<
(
std
::
ostream
&
os
,
const
Dim
<
i
>
&
d
)
{
// Allows us to output a Dim
// XXX For some reason, overloading fails to resolve this correctly
template
<
int
i
>
typename
std
::
enable_if
<
(
i
>
1
),
std
::
ostream
&>::
type
operator
<<
(
std
::
ostream
&
os
,
const
Dim
<
i
>
&
d
)
{
os
<<
d
.
head
<<
", "
<<
d
.
tail
;
return
os
;
}
}
// Base case that allows us to output a Dim
// XXX I wish this could be an overload instead of a template
template
<
int
i
>
typename
std
::
enable_if
<
(
i
==
1
),
std
::
ostream
&>::
type
operator
<<
(
std
::
ostream
&
os
,
const
Dim
<
i
>
&
d
)
{
// Base case that allows us to output a Dim
// XXX I wish this could be an overload instead of a template
template
<
int
i
>
typename
std
::
enable_if
<
(
i
==
1
),
std
::
ostream
&>::
type
operator
<<
(
std
::
ostream
&
os
,
const
Dim
<
i
>
&
d
)
{
os
<<
d
.
head
;
return
os
;
}
}
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Dim
<
0
>
&
d
)
{
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Dim
<
0
>
&
d
)
{
return
os
;
}
}
template
<
int
i
>
HOST
std
::
string
Dim
<
i
>::
to_string
()
const
{
template
<
int
i
>
HOST
std
::
string
Dim
<
i
>::
to_string
()
const
{
std
::
stringstream
stream
;
stream
<<
*
this
;
return
stream
.
str
();
}
}
template
<
int
D
>
HOSTDEVICE
Dim
<
D
>
linear_to_dimension
(
int
linear_index
,
Dim
<
D
>
extents
)
{
template
<
int
D
>
HOSTDEVICE
Dim
<
D
>
linear_to_dimension
(
int
linear_index
,
Dim
<
D
>
extents
)
{
Dim
<
D
>
result
;
for
(
int
i
=
0
;
i
<
D
-
1
;
++
i
)
{
...
...
@@ -424,7 +406,7 @@ namespace paddle_mobile {
result
[
D
-
1
]
=
linear_index
;
return
result
;
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/executor.cpp
浏览文件 @
47e5978f
...
...
@@ -23,10 +23,10 @@ SOFTWARE.
#include "variable.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
>
Executor
<
Dtype
>::
Executor
(
const
Program
<
Dtype
>
p
)
:
program_
(
p
)
{
template
<
typename
Dtype
>
Executor
<
Dtype
>::
Executor
(
const
Program
<
Dtype
>
p
)
:
program_
(
p
)
{
if
(
use_optimize_
)
{
to_predict_program_
=
program_
.
optimizeProgram
;
}
else
{
...
...
@@ -40,11 +40,9 @@ namespace paddle_mobile {
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
for
(
int
j
=
0
;
j
<
ops
.
size
();
++
j
)
{
std
::
shared_ptr
<
OpDesc
>
op
=
ops
[
j
];
if
(
op
->
Type
()
==
"conv2d"
&&
op
->
Input
(
"Input"
)[
0
]
==
"pixel"
)
{
if
(
op
->
Type
()
==
"conv2d"
&&
op
->
Input
(
"Input"
)[
0
]
==
"pixel"
)
{
Attribute
strides_attr
=
op
->
GetAttrMap
().
at
(
"strides"
);
std
::
vector
<
int
>
stride
=
strides_attr
.
Get
<
std
::
vector
<
int
>>
();
std
::
vector
<
int
>
stride
=
strides_attr
.
Get
<
std
::
vector
<
int
>>
();
for
(
int
k
=
0
;
k
<
stride
.
size
();
++
k
)
{
}
std
::
shared_ptr
<
operators
::
ConvOp
<
Dtype
,
float
>>
conv
=
...
...
@@ -55,10 +53,10 @@ namespace paddle_mobile {
}
}
}
}
}
template
<
typename
Dtype
>
std
::
shared_ptr
<
Tensor
>
Executor
<
Dtype
>::
predict
(
Tensor
&
t
)
{
template
<
typename
Dtype
>
std
::
shared_ptr
<
Tensor
>
Executor
<
Dtype
>::
predict
(
Tensor
&
t
)
{
// feed
auto
scope
=
program_
.
scope
;
Variable
*
g_feed_value
=
scope
->
Var
(
"pixel"
);
...
...
@@ -77,21 +75,20 @@ namespace paddle_mobile {
predict
(
t
,
0
);
return
out_tensor
;
}
}
template
<
typename
Dtype
>
void
Executor
<
Dtype
>::
predict
(
const
Tensor
&
t
,
int
block_id
)
{
template
<
typename
Dtype
>
void
Executor
<
Dtype
>::
predict
(
const
Tensor
&
t
,
int
block_id
)
{
std
::
shared_ptr
<
BlockDesc
>
to_predict_block
=
to_predict_program_
->
Block
(
block_id
);
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
();
++
j
)
{
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
();
++
j
)
{
auto
op
=
ops_of_block_
[
*
to_predict_block
.
get
()][
j
];
// std::cout << "开始run" << std::endl;
op
->
Run
();
}
}
}
template
class
Executor
<
CPU
>;
template
class
Executor
<
CPU
>;
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/executor.h
浏览文件 @
47e5978f
...
...
@@ -32,9 +32,9 @@ SOFTWARE.
#include "variable.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
>
class
Executor
{
template
<
typename
Dtype
>
class
Executor
{
public:
Executor
(
const
Program
<
Dtype
>
p
);
std
::
shared_ptr
<
Tensor
>
predict
(
Tensor
&
t
);
...
...
@@ -47,7 +47,7 @@ namespace paddle_mobile {
std
::
vector
<
std
::
shared_ptr
<
OperatorBase
<
Dtype
>>>>
ops_of_block_
;
bool
use_optimize_
=
false
;
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/framework.pb.cpp
浏览文件 @
47e5978f
因为 它太大了无法显示 source diff 。你可以改为
查看blob
。
src/framework/framework.pb.h
浏览文件 @
47e5978f
因为 它太大了无法显示 source diff 。你可以改为
查看blob
。
src/framework/lod_tensor.cc
浏览文件 @
47e5978f
...
...
@@ -19,9 +19,9 @@ limitations under the License. */
#include <string.h>
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoD
&
lod
)
{
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoD
&
lod
)
{
os
<<
"{"
;
for
(
auto
&
v
:
lod
)
{
os
<<
"{"
;
...
...
@@ -39,9 +39,9 @@ namespace paddle_mobile {
os
<<
"}"
;
return
os
;
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoDTensor
&
t
)
{
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoDTensor
&
t
)
{
// PADDLE_ENFORCE(t.type().hash_code() ==
// typeid(float).hash_code());
...
...
@@ -66,15 +66,15 @@ namespace paddle_mobile {
}
return
os
;
}
}
std
::
string
LoDToString
(
const
LoD
&
lod
)
{
std
::
string
LoDToString
(
const
LoD
&
lod
)
{
std
::
ostringstream
stream
;
stream
<<
lod
;
return
stream
.
str
();
}
}
LoD
SliceInLevel
(
const
LoD
&
in
,
size_t
level
,
size_t
elem_begin
,
LoD
SliceInLevel
(
const
LoD
&
in
,
size_t
level
,
size_t
elem_begin
,
size_t
elem_end
)
{
// PADDLE_ENFORCE_LT(level, in.size());
// PADDLE_ENFORCE_LT(elem_end, in[level].size());
...
...
@@ -101,24 +101,23 @@ namespace paddle_mobile {
}
}
return
res
;
}
}
LoD
ToAbsOffset
(
const
LoD
&
in
)
{
LoD
ToAbsOffset
(
const
LoD
&
in
)
{
// the lowest level stores relative offsets
if
(
in
.
empty
()
||
in
.
size
()
==
1
)
return
in
;
LoD
result
=
in
;
for
(
auto
level
=
static_cast
<
int
>
(
in
.
size
()
-
2
);
level
>=
0
;
level
--
)
{
for
(
auto
level
=
static_cast
<
int
>
(
in
.
size
()
-
2
);
level
>=
0
;
level
--
)
{
for
(
size_t
i
=
0
;
i
<
in
[
level
].
size
();
++
i
)
{
size_t
index
=
in
[
level
][
i
];
result
[
level
][
i
]
=
result
[
level
+
1
][
index
];
}
}
return
result
;
}
}
bool
operator
==
(
const
LoD
&
a
,
const
LoD
&
b
)
{
bool
operator
==
(
const
LoD
&
a
,
const
LoD
&
b
)
{
if
(
a
.
size
()
!=
b
.
size
())
{
return
false
;
}
...
...
@@ -136,9 +135,9 @@ namespace paddle_mobile {
}
}
return
true
;
}
}
bool
CheckLoD
(
const
LoD
&
in
,
int
tensor_height
)
{
bool
CheckLoD
(
const
LoD
&
in
,
int
tensor_height
)
{
if
(
in
.
empty
())
return
true
;
for
(
const
auto
&
level
:
in
)
{
...
...
@@ -180,9 +179,9 @@ namespace paddle_mobile {
return
false
;
}
return
true
;
}
}
bool
CheckAbsLoD
(
const
LoD
&
in
,
int
tensor_height
)
{
bool
CheckAbsLoD
(
const
LoD
&
in
,
int
tensor_height
)
{
if
(
in
.
empty
())
return
true
;
for
(
const
auto
&
level
:
in
)
{
...
...
@@ -215,24 +214,20 @@ namespace paddle_mobile {
}
}
return
true
;
}
}
using
LoDAndOffset
=
std
::
pair
<
LoD
,
std
::
pair
<
size_t
,
size_t
>>
;
using
LoDAndOffset
=
std
::
pair
<
LoD
,
std
::
pair
<
size_t
,
size_t
>>
;
LoDAndOffset
GetSubLoDAndAbsoluteOffset
(
const
LoD
&
lod
,
size_t
start_idx
,
size_t
end_idx
,
size_t
start_level
)
{
LoDAndOffset
GetSubLoDAndAbsoluteOffset
(
const
LoD
&
lod
,
size_t
start_idx
,
size_t
end_idx
,
size_t
start_level
)
{
LoD
sub_lod
;
for
(
size_t
level_idx
=
start_level
;
level_idx
<
lod
.
size
();
++
level_idx
)
{
for
(
size_t
level_idx
=
start_level
;
level_idx
<
lod
.
size
();
++
level_idx
)
{
// PADDLE_ENFORCE_LE(start_idx, end_idx);
// PADDLE_ENFORCE_LT(end_idx, lod[level_idx].size());
std
::
vector
<
size_t
>
level_lens
;
for
(
size_t
i
=
start_idx
;
i
<
end_idx
;
++
i
)
{
level_lens
.
push_back
(
lod
[
level_idx
][
i
+
1
]
-
lod
[
level_idx
][
i
]);
level_lens
.
push_back
(
lod
[
level_idx
][
i
+
1
]
-
lod
[
level_idx
][
i
]);
}
sub_lod
.
emplace_back
(
level_lens
);
start_idx
=
lod
[
level_idx
][
start_idx
];
...
...
@@ -240,9 +235,9 @@ namespace paddle_mobile {
}
return
LoDAndOffset
{
sub_lod
,
{
start_idx
,
end_idx
}};
}
}
void
AppendLoD
(
LoD
*
lod
,
const
LoD
&
lod_length
)
{
void
AppendLoD
(
LoD
*
lod
,
const
LoD
&
lod_length
)
{
// PADDLE_ENFORCE(
// lod->empty() || lod->size() == lod_length.size(),
// "The lod_length should has the same size with the appended
...
...
@@ -259,13 +254,12 @@ namespace paddle_mobile {
level
.
push_back
(
level
.
back
()
+
len
);
}
}
}
}
void
SerializeToStream
(
std
::
ostream
&
os
,
const
LoDTensor
&
tensor
)
{
void
SerializeToStream
(
std
::
ostream
&
os
,
const
LoDTensor
&
tensor
)
{
{
// the 1st field, uint32_t version for LoDTensor
constexpr
uint32_t
version
=
0
;
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
version
),
sizeof
(
version
));
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
version
),
sizeof
(
version
));
}
{
// the 2st field, LoD information
...
...
@@ -278,19 +272,17 @@ namespace paddle_mobile {
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
size
),
sizeof
(
size
));
for
(
auto
&
each
:
lod
)
{
size
=
each
.
size
()
*
sizeof
(
framework
::
LoD
::
value_type
::
value_type
);
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
size
),
sizeof
(
size
));
size
=
each
.
size
()
*
sizeof
(
framework
::
LoD
::
value_type
::
value_type
);
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
size
),
sizeof
(
size
));
os
.
write
(
reinterpret_cast
<
const
char
*>
(
each
.
data
()),
static_cast
<
std
::
streamsize
>
(
size
));
}
}
// the 3st field, Tensor
TensorToStream
(
os
,
static_cast
<
Tensor
>
(
tensor
));
}
}
void
DeserializeFromStream
(
std
::
istream
&
is
,
LoDTensor
*
tensor
)
{
void
DeserializeFromStream
(
std
::
istream
&
is
,
LoDTensor
*
tensor
)
{
{
// the 1st field, unit32_t version for LoDTensor
uint32_t
version
;
...
...
@@ -301,8 +293,7 @@ namespace paddle_mobile {
{
// the 2st field, LoD information
uint64_t
lod_level
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
lod_level
),
sizeof
(
lod_level
));
is
.
read
(
reinterpret_cast
<
char
*>
(
&
lod_level
),
sizeof
(
lod_level
));
auto
&
lod
=
*
tensor
->
mutable_lod
();
lod
.
resize
(
lod_level
);
for
(
uint64_t
i
=
0
;
i
<
lod_level
;
++
i
)
{
...
...
@@ -316,7 +307,7 @@ namespace paddle_mobile {
}
// the 3st filed, Tensor
TensorFromStream
(
is
,
static_cast
<
Tensor
*>
(
tensor
));
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/lod_tensor.h
浏览文件 @
47e5978f
...
...
@@ -23,9 +23,9 @@ limitations under the License. */
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
/*
/*
* LoD is short for Level of Details.
*
* - in a level, each element indicates relative offset of the lower
...
...
@@ -42,25 +42,25 @@ namespace paddle_mobile {
* 0 2 4 7
* 0 2 5 7 10 12 15 20
*/
using
LoD
=
std
::
vector
<
std
::
vector
<
size_t
>>
;
using
LoD
=
std
::
vector
<
std
::
vector
<
size_t
>>
;
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoD
&
lod
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoD
&
lod
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoDTensor
&
t
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
LoDTensor
&
t
);
std
::
string
LoDToString
(
const
LoD
&
lod
);
std
::
string
LoDToString
(
const
LoD
&
lod
);
LoD
SliceInLevel
(
const
LoD
&
in
,
size_t
level
,
size_t
elem_begin
,
LoD
SliceInLevel
(
const
LoD
&
in
,
size_t
level
,
size_t
elem_begin
,
size_t
elem_end
);
/*
/*
* Transform an LoD from relative offsets to absolute offsets.
*/
LoD
ToAbsOffset
(
const
LoD
&
in
);
LoD
ToAbsOffset
(
const
LoD
&
in
);
bool
operator
==
(
const
LoD
&
a
,
const
LoD
&
b
);
bool
operator
==
(
const
LoD
&
a
,
const
LoD
&
b
);
/*
/*
* Check whether this lod's format is valid.
*
* ATTENTION:
...
...
@@ -78,9 +78,9 @@ namespace paddle_mobile {
* tensor_height>0.
*/
bool
CheckLoD
(
const
LoD
&
in
,
int
tensor_height
=
-
1
);
bool
CheckLoD
(
const
LoD
&
in
,
int
tensor_height
=
-
1
);
/*
/*
* Check whether this absolute lod's format is valid.
*
* ATTENTION:
...
...
@@ -95,13 +95,13 @@ namespace paddle_mobile {
* same(the height of underlying tensor) or `tensor_height` if
* tensor_height>0.
*/
bool
CheckAbsLoD
(
const
LoD
&
in
,
int
tensor_height
=
-
1
);
bool
CheckAbsLoD
(
const
LoD
&
in
,
int
tensor_height
=
-
1
);
/*
/*
* LoDTensor (Level of details Tensor)
* see https://en.wikipedia.org/wiki/Level_of_details for reference.
*/
class
LoDTensor
:
public
Tensor
{
class
LoDTensor
:
public
Tensor
{
public:
LoDTensor
()
:
Tensor
()
{}
...
...
@@ -116,12 +116,10 @@ namespace paddle_mobile {
/*
* Get the start offset and end offset of an element from LoD.
*/
std
::
pair
<
size_t
,
size_t
>
lod_element
(
size_t
level
,
size_t
elem
)
const
{
std
::
pair
<
size_t
,
size_t
>
lod_element
(
size_t
level
,
size_t
elem
)
const
{
// PADDLE_ENFORCE_LT(level, NumLevels());
// PADDLE_ENFORCE_LT(elem, NumElements(level));
return
std
::
make_pair
((
lod_
)[
level
][
elem
],
(
lod_
)[
level
][
elem
+
1
]);
return
std
::
make_pair
((
lod_
)[
level
][
elem
],
(
lod_
)[
level
][
elem
+
1
]);
}
/*
...
...
@@ -143,9 +141,9 @@ namespace paddle_mobile {
private:
LoD
lod_
;
};
};
/*
/*
* Expand the `source` to fit the LoD of `lod`. For example, a `source`
* LoDTensor is
* - LoD: [0, 2]
...
...
@@ -155,9 +153,8 @@ namespace paddle_mobile {
* returns a new LoDTensor
* - [a0 a0 a0 a1 a1]
*/
template
<
typename
T
>
LoDTensor
LodExpand
(
const
LoDTensor
&
source
,
const
LoD
&
lod
,
size_t
level
)
{
template
<
typename
T
>
LoDTensor
LodExpand
(
const
LoDTensor
&
source
,
const
LoD
&
lod
,
size_t
level
)
{
LoD
abs_lod
=
ToAbsOffset
(
lod
);
const
auto
&
lod_level
=
lod
[
level
];
size_t
num_instances
=
source
.
dims
()[
0
];
...
...
@@ -172,41 +169,40 @@ namespace paddle_mobile {
// PADDLE_ENFORCE_EQ(num_instances, lod_level.size() - 1);
for
(
size_t
ins
=
0
;
ins
<
num_instances
;
ins
++
)
{
for
(
size_t
elem
=
lod_level
[
ins
];
elem
<
lod_level
[
ins
+
1
];
elem
++
)
{
for
(
size_t
elem
=
lod_level
[
ins
];
elem
<
lod_level
[
ins
+
1
];
elem
++
)
{
auto
slice
=
tensor
.
Slice
(
elem
,
elem
+
1
);
TensorCopy
(
source
.
Slice
(
ins
,
ins
+
1
),
&
slice
);
}
}
return
tensor
;
}
// Get the absolute offset of a lod[start_level][start_idx:end_idx] and
// relative length of details for every levels(i.e., [start_level: ]).
//
// For example,
// lod = [[0, 3, 4, 8], [0, 9, 10, 11, 13, 17, 19, 22, 24]]
// start_level = 0
// start_idx = 1
// end_idx = 3
//
// Returns:
// LoD = [[1, 4], [2, 4, 2, 3, 2]]
// pair<size_t, size_t> = {11, 24}
std
::
pair
<
LoD
,
std
::
pair
<
size_t
,
size_t
>>
GetSubLoDAndAbsoluteOffset
(
const
LoD
&
lod
,
size_t
start
_idx
,
size_t
end_idx
,
size_t
start_level
);
void
AppendLoD
(
LoD
*
lod
,
const
LoD
&
lod_length
);
/*
}
// Get the absolute offset of a lod[start_level][start_idx:end_idx] and
// relative length of details for every levels(i.e., [start_level: ]).
//
// For example,
// lod = [[0, 3, 4, 8], [0, 9, 10, 11, 13, 17, 19, 22, 24]]
// start_level = 0
// start_idx = 1
// end_idx = 3
//
// Returns:
// LoD = [[1, 4], [2, 4, 2, 3, 2]]
// pair<size_t, size_t> = {11, 24}
std
::
pair
<
LoD
,
std
::
pair
<
size_t
,
size_t
>>
GetSubLoDAndAbsoluteOffset
(
const
LoD
&
lod
,
size_t
start_idx
,
size_t
end
_idx
,
size_t
start_level
);
void
AppendLoD
(
LoD
*
lod
,
const
LoD
&
lod_length
);
/*
* Serialize/Desiralize LoDTensor to std::ostream
* You can pass ofstream or ostringstream to serilize to file
* or to a in memory string. GPU tensor will be copied to CPU.
*/
void
SerializeToStream
(
std
::
ostream
&
os
,
const
LoDTensor
&
tensor
);
void
SerializeToStream
(
std
::
ostream
&
os
,
const
LoDTensor
&
tensor
);
void
DeserializeFromStream
(
std
::
istream
&
is
,
LoDTensor
*
tensor
);
void
DeserializeFromStream
(
std
::
istream
&
is
,
LoDTensor
*
tensor
);
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/op_desc.cpp
浏览文件 @
47e5978f
...
...
@@ -5,9 +5,9 @@
#include "op_desc.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
OpDesc
::
OpDesc
(
const
proto
::
OpDesc
&
desc
)
:
desc_
(
desc
)
{
OpDesc
::
OpDesc
(
const
proto
::
OpDesc
&
desc
)
:
desc_
(
desc
)
{
for
(
int
i
=
0
;
i
<
desc_
.
inputs_size
();
++
i
)
{
const
proto
::
OpDesc
::
Var
&
var
=
desc_
.
inputs
(
i
);
std
::
vector
<
std
::
string
>
&
args
=
inputs_
[
var
.
parameter
()];
...
...
@@ -36,27 +36,24 @@ namespace paddle_mobile {
// }
}
}
}
}
const
std
::
vector
<
std
::
string
>
&
OpDesc
::
Input
(
const
std
::
string
&
name
)
const
{
const
std
::
vector
<
std
::
string
>
&
OpDesc
::
Input
(
const
std
::
string
&
name
)
const
{
return
inputs_
.
find
(
name
)
->
second
;
}
}
const
std
::
vector
<
std
::
string
>
&
OpDesc
::
Output
(
const
std
::
string
&
name
)
const
{
const
std
::
vector
<
std
::
string
>
&
OpDesc
::
Output
(
const
std
::
string
&
name
)
const
{
return
outputs_
.
find
(
name
)
->
second
;
}
}
Attribute
OpDesc
::
GetAttr
(
const
std
::
string
&
name
)
const
{
Attribute
OpDesc
::
GetAttr
(
const
std
::
string
&
name
)
const
{
auto
it
=
attrs_
.
find
(
name
);
return
it
->
second
;
}
}
const
std
::
unordered_map
<
std
::
string
,
Attribute
>
&
OpDesc
::
GetAttrMap
()
const
{
const
std
::
unordered_map
<
std
::
string
,
Attribute
>
&
OpDesc
::
GetAttrMap
()
const
{
return
attrs_
;
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/op_desc.h
浏览文件 @
47e5978f
...
...
@@ -23,15 +23,13 @@ SOFTWARE.
#include "paddle_mobile_object.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
class
OpDesc
:
PaddleMobileObject
{
class
OpDesc
:
PaddleMobileObject
{
public:
OpDesc
(
const
proto
::
OpDesc
&
desc
);
const
std
::
vector
<
std
::
string
>
&
Input
(
const
std
::
string
&
name
)
const
;
const
std
::
vector
<
std
::
string
>
&
Output
(
const
std
::
string
&
name
)
const
;
const
std
::
vector
<
std
::
string
>
&
Input
(
const
std
::
string
&
name
)
const
;
const
std
::
vector
<
std
::
string
>
&
Output
(
const
std
::
string
&
name
)
const
;
Attribute
GetAttr
(
const
std
::
string
&
name
)
const
;
const
VariableNameMap
&
GetInputs
()
{
return
inputs_
;
}
...
...
@@ -47,7 +45,7 @@ namespace paddle_mobile {
VariableNameMap
inputs_
;
VariableNameMap
outputs_
;
AttributeMap
attrs_
;
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/op_info.h
浏览文件 @
47e5978f
...
...
@@ -22,9 +22,9 @@ SOFTWARE.
#include "framework.pb.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
>
struct
OpInfo
{
template
<
typename
Dtype
>
struct
OpInfo
{
OpCreator
<
Dtype
>
creator_
;
const
OpCreator
<
Dtype
>
&
Creator
()
const
{
// PADDLE_ENFORCE_NOT_NULL(creator_,
...
...
@@ -32,14 +32,13 @@ namespace paddle_mobile {
// registered");
return
creator_
;
}
};
};
template
<
typename
Dtype
>
class
OpInfoMap
;
template
<
typename
Dtype
>
class
OpInfoMap
;
template
<
typename
Dtype
>
static
OpInfoMap
<
Dtype
>
*
g_op_info_map
=
nullptr
;
template
<
typename
Dtype
>
static
OpInfoMap
<
Dtype
>
*
g_op_info_map
=
nullptr
;
template
<
typename
Dtype
>
class
OpInfoMap
{
template
<
typename
Dtype
>
class
OpInfoMap
{
public:
static
OpInfoMap
&
Instance
()
{
if
(
g_op_info_map
<
Dtype
>
==
nullptr
)
{
...
...
@@ -89,7 +88,7 @@ namespace paddle_mobile {
std
::
unordered_map
<
std
::
string
,
OpInfo
<
Dtype
>>
map_
;
// DISABLE_COPY_AND_ASSIGN(OpInfoMap);
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/op_kernel_type.h
浏览文件 @
47e5978f
...
...
@@ -22,12 +22,11 @@ SOFTWARE.
#include "framework.pb.h"
namespace
paddle_mobile
{
namespace
framework
{
struct
OpKernelType
{
namespace
framework
{
struct
OpKernelType
{
struct
Hash
{
size_t
operator
()(
const
OpKernelType
&
key
)
const
{
int
data_type
=
static_cast
<
int
>
(
key
.
data_type_
)
<<
LEFT_SHIFT
;
int
data_type
=
static_cast
<
int
>
(
key
.
data_type_
)
<<
LEFT_SHIFT
;
int
data_layout
=
static_cast
<
int
>
(
key
.
data_layout_
)
<<
(
LEFT_SHIFT
*
2
);
...
...
@@ -47,26 +46,20 @@ namespace paddle_mobile {
:
data_type_
(
data_type
),
data_layout_
(
data_layout
)
{}
bool
operator
==
(
const
OpKernelType
&
o
)
const
{
return
data_type_
==
o
.
data_type_
&&
data_layout_
==
o
.
data_layout_
;
return
data_type_
==
o
.
data_type_
&&
data_layout_
==
o
.
data_layout_
;
}
bool
operator
!=
(
const
OpKernelType
&
o
)
const
{
return
!
(
*
this
==
o
);
}
};
bool
operator
!=
(
const
OpKernelType
&
o
)
const
{
return
!
(
*
this
==
o
);
}
};
inline
bool
NeedTransformLayout
(
const
DataLayout
&
l
,
const
DataLayout
&
r
)
{
return
l
!=
DataLayout
::
kAnyLayout
&&
r
!=
DataLayout
::
kAnyLayout
&&
l
!=
r
;
}
inline
bool
NeedTransformLayout
(
const
DataLayout
&
l
,
const
DataLayout
&
r
)
{
return
l
!=
DataLayout
::
kAnyLayout
&&
r
!=
DataLayout
::
kAnyLayout
&&
l
!=
r
;
}
inline
bool
TransFromNeeded
(
const
OpKernelType
&
l
,
const
OpKernelType
&
r
)
{
inline
bool
TransFromNeeded
(
const
OpKernelType
&
l
,
const
OpKernelType
&
r
)
{
return
(
l
.
data_type_
!=
r
.
data_type_
)
||
NeedTransformLayout
(
l
.
data_layout_
,
r
.
data_layout_
);
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/op_proto_maker.h
浏览文件 @
47e5978f
...
...
@@ -19,8 +19,8 @@ SOFTWARE.
#pragma once
namespace
paddle_mobile
{
namespace
framework
{
// this class not only make proto but also init attribute checkers.
class
OpProtoAndCheckerMaker
{};
}
// namespace framework
namespace
framework
{
// this class not only make proto but also init attribute checkers.
class
OpProtoAndCheckerMaker
{};
}
// namespace framework
}
// namespace paddle_mobile
src/framework/operator.cpp
浏览文件 @
47e5978f
...
...
@@ -20,10 +20,10 @@ SOFTWARE.
#include "op_info.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
>
OperatorBase
<
Dtype
>::
OperatorBase
(
const
std
::
string
&
type
,
template
<
typename
Dtype
>
OperatorBase
<
Dtype
>::
OperatorBase
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
...
...
@@ -31,12 +31,12 @@ namespace paddle_mobile {
:
type_
(
type
),
inputs_
(
inputs
),
outputs_
(
outputs
),
attrs_
(
attrs
),
scope_
(
scope
)
{
CheckAllInputOutputSet
();
}
template
<
typename
Dtype
>
void
OperatorBase
<
Dtype
>::
CheckAllInputOutputSet
()
const
{}
}
template
<
typename
Dtype
>
void
OperatorBase
<
Dtype
>::
CheckAllInputOutputSet
()
const
{}
template
class
OperatorBase
<
CPU
>;
template
class
OperatorWithKernel
<
CPU
>;
template
class
OperatorBase
<
CPU
>;
template
class
OperatorWithKernel
<
CPU
>;
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/operator.h
浏览文件 @
47e5978f
...
...
@@ -33,8 +33,8 @@ SOFTWARE.
#include "variable.h"
namespace
paddle_mobile
{
namespace
framework
{
static
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
namespace
framework
{
static
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
op_input_output_key
=
{
{
"conv2d"
,
{
"Input"
,
"Output"
}},
{
"relu"
,
{
"X"
,
"Out"
}},
{
"softmax"
,
{
"X"
,
"Out"
}},
{
"mul"
,
{
"X"
,
"Out"
}},
...
...
@@ -42,13 +42,12 @@ namespace paddle_mobile {
{
"batch_norm"
,
{
"X"
,
"Y"
}},
{
"lrn"
,
{
"X"
,
"Out"
}},
{
"concat"
,
{
"X"
,
"Out"
}},
};
};
template
<
typename
Dtype
>
class
OperatorBase
:
PaddleMobileObject
{
template
<
typename
Dtype
>
class
OperatorBase
:
PaddleMobileObject
{
public:
OperatorBase
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
std
::
shared_ptr
<
Scope
>
scope
);
virtual
~
OperatorBase
()
{}
virtual
void
Run
()
const
=
0
;
...
...
@@ -73,28 +72,25 @@ namespace paddle_mobile {
private:
void
CheckAllInputOutputSet
()
const
;
};
};
template
<
typename
Dtype
>
class
OperatorWithKernel
:
public
OperatorBase
<
Dtype
>
{
template
<
typename
Dtype
>
class
OperatorWithKernel
:
public
OperatorBase
<
Dtype
>
{
public:
OperatorWithKernel
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
OperatorWithKernel
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
std
::
shared_ptr
<
Scope
>
scope
)
const
AttributeMap
&
attrs
,
std
::
shared_ptr
<
Scope
>
scope
)
:
OperatorBase
<
Dtype
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
)
{}
virtual
void
InferShape
()
const
=
0
;
virtual
void
Run
()
const
=
0
;
};
};
template
<
typename
Dtype
,
typename
P
>
class
OpKernelBase
:
PaddleMobileObject
{
template
<
typename
Dtype
,
typename
P
>
class
OpKernelBase
:
PaddleMobileObject
{
public:
virtual
void
Compute
(
const
P
&
para
)
const
=
0
;
virtual
~
OpKernelBase
()
=
default
;
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/paddle_mobile_object.h
浏览文件 @
47e5978f
...
...
@@ -23,7 +23,7 @@ SOFTWARE.
namespace
paddle_mobile
{
class
PaddleMobileObject
{
class
PaddleMobileObject
{
public:
virtual
inline
const
std
::
string
&
ToString
()
{
char
address
[
128
]
=
{
0
};
...
...
@@ -32,5 +32,5 @@ namespace paddle_mobile {
}
private:
};
};
}
// namespace paddle_mobile
src/framework/program.cpp
浏览文件 @
47e5978f
...
...
@@ -17,5 +17,5 @@ SOFTWARE.
==============================================================================*/
namespace
paddle_mobile
{
namespace
framework
{}
namespace
framework
{}
}
// namespace paddle_mobile
src/framework/program.h
浏览文件 @
47e5978f
...
...
@@ -24,17 +24,17 @@ SOFTWARE.
#include "scope.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
,
Precision
P
=
Precision
::
FP32
>
class
Program
:
PaddleMobileObject
{
template
<
typename
Dtype
,
Precision
P
=
Precision
::
FP32
>
class
Program
:
PaddleMobileObject
{
public:
std
::
shared_ptr
<
ProgramDesc
>
originProgram
;
std
::
shared_ptr
<
ProgramDesc
>
optimizeProgram
;
std
::
shared_ptr
<
Scope
>
scope
;
private:
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/program_desc.cpp
浏览文件 @
47e5978f
...
...
@@ -5,18 +5,18 @@
#include "program_desc.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
ProgramDesc
::
ProgramDesc
(
const
proto
::
ProgramDesc
&
desc
)
:
desc_
(
desc
)
{
ProgramDesc
::
ProgramDesc
(
const
proto
::
ProgramDesc
&
desc
)
:
desc_
(
desc
)
{
for
(
auto
&
block_desc
:
*
desc_
.
mutable_blocks
())
{
// new framework::BlockDesc(block_desc)
blocks_
.
emplace_back
(
std
::
make_shared
<
BlockDesc
>
(
block_desc
));
}
}
}
std
::
shared_ptr
<
BlockDesc
>
ProgramDesc
::
Block
(
size_t
idx
)
{
std
::
shared_ptr
<
BlockDesc
>
ProgramDesc
::
Block
(
size_t
idx
)
{
return
blocks_
[
idx
];
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/program_desc.h
浏览文件 @
47e5978f
...
...
@@ -25,20 +25,18 @@ SOFTWARE.
#include "paddle_mobile_object.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
class
ProgramDesc
:
PaddleMobileObject
{
class
ProgramDesc
:
PaddleMobileObject
{
public:
ProgramDesc
(
const
proto
::
ProgramDesc
&
desc
);
std
::
shared_ptr
<
BlockDesc
>
Block
(
size_t
idx
);
const
std
::
vector
<
std
::
shared_ptr
<
BlockDesc
>>
&
Blocks
()
{
return
blocks_
;
};
const
std
::
vector
<
std
::
shared_ptr
<
BlockDesc
>>
&
Blocks
()
{
return
blocks_
;
};
private:
std
::
vector
<
std
::
shared_ptr
<
BlockDesc
>>
blocks_
;
proto
::
ProgramDesc
desc_
;
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/scope.cc
浏览文件 @
47e5978f
...
...
@@ -4,15 +4,15 @@
#include <vector>
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
Scope
&
Scope
::
NewScope
()
const
{
Scope
&
Scope
::
NewScope
()
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
kids_
.
push_back
(
new
Scope
(
this
));
return
*
kids_
.
back
();
}
}
Variable
*
Scope
::
Var
(
const
std
::
string
&
name
)
{
Variable
*
Scope
::
Var
(
const
std
::
string
&
name
)
{
auto
*
pvar
=
FindVarLocally
(
name
);
if
(
pvar
!=
nullptr
)
{
return
pvar
;
...
...
@@ -21,59 +21,59 @@ namespace paddle_mobile {
vars_
[
name
]
=
pvar
;
pvar
->
name_
=
&
(
vars_
.
find
(
name
)
->
first
);
return
pvar
;
}
}
// Variable* Scope::Var(std::string* name) {
// auto var_name = string::Sprintf("%p.%d", this,
// vars_.size());
// if (name != nullptr) {
// *name = var_name;
// }
// return Var(var_name);
// }
// Variable* Scope::Var(std::string* name) {
// auto var_name = string::Sprintf("%p.%d", this,
// vars_.size());
// if (name != nullptr) {
// *name = var_name;
// }
// return Var(var_name);
// }
Variable
*
Scope
::
FindVar
(
const
std
::
string
&
name
)
const
{
Variable
*
Scope
::
FindVar
(
const
std
::
string
&
name
)
const
{
auto
*
pvar
=
FindVarLocally
(
name
);
if
(
pvar
!=
nullptr
)
{
return
pvar
;
}
return
(
parent_
==
nullptr
)
?
nullptr
:
parent_
->
FindVar
(
name
);
}
}
const
Scope
*
Scope
::
FindScope
(
const
Variable
*
var
)
const
{
const
Scope
*
Scope
::
FindScope
(
const
Variable
*
var
)
const
{
for
(
auto
&
name_var
:
vars_
)
{
if
(
name_var
.
second
==
var
)
{
return
this
;
}
}
return
(
parent_
==
nullptr
)
?
nullptr
:
parent_
->
FindScope
(
var
);
}
}
void
Scope
::
DropKids
()
{
void
Scope
::
DropKids
()
{
for
(
Scope
*
s
:
kids_
)
{
delete
s
;
}
kids_
.
clear
();
}
}
std
::
vector
<
std
::
string
>
Scope
::
LocalVarNames
()
const
{
std
::
vector
<
std
::
string
>
Scope
::
LocalVarNames
()
const
{
std
::
vector
<
std
::
string
>
known_vars
;
known_vars
.
reserve
(
vars_
.
size
());
for
(
auto
&
name_var
:
vars_
)
{
known_vars
.
emplace_back
(
name_var
.
first
);
}
return
known_vars
;
}
}
void
Scope
::
DeleteScope
(
Scope
*
scope
)
const
{
void
Scope
::
DeleteScope
(
Scope
*
scope
)
const
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
auto
it
=
std
::
find
(
kids_
.
begin
(),
kids_
.
end
(),
scope
);
kids_
.
erase
(
it
);
delete
scope
;
// deferent
}
}
void
Scope
::
EraseVars
(
const
std
::
vector
<
std
::
string
>
&
var_names
)
{
void
Scope
::
EraseVars
(
const
std
::
vector
<
std
::
string
>
&
var_names
)
{
std
::
set
<
std
::
string
>
var_set
(
var_names
.
begin
(),
var_names
.
end
());
for
(
auto
it
=
vars_
.
begin
();
it
!=
vars_
.
end
();)
{
if
(
var_set
.
find
(
it
->
first
)
!=
var_set
.
end
())
{
...
...
@@ -83,9 +83,9 @@ namespace paddle_mobile {
++
it
;
}
}
}
}
void
Scope
::
Rename
(
const
std
::
string
&
origin_name
,
void
Scope
::
Rename
(
const
std
::
string
&
origin_name
,
const
std
::
string
&
new_name
)
const
{
auto
origin_it
=
vars_
.
find
(
origin_name
);
if
(
origin_it
==
vars_
.
end
())
{
...
...
@@ -97,23 +97,23 @@ namespace paddle_mobile {
}
vars_
[
new_name
]
=
origin_it
->
second
;
vars_
.
erase
(
origin_it
);
}
//
// std::string Scope::Rename(const std::string& origin_name)
// const {
// auto var_name = string::Sprintf("%p.%d", this,
// vars_.size());
// Rename(origin_name, var_name);
// return var_name;
// }
}
//
// std::string Scope::Rename(const std::string& origin_name)
// const {
// auto var_name = string::Sprintf("%p.%d", this,
// vars_.size());
// Rename(origin_name, var_name);
// return var_name;
// }
Variable
*
Scope
::
FindVarLocally
(
const
std
::
string
&
name
)
const
{
Variable
*
Scope
::
FindVarLocally
(
const
std
::
string
&
name
)
const
{
auto
it
=
vars_
.
find
(
name
);
if
(
it
!=
vars_
.
end
())
{
return
it
->
second
;
}
return
nullptr
;
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/scope.h
浏览文件 @
47e5978f
...
...
@@ -24,8 +24,8 @@ SOFTWARE.
#include <unordered_map> //std::unordered_map
namespace
paddle_mobile
{
namespace
framework
{
class
Scope
{
namespace
framework
{
class
Scope
{
public:
Scope
()
{}
~
Scope
()
{}
...
...
@@ -76,6 +76,6 @@ namespace paddle_mobile {
Scope
const
*
parent_
{
nullptr
};
mutable
std
::
mutex
mutex_
;
};
}
// namespace framework
};
}
// namespace framework
}
// namespace paddle_mobile
src/framework/selected_rows.h
浏览文件 @
47e5978f
...
...
@@ -24,12 +24,11 @@ SOFTWARE.
#include "tensor.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
class
SelectedRows
{
class
SelectedRows
{
public:
SelectedRows
(
const
std
::
vector
<
int64_t
>
&
rows
,
const
int64_t
&
height
)
SelectedRows
(
const
std
::
vector
<
int64_t
>
&
rows
,
const
int64_t
&
height
)
:
rows_
(
rows
),
height_
(
height
)
{
value_
.
reset
(
new
Tensor
());
}
...
...
@@ -76,7 +75,7 @@ namespace paddle_mobile {
std
::
vector
<
int64_t
>
rows_
;
std
::
unique_ptr
<
Tensor
>
value_
{
nullptr
};
int64_t
height_
;
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/tensor.h
浏览文件 @
47e5978f
...
...
@@ -25,10 +25,10 @@ limitations under the License. */
#include "memory/t_malloc.h"
namespace
paddle_mobile
{
namespace
framework
{
template
<
typename
...
T
>
struct
SizeOfTypeFunctor
;
namespace
framework
{
template
<
typename
...
T
>
struct
SizeOfTypeFunctor
;
template
<
typename
T
>
struct
SizeOfTypeFunctor
<
T
>
{
template
<
typename
T
>
struct
SizeOfTypeFunctor
<
T
>
{
size_t
operator
()(
std
::
type_index
type
)
const
{
if
(
typeid
(
T
).
hash_code
()
==
type
.
hash_code
())
{
return
sizeof
(
T
);
...
...
@@ -36,14 +36,14 @@ namespace paddle_mobile {
return
0UL
;
}
}
};
};
template
<
>
struct
SizeOfTypeFunctor
<>
{
template
<
>
struct
SizeOfTypeFunctor
<>
{
size_t
operator
()(
std
::
type_index
type
)
const
{
return
0UL
;
}
};
};
template
<
typename
HEAD
,
typename
...
TAIL
>
struct
SizeOfTypeFunctor
<
HEAD
,
TAIL
...
>
{
template
<
typename
HEAD
,
typename
...
TAIL
>
struct
SizeOfTypeFunctor
<
HEAD
,
TAIL
...
>
{
size_t
operator
()(
std
::
type_index
type
)
const
{
SizeOfTypeFunctor
<
HEAD
>
head
;
size_t
head_size
=
head
(
type
);
...
...
@@ -53,21 +53,20 @@ namespace paddle_mobile {
SizeOfTypeFunctor
<
TAIL
...
>
tail
;
return
tail
(
type
);
}
};
};
static
inline
size_t
SizeOfType
(
std
::
type_index
type
)
{
SizeOfTypeFunctor
<
int
,
float
,
double
,
int16_t
,
int64_t
,
bool
,
size_t
>
static
inline
size_t
SizeOfType
(
std
::
type_index
type
)
{
SizeOfTypeFunctor
<
int
,
float
,
double
,
int16_t
,
int64_t
,
bool
,
size_t
>
functor
;
size_t
size
=
functor
(
type
);
// PADDLE_ENFORCE(size != 0UL, "Cannot get size of type %s",
// type.name());
return
size
;
}
}
class
LoDTensor
;
class
LoDTensor
;
class
Tensor
{
class
Tensor
{
public:
Tensor
()
:
offset_
(
0
)
{}
...
...
@@ -200,8 +199,7 @@ namespace paddle_mobile {
DDim
dst_dims
=
dims_
;
dst_dims
[
0
]
=
end_idx
-
begin_idx
;
dst
.
Resize
(
dst_dims
);
dst
.
offset_
=
offset_
+
begin_idx
*
base
*
SizeOfType
(
type
());
dst
.
offset_
=
offset_
+
begin_idx
*
base
*
SizeOfType
(
type
());
return
dst
;
}
}
...
...
@@ -236,9 +234,7 @@ namespace paddle_mobile {
inline
DataLayout
layout
()
const
{
return
layout_
;
}
inline
void
set_layout
(
const
DataLayout
layout
)
{
layout_
=
layout
;
}
inline
void
set_layout
(
const
DataLayout
layout
)
{
layout_
=
layout
;
}
private:
/**
...
...
@@ -274,9 +270,7 @@ namespace paddle_mobile {
virtual
size_t
size
()
const
{
return
size_
;
}
virtual
void
*
ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
.
get
());
}
virtual
void
*
ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
.
get
());
}
virtual
std
::
type_index
type
()
const
{
return
type_
;
}
...
...
@@ -327,14 +321,14 @@ namespace paddle_mobile {
* begins.
*/
size_t
offset_
;
};
};
inline
Tensor
ReshapeToMatrix
(
const
Tensor
&
src
,
int
num_col_dims
)
{
inline
Tensor
ReshapeToMatrix
(
const
Tensor
&
src
,
int
num_col_dims
)
{
Tensor
res
;
res
.
ShareDataWith
(
src
);
res
.
Resize
(
flatten_to_2d
(
src
.
dims
(),
num_col_dims
));
return
res
;
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/tensor_util.cc
浏览文件 @
47e5978f
...
...
@@ -18,9 +18,9 @@
#include <vector>
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
void
TensorCopy
(
const
Tensor
&
src
,
Tensor
*
dst
)
{
void
TensorCopy
(
const
Tensor
&
src
,
Tensor
*
dst
)
{
// VLOG(3) << "TensorCopy " << src.dims() << " from " <<
// src.place() << " to
// "
...
...
@@ -36,9 +36,9 @@ namespace paddle_mobile {
auto
size
=
src
.
numel
()
*
SizeOfType
(
src
.
type
());
memory
::
Copy
(
dst_ptr
,
src_ptr
,
size
);
}
}
void
TensorCopySync
(
const
Tensor
&
src
,
Tensor
*
dst
)
{
void
TensorCopySync
(
const
Tensor
&
src
,
Tensor
*
dst
)
{
// VLOG(3) << "TensorCopySync " << src.dims() << " from " <<
// src.place()
// << " to " << dst_place;
...
...
@@ -49,15 +49,14 @@ namespace paddle_mobile {
auto
dst_ptr
=
dst
->
mutable_data
(
src
.
type
());
auto
size
=
src
.
numel
()
*
SizeOfType
(
src
.
type
());
memory
::
Copy
(
dst_ptr
,
src_ptr
,
size
);
}
}
template
<
typename
Predicate
>
struct
AnyDTypeVisitor
{
template
<
typename
Predicate
>
struct
AnyDTypeVisitor
{
Predicate
predicate_
;
const
Tensor
&
tensor_
;
Tensor
*
out_
;
AnyDTypeVisitor
(
Predicate
predicate
,
const
Tensor
&
tensor
,
Tensor
*
out
)
AnyDTypeVisitor
(
Predicate
predicate
,
const
Tensor
&
tensor
,
Tensor
*
out
)
:
predicate_
(
predicate
),
tensor_
(
tensor
),
out_
(
out
)
{}
template
<
typename
T
>
void
operator
()()
const
{
...
...
@@ -66,16 +65,16 @@ namespace paddle_mobile {
// return any of predicate_(t) is true.
// o.device(*ctx_.eigen_device()) = predicate_(t).any();
}
};
};
template
<
typename
Predicate
>
inline
void
AnyImpl
(
Predicate
predicate
,
const
Tensor
&
tensor
,
template
<
typename
Predicate
>
inline
void
AnyImpl
(
Predicate
predicate
,
const
Tensor
&
tensor
,
framework
::
Tensor
*
out
)
{
VisitDataType
(
ToDataType
(
tensor
.
type
()),
AnyDTypeVisitor
<
Predicate
>
(
predicate
,
tensor
,
out
));
}
}
template
<
typename
Predicate
>
struct
AnyVisitor
{
template
<
typename
Predicate
>
struct
AnyVisitor
{
const
framework
::
Tensor
&
tensor_
;
Predicate
predicate_
;
...
...
@@ -93,48 +92,47 @@ namespace paddle_mobile {
bool
GetResult
(
const
framework
::
Tensor
&
out
)
const
{
return
*
out
.
data
<
bool
>
();
}
};
};
template
<
typename
Predicate
>
inline
bool
Any
(
const
framework
::
Tensor
&
tensor
,
Predicate
predicate
)
{
template
<
typename
Predicate
>
inline
bool
Any
(
const
framework
::
Tensor
&
tensor
,
Predicate
predicate
)
{
AnyVisitor
<
Predicate
>
visitor
(
tensor
,
predicate
);
// return platform::VisitPlace(visitor);
return
visitor
();
}
}
struct
ContainsNANPredicate
{
struct
ContainsNANPredicate
{
template
<
typename
T
>
auto
operator
()(
const
T
&
eigen_vec
)
const
->
decltype
(
std
::
declval
<
T
>
().
isnan
())
{
// Cast eigen_vector to vector of bool. true if is inf.
return
eigen_vec
.
isnan
();
}
};
};
bool
TensorContainsNAN
(
const
framework
::
Tensor
&
tensor
)
{
bool
TensorContainsNAN
(
const
framework
::
Tensor
&
tensor
)
{
ContainsNANPredicate
predicate
;
return
Any
(
tensor
,
predicate
);
}
}
struct
ContainsInfPredicate
{
struct
ContainsInfPredicate
{
template
<
typename
T
>
auto
operator
()(
const
T
&
eigen_vec
)
const
->
decltype
(
std
::
declval
<
T
>
().
isinf
())
{
// Cast eigen_vector to vector of bool. true if is inf.
return
eigen_vec
.
isinf
();
}
};
};
bool
TensorContainsInf
(
const
framework
::
Tensor
&
tensor
)
{
bool
TensorContainsInf
(
const
framework
::
Tensor
&
tensor
)
{
ContainsInfPredicate
predicate
;
return
Any
(
tensor
,
predicate
);
}
}
void
TensorToStream
(
std
::
ostream
&
os
,
const
Tensor
&
tensor
)
{
void
TensorToStream
(
std
::
ostream
&
os
,
const
Tensor
&
tensor
)
{
{
// the 1st field, uint32_t version
constexpr
uint32_t
version
=
0
;
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
version
),
sizeof
(
version
));
os
.
write
(
reinterpret_cast
<
const
char
*>
(
&
version
),
sizeof
(
version
));
}
{
// the 2nd field, tensor description
// int32_t size
...
...
@@ -160,9 +158,9 @@ namespace paddle_mobile {
os
.
write
(
static_cast
<
const
char
*>
(
data_ptr
),
static_cast
<
std
::
streamsize
>
(
size
));
}
}
}
struct
DeserializedDataFunctor
{
struct
DeserializedDataFunctor
{
DeserializedDataFunctor
(
void
**
buf
,
Tensor
*
tensor
)
:
buf_
(
buf
),
tensor_
(
tensor
)
{}
...
...
@@ -172,9 +170,9 @@ namespace paddle_mobile {
void
**
buf_
;
Tensor
*
tensor_
;
};
};
void
TensorFromStream
(
std
::
istream
&
is
,
framework
::
Tensor
*
tensor
)
{
void
TensorFromStream
(
std
::
istream
&
is
,
framework
::
Tensor
*
tensor
)
{
uint32_t
version
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
version
),
sizeof
(
version
));
// PADDLE_ENFORCE_EQ(version, 0U, "Only version 0 is supported");
...
...
@@ -200,7 +198,7 @@ namespace paddle_mobile {
DeserializedDataFunctor
(
&
buf
,
tensor
));
is
.
read
(
static_cast
<
char
*>
(
buf
),
tensor
->
memory_size
());
}
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/tensor_util.h
浏览文件 @
47e5978f
...
...
@@ -20,39 +20,39 @@ limitations under the License. */
#include <vector>
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
void
TensorCopy
(
const
Tensor
&
src
,
Tensor
*
dst
);
void
TensorCopySync
(
const
Tensor
&
src
,
Tensor
*
dst
);
void
TensorCopy
(
const
Tensor
&
src
,
Tensor
*
dst
);
void
TensorCopySync
(
const
Tensor
&
src
,
Tensor
*
dst
);
template
<
typename
T
>
void
TensorFromVector
(
const
std
::
vector
<
T
>
&
src
,
Tensor
*
dst
);
template
<
typename
T
>
void
TensorFromVector
(
const
std
::
vector
<
T
>
&
src
,
Tensor
*
dst
);
template
<
typename
T
>
void
TesnorToVector
(
const
Tensor
&
src
,
std
::
vector
<
T
>
*
dst
);
template
<
typename
T
>
void
TesnorToVector
(
const
Tensor
&
src
,
std
::
vector
<
T
>
*
dst
);
bool
TensorContainsNAN
(
const
framework
::
Tensor
&
tensor
);
bool
TensorContainsInf
(
const
framework
::
Tensor
&
tensor
);
bool
TensorContainsNAN
(
const
framework
::
Tensor
&
tensor
);
bool
TensorContainsInf
(
const
framework
::
Tensor
&
tensor
);
void
TensorToStream
(
std
::
ostream
&
os
,
const
Tensor
&
tensor
);
void
TensorFromStream
(
std
::
istream
&
is
,
Tensor
*
tensor
);
void
TensorToStream
(
std
::
ostream
&
os
,
const
Tensor
&
tensor
);
void
TensorFromStream
(
std
::
istream
&
is
,
Tensor
*
tensor
);
//
// The implementation of template functions.
//
//
// The implementation of template functions.
//
template
<
typename
T
>
void
TensorFromVector
(
const
std
::
vector
<
T
>
&
src
,
Tensor
*
dst
)
{
template
<
typename
T
>
void
TensorFromVector
(
const
std
::
vector
<
T
>
&
src
,
Tensor
*
dst
)
{
auto
src_ptr
=
static_cast
<
const
void
*>
(
src
.
data
());
dst
->
Resize
({
static_cast
<
int64_t
>
(
src
.
size
())});
auto
dst_ptr
=
static_cast
<
void
*>
(
dst
->
mutable_data
<
T
>
());
auto
size
=
src
.
size
()
*
sizeof
(
T
);
memory
::
Copy
(
dst_ptr
,
src_ptr
,
size
);
}
}
template
<
typename
T
>
void
TensorToVector
(
const
Tensor
&
src
,
std
::
vector
<
T
>
*
dst
)
{
template
<
typename
T
>
void
TensorToVector
(
const
Tensor
&
src
,
std
::
vector
<
T
>
*
dst
)
{
auto
src_ptr
=
static_cast
<
const
void
*>
(
src
.
data
<
T
>
());
auto
size
=
src
.
numel
()
*
sizeof
(
T
);
...
...
@@ -60,7 +60,7 @@ namespace paddle_mobile {
auto
dst_ptr
=
static_cast
<
void
*>
(
dst
->
data
());
memory
::
Copy
(
dst_ptr
,
src_ptr
,
size
);
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/var_desc.cpp
浏览文件 @
47e5978f
...
...
@@ -20,9 +20,9 @@ SOFTWARE.
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
VarDesc
::
VarDesc
(
const
proto
::
VarDesc
&
desc
)
:
desc_
(
desc
)
{}
VarDesc
::
VarDesc
(
const
proto
::
VarDesc
&
desc
)
:
desc_
(
desc
)
{}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/var_desc.h
浏览文件 @
47e5978f
...
...
@@ -22,9 +22,9 @@ SOFTWARE.
#include "paddle_mobile_object.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
class
VarDesc
{
class
VarDesc
{
public:
VarDesc
(
const
proto
::
VarDesc
&
desc
);
...
...
@@ -68,8 +68,7 @@ namespace paddle_mobile {
template
<
typename
T
>
std
::
vector
<
T
>
RepeatedToVector
(
const
google
::
protobuf
::
RepeatedField
<
T
>
&
repeated_field
)
const
{
const
google
::
protobuf
::
RepeatedField
<
T
>
&
repeated_field
)
const
{
std
::
vector
<
T
>
ret
;
ret
.
reserve
(
repeated_field
.
size
());
std
::
copy
(
repeated_field
.
begin
(),
repeated_field
.
end
(),
...
...
@@ -83,7 +82,7 @@ namespace paddle_mobile {
private:
proto
::
VarDesc
desc_
;
};
};
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/var_type.h
浏览文件 @
47e5978f
...
...
@@ -23,8 +23,8 @@ SOFTWARE.
#include "variable.h"
namespace
paddle_mobile
{
namespace
framework
{
inline
proto
::
VarType
::
Type
ToVarType
(
std
::
type_index
type
)
{
namespace
framework
{
inline
proto
::
VarType
::
Type
ToVarType
(
std
::
type_index
type
)
{
if
(
type
.
hash_code
()
==
typeid
(
LoDTensor
).
hash_code
())
{
return
proto
::
VarType_Type_LOD_TENSOR
;
}
else
if
(
type
.
hash_code
()
==
typeid
(
SelectedRows
).
hash_code
())
{
...
...
@@ -33,7 +33,7 @@ namespace paddle_mobile {
// PADDLE_THROW("ToVarType:Unsupported type %s",
// type.name());
}
}
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle_mobile
src/framework/variable.h
浏览文件 @
47e5978f
...
...
@@ -26,8 +26,8 @@ SOFTWARE.
#include <typeinfo>
namespace
paddle_mobile
{
namespace
framework
{
class
Variable
:
public
PaddleMobileObject
{
namespace
framework
{
class
Variable
:
public
PaddleMobileObject
{
public:
template
<
typename
T
>
const
T
*
Get
()
const
{
return
static_cast
<
const
T
*>
(
holder_
->
Ptr
());
...
...
@@ -51,8 +51,8 @@ namespace paddle_mobile {
template
<
typename
T
>
bool
IsType
()
const
{
if
(
holder_
)
{
// printf("not null \n");
printf
(
" holder type : %s, this type %s
\n
"
,
holder_
->
Type
().
name
(),
typeid
(
T
).
name
());
printf
(
" holder type : %s, this type %s
\n
"
,
holder_
->
Type
().
name
()
,
typeid
(
T
).
name
());
}
// std::cout << " " << holder_->Type() << " " <<
...
...
@@ -91,6 +91,6 @@ namespace paddle_mobile {
std
::
unique_ptr
<
Placeholder
>
holder_
;
friend
class
Scope
;
const
std
::
string
*
name_
;
};
}
// namespace framework
};
}
// namespace framework
}
// namespace paddle_mobile
src/io.cpp
浏览文件 @
47e5978f
...
...
@@ -28,7 +28,7 @@ SOFTWARE.
namespace
paddle_mobile
{
void
ReadBinaryFile
(
const
std
::
string
&
filename
,
std
::
string
*
contents
)
{
void
ReadBinaryFile
(
const
std
::
string
&
filename
,
std
::
string
*
contents
)
{
std
::
ifstream
fin
(
filename
,
std
::
ios
::
in
|
std
::
ios
::
binary
);
fin
.
seekg
(
0
,
std
::
ios
::
end
);
contents
->
clear
();
...
...
@@ -36,10 +36,10 @@ namespace paddle_mobile {
fin
.
seekg
(
0
,
std
::
ios
::
beg
);
fin
.
read
(
&
(
contents
->
at
(
0
)),
contents
->
size
());
fin
.
close
();
}
}
template
<
typename
Dtype
,
Precision
P
>
void
Loader
<
Dtype
,
P
>::
LoadVar
(
framework
::
LoDTensor
*
tensor
,
template
<
typename
Dtype
,
Precision
P
>
void
Loader
<
Dtype
,
P
>::
LoadVar
(
framework
::
LoDTensor
*
tensor
,
const
std
::
string
&
file_path
)
{
// LOG(kLOG_DEBUG) << " to load " << file_path;
// Log(kLOG_DEBUG) << "123";
...
...
@@ -77,8 +77,7 @@ namespace paddle_mobile {
// 3. tensor version
uint32_t
tensor_version
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
tensor_version
),
sizeof
(
tensor_version
));
is
.
read
(
reinterpret_cast
<
char
*>
(
&
tensor_version
),
sizeof
(
tensor_version
));
// std::cout << " tensor_version: " << tensor_version << std::endl;
// 4. tensor desc
...
...
@@ -103,8 +102,7 @@ namespace paddle_mobile {
std
::
vector
<
int64_t
>
dims
;
dims
.
reserve
(
static_cast
<
size_t
>
(
desc
.
dims
().
size
()));
std
::
copy
(
desc
.
dims
().
begin
(),
desc
.
dims
().
end
(),
std
::
back_inserter
(
dims
));
std
::
copy
(
desc
.
dims
().
begin
(),
desc
.
dims
().
end
(),
std
::
back_inserter
(
dims
));
tensor
->
Resize
(
framework
::
make_ddim
(
dims
));
void
*
memory
;
...
...
@@ -146,11 +144,11 @@ namespace paddle_mobile {
is
.
read
(
static_cast
<
char
*>
(
memory
),
memory_size
*
type_size
);
// std::cout << " memory: " << memory << std::endl;
is
.
close
();
};
};
template
<
typename
Dtype
,
Precision
P
>
const
framework
::
Program
<
Dtype
,
P
>
Loader
<
Dtype
,
P
>::
Load
(
const
std
::
string
&
dirname
)
{
template
<
typename
Dtype
,
Precision
P
>
const
framework
::
Program
<
Dtype
,
P
>
Loader
<
Dtype
,
P
>::
Load
(
const
std
::
string
&
dirname
)
{
std
::
string
model_filename
=
dirname
+
"/__model__"
;
std
::
string
program_desc_str
;
ReadBinaryFile
(
model_filename
,
&
program_desc_str
);
...
...
@@ -174,8 +172,7 @@ namespace paddle_mobile {
for
(
int
i
=
0
;
i
<
block
->
Vars
().
size
();
++
i
)
{
std
::
shared_ptr
<
framework
::
VarDesc
>
var_desc
=
block
->
Vars
()[
i
];
auto
var
=
scope
->
Var
(
var_desc
->
Name
());
if
(
var_desc
->
GetType
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
if
(
var_desc
->
GetType
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
if
(
var_desc
->
Persistable
()
&&
var_desc
->
GetType
()
!=
framework
::
proto
::
VarType
::
FEED_MINIBATCH
&&
...
...
@@ -201,11 +198,9 @@ namespace paddle_mobile {
LOG
(
kLOG_DEBUG1
)
<<
" op: "
<<
op
.
type
();
for
(
int
m
=
0
;
m
<
op
.
inputs_size
();
++
m
)
{
const
framework
::
proto
::
OpDesc
::
Var
&
var
=
op
.
inputs
(
m
);
LOG
(
kLOG_DEBUG2
)
<<
" input parameter: "
<<
var
.
parameter
();
LOG
(
kLOG_DEBUG2
)
<<
" input parameter: "
<<
var
.
parameter
();
for
(
int
n
=
0
;
n
<
var
.
arguments
().
size
();
++
n
)
{
LOG
(
kLOG_DEBUG3
)
<<
" argument - "
<<
var
.
arguments
()[
n
];
LOG
(
kLOG_DEBUG3
)
<<
" argument - "
<<
var
.
arguments
()[
n
];
}
}
...
...
@@ -213,8 +208,7 @@ namespace paddle_mobile {
const
framework
::
proto
::
OpDesc
::
Var
&
var
=
op
.
outputs
(
y
);
LOG
(
kLOG_DEBUG2
)
<<
" out parameter: "
<<
var
.
parameter
();
for
(
int
z
=
0
;
z
<
var
.
arguments
().
size
();
++
z
)
{
LOG
(
kLOG_DEBUG3
)
<<
" argument - "
<<
var
.
arguments
()[
z
];
LOG
(
kLOG_DEBUG3
)
<<
" argument - "
<<
var
.
arguments
()[
z
];
}
}
...
...
@@ -278,8 +272,7 @@ namespace paddle_mobile {
for
(
int
k
=
0
;
k
<
block
.
vars
().
size
();
++
k
)
{
framework
::
proto
::
VarDesc
var
=
block
.
vars
()[
k
];
if
(
var
.
type
().
type
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
if
(
var
.
type
().
type
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
// std::cout << " var name: " << var.name() <<
// std::endl;
const
framework
::
proto
::
VarType
::
TensorDesc
&
tensor_desc
=
...
...
@@ -299,14 +292,12 @@ namespace paddle_mobile {
if
(
var
.
persistable
()
&&
var
.
type
().
type
()
!=
framework
::
proto
::
VarType
::
FEED_MINIBATCH
&&
var
.
type
().
type
()
!=
framework
::
proto
::
VarType
::
FETCH_LIST
)
{
var
.
type
().
type
()
!=
framework
::
proto
::
VarType
::
FETCH_LIST
)
{
// std::cout << " to load " << var.name() <<
// std::endl;
std
::
string
file_path
=
dirname
+
"/"
+
var
.
name
();
std
::
ifstream
is
(
file_path
);
std
::
streampos
pos
=
is
.
tellg
();
// save current position
std
::
streampos
pos
=
is
.
tellg
();
// save current position
is
.
seekg
(
0
,
std
::
ios
::
end
);
// std::cout << " file length = " << is.tellg() <<
// std::endl;
...
...
@@ -314,8 +305,7 @@ namespace paddle_mobile {
// 1. version
uint32_t
version
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
version
),
sizeof
(
version
));
is
.
read
(
reinterpret_cast
<
char
*>
(
&
version
),
sizeof
(
version
));
// std::cout << " version: " << version <<
// std::endl;
...
...
@@ -339,8 +329,7 @@ namespace paddle_mobile {
}
uint32_t
tensor_version
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
version
),
sizeof
(
version
));
is
.
read
(
reinterpret_cast
<
char
*>
(
&
version
),
sizeof
(
version
));
// std::cout << " tensor_version: " <<
// tensor_version <<
// std::endl;
...
...
@@ -404,8 +393,7 @@ namespace paddle_mobile {
// type_size
// << std::endl;
void
*
memory
=
malloc
(
memory_size
*
type_size
);
is
.
read
(
static_cast
<
char
*>
(
memory
),
memory_size
*
type_size
);
is
.
read
(
static_cast
<
char
*>
(
memory
),
memory_size
*
type_size
);
// std::cout << " memory: " << memory <<
// std::endl;
is
.
close
();
...
...
@@ -418,8 +406,8 @@ namespace paddle_mobile {
#endif
return
program
;
}
}
template
class
Loader
<
CPU
,
Precision
::
FP32
>;
template
class
Loader
<
CPU
,
Precision
::
FP32
>;
}
// namespace paddle_mobile
src/io.h
浏览文件 @
47e5978f
...
...
@@ -27,14 +27,13 @@ SOFTWARE.
namespace
paddle_mobile
{
template
<
typename
Dtype
,
Precision
P
=
Precision
::
FP32
>
class
Loader
:
PaddleMobileObject
{
template
<
typename
Dtype
,
Precision
P
=
Precision
::
FP32
>
class
Loader
:
PaddleMobileObject
{
public:
const
framework
::
Program
<
Dtype
,
P
>
Load
(
const
std
::
string
&
dirname
);
private:
void
LoadVar
(
framework
::
LoDTensor
*
tensor
,
const
std
::
string
&
file_path
);
};
void
LoadVar
(
framework
::
LoDTensor
*
tensor
,
const
std
::
string
&
file_path
);
};
}
// namespace paddle_mobile
src/memory/t_malloc.cc
浏览文件 @
47e5978f
...
...
@@ -22,30 +22,30 @@ SOFTWARE.
#include <cstring>
namespace
paddle_mobile
{
namespace
memory
{
const
int
MALLOC_ALIGN
=
16
;
namespace
memory
{
const
int
MALLOC_ALIGN
=
16
;
void
Copy
(
void
*
dst
,
const
void
*
src
,
size_t
num
)
{
void
Copy
(
void
*
dst
,
const
void
*
src
,
size_t
num
)
{
std
::
memcpy
(
dst
,
src
,
num
);
};
};
void
*
Alloc
(
size_t
size
)
{
void
*
Alloc
(
size_t
size
)
{
size_t
offset
=
sizeof
(
void
*
)
+
MALLOC_ALIGN
-
1
;
char
*
p
=
static_cast
<
char
*>
(
malloc
(
offset
+
size
));
if
(
!
p
)
{
return
nullptr
;
}
void
*
r
=
reinterpret_cast
<
void
*>
(
reinterpret_cast
<
size_t
>
(
p
+
offset
)
&
(
~
(
MALLOC_ALIGN
-
1
)));
void
*
r
=
reinterpret_cast
<
void
*>
(
reinterpret_cast
<
size_t
>
(
p
+
offset
)
&
(
~
(
MALLOC_ALIGN
-
1
)));
static_cast
<
void
**>
(
r
)[
-
1
]
=
p
;
return
r
;
}
}
void
Free
(
void
*
ptr
)
{
void
Free
(
void
*
ptr
)
{
if
(
ptr
)
{
free
(
static_cast
<
void
**>
(
ptr
)[
-
1
]);
}
}
}
}
// namespace memory
}
// namespace memory
}
// namespace paddle_mobile
src/memory/t_malloc.h
浏览文件 @
47e5978f
...
...
@@ -21,15 +21,15 @@ SOFTWARE.
#include <type_traits>
namespace
paddle_mobile
{
namespace
memory
{
namespace
memory
{
void
Copy
(
void
*
dst
,
const
void
*
src
,
size_t
num
);
void
Copy
(
void
*
dst
,
const
void
*
src
,
size_t
num
);
void
*
Alloc
(
size_t
size
);
void
*
Alloc
(
size_t
size
);
void
Free
(
void
*
ptr
);
void
Free
(
void
*
ptr
);
/**
/**
* \brief Free memory block in one place.
*
* \note In some cases, custom deleter is used to
...
...
@@ -37,16 +37,16 @@ namespace paddle_mobile {
* std::unique_ptr<T> in tensor.h.
* static_cast
*/
template
<
typename
T
>
class
PODDeleter
{
template
<
typename
T
>
class
PODDeleter
{
static_assert
(
std
::
is_pod
<
T
>::
value
,
"T must be POD"
);
public:
explicit
PODDeleter
(){};
void
operator
()(
T
*
ptr
)
{
Free
(
static_cast
<
void
*>
(
ptr
));
}
};
};
/**
/**
* \brief Free memory block in one place does not meet POD
*
* \note In some cases, custom deleter is used to
...
...
@@ -54,11 +54,11 @@ namespace paddle_mobile {
* std::unique_ptr<T> in tensor.h.
* reinterpret_cast
*/
template
<
typename
T
>
class
PlainDeleter
{
template
<
typename
T
>
class
PlainDeleter
{
public:
explicit
PlainDeleter
(){};
void
operator
()(
T
*
ptr
)
{
Free
(
reinterpret_cast
<
void
*>
(
ptr
));
}
};
}
// namespace memory
};
}
// namespace memory
}
// namespace paddle_mobile
src/operators/conv_op.cpp
浏览文件 @
47e5978f
...
...
@@ -23,14 +23,14 @@ SOFTWARE.
namespace
paddle_mobile
{
namespace
operators
{
int
ConvOutputSize
(
int
input_size
,
int
filter_size
,
int
dilation
,
int
padding
,
int
stride
)
{
int
ConvOutputSize
(
int
input_size
,
int
filter_size
,
int
dilation
,
int
padding
,
int
stride
)
{
const
int
dkernel
=
dilation
*
(
filter_size
-
1
)
+
1
;
int
output_size
=
(
input_size
+
2
*
padding
-
dkernel
)
/
stride
+
1
;
return
output_size
;
}
template
<
typename
Dtype
,
typename
T
>
template
<
typename
Dtype
,
typename
T
>
void
ConvOp
<
Dtype
,
T
>::
InferShape
()
const
{
// std::cout << " begin get dims: " << std::endl;
...
...
@@ -60,9 +60,9 @@ void ConvOp<Dtype, T>::InferShape() const {
std
::
vector
<
int64_t
>
output_shape
({
in_dims
[
0
],
filter_dims
[
0
]});
for
(
size_t
i
=
0
;
i
<
strides
.
size
();
++
i
)
{
output_shape
.
push_back
(
ConvOutputSize
(
in_dims
[
i
+
2
],
filter_dims
[
i
+
2
],
dilations
[
i
],
paddings
[
i
],
strides
[
i
]));
output_shape
.
push_back
(
ConvOutputSize
(
in_dims
[
i
+
2
],
filter_dims
[
i
+
2
],
dilations
[
i
],
paddings
[
i
],
strides
[
i
]));
}
framework
::
DDim
ddim
=
framework
::
make_ddim
(
output_shape
);
...
...
src/operators/conv_op.h
浏览文件 @
47e5978f
...
...
@@ -26,15 +26,14 @@ namespace operators {
using
namespace
framework
;
template
<
typename
DeviceType
,
typename
T
>
template
<
typename
DeviceType
,
typename
T
>
class
ConvOp
:
public
framework
::
OperatorWithKernel
<
DeviceType
>
{
public:
public:
ConvOp
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
std
::
shared_ptr
<
framework
::
Scope
>
scope
)
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
param_
(
inputs
,
outputs
,
attrs
,
*
scope
)
{}
using
framework
::
OperatorWithKernel
<
DeviceType
>::
OperatorWithKernel
;
...
...
@@ -46,9 +45,9 @@ public:
this
->
ClearVariables
();
}
private:
private:
ConvParam
param_
;
};
}
// operators
}
// paddle_mobile
}
//
namespace
operators
}
//
namespace
paddle_mobile
src/operators/elementwise_add_op.cpp
浏览文件 @
47e5978f
...
...
@@ -21,11 +21,11 @@ SOFTWARE.
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
Dtype
,
typename
T
>
template
<
typename
Dtype
,
typename
T
>
void
ElementwiseAddOp
<
Dtype
,
T
>::
InferShape
()
const
{
auto
x_dim
=
param_
.
InputX
()
->
dims
();
param_
.
Out
()
->
Resize
(
x_dim
);
}
template
class
ElementwiseAddOp
<
CPU
,
float
>;
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/elementwise_add_op.h
浏览文件 @
47e5978f
...
...
@@ -25,22 +25,19 @@ namespace operators {
using
namespace
framework
;
template
<
typename
DeviceType
,
typename
T
>
class
ElementwiseAddOp
:
public
framework
::
OperatorWithKernel
<
DeviceType
>
{
public:
ElementwiseAddOp
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
template
<
typename
DeviceType
,
typename
T
>
class
ElementwiseAddOp
:
public
framework
::
OperatorWithKernel
<
DeviceType
>
{
public:
ElementwiseAddOp
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
attrs
,
std
::
shared_ptr
<
framework
::
Scope
>
scope
)
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
param_
(
inputs
,
outputs
,
attrs
,
*
scope
)
{}
void
Run
()
const
{
operators
::
ElementwiseAddKernel
<
DeviceType
,
T
,
ElementwiseAddParam
>
operators
::
ElementwiseAddKernel
<
DeviceType
,
T
,
ElementwiseAddParam
>
kernel
;
kernel
.
Compute
(
param_
);
}
...
...
@@ -48,8 +45,8 @@ public:
using
framework
::
OperatorWithKernel
<
DeviceType
>::
OperatorWithKernel
;
void
InferShape
()
const
override
;
protected:
protected:
ElementwiseAddParam
param_
;
};
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/arm/conv_kernel.cpp
浏览文件 @
47e5978f
...
...
@@ -22,14 +22,11 @@ namespace paddle_mobile {
namespace
operators
{
bool
IsExpand
(
const
std
::
vector
<
int64_t
>
&
filter_dim
,
const
std
::
vector
<
int
>
&
strides
,
const
std
::
vector
<
int
>
&
paddings
,
const
std
::
vector
<
int
>
&
strides
,
const
std
::
vector
<
int
>
&
paddings
,
const
std
::
vector
<
int
>
&
dilations
)
{
bool
filter_1
=
true
,
strides_1
=
true
,
padding_0
=
true
,
dilation_1
=
true
;
bool
filter_1
=
true
,
strides_1
=
true
,
padding_0
=
true
,
dilation_1
=
true
;
for
(
size_t
j
=
0
;
j
<
strides
.
size
();
++
j
)
{
filter_1
=
filter_1
&&
(
static_cast
<
int
>
(
filter_dim
[
j
+
2
])
==
1
);
filter_1
=
filter_1
&&
(
static_cast
<
int
>
(
filter_dim
[
j
+
2
])
==
1
);
strides_1
=
strides_1
&&
(
strides
[
j
]
==
1
);
padding_0
=
padding_0
&&
(
paddings
[
j
]
==
0
);
dilation_1
=
dilation_1
&&
(
dilations
[
j
]
==
1
);
...
...
@@ -37,9 +34,8 @@ bool IsExpand(const std::vector<int64_t> &filter_dim,
return
!
(
filter_1
&&
strides_1
&&
padding_0
&&
dilation_1
);
}
template
<
>
void
ConvKernel
<
CPU
,
float
,
ConvParam
>::
Compute
(
const
ConvParam
&
param
)
const
{
template
<
>
void
ConvKernel
<
CPU
,
float
,
ConvParam
>::
Compute
(
const
ConvParam
&
param
)
const
{
LOG
(
kLOG_DEBUG
)
<<
param
;
const
Tensor
*
input
=
param
.
Input
();
...
...
@@ -63,12 +59,10 @@ void ConvKernel<CPU, float, ConvParam>::Compute(
// filter_shape_vec: {k_o, k_i, k_h, k_w} or {k_o, k_i, k_d, k_h,
// k_w}
std
::
vector
<
int64_t
>
filter_shape_vec
(
framework
::
vectorize
(
filter
.
dims
()));
std
::
vector
<
int64_t
>
filter_shape_vec
(
framework
::
vectorize
(
filter
.
dims
()));
// output_shape_vec: {o_n, o_c, o_h, o_w} or {o_n, o_c, o_d, o_h,
// o_w}
std
::
vector
<
int64_t
>
output_shape_vec
(
framework
::
vectorize
(
output
->
dims
()));
std
::
vector
<
int64_t
>
output_shape_vec
(
framework
::
vectorize
(
output
->
dims
()));
// use col_shape in the im2col calculation
// col_shape_vec: {i_c/g, k_h, k_w, o_h, o_w} or {i_c/g, k_d, k_h,
...
...
@@ -90,8 +84,7 @@ void ConvKernel<CPU, float, ConvParam>::Compute(
framework
::
DDim
col_matrix_shape
=
framework
::
flatten_to_2d
(
col_shape
,
data_dim
+
1
);
bool
is_expand
=
IsExpand
(
filter_shape_vec
,
strides
,
paddings
,
dilations
);
bool
is_expand
=
IsExpand
(
filter_shape_vec
,
strides
,
paddings
,
dilations
);
Tensor
col
;
// col_matrix shares the same piece of data with col,
// but will be reshaped into a two-dimensional matrix shape
...
...
@@ -106,8 +99,8 @@ void ConvKernel<CPU, float, ConvParam>::Compute(
framework
::
DDim
input_shape
=
framework
::
slice_ddim
(
input
->
dims
(),
1
,
static_cast
<
int
>
(
input
->
dims
().
size
()));
framework
::
DDim
filter_matrix_shape
=
{
filter
.
dims
()[
0
],
filter
.
numel
()
/
filter
.
dims
()[
0
]};
framework
::
DDim
filter_matrix_shape
=
{
filter
.
dims
()[
0
],
filter
.
numel
()
/
filter
.
dims
()[
0
]};
filter
.
Resize
(
filter_matrix_shape
);
framework
::
DDim
output_matrix_shape
=
{
...
...
@@ -125,12 +118,10 @@ void ConvKernel<CPU, float, ConvParam>::Compute(
// device_context<DeviceContext>();
for
(
int
i
=
0
;
i
<
batch_size
;
i
++
)
{
Tensor
in_batch
=
input
->
Slice
(
i
,
i
+
1
).
Resize
(
input_shape
);
Tensor
out_batch
=
output
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
Tensor
out_batch
=
output
->
Slice
(
i
,
i
+
1
).
Resize
(
output_matrix_shape
);
for
(
int
g
=
0
;
g
<
groups
;
g
++
)
{
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
Tensor
in_slice
=
in_batch
.
Slice
(
g
*
in_step
,
(
g
+
1
)
*
in_step
);
if
(
!
is_expand
)
{
col
.
ShareDataWith
(
in_slice
);
...
...
@@ -139,8 +130,8 @@ void ConvKernel<CPU, float, ConvParam>::Compute(
}
else
if
(
data_dim
==
2U
)
{
// im2col
im2col
(
in_slice
,
dilations
,
strides
,
std
::
vector
<
int
>
{
paddings
[
0
],
paddings
[
1
],
paddings
[
0
],
paddings
[
1
]},
std
::
vector
<
int
>
{
paddings
[
0
],
paddings
[
1
],
paddings
[
0
],
paddings
[
1
]},
&
col
);
}
else
if
(
data_dim
==
3U
)
{
// vol2col
...
...
src/operators/kernel/arm/elementwise_add_kernel.cpp
浏览文件 @
47e5978f
...
...
@@ -19,11 +19,11 @@ limitations under the License. */
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
T
>
struct
AddFunctor
{
template
<
typename
T
>
struct
AddFunctor
{
inline
T
operator
()(
T
a
,
T
b
)
const
{
return
a
+
b
;
}
};
template
<
>
template
<
>
void
ElementwiseAddKernel
<
CPU
,
float
,
ElementwiseAddParam
>::
Compute
(
const
ElementwiseAddParam
&
param
)
const
{
const
Tensor
*
input_x
=
param
.
InputX
();
...
...
@@ -31,11 +31,11 @@ void ElementwiseAddKernel<CPU, float, ElementwiseAddParam>::Compute(
Tensor
*
Out
=
param
.
Out
();
Out
->
mutable_data
<
float
>
();
const
int
axis
=
param
.
Axis
();
ElementwiseComputeEx
<
AddFunctor
<
float
>
,
float
>
(
input_x
,
input_y
,
axis
,
AddFunctor
<
float
>
(),
Out
);
ElementwiseComputeEx
<
AddFunctor
<
float
>
,
float
>
(
input_x
,
input_y
,
axis
,
AddFunctor
<
float
>
(),
Out
);
}
template
class
ElementwiseAddKernel
<
CPU
,
float
,
ElementwiseAddParam
>;
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
_mobile
src/operators/kernel/arm/mul_kernel.cpp
浏览文件 @
47e5978f
...
...
@@ -23,9 +23,8 @@ SOFTWARE.
namespace
paddle_mobile
{
namespace
operators
{
template
<
>
void
MulKernel
<
CPU
,
float
,
MulParam
>::
Compute
(
const
MulParam
&
param
)
const
{
template
<
>
void
MulKernel
<
CPU
,
float
,
MulParam
>::
Compute
(
const
MulParam
&
param
)
const
{
const
Tensor
*
input_x
=
param
.
InputX
();
const
Tensor
*
input_y
=
param
.
InputY
();
Tensor
*
out
=
param
.
Out
();
...
...
@@ -42,9 +41,8 @@ MulKernel<CPU, float, MulParam>::Compute(const MulParam ¶m) const {
if
(
out_dim
.
size
()
!=
2
)
{
out
->
Resize
({
x_matrix
.
dims
()[
0
],
y_matrix
.
dims
()[
1
]});
}
math
::
matmul
<
float
>
(
x_matrix
,
false
,
y_matrix
,
false
,
static_cast
<
float
>
(
1
),
out
,
static_cast
<
float
>
(
0
));
math
::
matmul
<
float
>
(
x_matrix
,
false
,
y_matrix
,
false
,
static_cast
<
float
>
(
1
),
out
,
static_cast
<
float
>
(
0
));
if
(
out_dim
.
size
()
!=
2
)
{
out
->
Resize
(
out_dim
);
}
...
...
@@ -53,4 +51,4 @@ MulKernel<CPU, float, MulParam>::Compute(const MulParam ¶m) const {
template
class
MulKernel
<
CPU
,
float
,
MulParam
>;
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
_mobile
src/operators/kernel/conv_kernel.h
浏览文件 @
47e5978f
...
...
@@ -29,11 +29,10 @@ namespace operators {
using
namespace
framework
;
template
<
typename
DeviceType
,
typename
T
,
typename
P
>
class
ConvKernel
:
public
framework
::
OpKernelBase
<
DeviceType
,
ConvParam
>
{
public:
template
<
typename
DeviceType
,
typename
T
,
typename
P
>
class
ConvKernel
:
public
framework
::
OpKernelBase
<
DeviceType
,
ConvParam
>
{
public:
void
Compute
(
const
ConvParam
&
param
)
const
;
};
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/elementwise_add_kernel.h
浏览文件 @
47e5978f
...
...
@@ -26,11 +26,11 @@ namespace operators {
using
namespace
framework
;
template
<
typename
DeviceType
,
typename
T
,
typename
P
>
template
<
typename
DeviceType
,
typename
T
,
typename
P
>
class
ElementwiseAddKernel
:
public
framework
::
OpKernelBase
<
DeviceType
,
ElementwiseAddParam
>
{
public:
public:
void
Compute
(
const
ElementwiseAddParam
&
param
)
const
;
};
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/fpga/conv_kernel.cpp
浏览文件 @
47e5978f
...
...
@@ -25,4 +25,4 @@ namespace operators {
//
// template class ConvKernel<FPGA, float>;
}
}
}
// namespace paddle_mobile
src/operators/kernel/mul_kernel.h
浏览文件 @
47e5978f
...
...
@@ -26,10 +26,10 @@ namespace operators {
using
namespace
framework
;
template
<
typename
DeviceType
,
typename
T
,
typename
P
>
template
<
typename
DeviceType
,
typename
T
,
typename
P
>
class
MulKernel
:
public
framework
::
OpKernelBase
<
DeviceType
,
MulParam
>
{
public:
public:
void
Compute
(
const
MulParam
&
param
)
const
;
};
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/math/elementwise_op_function.h
浏览文件 @
47e5978f
...
...
@@ -68,10 +68,9 @@ inline void trim_trailing_singular_dims(framework::DDim *dims) {
}
}
template
<
typename
T
>
class
RowwiseTransformIterator
{
public:
RowwiseTransformIterator
(
const
T
*
ptr
,
int
n
)
:
ptr_
(
ptr
),
i_
(
0
),
n_
(
n
)
{}
template
<
typename
T
>
class
RowwiseTransformIterator
{
public:
RowwiseTransformIterator
(
const
T
*
ptr
,
int
n
)
:
ptr_
(
ptr
),
i_
(
0
),
n_
(
n
)
{}
RowwiseTransformIterator
<
T
>
&
operator
++
()
{
++
i_
;
...
...
@@ -91,7 +90,7 @@ public:
const
T
&
operator
*
()
{
return
ptr_
[
i_
];
}
private:
private:
const
T
*
ptr_
;
int
i_
;
int64_t
n_
;
...
...
@@ -101,8 +100,8 @@ private:
/// dimension
/// in (4,20,2) is 2 ,
/// (20,1) move 1 stride , to fill(add) 2 element with the same number.
template
<
typename
T
>
class
MidWiseTransformIterator
{
public:
template
<
typename
T
>
class
MidWiseTransformIterator
{
public:
MidWiseTransformIterator
(
const
T
*
ptr
,
int
n
,
int
post
)
:
ptr_
(
ptr
),
i_
(
0
),
j_
(
0
),
n_
(
n
),
post_
(
post
)
{}
...
...
@@ -128,7 +127,7 @@ public:
const
T
&
operator
*
()
{
return
ptr_
[
i_
];
}
private:
private:
const
T
*
ptr_
;
int64_t
i_
;
int64_t
j_
;
...
...
@@ -136,15 +135,13 @@ private:
int64_t
post_
;
};
template
<
typename
Functor
,
typename
T
,
typename
OutType
=
T
>
template
<
typename
Functor
,
typename
T
,
typename
OutType
=
T
>
class
TransformFunctor
{
public:
TransformFunctor
(
const
framework
::
Tensor
*
x
,
const
framework
::
Tensor
*
y
,
framework
::
Tensor
*
z
,
Functor
func
)
:
x_
(
x
->
data
<
T
>
()),
y_
(
y
->
data
<
T
>
()),
z_
(
z
->
mutable_data
<
OutType
>
()),
nx_
(
x
->
numel
()),
func_
(
func
)
{
}
public:
TransformFunctor
(
const
framework
::
Tensor
*
x
,
const
framework
::
Tensor
*
y
,
framework
::
Tensor
*
z
,
Functor
func
)
:
x_
(
x
->
data
<
T
>
()),
y_
(
y
->
data
<
T
>
()),
z_
(
z
->
mutable_data
<
OutType
>
()),
nx_
(
x
->
numel
()),
func_
(
func
)
{}
inline
void
Run
()
const
{
math
::
Transform
trans
;
...
...
@@ -154,17 +151,16 @@ public:
inline
void
RunRowWise
(
int
n
,
int
pre
)
const
{
math
::
Transform
trans
;
trans
(
x_
,
x_
+
nx_
,
RowwiseTransformIterator
<
T
>
(
y_
,
n
),
z_
,
func_
);
trans
(
x_
,
x_
+
nx_
,
RowwiseTransformIterator
<
T
>
(
y_
,
n
),
z_
,
func_
);
}
inline
void
RunMidWise
(
int
n
,
int
pre
,
int
post
)
const
{
math
::
Transform
trans
;
trans
(
x_
,
x_
+
nx_
,
MidWiseTransformIterator
<
T
>
(
y_
,
n
,
post
)
,
z_
,
func_
);
trans
(
x_
,
x_
+
nx_
,
MidWiseTransformIterator
<
T
>
(
y_
,
n
,
post
),
z_
,
func_
);
}
private:
private:
const
T
*
x_
;
const
T
*
y_
;
OutType
*
z_
;
...
...
@@ -172,10 +168,10 @@ private:
Functor
func_
;
};
template
<
typename
Functor
,
typename
T
,
typename
OutType
=
T
>
template
<
typename
Functor
,
typename
T
,
typename
OutType
=
T
>
void
ElementwiseComputeEx
(
const
framework
::
Tensor
*
x
,
const
framework
::
Tensor
*
y
,
int
axis
,
Functor
func
,
framework
::
Tensor
*
z
)
{
const
framework
::
Tensor
*
y
,
int
axis
,
Functor
func
,
framework
::
Tensor
*
z
)
{
TransformFunctor
<
Functor
,
T
,
OutType
>
functor
(
x
,
y
,
z
,
func
);
auto
x_dims
=
x
->
dims
();
...
...
@@ -208,4 +204,4 @@ void ElementwiseComputeEx(const framework::Tensor *x,
}
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
_mobile
src/operators/math/im2col.cc
浏览文件 @
47e5978f
...
...
@@ -25,13 +25,12 @@ namespace math {
* [input_channels, filter_height, filter_width, output_height,
* output_width]
*/
template
<
class
T
>
class
Im2ColFunctor
<
ColFormat
::
kCFO
,
CPU
,
T
>
{
public:
template
<
class
T
>
class
Im2ColFunctor
<
ColFormat
::
kCFO
,
CPU
,
T
>
{
public:
void
operator
()(
const
framework
::
Tensor
&
im
,
const
std
::
vector
<
int
>
&
dilation
,
const
std
::
vector
<
int
>
&
stride
,
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
col
)
{
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
col
)
{
// PADDLE_ENFORCE(im.dims().size() == 3);
// PADDLE_ENFORCE(col->dims().size() == 5);
...
...
@@ -64,8 +63,7 @@ public:
// padding(padding_up, padding_down)
// are " "inconsistent.");
int
channels_col
=
im_channels
*
filter_height
*
filter_width
;
int
channels_col
=
im_channels
*
filter_height
*
filter_width
;
const
T
*
im_data
=
im
.
data
<
T
>
();
T
*
col_data
=
col
->
data
<
T
>
();
...
...
@@ -74,20 +72,17 @@ public:
int
h_offset
=
(
c
/
filter_width
)
%
filter_height
;
int
c_im
=
c
/
(
filter_width
*
filter_height
);
for
(
int
h
=
0
;
h
<
col_height
;
++
h
)
{
int
im_row_idx
=
h
*
stride
[
0
]
-
padding
[
0
]
+
h_offset
*
dilation
[
0
];
int
im_row_idx
=
h
*
stride
[
0
]
-
padding
[
0
]
+
h_offset
*
dilation
[
0
];
for
(
int
w
=
0
;
w
<
col_width
;
++
w
)
{
int
im_col_idx
=
w
*
stride
[
1
]
-
padding
[
1
]
+
w_offset
*
dilation
[
1
];
int
col_idx
=
(
c
*
col_height
+
h
)
*
col_width
+
w
;
int
im_col_idx
=
w
*
stride
[
1
]
-
padding
[
1
]
+
w_offset
*
dilation
[
1
];
int
col_idx
=
(
c
*
col_height
+
h
)
*
col_width
+
w
;
int
im_idx
=
(
im_row_idx
+
c_im
*
im_height
)
*
im_width
+
im_col_idx
;
(
im_row_idx
+
c_im
*
im_height
)
*
im_width
+
im_col_idx
;
col_data
[
col_idx
]
=
(
im_row_idx
<
0
||
im_row_idx
>=
im_height
||
(
im_row_idx
<
0
||
im_row_idx
>=
im_height
||
im_col_idx
<
0
||
im_col_idx
>=
im_width
)
?
static_cast
<
T
>
(
0
)
:
im_data
[
im_idx
];
...
...
@@ -103,13 +98,12 @@ public:
* [input_channels, filter_height, filter_width, output_height,
* output_width]
*/
template
<
class
T
>
class
Col2ImFunctor
<
ColFormat
::
kCFO
,
CPU
,
T
>
{
public:
template
<
class
T
>
class
Col2ImFunctor
<
ColFormat
::
kCFO
,
CPU
,
T
>
{
public:
void
operator
()(
const
framework
::
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilation
,
const
std
::
vector
<
int
>
&
stride
,
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
im
)
{
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
im
)
{
// PADDLE_ENFORCE(im->dims().size() == 3);
// PADDLE_ENFORCE(col.dims().size() == 5);
int
im_channels
=
im
->
dims
()[
0
];
...
...
@@ -141,8 +135,7 @@ public:
// padding(padding_up, padding_down)
// are " "inconsistent.");
int
channels_col
=
im_channels
*
filter_height
*
filter_width
;
int
channels_col
=
im_channels
*
filter_height
*
filter_width
;
T
*
im_data
=
im
->
data
<
T
>
();
const
T
*
col_data
=
col
.
data
<
T
>
();
...
...
@@ -152,21 +145,16 @@ public:
int
h_offset
=
(
c
/
filter_width
)
%
filter_height
;
int
c_im
=
c
/
(
filter_width
*
filter_height
);
for
(
int
h
=
0
;
h
<
col_height
;
++
h
)
{
int
im_row_idx
=
h
*
stride
[
0
]
-
padding
[
0
]
+
h_offset
*
dilation
[
0
];
int
im_row_idx
=
h
*
stride
[
0
]
-
padding
[
0
]
+
h_offset
*
dilation
[
0
];
for
(
int
w
=
0
;
w
<
col_width
;
++
w
)
{
int
im_col_idx
=
w
*
stride
[
1
]
-
padding
[
1
]
+
w_offset
*
dilation
[
1
];
if
((
im_row_idx
)
>=
0
&&
(
im_row_idx
)
<
im_height
&&
(
im_col_idx
)
>=
0
&&
(
im_col_idx
)
<
im_width
)
{
im_data
[(
im_row_idx
+
c_im
*
im_height
)
*
im_width
+
int
im_col_idx
=
w
*
stride
[
1
]
-
padding
[
1
]
+
w_offset
*
dilation
[
1
];
if
((
im_row_idx
)
>=
0
&&
(
im_row_idx
)
<
im_height
&&
(
im_col_idx
)
>=
0
&&
(
im_col_idx
)
<
im_width
)
{
im_data
[(
im_row_idx
+
c_im
*
im_height
)
*
im_width
+
im_col_idx
]
+=
col_data
[(
c
*
col_height
+
h
)
*
col_width
+
w
];
col_data
[(
c
*
col_height
+
h
)
*
col_width
+
w
];
}
}
}
...
...
@@ -185,13 +173,12 @@ template class Col2ImFunctor<ColFormat::kCFO, CPU, double>;
* [output_height, output_width, input_channels, filter_height,
* filter_width]
*/
template
<
class
T
>
class
Im2ColFunctor
<
ColFormat
::
kOCF
,
CPU
,
T
>
{
public:
template
<
class
T
>
class
Im2ColFunctor
<
ColFormat
::
kOCF
,
CPU
,
T
>
{
public:
void
operator
()(
const
framework
::
Tensor
&
im
,
const
std
::
vector
<
int
>
&
dilation
,
const
std
::
vector
<
int
>
&
stride
,
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
col
)
{
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
col
)
{
// PADDLE_ENFORCE(im.dims().size() == 3);
// PADDLE_ENFORCE(col->dims().size() == 5);
int
im_channels
=
im
.
dims
()[
0
];
...
...
@@ -218,28 +205,20 @@ public:
const
T
*
im_data
=
im
.
data
<
T
>
();
T
*
col_data
=
col
->
data
<
T
>
();
for
(
int
col_row_idx
=
0
;
col_row_idx
<
col_height
;
++
col_row_idx
)
{
for
(
int
col_col_idx
=
0
;
col_col_idx
<
col_width
;
++
col_col_idx
)
{
for
(
int
channel
=
0
;
channel
<
im_channels
;
++
channel
)
{
for
(
int
filter_row_idx
=
0
;
filter_row_idx
<
filter_height
;
for
(
int
col_row_idx
=
0
;
col_row_idx
<
col_height
;
++
col_row_idx
)
{
for
(
int
col_col_idx
=
0
;
col_col_idx
<
col_width
;
++
col_col_idx
)
{
for
(
int
channel
=
0
;
channel
<
im_channels
;
++
channel
)
{
for
(
int
filter_row_idx
=
0
;
filter_row_idx
<
filter_height
;
++
filter_row_idx
)
{
int
im_row_offset
=
col_row_idx
*
stride
[
0
]
+
int
im_row_offset
=
col_row_idx
*
stride
[
0
]
+
filter_row_idx
-
padding
[
0
];
for
(
int
filter_col_idx
=
0
;
filter_col_idx
<
filter_width
;
++
filter_col_idx
)
{
int
im_col_offset
=
col_col_idx
*
stride
[
1
]
+
filter_col_idx
<
filter_width
;
++
filter_col_idx
)
{
int
im_col_offset
=
col_col_idx
*
stride
[
1
]
+
filter_col_idx
-
padding
[
1
];
int
col_offset
=
((((
col_row_idx
)
*
col_width
+
col_col_idx
)
*
((((
col_row_idx
)
*
col_width
+
col_col_idx
)
*
im_channels
+
channel
)
*
filter_height
+
...
...
@@ -247,15 +226,14 @@ public:
filter_width
+
filter_col_idx
;
int
im_offset
=
(
channel
*
im_height
+
im_row_offset
)
*
int
im_offset
=
(
channel
*
im_height
+
im_row_offset
)
*
im_width
+
im_col_offset
;
col_data
[
col_offset
]
=
(
im_row_offset
<
0
||
im_row_offset
>=
im_height
||
im_col_offset
<
0
||
im_col_offset
>=
im_width
)
im_col_offset
<
0
||
im_col_offset
>=
im_width
)
?
static_cast
<
T
>
(
0
)
:
im_data
[
im_offset
];
}
...
...
@@ -272,13 +250,12 @@ public:
* [output_height, output_width, input_channels, filter_height,
* filter_width]
*/
template
<
class
T
>
class
Col2ImFunctor
<
ColFormat
::
kOCF
,
CPU
,
T
>
{
public:
template
<
class
T
>
class
Col2ImFunctor
<
ColFormat
::
kOCF
,
CPU
,
T
>
{
public:
void
operator
()(
const
framework
::
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilation
,
const
std
::
vector
<
int
>
&
stride
,
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
im
)
{
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
im
)
{
// PADDLE_ENFORCE(im->dims().size() == 3);
// PADDLE_ENFORCE(col.dims().size() == 5);
int
im_channels
=
im
->
dims
()[
0
];
...
...
@@ -305,28 +282,20 @@ public:
T
*
im_data
=
im
->
data
<
T
>
();
const
T
*
col_data
=
col
.
data
<
T
>
();
for
(
int
col_row_idx
=
0
;
col_row_idx
<
col_height
;
++
col_row_idx
)
{
for
(
int
col_col_idx
=
0
;
col_col_idx
<
col_width
;
++
col_col_idx
)
{
for
(
int
channel
=
0
;
channel
<
im_channels
;
++
channel
)
{
for
(
int
filter_row_idx
=
0
;
filter_row_idx
<
filter_height
;
for
(
int
col_row_idx
=
0
;
col_row_idx
<
col_height
;
++
col_row_idx
)
{
for
(
int
col_col_idx
=
0
;
col_col_idx
<
col_width
;
++
col_col_idx
)
{
for
(
int
channel
=
0
;
channel
<
im_channels
;
++
channel
)
{
for
(
int
filter_row_idx
=
0
;
filter_row_idx
<
filter_height
;
++
filter_row_idx
)
{
int
im_row_offset
=
col_row_idx
*
stride
[
0
]
+
int
im_row_offset
=
col_row_idx
*
stride
[
0
]
+
filter_row_idx
-
padding
[
0
];
for
(
int
filter_col_idx
=
0
;
filter_col_idx
<
filter_width
;
++
filter_col_idx
)
{
int
im_col_offset
=
col_col_idx
*
stride
[
1
]
+
filter_col_idx
<
filter_width
;
++
filter_col_idx
)
{
int
im_col_offset
=
col_col_idx
*
stride
[
1
]
+
filter_col_idx
-
padding
[
1
];
int
col_offset
=
(((
col_row_idx
*
col_width
+
col_col_idx
)
*
(((
col_row_idx
*
col_width
+
col_col_idx
)
*
im_channels
+
channel
)
*
filter_height
+
...
...
@@ -339,12 +308,10 @@ public:
im_col_offset
>=
0
&&
im_col_offset
<
im_width
)
{
int
im_offset
=
(
channel
*
im_height
+
im_row_offset
)
*
(
channel
*
im_height
+
im_row_offset
)
*
im_width
+
im_col_offset
;
im_data
[
im_offset
]
+=
col_data
[
col_offset
];
im_data
[
im_offset
]
+=
col_data
[
col_offset
];
}
}
}
...
...
src/operators/math/im2col.h
浏览文件 @
47e5978f
...
...
@@ -87,24 +87,22 @@ enum class ColFormat { kCFO = 0, kOCF = 1 };
* equal to
* colShape.inputChannels.
*/
template
<
ColFormat
Format
,
typename
DeviceType
,
typename
T
>
template
<
ColFormat
Format
,
typename
DeviceType
,
typename
T
>
class
Im2ColFunctor
{
public:
public:
void
operator
()(
const
framework
::
Tensor
&
im
,
const
std
::
vector
<
int
>
&
dilation
,
const
std
::
vector
<
int
>
&
stride
,
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
col
);
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
col
);
};
template
<
ColFormat
Format
,
typename
DeviceType
,
typename
T
>
template
<
ColFormat
Format
,
typename
DeviceType
,
typename
T
>
class
Col2ImFunctor
{
public:
public:
void
operator
()(
const
framework
::
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilation
,
const
std
::
vector
<
int
>
&
stride
,
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
im
);
const
std
::
vector
<
int
>
&
padding
,
framework
::
Tensor
*
im
);
};
}
// namespace math
...
...
src/operators/math/math_function.cc
浏览文件 @
47e5978f
...
...
@@ -18,61 +18,53 @@ namespace paddle_mobile {
namespace
operators
{
namespace
math
{
template
<
>
void
gemm
<
float
>
(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
float
alpha
,
const
float
*
A
,
const
float
*
B
,
const
float
beta
,
float
*
C
)
{
template
<
>
void
gemm
<
float
>
(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
float
alpha
,
const
float
*
A
,
const
float
*
B
,
const
float
beta
,
float
*
C
)
{
int
lda
=
(
transA
==
CblasNoTrans
)
?
K
:
M
;
int
ldb
=
(
transB
==
CblasNoTrans
)
?
N
:
K
;
int
ldc
=
N
;
cblas_sgemm
(
CblasRowMajor
,
transA
,
transB
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
cblas_sgemm
(
CblasRowMajor
,
transA
,
transB
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
}
template
<
>
void
gemm
<
double
>
(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
double
alpha
,
const
double
*
A
,
const
double
*
B
,
const
double
beta
,
double
*
C
)
{
template
<
>
void
gemm
<
double
>
(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
double
alpha
,
const
double
*
A
,
const
double
*
B
,
const
double
beta
,
double
*
C
)
{
int
lda
=
(
transA
==
CblasNoTrans
)
?
K
:
M
;
int
ldb
=
(
transB
==
CblasNoTrans
)
?
N
:
K
;
int
ldc
=
N
;
cblas_dgemm
(
CblasRowMajor
,
transA
,
transB
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
cblas_dgemm
(
CblasRowMajor
,
transA
,
transB
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
}
template
<
>
void
gemm
<
float
>
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
float
alpha
,
const
float
*
A
,
const
int
lda
,
const
float
*
B
,
const
int
ldb
,
const
float
beta
,
float
*
C
,
template
<
>
void
gemm
<
float
>
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
float
alpha
,
const
float
*
A
,
const
int
lda
,
const
float
*
B
,
const
int
ldb
,
const
float
beta
,
float
*
C
,
const
int
ldc
)
{
cblas_sgemm
(
CblasRowMajor
,
transA
==
false
?
CblasNoTrans
:
CblasTrans
,
transB
==
false
?
CblasNoTrans
:
CblasTrans
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
cblas_sgemm
(
CblasRowMajor
,
transA
==
false
?
CblasNoTrans
:
CblasTrans
,
transB
==
false
?
CblasNoTrans
:
CblasTrans
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
}
template
<
>
template
<
>
void
gemm
<
double
>
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
double
alpha
,
const
double
*
A
,
const
int
lda
,
const
double
*
B
,
const
int
ldb
,
const
double
beta
,
double
*
C
,
const
int
ldc
)
{
cblas_dgemm
(
CblasRowMajor
,
transA
==
false
?
CblasNoTrans
:
CblasTrans
,
transB
==
false
?
CblasNoTrans
:
CblasTrans
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
const
int
N
,
const
int
K
,
const
double
alpha
,
const
double
*
A
,
const
int
lda
,
const
double
*
B
,
const
int
ldb
,
const
double
beta
,
double
*
C
,
const
int
ldc
)
{
cblas_dgemm
(
CblasRowMajor
,
transA
==
false
?
CblasNoTrans
:
CblasTrans
,
transB
==
false
?
CblasNoTrans
:
CblasTrans
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
}
template
<
>
template
<
>
void
matmul
<
float
>
(
const
framework
::
Tensor
&
matrix_a
,
bool
trans_a
,
const
framework
::
Tensor
&
matrix_b
,
bool
trans_b
,
float
alpha
,
framework
::
Tensor
*
matrix_out
,
float
beta
)
{
const
framework
::
Tensor
&
matrix_b
,
bool
trans_b
,
float
alpha
,
framework
::
Tensor
*
matrix_out
,
float
beta
)
{
auto
dim_a
=
matrix_a
.
dims
();
auto
dim_b
=
matrix_b
.
dims
();
auto
dim_out
=
matrix_out
->
dims
();
...
...
@@ -91,21 +83,17 @@ void matmul<float>(const framework::Tensor &matrix_a, bool trans_a,
int
N
=
dim_out
[
1
];
int
K
=
(
trans_a
==
false
)
?
dim_a
[
1
]
:
dim_a
[
0
];
CBLAS_TRANSPOSE
transA
=
(
trans_a
==
false
)
?
CblasNoTrans
:
CblasTrans
;
CBLAS_TRANSPOSE
transB
=
(
trans_b
==
false
)
?
CblasNoTrans
:
CblasTrans
;
CBLAS_TRANSPOSE
transA
=
(
trans_a
==
false
)
?
CblasNoTrans
:
CblasTrans
;
CBLAS_TRANSPOSE
transB
=
(
trans_b
==
false
)
?
CblasNoTrans
:
CblasTrans
;
gemm
<
float
>
(
transA
,
transB
,
M
,
N
,
K
,
alpha
,
matrix_a
.
data
<
float
>
(),
matrix_b
.
data
<
float
>
(),
beta
,
matrix_out
->
data
<
float
>
());
gemm
<
float
>
(
transA
,
transB
,
M
,
N
,
K
,
alpha
,
matrix_a
.
data
<
float
>
(),
matrix_b
.
data
<
float
>
(),
beta
,
matrix_out
->
data
<
float
>
());
}
template
<
>
template
<
>
void
matmul
<
double
>
(
const
framework
::
Tensor
&
matrix_a
,
bool
trans_a
,
const
framework
::
Tensor
&
matrix_b
,
bool
trans_b
,
double
alpha
,
framework
::
Tensor
*
matrix_out
,
double
beta
)
{
double
alpha
,
framework
::
Tensor
*
matrix_out
,
double
beta
)
{
auto
dim_a
=
matrix_a
.
dims
();
auto
dim_b
=
matrix_b
.
dims
();
auto
dim_out
=
matrix_out
->
dims
();
...
...
@@ -124,14 +112,11 @@ void matmul<double>(const framework::Tensor &matrix_a, bool trans_a,
int
N
=
dim_out
[
1
];
int
K
=
(
trans_a
==
false
)
?
dim_a
[
1
]
:
dim_a
[
0
];
CBLAS_TRANSPOSE
transA
=
(
trans_a
==
false
)
?
CblasNoTrans
:
CblasTrans
;
CBLAS_TRANSPOSE
transB
=
(
trans_b
==
false
)
?
CblasNoTrans
:
CblasTrans
;
CBLAS_TRANSPOSE
transA
=
(
trans_a
==
false
)
?
CblasNoTrans
:
CblasTrans
;
CBLAS_TRANSPOSE
transB
=
(
trans_b
==
false
)
?
CblasNoTrans
:
CblasTrans
;
gemm
<
double
>
(
transA
,
transB
,
M
,
N
,
K
,
alpha
,
matrix_a
.
data
<
double
>
(),
matrix_b
.
data
<
double
>
(),
beta
,
matrix_out
->
data
<
double
>
());
gemm
<
double
>
(
transA
,
transB
,
M
,
N
,
K
,
alpha
,
matrix_a
.
data
<
double
>
(),
matrix_b
.
data
<
double
>
(),
beta
,
matrix_out
->
data
<
double
>
());
}
}
// namespace math
...
...
src/operators/math/math_function.h
浏览文件 @
47e5978f
...
...
@@ -22,23 +22,21 @@ namespace paddle_mobile {
namespace
operators
{
namespace
math
{
template
<
typename
T
>
void
gemm
(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
T
*
B
,
const
T
beta
,
T
*
C
);
template
<
typename
T
>
void
gemm
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
int
lda
,
const
T
*
B
,
const
int
ldb
,
const
T
beta
,
T
*
C
,
const
int
ldc
);
template
<
typename
T
>
void
gemm
(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
T
*
B
,
const
T
beta
,
T
*
C
);
template
<
typename
T
>
void
gemm
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
int
lda
,
const
T
*
B
,
const
int
ldb
,
const
T
beta
,
T
*
C
,
const
int
ldc
);
// matrix multiply with continuous memory
template
<
typename
T
>
template
<
typename
T
>
void
matmul
(
const
framework
::
Tensor
&
matrix_a
,
bool
trans_a
,
const
framework
::
Tensor
&
matrix_b
,
bool
trans_b
,
T
alpha
,
framework
::
Tensor
*
matrix_out
,
T
beta
);
const
framework
::
Tensor
&
matrix_b
,
bool
trans_b
,
T
alpha
,
framework
::
Tensor
*
matrix_out
,
T
beta
);
}
// namespace math
}
// namespace operators
}
// namespace paddle_mobile
src/operators/math/transform.h
浏览文件 @
47e5978f
...
...
@@ -37,21 +37,19 @@ namespace math {
// class, paddle::fluid::operators::RowwiseTRansformIterator.
struct
Transform
{
template
<
typename
InputIter
,
typename
OutputIter
,
typename
UnaryOperation
>
void
operator
()(
InputIter
first
,
InputIter
last
,
OutputIter
result
,
UnaryOperation
op
)
{
template
<
typename
InputIter
,
typename
OutputIter
,
typename
UnaryOperation
>
void
operator
()(
InputIter
first
,
InputIter
last
,
OutputIter
result
,
UnaryOperation
op
)
{
std
::
transform
(
first
,
last
,
result
,
op
);
}
template
<
typename
InputIter1
,
typename
InputIter2
,
typename
OutputIter
,
typename
BinaryOperation
>
void
operator
()(
InputIter1
first1
,
InputIter1
last1
,
InputIter2
first2
,
OutputIter
result
,
BinaryOperation
op
)
{
template
<
typename
InputIter1
,
typename
InputIter2
,
typename
OutputIter
,
typename
BinaryOperation
>
void
operator
()(
InputIter1
first1
,
InputIter1
last1
,
InputIter2
first2
,
OutputIter
result
,
BinaryOperation
op
)
{
std
::
transform
(
first1
,
last1
,
first2
,
result
,
op
);
}
};
}
}
// namespace
platform
}
// namespace paddle
}
// namespace math
}
// namespace
operators
}
// namespace paddle
_mobile
src/operators/math/vol2col.cc
浏览文件 @
47e5978f
...
...
@@ -25,13 +25,11 @@ using Tensor = paddle_mobile::framework::Tensor;
* [input_channels, filter_depth, filter_height, filter_width,
* output_depth, output_height, output_width]
*/
template
<
typename
T
>
class
Vol2ColFunctor
<
CPU
,
T
>
{
public:
void
operator
()(
const
Tensor
&
vol
,
const
std
::
vector
<
int
>
&
dilations
,
template
<
typename
T
>
class
Vol2ColFunctor
<
CPU
,
T
>
{
public:
void
operator
()(
const
Tensor
&
vol
,
const
std
::
vector
<
int
>
&
dilations
,
const
std
::
vector
<
int
>
&
strides
,
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
col
)
const
{
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
col
)
const
{
// PADDLE_ENFORCE(vol.dims().size() == 4);
// PADDLE_ENFORCE(col->dims().size() == 7);
...
...
@@ -45,8 +43,8 @@ public:
int
output_depth
=
col
->
dims
()[
4
];
int
output_height
=
col
->
dims
()[
5
];
int
output_width
=
col
->
dims
()[
6
];
int
channels_col
=
input_channels
*
filter_depth
*
filter_height
*
filter_width
;
int
channels_col
=
input_channels
*
filter_depth
*
filter_height
*
filter_width
;
// PADDLE_ENFORCE_EQ((input_depth + 2 * paddings[0] -
// ((dilations[0] * (filter_depth - 1)
...
...
@@ -80,35 +78,31 @@ public:
for
(
int
c
=
0
;
c
<
channels_col
;
++
c
)
{
int
w_offset
=
c
%
filter_width
;
int
h_offset
=
(
c
/
filter_width
)
%
filter_height
;
int
d_offset
=
(
c
/
filter_width
/
filter_height
)
%
filter_depth
;
int
c_in
=
c
/
filter_width
/
filter_height
/
filter_depth
;
int
d_offset
=
(
c
/
filter_width
/
filter_height
)
%
filter_depth
;
int
c_in
=
c
/
filter_width
/
filter_height
/
filter_depth
;
for
(
int
d
=
0
;
d
<
output_depth
;
++
d
)
{
int
d_pad
=
d
*
strides
[
0
]
-
paddings
[
0
]
+
d_offset
*
dilations
[
0
];
int
d_pad
=
d
*
strides
[
0
]
-
paddings
[
0
]
+
d_offset
*
dilations
[
0
];
for
(
int
h
=
0
;
h
<
output_height
;
++
h
)
{
int
h_pad
=
h
*
strides
[
1
]
-
paddings
[
1
]
+
h_offset
*
dilations
[
1
];
int
h_pad
=
h
*
strides
[
1
]
-
paddings
[
1
]
+
h_offset
*
dilations
[
1
];
for
(
int
w
=
0
;
w
<
output_width
;
++
w
)
{
int
w_pad
=
w
*
strides
[
2
]
-
paddings
[
2
]
+
w_offset
*
dilations
[
2
];
int
col_idx
=
((
c
*
output_depth
+
d
)
*
output_height
+
h
)
*
int
col_idx
=
((
c
*
output_depth
+
d
)
*
output_height
+
h
)
*
output_width
+
w
;
int
vol_idx
=
((
c_in
*
input_depth
+
d_pad
)
*
input_height
+
((
c_in
*
input_depth
+
d_pad
)
*
input_height
+
h_pad
)
*
input_width
+
w_pad
;
col_data
[
col_idx
]
=
(
h_pad
<
0
||
h_pad
>=
input_height
||
w_pad
<
0
||
w_pad
>=
input_width
||
d_pad
<
0
||
d_pad
>=
input_depth
)
(
h_pad
<
0
||
h_pad
>=
input_height
||
w_pad
<
0
||
w_pad
>=
input_width
||
d_pad
<
0
||
d_pad
>=
input_depth
)
?
static_cast
<
T
>
(
0
)
:
vol_data
[
vol_idx
];
}
...
...
@@ -124,13 +118,11 @@ public:
* [input_channels, filter_depth, filter_height, filter_width,
* output_depth, output_height, output_width]
*/
template
<
typename
T
>
class
Col2VolFunctor
<
CPU
,
T
>
{
public:
void
operator
()(
const
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilations
,
template
<
typename
T
>
class
Col2VolFunctor
<
CPU
,
T
>
{
public:
void
operator
()(
const
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilations
,
const
std
::
vector
<
int
>
&
strides
,
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
vol
)
const
{
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
vol
)
const
{
// PADDLE_ENFORCE(vol->dims().size() == 4);
// PADDLE_ENFORCE(col.dims().size() == 7);
...
...
@@ -144,8 +136,8 @@ public:
int
output_depth
=
col
.
dims
()[
4
];
int
output_height
=
col
.
dims
()[
5
];
int
output_width
=
col
.
dims
()[
6
];
int
channels_col
=
input_channels
*
filter_depth
*
filter_height
*
filter_width
;
int
channels_col
=
input_channels
*
filter_depth
*
filter_height
*
filter_width
;
// PADDLE_ENFORCE_EQ((input_depth + 2 * paddings[0] -
// ((dilations[0] * (filter_depth - 1)
...
...
@@ -178,33 +170,29 @@ public:
for
(
int
c
=
0
;
c
<
channels_col
;
++
c
)
{
int
w_offset
=
c
%
filter_width
;
int
h_offset
=
(
c
/
filter_width
)
%
filter_height
;
int
d_offset
=
(
c
/
filter_width
/
filter_height
)
%
filter_depth
;
int
cIm
=
c
/
filter_width
/
filter_height
/
filter_depth
;
int
d_offset
=
(
c
/
filter_width
/
filter_height
)
%
filter_depth
;
int
cIm
=
c
/
filter_width
/
filter_height
/
filter_depth
;
for
(
int
d
=
0
;
d
<
output_depth
;
++
d
)
{
int
d_pad
=
d
*
strides
[
0
]
-
paddings
[
0
]
+
d_offset
*
dilations
[
0
];
int
d_pad
=
d
*
strides
[
0
]
-
paddings
[
0
]
+
d_offset
*
dilations
[
0
];
for
(
int
h
=
0
;
h
<
output_height
;
++
h
)
{
int
h_pad
=
h
*
strides
[
1
]
-
paddings
[
1
]
+
h_offset
*
dilations
[
1
];
int
h_pad
=
h
*
strides
[
1
]
-
paddings
[
1
]
+
h_offset
*
dilations
[
1
];
for
(
int
w
=
0
;
w
<
output_width
;
++
w
)
{
int
w_pad
=
w
*
strides
[
2
]
-
paddings
[
2
]
+
w_offset
*
dilations
[
2
];
if
(
h_pad
>=
0
&&
h_pad
<
input_height
&&
w_pad
>=
0
&&
w_pad
<
input_width
&&
d_pad
>=
0
&&
d_pad
<
input_depth
)
{
if
(
h_pad
>=
0
&&
h_pad
<
input_height
&&
w_pad
>=
0
&&
w_pad
<
input_width
&&
d_pad
>=
0
&&
d_pad
<
input_depth
)
{
int
vol_idx
=
((
cIm
*
input_depth
+
d_pad
)
*
input_height
+
((
cIm
*
input_depth
+
d_pad
)
*
input_height
+
h_pad
)
*
input_width
+
w_pad
;
int
col_idx
=
((
c
*
output_depth
+
d
)
*
output_height
+
h
)
*
int
col_idx
=
((
c
*
output_depth
+
d
)
*
output_height
+
h
)
*
output_width
+
w
;
vol_data
[
vol_idx
]
+=
col_data
[
col_idx
];
...
...
src/operators/math/vol2col.h
浏览文件 @
47e5978f
...
...
@@ -72,22 +72,18 @@ namespace math {
*/
using
Tensor
=
paddle_mobile
::
framework
::
Tensor
;
template
<
typename
DeviceType
,
typename
T
>
class
Vol2ColFunctor
{
public:
void
operator
()(
const
Tensor
&
vol
,
const
std
::
vector
<
int
>
&
dilations
,
template
<
typename
DeviceType
,
typename
T
>
class
Vol2ColFunctor
{
public:
void
operator
()(
const
Tensor
&
vol
,
const
std
::
vector
<
int
>
&
dilations
,
const
std
::
vector
<
int
>
&
strides
,
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
col
)
const
;
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
col
)
const
;
};
template
<
typename
DeviceType
,
typename
T
>
class
Col2VolFunctor
{
public:
void
operator
()(
const
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilations
,
template
<
typename
DeviceType
,
typename
T
>
class
Col2VolFunctor
{
public:
void
operator
()(
const
Tensor
&
col
,
const
std
::
vector
<
int
>
&
dilations
,
const
std
::
vector
<
int
>
&
strides
,
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
vol
)
const
;
const
std
::
vector
<
int
>
&
paddings
,
Tensor
*
vol
)
const
;
};
}
// namespace math
...
...
src/operators/mul_op.cpp
浏览文件 @
47e5978f
...
...
@@ -21,8 +21,7 @@ SOFTWARE.
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
Dtype
,
typename
T
>
void
MulOp
<
Dtype
,
T
>::
InferShape
()
const
{
template
<
typename
Dtype
,
typename
T
>
void
MulOp
<
Dtype
,
T
>::
InferShape
()
const
{
auto
x_dims
=
param_
.
InputX
()
->
dims
();
auto
y_dims
=
param_
.
InputY
()
->
dims
();
int
x_num_col_dims
=
param_
.
XNumColDims
();
...
...
@@ -38,8 +37,8 @@ void MulOp<Dtype, T>::InferShape() const {
assert
(
x_mat_dims
[
1
]
==
y_mat_dims
[
0
]);
std
::
vector
<
int64_t
>
output_dims
;
output_dims
.
reserve
(
static_cast
<
size_t
>
(
x_num_col_dims
+
y_dims
.
size
()
-
y_num_col_dims
));
output_dims
.
reserve
(
static_cast
<
size_t
>
(
x_num_col_dims
+
y_dims
.
size
()
-
y_num_col_dims
));
for
(
int
i
=
0
;
i
<
x_num_col_dims
;
++
i
)
{
output_dims
.
push_back
(
x_dims
[
i
]);
...
...
@@ -53,5 +52,5 @@ void MulOp<Dtype, T>::InferShape() const {
param_
.
Out
()
->
Resize
(
ddim
);
}
template
class
MulOp
<
CPU
,
float
>;
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/mul_op.h
浏览文件 @
47e5978f
...
...
@@ -25,15 +25,14 @@ namespace operators {
using
namespace
framework
;
template
<
typename
DeviceType
,
typename
T
>
template
<
typename
DeviceType
,
typename
T
>
class
MulOp
:
public
framework
::
OperatorWithKernel
<
DeviceType
>
{
public:
public:
MulOp
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
attrs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
attrs
,
std
::
shared_ptr
<
framework
::
Scope
>
scope
)
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
param_
(
inputs
,
outputs
,
attrs
,
*
scope
)
{}
void
Run
()
const
{
...
...
@@ -44,9 +43,9 @@ public:
using
framework
::
OperatorWithKernel
<
DeviceType
>::
OperatorWithKernel
;
void
InferShape
()
const
override
;
protected:
protected:
MulParam
param_
;
};
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
_mobile
src/operators/op_param.cpp
浏览文件 @
47e5978f
...
...
@@ -24,16 +24,16 @@ Print &operator<<(Print &printer, const ConvParam &conv_param) {
printer
<<
"parameter of conv: "
<<
"
\n
"
;
printer
<<
" stride: "
<<
" ("
<<
conv_param
.
Strides
()[
0
]
<<
conv_param
.
Strides
()[
1
]
<<
") "
<<
" ("
<<
conv_param
.
Strides
()[
0
]
<<
conv_param
.
Strides
()[
1
]
<<
") "
<<
"
\n
"
;
printer
<<
" paddings: "
<<
" ("
<<
conv_param
.
Paddings
()[
0
]
<<
conv_param
.
Paddings
()[
1
]
<<
") "
<<
" ("
<<
conv_param
.
Paddings
()[
0
]
<<
conv_param
.
Paddings
()[
1
]
<<
") "
<<
"
\n
"
;
printer
<<
" dilations: "
<<
" ("
<<
conv_param
.
Dilations
()[
0
]
<<
conv_param
.
Dilations
()[
1
]
<<
") "
<<
" ("
<<
conv_param
.
Dilations
()[
0
]
<<
conv_param
.
Dilations
()[
1
]
<<
") "
<<
"
\n
"
;
printer
<<
" groups: "
<<
conv_param
.
Groups
()
<<
"
\n
"
;
printer
<<
" input dims: "
<<
conv_param
.
Input
()
->
dims
()
<<
"
\n
"
;
...
...
src/operators/op_param.h
浏览文件 @
47e5978f
...
...
@@ -31,58 +31,51 @@ namespace operators {
using
namespace
framework
;
class
OpParam
:
PaddleMobileObject
{
public:
protected:
template
<
typename
T
>
static
T
*
InputFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
public:
protected:
template
<
typename
T
>
static
T
*
InputFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"Input"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
T
*
InputXFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
template
<
typename
T
>
static
T
*
InputXFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"X"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
T
*
InputYFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
template
<
typename
T
>
static
T
*
InputYFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"Y"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
std
::
vector
<
T
*>
InputMultiFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
template
<
typename
T
>
static
std
::
vector
<
T
*>
InputMultiFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetMultiVarValue
<
T
>
(
"Input"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
T
*
OutputFrom
(
const
VariableNameMap
&
outputs
,
const
Scope
&
scope
)
{
template
<
typename
T
>
static
T
*
OutputFrom
(
const
VariableNameMap
&
outputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"Output"
,
outputs
,
scope
);
}
template
<
typename
T
>
static
T
*
OutFrom
(
const
VariableNameMap
&
outputs
,
const
Scope
&
scope
)
{
template
<
typename
T
>
static
T
*
OutFrom
(
const
VariableNameMap
&
outputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"Out"
,
outputs
,
scope
);
}
template
<
typename
T
>
static
T
*
FilterFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
template
<
typename
T
>
static
T
*
FilterFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"Filter"
,
inputs
,
scope
);
}
template
<
typename
T
>
template
<
typename
T
>
static
const
T
GetAttr
(
std
::
string
key
,
const
AttributeMap
&
map
)
{
return
((
Attribute
)
map
.
at
(
key
)).
Get
<
T
>
();
return
((
Attribute
)
map
.
at
(
key
)).
Get
<
T
>
();
}
template
<
typename
T
>
static
T
*
GetVarValue
(
std
::
string
key
,
const
VariableNameMap
&
var_map
,
template
<
typename
T
>
static
T
*
GetVarValue
(
std
::
string
key
,
const
VariableNameMap
&
var_map
,
const
Scope
&
scope
)
{
auto
var_vec
=
var_map
.
at
(
key
);
if
(
var_vec
.
size
())
{
...
...
@@ -95,9 +88,9 @@ protected:
}
}
template
<
typename
T
>
static
std
::
vector
<
T
*>
GetMultiVarValue
(
std
::
string
key
,
const
VariableNameMap
&
var_map
,
template
<
typename
T
>
static
std
::
vector
<
T
*>
GetMultiVarValue
(
std
::
string
key
,
const
VariableNameMap
&
var_map
,
const
Scope
&
scope
)
{
auto
var_vecs
=
var_map
.
at
(
key
);
assert
(
var_vecs
.
size
()
>
1
);
...
...
@@ -111,9 +104,8 @@ protected:
};
class
ConvParam
:
OpParam
{
public:
ConvParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
public:
ConvParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
const
framework
::
Scope
&
scope
)
{
filter_
=
FilterFrom
<
framework
::
LoDTensor
>
(
inputs
,
scope
);
...
...
@@ -139,7 +131,7 @@ public:
const
int
&
Groups
()
const
{
return
groups
;
}
private:
private:
Tensor
*
input_
;
Tensor
*
output_
;
LoDTensor
*
filter_
;
...
...
@@ -152,7 +144,7 @@ private:
Print
&
operator
<<
(
Print
&
printer
,
const
ConvParam
&
conv_param
);
class
ElementwiseAddParam
:
OpParam
{
public:
public:
ElementwiseAddParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
...
...
@@ -171,7 +163,7 @@ public:
const
int
&
Axis
()
const
{
return
axis_
;
}
private:
private:
Tensor
*
input_x_
;
Tensor
*
input_y_
;
Tensor
*
out_
;
...
...
@@ -179,9 +171,8 @@ private:
};
class
MulParam
:
OpParam
{
public:
MulParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
public:
MulParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
const
framework
::
Scope
&
scope
)
{
input_x_
=
InputXFrom
<
framework
::
Tensor
>
(
inputs
,
scope
);
...
...
@@ -201,7 +192,7 @@ public:
const
int
&
YNumColDims
()
const
{
return
y_num_col_dims_
;
}
private:
private:
Tensor
*
input_x_
;
Tensor
*
input_y_
;
Tensor
*
out_
;
...
...
@@ -210,9 +201,8 @@ private:
};
class
ConcatParam
:
public
OpParam
{
public:
ConcatParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
public:
ConcatParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
const
framework
::
Scope
&
scope
)
{
inputs_
=
InputMultiFrom
<
framework
::
Tensor
>
(
inputs
,
scope
);
...
...
@@ -226,7 +216,7 @@ public:
const
int
&
Axis
()
const
{
return
axis_
;
}
private:
private:
std
::
vector
<
Tensor
*>
inputs_
;
Tensor
*
out_
;
int
axis_
;
...
...
test/common/test_log.cpp
浏览文件 @
47e5978f
...
...
@@ -22,9 +22,10 @@ int main() {
DLOGF
(
"DASJFDAFJ%d -- %f"
,
12345
,
344.234
);
LOGF
(
paddle_mobile
::
kLOG_DEBUG
,
"DASJFDAFJ%d -- %f"
,
12345
,
344.234
);
LOGF
(
paddle_mobile
::
kLOG_DEBUG
,
"DASJFDAFJ%d -- %f"
,
12345
,
344.234
);
LOG
(
paddle_mobile
::
kLOG_DEBUG
)
<<
"test debug"
<<
" next log"
;
LOG
(
paddle_mobile
::
kLOG_DEBUG
)
<<
"test debug"
<<
" next log"
;
LOG
(
paddle_mobile
::
kLOG_DEBUG1
)
<<
"test debug1"
<<
" next log"
;
...
...
test/elementwise_add_op_test.h
浏览文件 @
47e5978f
...
...
@@ -21,9 +21,9 @@ SOFTWARE.
#include "test_include.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
>
class
TestElementwiseAddOp
{
template
<
typename
Dtype
>
class
TestElementwiseAddOp
{
public:
TestElementwiseAddOp
(
const
Program
<
Dtype
>
p
)
:
program_
(
p
)
{
if
(
use_optimize_
)
{
...
...
@@ -37,8 +37,7 @@ namespace paddle_mobile {
// DLOG << " **block size " << blocks.size();
for
(
int
i
=
0
;
i
<
blocks
.
size
();
++
i
)
{
std
::
shared_ptr
<
BlockDesc
>
block_desc
=
blocks
[
i
];
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
// DLOG << " ops " << ops.size();
for
(
int
j
=
0
;
j
<
ops
.
size
();
++
j
)
{
std
::
shared_ptr
<
OpDesc
>
op
=
ops
[
j
];
...
...
@@ -59,8 +58,7 @@ namespace paddle_mobile {
DLOG
<<
" elementwise_add attr size: "
<<
op
->
GetAttrMap
().
size
();
DLOG
<<
" inputs size: "
<<
op
->
GetInputs
().
size
();
DLOG
<<
" outputs size: "
<<
op
->
GetOutputs
().
size
();
DLOG
<<
" outputs size: "
<<
op
->
GetOutputs
().
size
();
DLOG
<<
" Input X is : "
<<
op
->
Input
(
"X"
)[
0
];
DLOG
<<
" Input Y is : "
<<
op
->
Input
(
"Y"
)[
0
];
DLOG
<<
" Output Out is : "
<<
op
->
Output
(
"Out"
)[
0
];
...
...
@@ -68,13 +66,11 @@ namespace paddle_mobile {
int
axis
=
axis_attr
.
Get
<
int
>
();
DLOG
<<
" Attr axis is : "
<<
axis
;
std
::
shared_ptr
<
operators
::
ElementwiseAddOp
<
Dtype
,
float
>>
std
::
shared_ptr
<
operators
::
ElementwiseAddOp
<
Dtype
,
float
>>
add
=
std
::
make_shared
<
operators
::
ElementwiseAddOp
<
Dtype
,
float
>>
(
op
->
Type
(),
op
->
GetInputs
(),
op
->
GetOutputs
(),
op
->
GetAttrMap
(),
program_
.
scope
);
op
->
Type
(),
op
->
GetInputs
(),
op
->
GetOutputs
(),
op
->
GetAttrMap
(),
program_
.
scope
);
ops_of_block_
[
*
block_desc
.
get
()].
push_back
(
add
);
}
}
...
...
@@ -98,8 +94,7 @@ namespace paddle_mobile {
// DLOG << typeid(output_tensor).name();
// DLOG << "output_tensor dims: " << output_tensor->dims();
std
::
shared_ptr
<
Tensor
>
out_tensor
=
std
::
make_shared
<
LoDTensor
>
();
std
::
shared_ptr
<
Tensor
>
out_tensor
=
std
::
make_shared
<
LoDTensor
>
();
out_tensor
.
reset
(
output_tensor
);
predict_add
(
t1
,
t2
,
0
);
...
...
@@ -117,25 +112,25 @@ namespace paddle_mobile {
void
predict_add
(
const
Tensor
&
t1
,
const
Tensor
&
t2
,
int
block_id
)
{
std
::
shared_ptr
<
BlockDesc
>
to_predict_block
=
to_predict_program_
->
Block
(
block_id
);
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
();
++
j
)
{
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
()
;
++
j
)
{
auto
op
=
ops_of_block_
[
*
to_predict_block
.
get
()][
j
];
DLOG
<<
"op -> run()"
;
op
->
Run
();
}
}
};
};
template
class
TestElementwiseAddOp
<
CPU
>;
}
// namespace framework
template
class
TestElementwiseAddOp
<
CPU
>;
}
// namespace framework
namespace
test
{
void
testElementwiseAdd
()
{
namespace
test
{
void
testElementwiseAdd
()
{
DLOG
<<
"----------**********----------"
;
DLOG
<<
"begin to run ElementAddOp Test"
;
paddle_mobile
::
Loader
<
paddle_mobile
::
CPU
>
loader
;
auto
program
=
loader
.
Load
(
std
::
string
(
"../../test/models/"
auto
program
=
loader
.
Load
(
std
::
string
(
"../../test/models/"
"image_classification_resnet.inference.model"
));
/// input x (1,3,224,224)
...
...
@@ -164,6 +159,6 @@ namespace paddle_mobile {
DLOG
<<
inputx_ptr
[
226
]
<<
" + "
<<
inputy_ptr
[
2
]
<<
" = "
<<
output_add_ptr
[
226
];
}
}
// namespace test
}
}
// namespace test
}
// namespace paddle_mobile
test/mul_op_test.h
浏览文件 @
47e5978f
...
...
@@ -21,9 +21,9 @@ SOFTWARE.
#include "test_include.h"
namespace
paddle_mobile
{
namespace
framework
{
namespace
framework
{
template
<
typename
Dtype
>
class
TestMulOp
{
template
<
typename
Dtype
>
class
TestMulOp
{
public:
TestMulOp
(
const
Program
<
Dtype
>
p
)
:
program_
(
p
)
{
if
(
use_optimize_
)
{
...
...
@@ -37,8 +37,7 @@ namespace paddle_mobile {
// DLOG << " **block size " << blocks.size();
for
(
int
i
=
0
;
i
<
blocks
.
size
();
++
i
)
{
std
::
shared_ptr
<
BlockDesc
>
block_desc
=
blocks
[
i
];
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
// DLOG << " ops " << ops.size();
for
(
int
j
=
0
;
j
<
ops
.
size
();
++
j
)
{
std
::
shared_ptr
<
OpDesc
>
op
=
ops
[
j
];
...
...
@@ -59,29 +58,21 @@ namespace paddle_mobile {
// DLOG << "op:" << op->Type();
if
(
op
->
Type
()
==
"mul"
&&
op
->
Input
(
"X"
)[
0
]
==
"pool2d_0.tmp_0"
)
{
DLOG
<<
" mul attr size: "
<<
op
->
GetAttrMap
().
size
();
DLOG
<<
" mul attr size: "
<<
op
->
GetAttrMap
().
size
();
DLOG
<<
" inputs size: "
<<
op
->
GetInputs
().
size
();
DLOG
<<
" outputs size: "
<<
op
->
GetOutputs
().
size
();
DLOG
<<
" outputs size: "
<<
op
->
GetOutputs
().
size
();
DLOG
<<
" Input X is : "
<<
op
->
Input
(
"X"
)[
0
];
DLOG
<<
" Input Y is : "
<<
op
->
Input
(
"Y"
)[
0
];
DLOG
<<
" Output Out is : "
<<
op
->
Output
(
"Out"
)[
0
];
DLOG
<<
"x_num_col_dims : "
<<
op
->
GetAttrMap
()
.
at
(
"x_num_col_dims"
)
.
Get
<
int
>
();
<<
op
->
GetAttrMap
().
at
(
"x_num_col_dims"
).
Get
<
int
>
();
DLOG
<<
"y_num_col_dims : "
<<
op
->
GetAttrMap
()
.
at
(
"y_num_col_dims"
)
.
Get
<
int
>
();
std
::
shared_ptr
<
operators
::
MulOp
<
Dtype
,
float
>>
add
=
std
::
make_shared
<
operators
::
MulOp
<
Dtype
,
float
>>
(
op
->
Type
(),
op
->
GetInputs
(),
op
->
GetOutputs
(),
op
->
GetAttrMap
(),
program_
.
scope
);
<<
op
->
GetAttrMap
().
at
(
"y_num_col_dims"
).
Get
<
int
>
();
std
::
shared_ptr
<
operators
::
MulOp
<
Dtype
,
float
>>
add
=
std
::
make_shared
<
operators
::
MulOp
<
Dtype
,
float
>>
(
op
->
Type
(),
op
->
GetInputs
(),
op
->
GetOutputs
(),
op
->
GetAttrMap
(),
program_
.
scope
);
ops_of_block_
[
*
block_desc
.
get
()].
push_back
(
add
);
}
}
...
...
@@ -105,8 +96,7 @@ namespace paddle_mobile {
// DLOG << typeid(output_tensor).name();
// DLOG << "output_tensor dims: " << output_tensor->dims();
std
::
shared_ptr
<
Tensor
>
out_tensor
=
std
::
make_shared
<
LoDTensor
>
();
std
::
shared_ptr
<
Tensor
>
out_tensor
=
std
::
make_shared
<
LoDTensor
>
();
out_tensor
.
reset
(
output_tensor
);
predict_add
(
t1
,
t2
,
0
);
...
...
@@ -124,25 +114,25 @@ namespace paddle_mobile {
void
predict_add
(
const
Tensor
&
t1
,
const
Tensor
&
t2
,
int
block_id
)
{
std
::
shared_ptr
<
BlockDesc
>
to_predict_block
=
to_predict_program_
->
Block
(
block_id
);
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
();
++
j
)
{
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
()
;
++
j
)
{
auto
op
=
ops_of_block_
[
*
to_predict_block
.
get
()][
j
];
DLOG
<<
"op -> run()"
;
op
->
Run
();
}
}
};
};
template
class
TestMulOp
<
CPU
>;
}
// namespace framework
template
class
TestMulOp
<
CPU
>;
}
// namespace framework
namespace
test
{
void
testMul
()
{
namespace
test
{
void
testMul
()
{
DLOG
<<
"----------**********----------"
;
DLOG
<<
"begin to run MulOp Test"
;
paddle_mobile
::
Loader
<
paddle_mobile
::
CPU
>
loader
;
auto
program
=
loader
.
Load
(
std
::
string
(
"../../test/models/"
auto
program
=
loader
.
Load
(
std
::
string
(
"../../test/models/"
"image_classification_resnet.inference.model"
));
/// input x (3,2,1,1)
...
...
@@ -157,8 +147,7 @@ namespace paddle_mobile {
static_cast
<
float
>
(
1
));
float
*
inputy_ptr
=
inputy
.
data
<
float
>
();
paddle_mobile
::
framework
::
TestMulOp
<
paddle_mobile
::
CPU
>
testMulOp
(
program
);
paddle_mobile
::
framework
::
TestMulOp
<
paddle_mobile
::
CPU
>
testMulOp
(
program
);
auto
output_mul
=
testMulOp
.
predict_add
(
inputx
,
inputy
);
float
*
output_mul_ptr
=
output_mul
->
data
<
float
>
();
...
...
@@ -194,9 +183,8 @@ namespace paddle_mobile {
DLOG
<<
"output memory size : "
<<
output_mul
->
memory_size
();
DLOG
<<
"output numel : "
<<
output_mul
->
numel
();
DLOG
<<
inputx_ptr
[
0
]
<<
" x "
<<
inputy_ptr
[
0
]
<<
" + "
<<
inputx_ptr
[
1
]
<<
" x "
<<
inputy_ptr
[
0
+
3
]
<<
" = "
<<
output_mul_ptr
[
0
];
}
}
// namespace test
DLOG
<<
inputx_ptr
[
0
]
<<
" x "
<<
inputy_ptr
[
0
]
<<
" + "
<<
inputx_ptr
[
1
]
<<
" x "
<<
inputy_ptr
[
0
+
3
]
<<
" = "
<<
output_mul_ptr
[
0
];
}
}
// namespace test
}
// namespace paddle_mobile
test/test_include.h
浏览文件 @
47e5978f
...
...
@@ -7,7 +7,6 @@
#include "framework/scope.h"
#include "framework/tensor.h"
#include "framework/variable.h"
#include "framework/variable.h"
#include "io.h"
#include "test_helper.h"
#include <map>
...
...
tools/pre-commit.hooks/.clang-format.hook
0 → 100644
浏览文件 @
47e5978f
#!/bin/bash
#set -e
#
#readonly VERSION="3.8"
#
#version=$(clang-format -version)
#
#if ! [[ $version == *"$VERSION"* ]]; then
# echo "clang-format version check failed."
# echo "a version contains '$VERSION' is needed, but get '$version'"
# echo "you can install the right version, and make an soft-link to '\$PATH' env"
# exit -1
#fi
clang-format
$@
tools/pre-commit.hooks/.clang-tidy.hook
浏览文件 @
47e5978f
#!/bin/bash
echo
"allonli clang-tidy formating init"
cmake
-DCMAKE_EXPORT_COMPILE_COMMANDS
=
ON
clang-foramt
-version
clang-tidy
-version
python ./tools/pre-commit.hooks/run-clang-tidy.py
python ./tools/pre-commit.hooks/run-clang-tidy.py
-header-filter
=
third-party/
#TOTAL_ERRORS=0
# The trick to remove deleted files: https://stackoverflow.com/a/2413151
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录