Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
9f0b5cf2
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看板
提交
9f0b5cf2
编写于
8月 09, 2018
作者:
H
hanbuhe
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change concat template
上级
0e128b67
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
106 addition
and
36 deletion
+106
-36
src/fpga/fpga_quantilization.cpp
src/fpga/fpga_quantilization.cpp
+83
-0
src/fpga/fpga_quantilization.h
src/fpga/fpga_quantilization.h
+6
-28
src/operators/kernel/fpga/concat_kernel.cpp
src/operators/kernel/fpga/concat_kernel.cpp
+2
-2
src/operators/kernel/fpga/conv_add_bn_kernel.cpp
src/operators/kernel/fpga/conv_add_bn_kernel.cpp
+15
-6
未找到文件。
src/fpga/fpga_quantilization.cpp
0 → 100644
浏览文件 @
9f0b5cf2
/* 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 "fpga/fpga_quantilization.h"
#include <algorithm>
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
;
for
(
int
n
=
0
;
n
<
num
;
n
++
)
{
int
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
;
for
(
int
w
=
0
;
w
<
width
;
w
++
)
{
*
(
data_out
+
offset_height
+
w
*
channel
+
c
)
=
*
(
data_in
++
);
}
}
}
data_out
+=
num
;
}
}
template
<
typename
Dtype
>
framework
::
Tensor
*
quantilize_filter
(
framework
::
Tensor
*
filter
)
{
float
scale
=
0
;
float
max
=
0
f
;
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
];
// 32bit filter -> 8bit filter;
if
(
filter
->
type
()
==
typeid
(
float
))
{
float
*
float_data
=
filter
->
data
<
float
>
();
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
max
=
std
::
max
(
max
,
float_data
[
i
]);
}
float
fix_range
=
static_cast
<
float
>
((
1
<<
(
8
-
1
))
-
1
);
float
float_range
=
max
;
scale
=
(
float_range
/
fix_range
);
framework
::
Tensor
*
filter
=
filter
;
framework
::
Tensor
*
quant_filter
=
new
framework
::
Tensor
();
int8_t
*
temp
=
new
int8_t
[
filter
->
numel
()];
int8_t
*
int_data
=
quant_filter
->
mutable_data
<
int8_t
>
();
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
temp
[
i
]
=
(
int8_t
)
float_data
[
i
]
*
scale
;
}
quant_filter
.
scale
=
scale
;
// NCHW -> NHWC;
chw_to_hwc
<
int8_t
>
(
temp
,
int_data
,
in_batch_size
,
channel
,
height
,
width
);
return
quantFilter
;
}
else
if
(
filter
->
type
()
==
typeid
(
int8_t
))
{
// model is already quantilized
int8_t
*
int_data
=
filter
->
data
<
int8_t
>
();
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
max
=
std
::
max
(
max
,
int_data
[
i
]);
}
}
return
filter
;
}
}
// namespace fpga
}
// namespace paddle_mobile
src/fpga/fpga_quantilization.h
浏览文件 @
9f0b5cf2
...
@@ -18,35 +18,13 @@ limitations under the License. */
...
@@ -18,35 +18,13 @@ limitations under the License. */
#include "framework/tensor.h"
#include "framework/tensor.h"
namespace
paddle_mobile
{
namespace
paddle_mobile
{
namespace
fpga
{
template
<
typename
Dtype
>
template
<
typename
Dtype
>
framework
::
Tensor
*
quantilize_filter
(
framework
::
Tensor
*
filter
)
{
static
void
chw_to_hwc
(
Dtype
*
data_in
,
Dtype
*
data_out
,
int
num
,
int
channel
,
float
scale
=
0
;
int
height
,
int
width
);
// 32bit filter -> 8bit filter;
float
min
=
0
f
;
float
max
=
0
f
;
if
(
filter
->
type
()
==
typeid
(
float
))
{
float
*
floatData
=
originalFilter
->
data
<
float
>
();
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
min
=
std
::
min
(
min
,
floatData
[
i
]);
max
=
std
::
max
(
max
,
floatData
[
i
]);
}
float
fix_range
=
(
float
)((
1
<<
(
8
-
1
))
-
1
);
float
float_range
=
max
;
scale
=
(
float_range
/
fix_range
);
framework
::
Tensor
*
originalFilter
=
filter
;
framework
::
Tensor
*
quantFilter
=
new
framework
::
Tensor
();
int8_t
*
intData
=
quantFilter
->
mutable_data
<
int8_t
>
();
for
(
int
i
=
0
;
i
<
filter
->
numel
();
++
i
)
{
intData
[
i
]
=
(
int8_t
)
floatData
[
i
]
*
scale
;
}
quantFilter
.
scale
=
scale
;
// NCHW -> NHWC;
return
quantFilter
;
}
return
filter
;
}
template
<
typename
Dtype
>
framework
::
Tensor
*
quantilize_filter
(
framework
::
Tensor
*
filter
);
}
// namespace fpga
}
// namespace paddle_mobile
}
// namespace paddle_mobile
src/operators/kernel/fpga/concat_kernel.cpp
浏览文件 @
9f0b5cf2
...
@@ -20,12 +20,12 @@ namespace paddle_mobile {
...
@@ -20,12 +20,12 @@ namespace paddle_mobile {
namespace
operators
{
namespace
operators
{
template
<
>
template
<
>
bool
ConcatKernel
<
FPGA
,
half
>::
Init
(
ConcatParam
*
param
)
{
bool
ConcatKernel
<
FPGA
,
float
>::
Init
(
ConcatParam
*
param
)
{
return
true
;
return
true
;
}
}
template
<
>
template
<
>
void
ConcatKernel
<
FPGA
,
half
>::
Compute
(
const
ConcatParam
&
param
)
const
{
void
ConcatKernel
<
FPGA
,
float
>::
Compute
(
const
ConcatParam
&
param
)
const
{
auto
inputs
=
param
.
Inputs
();
auto
inputs
=
param
.
Inputs
();
auto
*
out
=
param
.
Out
();
auto
*
out
=
param
.
Out
();
int64_t
axis
=
param
.
Axis
();
int64_t
axis
=
param
.
Axis
();
...
...
src/operators/kernel/fpga/conv_add_bn_kernel.cpp
浏览文件 @
9f0b5cf2
...
@@ -16,6 +16,7 @@ limitations under the License. */
...
@@ -16,6 +16,7 @@ limitations under the License. */
#include "operators/kernel/conv_add_bn_kernel.h"
#include "operators/kernel/conv_add_bn_kernel.h"
#include "fpga/api/fpga_api.h"
#include "fpga/api/fpga_api.h"
#include "fpga/quantilization.h"
namespace
paddle_mobile
{
namespace
paddle_mobile
{
namespace
operators
{
namespace
operators
{
...
@@ -28,7 +29,7 @@ bool ConvAddBNKernel<FPGA, float>::Init(FusionConvAddBNParam *param) {
...
@@ -28,7 +29,7 @@ bool ConvAddBNKernel<FPGA, float>::Init(FusionConvAddBNParam *param) {
const
Tensor
*
bias
=
param
->
Bias
();
const
Tensor
*
bias
=
param
->
Bias
();
auto
bias_ptr
=
bias
->
data
<
float
>
();
auto
bias_ptr
=
bias
->
data
<
float
>
();
const
Tensor
*
filter
=
param
->
Filter
();
const
Tensor
*
filter
=
param
->
Filter
();
auto
filter_ptr
=
filter
->
data
<
float
>
();
Tensor
*
out
=
param
->
Output
();
Tensor
*
out
=
param
->
Output
();
auto
out_ptr
=
out
->
mutable_data
<
half
>
();
auto
out_ptr
=
out
->
mutable_data
<
half
>
();
auto
bn_mean_ptr
=
param
->
InputMean
()
->
data
<
float
>
();
auto
bn_mean_ptr
=
param
->
InputMean
()
->
data
<
float
>
();
...
@@ -41,7 +42,8 @@ bool ConvAddBNKernel<FPGA, float>::Init(FusionConvAddBNParam *param) {
...
@@ -41,7 +42,8 @@ bool ConvAddBNKernel<FPGA, float>::Init(FusionConvAddBNParam *param) {
"Image channel should be equal to bias number"
);
"Image channel should be equal to bias number"
);
const
int
channel
=
input
->
dims
()[
1
];
const
int
channel
=
input
->
dims
()[
1
];
float
*
bs_ptr
=
(
float
*
)
fpga
::
fpga_malloc
(
2
*
channel
*
sizeof
(
float
));
float
*
bs_ptr
=
reinterpret_cast
<
float
*>
(
fpga
::
fpga_malloc
(
2
*
channel
*
sizeof
(
float
)));
Tensor
*
new_scale
=
new
Tensor
();
Tensor
*
new_scale
=
new
Tensor
();
Tensor
*
new_bias
=
new
Tensor
();
Tensor
*
new_bias
=
new
Tensor
();
auto
new_scale_ptr
=
new_scale
->
mutable_data
<
float
>
({
channel
});
auto
new_scale_ptr
=
new_scale
->
mutable_data
<
float
>
({
channel
});
...
@@ -58,26 +60,33 @@ bool ConvAddBNKernel<FPGA, float>::Init(FusionConvAddBNParam *param) {
...
@@ -58,26 +60,33 @@ bool ConvAddBNKernel<FPGA, float>::Init(FusionConvAddBNParam *param) {
param
->
SetNewScale
(
new_scale
);
param
->
SetNewScale
(
new_scale
);
param
->
SetNewBias
(
new_bias
);
param
->
SetNewBias
(
new_bias
);
const
Tensor
*
quant_filter
=
quantilize_filter
(
filter
);
// delete original filter?
filter
=
quant_filter
;
auto
filter_ptr
=
filter
->
data
<
float
>
();
fpga
::
ConvArgs
convArgs
;
fpga
::
ConvArgs
convArgs
;
convArgs
.
relu_enabled
=
relu_enabled
;
convArgs
.
relu_enabled
=
relu_enabled
;
convArgs
.
filter_address
=
(
void
*
)
filter_ptr
;
convArgs
.
filter_address
=
reinterpret_cast
<
void
*>
filter_ptr
;
convArgs
.
filter_num
=
filter
->
dims
()[
0
];
convArgs
.
filter_num
=
filter
->
dims
()[
0
];
convArgs
.
group_num
=
param
->
Groups
();
convArgs
.
group_num
=
param
->
Groups
();
convArgs
.
sb_address
=
(
void
*
)
bs_ptr
;
convArgs
.
sb_address
=
reinterpret_cast
<
void
*>
bs_ptr
;
convArgs
.
kernel
.
stride_h
=
param
->
Strides
()[
0
];
convArgs
.
kernel
.
stride_h
=
param
->
Strides
()[
0
];
convArgs
.
kernel
.
stride_w
=
param
->
Strides
()[
1
];
convArgs
.
kernel
.
stride_w
=
param
->
Strides
()[
1
];
convArgs
.
kernel
.
height
=
filter
->
dims
()[
2
];
convArgs
.
kernel
.
height
=
filter
->
dims
()[
2
];
convArgs
.
kernel
.
width
=
filter
->
dims
()[
3
];
convArgs
.
kernel
.
width
=
filter
->
dims
()[
3
];
convArgs
.
image
.
address
=
(
void
*
)
input_ptr
;
convArgs
.
image
.
address
=
reinterpret_cast
<
void
*>
input_ptr
;
convArgs
.
image
.
channels
=
input
->
dims
()[
1
];
convArgs
.
image
.
channels
=
input
->
dims
()[
1
];
convArgs
.
image
.
height
=
input
->
dims
()[
2
];
convArgs
.
image
.
height
=
input
->
dims
()[
2
];
convArgs
.
image
.
width
=
input
->
dims
()[
3
];
convArgs
.
image
.
width
=
input
->
dims
()[
3
];
convArgs
.
image
.
pad_height
=
param
->
Paddings
()[
0
];
convArgs
.
image
.
pad_height
=
param
->
Paddings
()[
0
];
convArgs
.
image
.
pad_width
=
param
->
Paddings
()[
1
];
convArgs
.
image
.
pad_width
=
param
->
Paddings
()[
1
];
convArgs
.
image
.
scale_address
=
input
->
fpga_args
().
scale_pointer
();
convArgs
.
image
.
scale_address
=
input
->
fpga_args
().
scale_pointer
();
convArgs
.
output
.
address
=
(
void
*
)
out_ptr
;
convArgs
.
output
.
address
=
reinterpret_cast
<
void
*>
out_ptr
;
convArgs
.
output
.
scale_address
=
out
->
fpga_args
().
scale_pointer
();
convArgs
.
output
.
scale_address
=
out
->
fpga_args
().
scale_pointer
();
param
->
SetFpgaArgs
(
convArgs
);
param
->
SetFpgaArgs
(
convArgs
);
return
true
;
return
true
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录