Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
efd410c8
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2310
Star
20933
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
efd410c8
编写于
5月 15, 2023
作者:
H
huangjiyi
提交者:
GitHub
5月 15, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move dequantize kernel to phi (#53739)
* update * fix bug * fix output type def
上级
56fded1b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
140 addition
and
96 deletion
+140
-96
paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc
paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc
+0
-96
paddle/phi/kernels/dequantize_kernel.h
paddle/phi/kernels/dequantize_kernel.h
+28
-0
paddle/phi/kernels/onednn/dequantize_kernel.cc
paddle/phi/kernels/onednn/dequantize_kernel.cc
+86
-0
paddle/phi/ops/compat/dequantize_sig.cc
paddle/phi/ops/compat/dequantize_sig.cc
+26
-0
未找到文件。
paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc
已删除
100644 → 0
浏览文件 @
56fded1b
/* Copyright (c) 2016 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 "paddle/fluid/operators/dequantize_op.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/mkldnn_helper.h"
#include "paddle/phi/backends/onednn/onednn_reuse.h"
#include "paddle/phi/core/errors.h"
namespace
paddle
{
namespace
operators
{
using
dnnl
::
memory
;
using
dnnl
::
primitive
;
using
dnnl
::
reorder
;
using
dnnl
::
stream
;
template
<
typename
T
>
class
DeQuantOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
phi
::
DenseTensor
>
(
"Input"
);
const
auto
quantization_scale
=
ctx
.
Attr
<
float
>
(
"Scale"
);
const
auto
quantization_shift
=
ctx
.
Attr
<
float
>
(
"Shift"
);
const
bool
with_shift
=
quantization_shift
!=
0.0
f
;
auto
*
out
=
ctx
.
Output
<
phi
::
DenseTensor
>
(
"Output"
);
PADDLE_ENFORCE
(
quantization_scale
!=
0.0
f
,
phi
::
errors
::
InvalidArgument
(
"Dequantization scale must be different than 0.0f"
));
PADDLE_ENFORCE
(
quantization_shift
<=
255
&&
quantization_shift
>=
0
,
phi
::
errors
::
InvalidArgument
(
"Dequantization shift must be lower or equal to "
,
"255 and greater or equal to 0, but got %f"
,
quantization_shift
));
auto
&
dev_ctx
=
ctx
.
template
device_context
<
phi
::
OneDNNContext
>();
auto
x_tz
=
phi
::
vectorize
<
int64_t
>
(
x
->
dims
());
auto
x_type
=
phi
::
funcs
::
ToOneDNNDataType
(
x
->
dtype
());
auto
out_type
=
phi
::
funcs
::
ToOneDNNDataType
(
out
->
dtype
());
dnnl
::
primitive_attr
attrs
;
static
constexpr
int32_t
mask
=
0
;
// same shift and scale for whole tensor
const
float
reorder_scale
=
1.
/
quantization_scale
;
attrs
.
set_output_scales
(
mask
,
{
reorder_scale
});
if
(
with_shift
)
{
attrs
.
set_zero_points
(
DNNL_ARG_SRC
,
mask
,
{
static_cast
<
int32_t
>
(
quantization_shift
)});
}
phi
::
funcs
::
ReorderOneDNNHandler
reorder_handler
(
x_tz
,
x
->
dtype
(),
x_type
,
out
->
dtype
(),
out_type
,
dev_ctx
.
GetEngine
());
auto
reorder_src_memory_p
=
reorder_handler
.
AcquireSrcMemory
(
x
->
mem_desc
(),
phi
::
funcs
::
to_void_cast
(
x
->
data
<
T
>
()));
auto
reorder_dst_memory_p
=
reorder_handler
.
AcquireDstMemory
(
out
,
x
->
mem_desc
(),
dev_ctx
.
GetPlace
());
auto
reorder_p
=
reorder_handler
.
AcquireReorder
(
reorder_dst_memory_p
,
reorder_src_memory_p
,
attrs
);
auto
&
astream
=
phi
::
OneDNNContext
::
tls
().
get_stream
();
reorder_p
->
execute
(
astream
,
*
reorder_src_memory_p
,
*
reorder_dst_memory_p
);
astream
.
wait
();
out
->
set_mem_desc
(
reorder_dst_memory_p
->
get_desc
());
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_KERNEL
(
dequantize
,
MKLDNN
,
::
phi
::
CPUPlace
,
ops
::
DeQuantOpKernel
<
uint8_t
>
,
ops
::
DeQuantOpKernel
<
int8_t
>
,
ops
::
DeQuantOpKernel
<
paddle
::
platform
::
bfloat16
>
);
paddle/phi/kernels/dequantize_kernel.h
0 → 100644
浏览文件 @
efd410c8
// Copyright (c) 2023 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 "paddle/phi/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
DeQuantKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
float
quantization_scale
,
const
float
quantization_shift
,
DenseTensor
*
out
);
}
// namespace phi
paddle/phi/kernels/onednn/dequantize_kernel.cc
0 → 100644
浏览文件 @
efd410c8
// Copyright (c) 2023 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 "paddle/phi/kernels/dequantize_kernel.h"
#include "paddle/phi/backends/onednn/onednn_context.h"
#include "paddle/phi/backends/onednn/onednn_helper.h"
#include "paddle/phi/backends/onednn/onednn_reuse.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
DeQuantKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
float
quantization_scale
,
const
float
quantization_shift
,
DenseTensor
*
out
)
{
const
bool
with_shift
=
quantization_shift
!=
0.0
f
;
PADDLE_ENFORCE
(
quantization_scale
!=
0.0
f
,
phi
::
errors
::
InvalidArgument
(
"Dequantization scale must be different than 0.0f"
));
PADDLE_ENFORCE
(
quantization_shift
<=
255
&&
quantization_shift
>=
0
,
phi
::
errors
::
InvalidArgument
(
"Dequantization shift must be lower or equal to "
,
"255 and greater or equal to 0, but got %f"
,
quantization_shift
));
auto
x_tz
=
phi
::
vectorize
<
int64_t
>
(
x
.
dims
());
auto
x_type
=
phi
::
funcs
::
ToOneDNNDataType
(
x
.
dtype
());
auto
out_type
=
phi
::
funcs
::
ToOneDNNDataType
(
out
->
dtype
());
dnnl
::
primitive_attr
attrs
;
static
constexpr
int32_t
mask
=
0
;
// same shift and scale for whole tensor
const
float
reorder_scale
=
1.
/
quantization_scale
;
attrs
.
set_output_scales
(
mask
,
{
reorder_scale
});
if
(
with_shift
)
{
attrs
.
set_zero_points
(
DNNL_ARG_SRC
,
mask
,
{
static_cast
<
int32_t
>
(
quantization_shift
)});
}
phi
::
funcs
::
ReorderOneDNNHandler
reorder_handler
(
x_tz
,
x
.
dtype
(),
x_type
,
out
->
dtype
(),
out_type
,
dev_ctx
.
GetEngine
());
auto
reorder_src_memory_p
=
reorder_handler
.
AcquireSrcMemory
(
x
.
mem_desc
(),
phi
::
funcs
::
to_void_cast
(
x
.
data
<
T
>
()));
auto
reorder_dst_memory_p
=
reorder_handler
.
AcquireDstMemory
(
out
,
x
.
mem_desc
(),
dev_ctx
.
GetPlace
());
auto
reorder_p
=
reorder_handler
.
AcquireReorder
(
reorder_dst_memory_p
,
reorder_src_memory_p
,
attrs
);
auto
&
astream
=
phi
::
OneDNNContext
::
tls
().
get_stream
();
reorder_p
->
execute
(
astream
,
*
reorder_src_memory_p
,
*
reorder_dst_memory_p
);
astream
.
wait
();
out
->
set_mem_desc
(
reorder_dst_memory_p
->
get_desc
());
}
}
// namespace phi
PD_REGISTER_KERNEL
(
dequantize
,
OneDNN
,
ONEDNN
,
phi
::
DeQuantKernel
,
uint8_t
,
int8_t
,
phi
::
dtype
::
bfloat16
)
{
kernel
->
OutputAt
(
0
).
SetDataType
(
phi
::
DataType
::
FLOAT32
);
}
paddle/phi/ops/compat/dequantize_sig.cc
0 → 100644
浏览文件 @
efd410c8
/* Copyright (c) 2023 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 "paddle/phi/core/compat/op_utils.h"
namespace
phi
{
KernelSignature
DeQuantOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"dequantize"
,
{
"Input"
},
{
"Scale"
,
"Shift"
},
{
"Output"
});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
dequantize
,
phi
::
DeQuantOpArgumentMapping
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录