Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
eca4dcbb
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看板
未验证
提交
eca4dcbb
编写于
9月 14, 2020
作者:
C
cc
提交者:
GitHub
9月 14, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize obtaining quantized scale in inference stage, test=develop (#4308)
* Optimize obtaining quantized scale in inference, test=develop
上级
e45c4242
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
114 addition
and
71 deletion
+114
-71
lite/core/op_lite.cc
lite/core/op_lite.cc
+77
-47
lite/core/op_lite.h
lite/core/op_lite.h
+18
-6
lite/operators/conv_op.h
lite/operators/conv_op.h
+10
-9
lite/operators/fc_op.cc
lite/operators/fc_op.cc
+9
-9
未找到文件。
lite/core/op_lite.cc
浏览文件 @
eca4dcbb
...
...
@@ -233,67 +233,97 @@ bool OpInfo::GetOutputIndex(const std::string &output_name, int *out) const {
return
false
;
}
bool
OpInfo
::
HasInputScale
(
const
std
::
string
&
input_name
)
const
{
bool
OpInfo
::
HasInputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
)
const
{
bool
res
=
false
;
if
(
is_scale_name
)
{
res
=
HasAttr
(
name
);
}
else
{
std
::
string
argname
;
int
index
;
if
(
GetInputArgname
(
input_name
,
&
argname
)
&&
GetInputIndex
(
input_name
,
&
index
))
{
return
HasAttr
(
argname
+
to_string
(
index
)
+
"_scale"
);
}
else
{
return
false
;
if
(
GetInputArgname
(
name
,
&
argname
)
&&
GetInputIndex
(
name
,
&
index
))
{
res
=
HasAttr
(
argname
+
to_string
(
index
)
+
"_scale"
);
}
}
return
res
;
}
bool
OpInfo
::
HasOutputScale
(
const
std
::
string
&
output_name
)
const
{
bool
OpInfo
::
HasOutputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
)
const
{
bool
res
=
false
;
if
(
is_scale_name
)
{
res
=
HasAttr
(
name
);
}
else
{
std
::
string
argname
;
int
index
;
if
(
GetOutputArgname
(
output_name
,
&
argname
)
&&
GetOutputIndex
(
output_name
,
&
index
))
{
return
HasAttr
(
argname
+
to_string
(
index
)
+
"_scale"
);
}
else
{
return
false
;
if
(
GetOutputArgname
(
name
,
&
argname
)
&&
GetOutputIndex
(
name
,
&
index
))
{
res
=
HasAttr
(
argname
+
to_string
(
index
)
+
"_scale"
);
}
}
return
res
;
}
void
OpInfo
::
SetInputScale
(
const
std
::
string
&
input_name
,
const
std
::
vector
<
float
>
&
scale_value
)
{
void
OpInfo
::
SetInputScale
(
const
std
::
string
&
name
,
const
std
::
vector
<
float
>
&
scale_value
,
bool
is_scale_name
)
{
std
::
string
scale_name
;
if
(
is_scale_name
)
{
scale_name
=
name
;
}
else
{
std
::
string
argname
;
int
index
;
CHECK
(
GetInputArgname
(
input_
name
,
&
argname
));
CHECK
(
GetInputIndex
(
input_
name
,
&
index
));
CHECK
(
GetInputArgname
(
name
,
&
argname
));
CHECK
(
GetInputIndex
(
name
,
&
index
));
CHECK
(
scale_value
.
size
()
>
0
)
<<
"Error in SetInputScale: the scales should not be empty"
;
SetAttr
<
std
::
vector
<
float
>>
(
argname
+
to_string
(
index
)
+
"_scale"
,
scale_value
);
scale_name
=
argname
+
to_string
(
index
)
+
"_scale"
;
}
SetAttr
<
std
::
vector
<
float
>>
(
scale_name
,
scale_value
);
}
void
OpInfo
::
SetOutputScale
(
const
std
::
string
&
output_name
,
const
std
::
vector
<
float
>
&
scale_value
)
{
void
OpInfo
::
SetOutputScale
(
const
std
::
string
&
name
,
const
std
::
vector
<
float
>
&
scale_value
,
bool
is_scale_name
)
{
std
::
string
scale_name
;
if
(
is_scale_name
)
{
scale_name
=
name
;
}
else
{
std
::
string
argname
;
int
index
;
CHECK
(
GetOutputArgname
(
output_
name
,
&
argname
));
CHECK
(
GetOutputIndex
(
output_
name
,
&
index
));
CHECK
(
GetOutputArgname
(
name
,
&
argname
));
CHECK
(
GetOutputIndex
(
name
,
&
index
));
CHECK
(
scale_value
.
size
()
>
0
)
<<
"Error in SetOutputScale: the scales should not be empty"
;
SetAttr
<
std
::
vector
<
float
>>
(
argname
+
to_string
(
index
)
+
"_scale"
,
scale_value
);
scale_name
=
argname
+
to_string
(
index
)
+
"_scale"
;
}
SetAttr
<
std
::
vector
<
float
>>
(
scale_name
,
scale_value
);
}
std
::
vector
<
float
>
OpInfo
::
GetInputScale
(
const
std
::
string
&
input_name
)
const
{
std
::
vector
<
float
>
OpInfo
::
GetInputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
)
const
{
std
::
string
scale_name
;
if
(
is_scale_name
)
{
scale_name
=
name
;
}
else
{
std
::
string
argname
;
int
index
;
CHECK
(
GetInputArgname
(
input_name
,
&
argname
));
CHECK
(
GetInputIndex
(
input_name
,
&
index
));
return
GetAttr
<
std
::
vector
<
float
>>
(
argname
+
to_string
(
index
)
+
"_scale"
);
CHECK
(
GetInputArgname
(
name
,
&
argname
));
CHECK
(
GetInputIndex
(
name
,
&
index
));
scale_name
=
argname
+
to_string
(
index
)
+
"_scale"
;
}
return
GetAttr
<
std
::
vector
<
float
>>
(
scale_name
);
}
std
::
vector
<
float
>
OpInfo
::
GetOutputScale
(
const
std
::
string
&
output_name
)
const
{
std
::
vector
<
float
>
OpInfo
::
GetOutputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
)
const
{
std
::
string
scale_name
;
if
(
is_scale_name
)
{
scale_name
=
name
;
}
else
{
std
::
string
argname
;
int
index
;
CHECK
(
GetOutputArgname
(
output_name
,
&
argname
));
CHECK
(
GetOutputIndex
(
output_name
,
&
index
));
return
GetAttr
<
std
::
vector
<
float
>>
(
argname
+
to_string
(
index
)
+
"_scale"
);
CHECK
(
GetOutputArgname
(
name
,
&
argname
));
CHECK
(
GetOutputIndex
(
name
,
&
index
));
}
return
GetAttr
<
std
::
vector
<
float
>>
(
scale_name
);
}
}
// namespace lite
...
...
lite/core/op_lite.h
浏览文件 @
eca4dcbb
...
...
@@ -251,19 +251,31 @@ class OpInfo : public cpp::OpDesc {
bool
GetInputIndex
(
const
std
::
string
&
input_name
,
int
*
out
)
const
;
bool
GetOutputIndex
(
const
std
::
string
&
output_name
,
int
*
out
)
const
;
bool
HasInputScale
(
const
std
::
string
&
input_name
)
const
;
bool
HasOutputScale
(
const
std
::
string
&
output_name
)
const
;
// If a quantized op has two input argname (X, Y) and one output
// argname (Out). The scales of input argname X are saved in op desc as
// (X0_scale, scale_value_0), (X1_scale, scale_value_1)...
// The following APIs get or set the quantized scale in op_desc.
// If use the input or output name, the is_scale_name should be false.
// If use the scale_name such as (X0_scale, scale_value_0),
// the is_scale_name should be true.
bool
HasInputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
=
false
)
const
;
bool
HasOutputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
=
false
)
const
;
void
SetInputScale
(
const
std
::
string
&
input_name
,
const
std
::
vector
<
float
>
&
scale_value
);
const
std
::
vector
<
float
>
&
scale_value
,
bool
is_scale_name
=
false
);
void
SetOutputScale
(
const
std
::
string
&
output_name
,
const
std
::
vector
<
float
>
&
scale_value
);
const
std
::
vector
<
float
>
&
scale_value
,
bool
is_scale_name
=
false
);
// For conv2d, depthwise_conv2d and mul, the scale of weight are a vector.
// Otherwise, all input and output scales are scalar, but we save these
// as vecotr.
std
::
vector
<
float
>
GetInputScale
(
const
std
::
string
&
input_name
)
const
;
std
::
vector
<
float
>
GetOutputScale
(
const
std
::
string
&
output_name
)
const
;
std
::
vector
<
float
>
GetInputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
=
false
)
const
;
std
::
vector
<
float
>
GetOutputScale
(
const
std
::
string
&
name
,
bool
is_scale_name
=
false
)
const
;
};
}
// namespace lite
...
...
lite/operators/conv_op.h
浏览文件 @
eca4dcbb
...
...
@@ -133,15 +133,16 @@ class ConvOpLite : public OpLite {
const
OpInfo
*
op_info
=
dynamic_cast
<
const
OpInfo
*>
(
&
op_desc
);
if
(
op_info
!=
nullptr
&&
op_info
->
HasAttr
(
"enable_int8"
))
{
param_
.
enable_int8
=
op_info
->
GetAttr
<
bool
>
(
"enable_int8"
);
auto
input_name
=
op_info
->
Input
(
"Input"
).
front
();
auto
filter_name
=
op_info
->
Input
(
"Filter"
).
front
();
auto
output_name
=
op_info
->
Output
(
"Output"
).
front
();
if
(
op_info
->
HasInputScale
(
input_name
))
param_
.
input_scale
=
op_info
->
GetInputScale
(
input_name
)[
0
];
if
(
op_info
->
HasInputScale
(
filter_name
))
param_
.
weight_scale
=
op_info
->
GetInputScale
(
filter_name
);
if
(
op_info
->
HasOutputScale
(
output_name
))
{
param_
.
output_scale
=
op_info
->
GetOutputScale
(
output_name
)[
0
];
auto
input_scale_name
=
"Input0_scale"
;
auto
filter_scale_name
=
"Filter0_scale"
;
auto
output_scale_name
=
"Output0_scale"
;
if
(
op_info
->
HasInputScale
(
input_scale_name
,
true
))
param_
.
input_scale
=
op_info
->
GetInputScale
(
input_scale_name
,
true
)[
0
];
if
(
op_info
->
HasInputScale
(
filter_scale_name
,
true
))
param_
.
weight_scale
=
op_info
->
GetInputScale
(
filter_scale_name
,
true
);
if
(
op_info
->
HasOutputScale
(
output_scale_name
,
true
))
{
param_
.
output_scale
=
op_info
->
GetOutputScale
(
output_scale_name
,
true
)[
0
];
}
}
...
...
lite/operators/fc_op.cc
浏览文件 @
eca4dcbb
...
...
@@ -112,15 +112,15 @@ bool FcOpLite::AttachImpl(const cpp::OpDesc& op_desc, lite::Scope* scope) {
const
OpInfo
*
op_info
=
dynamic_cast
<
const
OpInfo
*>
(
&
op_desc
);
if
(
op_info
!=
nullptr
&&
op_info
->
HasAttr
(
"enable_int8"
))
{
param_
.
enable_int8
=
op_info
->
GetAttr
<
bool
>
(
"enable_int8"
);
auto
input_
name
=
op_info
->
Input
(
"Input"
).
front
()
;
auto
weight_
name
=
op_info
->
Input
(
"W"
).
front
()
;
auto
out_
name
=
op_info
->
Output
(
"Out"
).
front
()
;
if
(
op_info
->
HasInputScale
(
input_
nam
e
))
param_
.
input_scale
=
op_info
->
GetInputScale
(
input_
nam
e
)[
0
];
if
(
op_info
->
HasInputScale
(
weight_
nam
e
))
param_
.
weight_scale
=
op_info
->
GetInputScale
(
weight_
nam
e
);
if
(
op_info
->
HasOutputScale
(
out_
nam
e
))
param_
.
output_scale
=
op_info
->
GetOutputScale
(
out_
nam
e
)[
0
];
auto
input_
scale_name
=
"Input0_scale"
;
auto
weight_
scale_name
=
"W0_scale"
;
auto
out_
scale_name
=
"Out0_scale"
;
if
(
op_info
->
HasInputScale
(
input_
scale_name
,
tru
e
))
param_
.
input_scale
=
op_info
->
GetInputScale
(
input_
scale_name
,
tru
e
)[
0
];
if
(
op_info
->
HasInputScale
(
weight_
scale_name
,
tru
e
))
param_
.
weight_scale
=
op_info
->
GetInputScale
(
weight_
scale_name
,
tru
e
);
if
(
op_info
->
HasOutputScale
(
out_
scale_name
,
tru
e
))
param_
.
output_scale
=
op_info
->
GetOutputScale
(
out_
scale_name
,
tru
e
)[
0
];
}
return
true
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录