Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
75c3b905
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看板
提交
75c3b905
编写于
8月 16, 2018
作者:
H
hanbuhe
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added fpga softmax kernel
上级
48ea7282
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
123 addition
and
40 deletion
+123
-40
src/fpga/api/fpga_api.cpp
src/fpga/api/fpga_api.cpp
+8
-0
src/fpga/fpga_quantilization.cpp
src/fpga/fpga_quantilization.cpp
+25
-29
src/fpga/fpga_quantilization.h
src/fpga/fpga_quantilization.h
+2
-3
src/framework/tensor.h
src/framework/tensor.h
+17
-8
src/operators/kernel/fpga/softmax_kernel.cpp
src/operators/kernel/fpga/softmax_kernel.cpp
+54
-0
src/operators/op_param.h
src/operators/op_param.h
+15
-0
src/operators/softmax_op.cpp
src/operators/softmax_op.cpp
+1
-0
src/operators/softmax_op.h
src/operators/softmax_op.h
+1
-0
未找到文件。
src/fpga/api/fpga_api.cpp
浏览文件 @
75c3b905
...
...
@@ -36,7 +36,11 @@ static int fd = -1;
static
const
char
*
device_path
=
"/dev/fpgadrv0"
;
static
inline
int
do_ioctl
(
int
req
,
const
void
*
arg
)
{
#ifdef PADDLE_MOBILE_OS_LINUX
return
ioctl
(
req
,
(
unsigned
int64_t
)
arg
);
#else
return
-
1
;
#endif
}
int
open_device
()
{
...
...
@@ -48,8 +52,12 @@ int open_device() {
// memory management;
void
*
fpga_malloc
(
size_t
size
)
{
#ifdef PADDLE_MOBILE_OS_LINUX
return
reinterpret_cast
<
void
*>
(
mmap64
(
NULL
,
size
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
));
#else
return
NULL
;
#endif
}
void
fpga_free
(
void
*
ptr
)
{
munmap
(
ptr
,
0
);
}
...
...
src/fpga/fpga_quantilization.cpp
浏览文件 @
75c3b905
...
...
@@ -19,15 +19,13 @@ namespace paddle_mobile {
namespace
fpga
{
template
<
typename
Dtype
>
static
void
chw_to_hwc
(
Dtype
*
data_in
,
Dtype
*
data_out
,
int
num
,
int
channel
,
int
height
,
int
width
)
{
int
offset_height
=
0
;
static
void
chw_to_hwc
(
Dtype
*
data_in
,
Dtype
*
data_out
,
int64_t
num
,
int64_t
channel
,
int64_t
height
,
int64_t
width
)
{
for
(
int
n
=
0
;
n
<
num
;
n
++
)
{
int
amount_per_row
=
width
*
channel
;
int
64_t
amount_per_row
=
width
*
channel
;
for
(
int
c
=
0
;
c
<
channel
;
c
++
)
{
for
(
int
h
=
0
;
h
<
height
;
h
++
)
{
int
offset_height
=
h
*
amount_per_row
;
int
64_t
offset_height
=
h
*
amount_per_row
;
for
(
int
w
=
0
;
w
<
width
;
w
++
)
{
*
(
data_out
+
offset_height
+
w
*
channel
+
c
)
=
*
(
data_in
++
);
}
...
...
@@ -38,10 +36,12 @@ static void chw_to_hwc(Dtype* data_in, Dtype* data_out, int num, int channel,
}
template
<
typename
Dtype
>
static
Dtype
find_max
(
Dtype
*
data
,
int
num
)
{
static
Dtype
find_max
(
Dtype
*
data
,
int
64_t
num
)
{
Dtype
max
=
0
;
for
(
int
i
=
0
;
i
<
num
;
++
i
)
{
max
=
std
::
max
(
max
,
data
[
i
]);
Dtype
value
=
data
[
i
];
Dtype
abs
=
value
>
0
?
value
:
-
value
;
max
=
std
::
max
(
max
,
abs
);
}
return
max
;
}
...
...
@@ -51,40 +51,36 @@ void quantify_filter(framework::Tensor* filter) {
DLOG
<<
"quantilize_filter........"
;
float
scale
=
0
;
float
fix_range
=
static_cast
<
float
>
((
1
<<
(
8
-
1
)
)
-
1
);
auto
fix_range
=
static_cast
<
float
>
(
std
::
pow
(
2
,
8
-
1
)
-
1
);
const
int
batch_size
=
filter
->
dims
()[
0
];
const
int
channel
=
filter
->
dims
()[
1
];
const
int
height
=
filter
->
dims
()[
2
];
const
int
width
=
filter
->
dims
()[
3
];
const
auto
batch_size
=
filter
->
dims
()[
0
];
const
auto
channel
=
filter
->
dims
()[
1
];
const
auto
height
=
filter
->
dims
()[
2
];
const
auto
width
=
filter
->
dims
()[
3
];
int8_t
*
int_data
=
nullptr
;
int8_t
*
tmp_data
=
new
int8_t
[
filter
->
numel
()];
auto
*
tmp_data
=
new
int8_t
[
filter
->
numel
()];
// 32bit filter -> 8bit filter;
if
(
filter
->
type
()
==
typeid
(
float
))
{
float
*
float_data
=
filter
->
data
<
float
>
();
float
max
=
find_max
<
float
>
(
float_data
,
filter
->
numel
());
auto
*
float_data
=
filter
->
data
<
float
>
();
auto
max
=
find_max
<
float
>
(
float_data
,
filter
->
numel
());
scale
=
(
max
/
fix_range
);
scale
=
(
fix_range
/
max
);
DLOG
<<
"scale:"
<<
scale
;
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
tmp_data
[
i
]
=
(
int8_t
)
float_data
[
i
]
*
scale
;
tmp_data
[
i
]
=
(
int8_t
)
(
float_data
[
i
]
*
scale
)
;
}
int_data
=
filter
->
mutable_data
<
int8_t
>
();
}
else
{
int8_t
max
=
find_max
<
int8_t
>
(
filter
->
data
<
int8_t
>
(),
filter
->
numel
());
scale
=
(
max
/
fix_range
);
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
tmp_data
[
i
]
=
filter
->
data
<
int8_t
>
()[
i
];
}
int_data
=
filter
->
mutable_data
<
int8_t
>
();
auto
max
=
find_max
<
int8_t
>
(
filter
->
data
<
int8_t
>
(),
filter
->
numel
());
scale
=
(
fix_range
/
max
);
std
::
memcpy
(
tmp_data
,
filter
->
data
<
int8_t
>
(),
(
size_t
)
filter
->
numel
());
}
// NCHW -> NHWC;
chw_to_hwc
<
int8_t
>
(
tmp_data
,
int_data
,
batch_size
,
channel
,
height
,
width
);
chw_to_hwc
<
int8_t
>
(
tmp_data
,
filter
->
mutable_data
<
int8_t
>
(),
batch_size
,
channel
,
height
,
width
);
delete
tmp_data
;
*
(
filter
->
fpga_args
().
scale_pointer
())
=
scale
;
filter
->
SetFpgaScale
(
scale
)
;
}
}
// namespace fpga
...
...
src/fpga/fpga_quantilization.h
浏览文件 @
75c3b905
...
...
@@ -21,10 +21,9 @@ namespace paddle_mobile {
namespace
fpga
{
template
<
typename
Dtype
>
static
void
chw_to_hwc
(
Dtype
*
data_in
,
Dtype
*
data_out
,
int
num
,
int
channel
,
int
height
,
in
t
width
);
static
void
chw_to_hwc
(
Dtype
*
data_in
,
Dtype
*
data_out
,
int
64_t
num
,
int
64_t
channel
,
int64_t
height
,
int64_
t
width
);
// template <typename Dtype>
void
quantify_filter
(
framework
::
Tensor
*
filter
);
}
// namespace fpga
...
...
src/framework/tensor.h
浏览文件 @
75c3b905
...
...
@@ -64,7 +64,8 @@ struct SizeOfTypeFunctor<HEAD, TAIL...> {
};
static
inline
size_t
SizeOfType
(
std
::
type_index
type
)
{
SizeOfTypeFunctor
<
int
,
half
,
float
,
double
,
int16_t
,
int64_t
,
bool
,
size_t
>
SizeOfTypeFunctor
<
int8_t
,
int
,
half
,
float
,
double
,
int16_t
,
int64_t
,
bool
,
size_t
>
functor
;
size_t
size
=
functor
(
type
);
...
...
@@ -255,14 +256,26 @@ class Tensor {
#ifdef PADDLE_MOBILE_FPGA
struct
FPGAArgs
{
f
loat
scale
;
f
riend
class
Tensor
;
inline
float
*
scale_pointer
()
{
return
&
scale
;
}
inline
float
*
scale_pointer
()
{
return
scale_
;
}
inline
float
scale
()
{
return
*
scale_
;
}
private:
float
*
scale_
;
};
struct
FPGAArgs
fpga_args
()
const
{
return
fpgaArgs_
;
FPGAArgs
args
;
args
.
scale_
=
scale
.
get
();
return
args
;
}
void
SetFpgaScale
(
float
s
)
{
*
(
scale
.
get
())
=
s
;
}
private:
std
::
shared_ptr
<
float
>
scale
=
std
::
make_shared
<
float
>
(
0
);
#endif
private:
...
...
@@ -331,10 +344,6 @@ class Tensor {
* begins.
*/
size_t
offset_
;
#ifdef PADDLE_MOBILE_FPGA
FPGAArgs
fpgaArgs_
;
#endif
};
#ifdef PADDLE_MOBILE_DEBUG
...
...
src/operators/kernel/fpga/softmax_kernel.cpp
0 → 100644
浏览文件 @
75c3b905
/* 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. */
#ifdef SOFTMAX_OP
#include "../softmax_kernel.h"
#include "../central-arm-func/softmax_arm_func.h"
#include "common/types.h"
#include "fpga/api/fpga_api.h"
#include "operators/math/softmax.h"
namespace
paddle_mobile
{
namespace
operators
{
template
<
>
bool
SoftmaxKernel
<
FPGA
,
float
>::
Init
(
SoftmaxParam
*
param
)
{
const
Tensor
*
input
=
param
->
InputX
();
if
(
input
->
type
()
==
typeid
(
half
))
{
auto
input_ptr
=
input
->
data
<
half
>
();
auto
output_ptr
=
param
->
Out
();
fpga
::
BypassArgs
args
;
args
.
convert_type
=
fpga
::
DATA_FP16_TO_FP32
;
args
.
layout_type
=
fpga
::
LAYOUT_HWC_TO_CHW
;
args
.
image
.
address
=
reinterpret_cast
<
void
*>
(
input_ptr
);
args
.
image
.
height
=
input
->
dims
()[
1
];
args
.
image
.
width
=
input
->
dims
()[
2
];
args
.
image
.
channels
=
input
->
dims
()[
3
];
args
.
output
.
address
=
output_ptr
;
param
->
SetFpgaArgs
(
args
);
}
return
true
;
}
template
<
>
void
SoftmaxKernel
<
FPGA
,
float
>::
Compute
(
const
SoftmaxParam
&
param
)
const
{
// SoftmaxCompute<float>(param);
}
template
class
SoftmaxKernel
<
FPGA
,
float
>;
}
// namespace operators
}
// namespace paddle_mobile
#endif
src/operators/op_param.h
浏览文件 @
75c3b905
...
...
@@ -580,6 +580,21 @@ class SoftmaxParam : public OpParam {
private:
Tensor
*
input_x_
;
Tensor
*
out_
;
#ifdef PADDLE_MOBILE_FPGA
private:
std
::
shared_ptr
<
Tensor
>
float_input_x_
;
fpga
::
BypassArgs
fpga_bypass_args
;
public:
Tensor
*
FloatInput
()
{
return
float_input_x_
==
nullptr
?
input_x_
:
float_input_x_
.
get
();
}
void
SetFloatInput
(
Tensor
*
input
)
{
float_input_x_
.
reset
(
input
);
}
const
fpga
::
BypassArgs
&
FpgaArgs
()
const
{
return
fpga_bypass_args
;
}
void
SetFpgaArgs
(
const
fpga
::
BypassArgs
&
args
)
{
fpga_bypass_args
=
args
;
}
#endif
};
#endif
...
...
src/operators/softmax_op.cpp
浏览文件 @
75c3b905
...
...
@@ -34,6 +34,7 @@ REGISTER_OPERATOR_CPU(softmax, ops::SoftmaxOp);
REGISTER_OPERATOR_MALI_GPU
(
softmax
,
ops
::
SoftmaxOp
);
#endif
#ifdef PADDLE_MOBILE_FPGA
REGISTER_OPERATOR_FPGA
(
softmax
,
ops
::
SoftmaxOp
);
#endif
#endif
src/operators/softmax_op.h
浏览文件 @
75c3b905
...
...
@@ -55,6 +55,7 @@ USE_OP_CPU(softmax);
USE_OP_MALI_GPU
(
softmax
);
#endif
#ifdef PADDLE_MOBILE_FPGA
USE_OP_FPGA
(
softmax
);
#endif
#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录