Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
806832e0
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
806832e0
编写于
3月 05, 2019
作者:
Z
Zhen Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the input format of channel wise dequantize op.
上级
89dee160
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
46 addition
and
61 deletion
+46
-61
paddle/fluid/operators/fake_dequantize_op.cc
paddle/fluid/operators/fake_dequantize_op.cc
+18
-24
paddle/fluid/operators/fake_dequantize_op.h
paddle/fluid/operators/fake_dequantize_op.h
+16
-22
python/paddle/fluid/tests/unittests/test_fake_dequantize_op.py
...n/paddle/fluid/tests/unittests/test_fake_dequantize_op.py
+12
-15
未找到文件。
paddle/fluid/operators/fake_dequantize_op.cc
浏览文件 @
806832e0
...
@@ -14,6 +14,7 @@ limitations under the License. */
...
@@ -14,6 +14,7 @@ limitations under the License. */
#include "paddle/fluid/operators/fake_dequantize_op.h"
#include "paddle/fluid/operators/fake_dequantize_op.h"
#include <string>
#include <string>
#include <vector>
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -84,8 +85,8 @@ class FakeChannelWiseDequantizeMaxAbsOp : public framework::OperatorWithKernel {
...
@@ -84,8 +85,8 @@ class FakeChannelWiseDequantizeMaxAbsOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
ctx
->
HasInput
(
"X"
),
"Input(X) of FakeChannelWiseDequantizeMaxAbsOp should not be null."
);
"Input(X) of FakeChannelWiseDequantizeMaxAbsOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Weight
Scales"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
s
(
"
Scales"
),
"Input(
Weight
Scales) of FakeChannelWiseDequantizeMaxAbsOp "
"Input(Scales) of FakeChannelWiseDequantizeMaxAbsOp "
"should not be null."
);
"should not be null."
);
PADDLE_ENFORCE
(
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
ctx
->
HasOutput
(
"Out"
),
...
@@ -103,39 +104,32 @@ class FakeChannelWiseDequantizeMaxAbsOpMaker
...
@@ -103,39 +104,32 @@ class FakeChannelWiseDequantizeMaxAbsOpMaker
AddInput
(
"X"
,
AddInput
(
"X"
,
"(Tensor) The input with float-32/64 type is the "
"(Tensor) The input with float-32/64 type is the "
"low precision tensor."
);
"low precision tensor."
);
AddInput
(
"ActivationScale"
,
AddInput
(
"Scales"
,
"(float) The activation scale in quantization stage."
)
"(Tensors) The scales in quantization stage. "
.
AsDispensable
();
"Now, `Scales` is a vector with at most two tensors. "
AddInput
(
"WeightScales"
,
"If Scales has two elements, the second tensor should only have "
"(float array) The weight scales in quantization stage."
);
"one value."
)
.
AsDuplicable
();
AddOutput
(
"Out"
,
AddOutput
(
"Out"
,
"(Tensor) The output is the dequantized high "
"(Tensor) The output is the dequantized high "
"precision tensor."
);
"precision tensor."
);
AddAttr
<
int
>
(
"activation_bits"
,
"Quantization bit number for activation."
)
AddAttr
<
std
::
vector
<
int
>>
(
.
SetDefault
(
8
)
"quant_bits"
,
.
AddCustomChecker
([](
const
int
&
bit_length
)
{
"Quantization bit numbers in quantization stage. "
PADDLE_ENFORCE
(
bit_length
>=
1
&&
bit_length
<=
16
,
"The size of `quant_bits` should be equal to the size of `Scales`."
)
"'activation_bits' should be between 1 and 16."
);
.
SetDefault
({
8
});
});
AddAttr
<
int
>
(
"weight_bits"
,
"Quantization bit number for weights."
)
.
SetDefault
(
8
)
.
AddCustomChecker
([](
const
int
&
bit_length
)
{
PADDLE_ENFORCE
(
bit_length
>=
1
&&
bit_length
<=
16
,
"'weight_bits' should be between 1 and 16."
);
});
AddComment
(
R"DOC(
AddComment
(
R"DOC(
FakeChannelWiseDequantizeMaxAbsOp operator.
FakeChannelWiseDequantizeMaxAbsOp operator.
This calculation is an opposite operation of FakeChannelWiseQuantizeMaxAbsOp:
This calculation is an opposite operation of FakeChannelWiseQuantizeMaxAbsOp:
$$Out_c = \frac{
ActivationScale*WeightScale_c*X_c}{(2^{weight\_bits-1}-1)*(2^{activation\_bits
-1}-1)}$$
$$Out_c = \frac{
X_c\prod_{i=1}^{n}Scales_{ic}}{\prod_{i=1}^{n}(2^{quant\_bits_i
-1}-1)}$$
In the above formula, the range value of
c is as follow:
In the above formula, the range value of
$c$ can be represented as $0 \leq c \lt \ the\ channel\ number\ of\ X$.
$$0 \leq c \lt \ the\ channel\ number\ of\ X$$
Besides, the size of $quant\_bits$ should be equal to the size of $Scales$, and it is called $n$ in the formula.
Notes: Tha per-channel quantization is only applied to weights(channel size scale).
Notes: In general, the per-channel quantization is only applied to weights and the activations use per-layer quantization.
And the activations use per-layer quantization(only one scale).
)DOC"
);
)DOC"
);
}
}
};
};
...
...
paddle/fluid/operators/fake_dequantize_op.h
浏览文件 @
806832e0
...
@@ -14,6 +14,7 @@ limitations under the License. */
...
@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#pragma once
#include <vector>
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
...
@@ -50,47 +51,40 @@ class FakeChannelWiseDequantizeMaxAbsKernel : public framework::OpKernel<T> {
...
@@ -50,47 +51,40 @@ class FakeChannelWiseDequantizeMaxAbsKernel : public framework::OpKernel<T> {
public:
public:
virtual
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
virtual
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
weight_scales
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Weight
Scales"
);
auto
scales
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"
Scales"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
PADDLE_ENFORCE_EQ
(
weight_scales
->
numel
(),
in
->
dims
()[
0
],
PADDLE_ENFORCE_EQ
(
scales
[
0
]
->
numel
(),
in
->
dims
()[
0
],
"The weight uses the per-channel quantization type, so "
"The number of first scale values must be the same with "
"the number of weight scale values must be the same with "
"first dimension value of Input(X)."
);
"first dimension value of Input(X)."
);
int
ativation_bits
=
ctx
.
Attr
<
int
>
(
"activation_bits"
);
auto
quant_bits
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"quant_bits"
);
int
weight_bits
=
ctx
.
Attr
<
int
>
(
"weight_bits"
);
int
max_range
=
std
::
pow
(
2
,
quant_bits
[
0
]
-
1
)
-
1
;
int
range
=
std
::
pow
(
2
,
weight_bits
-
1
)
-
1
;
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
out
->
mutable_data
<
T
>
(
dev_ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
dev_ctx
.
GetPlace
());
auto
dequant
=
DequantizeFunctor
<
DeviceContext
,
T
>
();
auto
dequant
=
DequantizeFunctor
<
DeviceContext
,
T
>
();
if
(
ctx
.
HasInput
(
"ActivationScale"
))
{
if
(
scales
.
size
()
==
2
)
{
auto
*
activation_scale
=
ctx
.
Input
<
framework
::
Tensor
>
(
"ActivationScale"
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
activation_scale
->
numel
(),
1
,
scales
[
1
]
->
numel
(),
1
,
"The activation uses per-layer quantization type, so "
"The second scale tensor should only have one value at now."
);
"it must have only one value."
);
framework
::
Tensor
cpu_weigth_scales
;
framework
::
TensorCopy
(
*
weight_scales
,
platform
::
CPUPlace
(),
&
cpu_weigth_scales
);
dev_ctx
.
Wait
();
const
T
*
weight_scales_data
=
cpu_weigth_scales
.
data
<
T
>
();
range
*=
(
std
::
pow
(
2
,
ativation_bits
-
1
)
-
1
);
for
(
int64_t
i
=
0
;
i
<
in
->
dims
()[
0
];
i
++
)
{
for
(
int64_t
i
=
0
;
i
<
in
->
dims
()[
0
];
i
++
)
{
framework
::
Tensor
one_channel_in
=
in
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_in
=
in
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_out
=
out
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_out
=
out
->
Slice
(
i
,
i
+
1
);
auto
max_range
=
range
/
weight_scales_data
[
i
];
framework
::
Tensor
one_channel_scale
=
scales
[
0
]
->
Slice
(
i
,
i
+
1
);
dequant
(
dev_ctx
,
&
one_channel_in
,
activation_scale
,
max_range
*=
(
std
::
pow
(
2
,
quant_bits
[
1
]
-
1
)
-
1
);
dequant
(
dev_ctx
,
&
one_channel_in
,
&
one_channel_scale
,
static_cast
<
T
>
(
max_range
),
&
one_channel_out
);
static_cast
<
T
>
(
max_range
),
&
one_channel_out
);
}
}
dequant
(
dev_ctx
,
out
,
scales
[
1
],
static_cast
<
T
>
(
1
),
out
);
}
else
{
}
else
{
for
(
int64_t
i
=
0
;
i
<
in
->
dims
()[
0
];
i
++
)
{
for
(
int64_t
i
=
0
;
i
<
in
->
dims
()[
0
];
i
++
)
{
framework
::
Tensor
one_channel_in
=
in
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_in
=
in
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_out
=
out
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_out
=
out
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_scale
=
weight_scales
->
Slice
(
i
,
i
+
1
);
framework
::
Tensor
one_channel_scale
=
scales
[
0
]
->
Slice
(
i
,
i
+
1
);
dequant
(
dev_ctx
,
&
one_channel_in
,
&
one_channel_scale
,
dequant
(
dev_ctx
,
&
one_channel_in
,
&
one_channel_scale
,
static_cast
<
T
>
(
range
),
&
one_channel_out
);
static_cast
<
T
>
(
max_
range
),
&
one_channel_out
);
}
}
}
}
}
}
...
...
python/paddle/fluid/tests/unittests/test_fake_dequantize_op.py
浏览文件 @
806832e0
...
@@ -49,53 +49,50 @@ def channel_wise_dequantize_max_abs(x, scales, max_range):
...
@@ -49,53 +49,50 @@ def channel_wise_dequantize_max_abs(x, scales, max_range):
return
y
return
y
class
TestFakeChannelWiseDequantizeMaxAbsOp
(
OpTest
):
class
TestFakeChannelWiseDequantizeMaxAbsOp
TwoScales
(
OpTest
):
def
set_args
(
self
):
def
set_args
(
self
):
self
.
weight_bits
=
8
self
.
quant_bits
=
[
8
,
2
]
self
.
activation_bits
=
2
self
.
data_type
=
"float32"
self
.
data_type
=
"float32"
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_args
()
self
.
set_args
()
self
.
op_type
=
"fake_channel_wise_dequantize_max_abs"
self
.
op_type
=
"fake_channel_wise_dequantize_max_abs"
x
=
np
.
random
.
randn
(
4
,
3
,
64
,
64
).
astype
(
self
.
data_type
)
x
=
np
.
random
.
randn
(
4
,
3
,
64
,
64
).
astype
(
self
.
data_type
)
max_range
=
math
.
pow
(
2
,
self
.
weight_bits
-
1
)
-
1
max_range
=
math
.
pow
(
2
,
self
.
quant_bits
[
0
]
-
1
)
-
1
max_range
*=
(
math
.
pow
(
2
,
self
.
quant_bits
[
1
]
-
1
)
-
1
)
yq
,
scales
=
channel_wise_quantize_max_abs
(
x
,
max_range
)
yq
,
scales
=
channel_wise_quantize_max_abs
(
x
,
max_range
)
ydq
=
channel_wise_dequantize_max_abs
(
yq
,
scales
,
max_range
)
ydq
=
channel_wise_dequantize_max_abs
(
yq
,
scales
,
max_range
)
self
.
inputs
=
{
self
.
inputs
=
{
'X'
:
yq
,
'X'
:
yq
,
'ActivationScale'
:
np
.
array
(
1.0
).
astype
(
self
.
data_type
),
'Scales'
:
[(
"scales0"
,
np
.
array
(
scales
).
astype
(
self
.
data_type
)),
'WeightScales'
:
np
.
array
(
scales
).
astype
(
self
.
data_type
)
(
"scales1"
,
np
.
array
([
1.0
]).
astype
(
self
.
data_type
))]
}
self
.
attrs
=
{
'weight_bits'
:
self
.
weight_bits
,
'activation_bits'
:
self
.
activation_bits
}
}
self
.
attrs
=
{
'quant_bits'
:
self
.
quant_bits
}
self
.
outputs
=
{
'Out'
:
ydq
}
self
.
outputs
=
{
'Out'
:
ydq
}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
()
class
TestFakeChannelWiseDequantizeMaxAbsOp
NoActivation
Scale
(
OpTest
):
class
TestFakeChannelWiseDequantizeMaxAbsOp
One
Scale
(
OpTest
):
def
set_args
(
self
):
def
set_args
(
self
):
self
.
weight_bits
=
8
self
.
quant_bits
=
[
8
]
self
.
data_type
=
"float32"
self
.
data_type
=
"float32"
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_args
()
self
.
set_args
()
self
.
op_type
=
"fake_channel_wise_dequantize_max_abs"
self
.
op_type
=
"fake_channel_wise_dequantize_max_abs"
x
=
np
.
random
.
randn
(
4
,
3
,
64
,
64
).
astype
(
self
.
data_type
)
x
=
np
.
random
.
randn
(
4
,
3
,
64
,
64
).
astype
(
self
.
data_type
)
max_range
=
math
.
pow
(
2
,
self
.
weight_bits
-
1
)
-
1
max_range
=
math
.
pow
(
2
,
self
.
quant_bits
[
0
]
-
1
)
-
1
yq
,
scales
=
channel_wise_quantize_max_abs
(
x
,
max_range
)
yq
,
scales
=
channel_wise_quantize_max_abs
(
x
,
max_range
)
ydq
=
channel_wise_dequantize_max_abs
(
yq
,
scales
,
max_range
)
ydq
=
channel_wise_dequantize_max_abs
(
yq
,
scales
,
max_range
)
self
.
inputs
=
{
self
.
inputs
=
{
'X'
:
yq
,
'X'
:
yq
,
'
WeightScales'
:
np
.
array
(
scales
).
astype
(
self
.
data_type
)
'
Scales'
:
[(
"scales0"
,
np
.
array
(
scales
).
astype
(
self
.
data_type
))]
}
}
self
.
attrs
=
{
'
weight_bits'
:
self
.
weigh
t_bits
}
self
.
attrs
=
{
'
quant_bits'
:
self
.
quan
t_bits
}
self
.
outputs
=
{
'Out'
:
ydq
}
self
.
outputs
=
{
'Out'
:
ydq
}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录