Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
e856d5b3
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
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看板
提交
e856d5b3
编写于
5月 26, 2018
作者:
E
eclipsess
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add relu op and test
上级
31e9411b
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
302 addition
and
20 deletion
+302
-20
src/io.cpp
src/io.cpp
+19
-20
src/operators/kernel/arm/relu_kernel.cpp
src/operators/kernel/arm/relu_kernel.cpp
+33
-0
src/operators/kernel/relu_kernel.h
src/operators/kernel/relu_kernel.h
+29
-0
src/operators/op_param.h
src/operators/op_param.h
+18
-0
src/operators/relu_op.cpp
src/operators/relu_op.cpp
+30
-0
src/operators/relu_op.h
src/operators/relu_op.h
+51
-0
test/CMakeLists.txt
test/CMakeLists.txt
+3
-0
test/operators/test_relu_op.cpp
test/operators/test_relu_op.cpp
+119
-0
未找到文件。
src/io.cpp
浏览文件 @
e856d5b3
...
@@ -144,26 +144,25 @@ const framework::Program<Dtype, P> Loader<Dtype, P>::Load(
...
@@ -144,26 +144,25 @@ const framework::Program<Dtype, P> Loader<Dtype, P>::Load(
std
::
make_shared
<
framework
::
Scope
>
();
std
::
make_shared
<
framework
::
Scope
>
();
program
.
scope
=
scope
;
program
.
scope
=
scope
;
// originProgramDesc->Block(0);
originProgramDesc
->
Block
(
0
);
// for (const auto &block : originProgramDesc->Blocks()) {
for
(
const
auto
&
block
:
originProgramDesc
->
Blocks
())
{
// for (int i = 0; i < block->Vars().size(); ++i) {
for
(
int
i
=
0
;
i
<
block
->
Vars
().
size
();
++
i
)
{
// std::shared_ptr<framework::VarDesc> var_desc = block->Vars()[i];
std
::
shared_ptr
<
framework
::
VarDesc
>
var_desc
=
block
->
Vars
()[
i
];
//// auto var = scope->Var(var_desc->Name());
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() &&
if
(
var_desc
->
Persistable
()
&&
// var_desc->GetType() != framework::proto::VarType::FEED_MINIBATCH
var_desc
->
GetType
()
!=
framework
::
proto
::
VarType
::
FEED_MINIBATCH
&&
// && var_desc->GetType() != framework::proto::VarType::FETCH_LIST)
var_desc
->
GetType
()
!=
framework
::
proto
::
VarType
::
FETCH_LIST
)
{
// {
auto
tensor
=
var
->
GetMutable
<
framework
::
LoDTensor
>
();
// // auto tensor = var->GetMutable<framework::LoDTensor>();
// to load
// // to load
LoadVar
(
tensor
,
dirname
+
"/"
+
var_desc
->
Name
());
// // LoadVar(tensor, dirname + "/" + var_desc->Name());
}
// }
}
else
{
// } else {
// TODO(codeWorm): some.
// // TODO(codeWorm): some.
}
// }
}
// }
}
// }
#ifdef PADDLE_MOBILE_DEBUG
#ifdef PADDLE_MOBILE_DEBUG
for
(
const
auto
&
block
:
program_desc_proto
.
blocks
())
{
for
(
const
auto
&
block
:
program_desc_proto
.
blocks
())
{
...
...
src/operators/kernel/arm/relu_kernel.cpp
0 → 100644
浏览文件 @
e856d5b3
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "operators/kernel/relu_kernel.h"
namespace
paddle_mobile
{
namespace
operators
{
template
<
>
void
ReluKernel
<
CPU
,
float
>::
Compute
(
const
ReluParam
&
param
)
const
{
const
auto
*
input_x
=
param
.
InputX
();
auto
*
input_x_ptr
=
input_x
->
data
<
float
>
();
auto
*
out
=
param
.
Out
();
auto
*
out_ptr
=
out
->
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
input_x
->
numel
();
i
++
)
{
out_ptr
[
i
]
=
input_x_ptr
[
i
]
>
0
?
input_x_ptr
[
i
]
:
0
;
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/relu_kernel.h
0 → 100644
浏览文件 @
e856d5b3
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "framework/operator.h"
#include "operators/op_param.h"
#pragma once;
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
DeviceType
,
typename
T
>
class
ReluKernel
:
public
framework
::
OpKernelBase
<
DeviceType
,
ReluParam
>
{
public:
void
Compute
(
const
ReluParam
&
param
)
const
;
};
}
// namespace operators
}
// namespace paddle_mobile
src/operators/op_param.h
浏览文件 @
e856d5b3
...
@@ -669,5 +669,23 @@ class ReshapeParam : public OpParam {
...
@@ -669,5 +669,23 @@ class ReshapeParam : public OpParam {
vector
<
int
>
shape_
;
vector
<
int
>
shape_
;
bool
inplace_
;
bool
inplace_
;
};
};
class
ReluParam
:
public
OpParam
{
public:
ReluParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
const
Scope
&
scope
)
{
input_x_
=
InputXFrom
<
Tensor
>
(
inputs
,
scope
);
out_
=
OutFrom
<
Tensor
>
(
outputs
,
scope
);
}
const
Tensor
*
InputX
()
const
{
return
input_x_
;
}
Tensor
*
Out
()
const
{
return
out_
;
}
private:
Tensor
*
input_x_
;
Tensor
*
out_
;
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle_mobile
}
// namespace paddle_mobile
src/operators/relu_op.cpp
0 → 100644
浏览文件 @
e856d5b3
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "operators/relu_op.h"
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
Dtype
,
typename
T
>
void
ReluOp
<
Dtype
,
T
>::
InferShape
()
const
{
auto
input_dims
=
param_
.
InputX
()
->
dims
();
param_
.
Out
()
->
Resize
(
input_dims
);
}
template
class
ReluOp
<
CPU
,
float
>;
}
// namespace operators
}
// namespace paddle_mobile
namespace
ops
=
paddle_mobile
::
operators
;
USE_OP
(
relu
);
REGISTER_OPERATOR
(
relu
,
ops
::
ReluOp
);
src/operators/relu_op.h
0 → 100644
浏览文件 @
e856d5b3
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <string>
#include "framework/operator.h"
#include "operators/kernel/relu_kernel.h"
#include "operators/op_param.h"
namespace
paddle_mobile
{
namespace
operators
{
using
paddle_mobile
::
framework
::
Tensor
;
template
<
typename
DeviceType
,
typename
T
>
class
ReluOp
:
public
framework
::
OperatorWithKernel
<
DeviceType
>
{
public:
ReluOp
(
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
),
param_
(
inputs
,
outputs
,
attrs
,
*
scope
)
{}
void
Run
()
const
{
operators
::
ReluKernel
<
DeviceType
,
T
>
kernel
;
kernel
.
Compute
(
param_
);
}
using
framework
::
OperatorWithKernel
<
DeviceType
>::
OperatorWithKernel
;
void
InferShape
()
const
override
;
protected:
ReluParam
param_
;
};
}
// namespace operators
}
// namespace paddle_mobile
test/CMakeLists.txt
浏览文件 @
e856d5b3
...
@@ -45,6 +45,9 @@ target_link_libraries(test-multiclassnms-op paddle-mobile)
...
@@ -45,6 +45,9 @@ target_link_libraries(test-multiclassnms-op paddle-mobile)
# gen test
# gen test
ADD_EXECUTABLE
(
test-reshape-op operators/test_reshape_op.cpp test_helper.h test_include.h
)
ADD_EXECUTABLE
(
test-reshape-op operators/test_reshape_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-reshape-op paddle-mobile
)
target_link_libraries
(
test-reshape-op paddle-mobile
)
# gen test
ADD_EXECUTABLE
(
test-relu-op operators/test_relu_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-relu-op paddle-mobile
)
# gen test log
# gen test log
ADD_EXECUTABLE
(
test-log common/test_log.cpp
)
ADD_EXECUTABLE
(
test-log common/test_log.cpp
)
...
...
test/operators/test_relu_op.cpp
0 → 100644
浏览文件 @
e856d5b3
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "../test_include.h"
#include "operators/relu_op.h"
namespace
paddle_mobile
{
namespace
framework
{
template
<
typename
Dtype
>
class
TestReluOp
{
public:
explicit
TestReluOp
(
const
Program
<
Dtype
>
p
)
:
program_
(
p
)
{
if
(
use_optimize_
)
{
to_predict_program_
=
program_
.
optimizeProgram
;
}
else
{
to_predict_program_
=
program_
.
originProgram
;
}
const
std
::
vector
<
std
::
shared_ptr
<
BlockDesc
>>
blocks
=
to_predict_program_
->
Blocks
();
// DLOG << " **block size " << blocks.size();
for
(
auto
block_desc
:
blocks
)
{
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
// DLOG << " ops " << ops.size();
for
(
auto
op
:
ops
)
{
if
(
op
->
Type
()
==
"relu"
&&
op
->
Input
(
"X"
)[
0
]
==
"batch_norm_34.tmp_2"
)
{
DLOG
<<
"in"
;
std
::
shared_ptr
<
operators
::
ReluOp
<
Dtype
,
float
>>
test_op
=
std
::
make_shared
<
operators
::
ReluOp
<
Dtype
,
float
>>
(
op
->
Type
(),
op
->
GetInputs
(),
op
->
GetOutputs
(),
op
->
GetAttrMap
(),
program_
.
scope
);
ops_of_block_
[
*
block_desc
.
get
()].
push_back
(
test_op
);
}
}
}
}
std
::
shared_ptr
<
Tensor
>
predict
(
const
Tensor
&
t1
)
{
// feed
auto
scope
=
program_
.
scope
;
Variable
*
x1_feed_value
=
scope
->
Var
(
"batch_norm_34.tmp_2"
);
auto
tensor_x1
=
x1_feed_value
->
GetMutable
<
Tensor
>
();
tensor_x1
->
ShareDataWith
(
t1
);
Variable
*
output
=
scope
->
Var
(
"batch_norm_34.tmp_3"
);
auto
*
output_tensor
=
output
->
GetMutable
<
Tensor
>
();
output_tensor
->
mutable_data
<
float
>
({
1
,
2
,
3
,
4
});
// DLOG << typeid(output_tensor).name();
// DLOG << "output_tensor dims: " << output_tensor->dims();
std
::
shared_ptr
<
Tensor
>
out_tensor
=
std
::
make_shared
<
LoDTensor
>
();
out_tensor
.
reset
(
output_tensor
);
predict
(
t1
,
0
);
return
out_tensor
;
// return outvars_tensor;
}
private:
const
framework
::
Program
<
Dtype
>
program_
;
std
::
shared_ptr
<
ProgramDesc
>
to_predict_program_
;
std
::
map
<
framework
::
BlockDesc
,
std
::
vector
<
std
::
shared_ptr
<
OperatorBase
<
Dtype
>>>>
ops_of_block_
;
bool
use_optimize_
=
false
;
void
predict
(
const
Tensor
&
t1
,
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
)
{
auto
op
=
ops_of_block_
[
*
to_predict_block
.
get
()][
j
];
DLOG
<<
"op -> run()"
;
op
->
Run
();
}
}
};
template
class
TestReluOp
<
CPU
>;
}
// namespace framework
}
// namespace paddle_mobile
int
main
()
{
DLOG
<<
"----------**********----------"
;
DLOG
<<
"begin to run Relu Test"
;
paddle_mobile
::
Loader
<
paddle_mobile
::
CPU
>
loader
;
auto
program
=
loader
.
Load
(
std
::
string
(
"../../test/models/mobilenet+ssd"
));
/// input x (1,3,300,300)
paddle_mobile
::
framework
::
Tensor
inputx1
;
SetupTensor
<
float
>
(
&
inputx1
,
{
1
,
2
,
3
,
4
},
static_cast
<
float
>
(
-
1
),
static_cast
<
float
>
(
1
));
auto
*
inputx1_ptr
=
inputx1
.
data
<
float
>
();
paddle_mobile
::
framework
::
TestReluOp
<
paddle_mobile
::
CPU
>
testReluOp
(
program
);
auto
output
=
testReluOp
.
predict
(
inputx1
);
auto
*
output_ptr
=
output
->
data
<
float
>
();
for
(
int
i
=
0
;
i
<
output
->
numel
();
i
++
)
{
DLOG
<<
output_ptr
[
i
];
}
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录