Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
ed2ad5d9
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ed2ad5d9
编写于
9月 01, 2022
作者:
H
houj04
提交者:
GitHub
9月 01, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[XPU] add c_embedding_op_xpu. (#45617)
上级
4ed6f3bc
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
134 addition
and
2 deletion
+134
-2
cmake/external/xpu.cmake
cmake/external/xpu.cmake
+2
-2
paddle/fluid/operators/collective/c_embedding_op_xpu.cc
paddle/fluid/operators/collective/c_embedding_op_xpu.cc
+84
-0
paddle/fluid/platform/device/xpu/xpu2_op_list.h
paddle/fluid/platform/device/xpu/xpu2_op_list.h
+1
-0
python/paddle/fluid/tests/unittests/c_embedding_op_base.py
python/paddle/fluid/tests/unittests/c_embedding_op_base.py
+13
-0
python/paddle/fluid/tests/unittests/xpu/test_c_embedding_op_xpu.py
...ddle/fluid/tests/unittests/xpu/test_c_embedding_op_xpu.py
+34
-0
未找到文件。
cmake/external/xpu.cmake
浏览文件 @
ed2ad5d9
...
...
@@ -10,7 +10,7 @@ set(XPU_RT_LIB_NAME "libxpurt.so")
if
(
NOT DEFINED XPU_BASE_URL
)
set
(
XPU_BASE_URL_WITHOUT_DATE
"https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev"
)
set
(
XPU_BASE_URL
"
${
XPU_BASE_URL_WITHOUT_DATE
}
/202208
20
"
)
set
(
XPU_BASE_URL
"
${
XPU_BASE_URL_WITHOUT_DATE
}
/202208
31
"
)
else
()
set
(
XPU_BASE_URL
"
${
XPU_BASE_URL
}
"
)
endif
()
...
...
@@ -19,7 +19,7 @@ endif()
if
(
NOT DEFINED XPU_XDNN_BASE_URL
)
set
(
XPU_XDNN_BASE_URL_WITHOUT_DATE
"https://klx-sdk-release-public.su.bcebos.com/xdnn/dev"
)
set
(
XPU_XDNN_BASE_URL
"
${
XPU_XDNN_BASE_URL_WITHOUT_DATE
}
/202208
20
"
)
set
(
XPU_XDNN_BASE_URL
"
${
XPU_XDNN_BASE_URL_WITHOUT_DATE
}
/202208
31
"
)
else
()
set
(
XPU_XDNN_BASE_URL
"
${
XPU_XDNN_BASE_URL
}
"
)
endif
()
...
...
paddle/fluid/operators/collective/c_embedding_op_xpu.cc
0 → 100644
浏览文件 @
ed2ad5d9
/* Copyright (c) 2022 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/collective/c_embedding_op.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
namespace
paddle
{
namespace
operators
{
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
CEmbeddingOpXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
table_t
=
ctx
.
Input
<
LoDTensor
>
(
"W"
);
auto
*
ids_t
=
ctx
.
Input
<
LoDTensor
>
(
"Ids"
);
auto
*
output_t
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
const
int64_t
start_index
=
ctx
.
Attr
<
int64_t
>
(
"start_index"
);
const
T
*
table_data
=
table_t
->
data
<
T
>
();
T
*
output_data
=
output_t
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
const
int64_t
height
=
table_t
->
dims
()[
0
];
const
int64_t
width
=
table_t
->
dims
()[
1
];
// int embedding(Context* ctx, const T* x, const TID* indices, T* y, int xm,
// int n, int ym, int padding_idx, TID start_index = 0);
// xm: table height: number of entries of table.
// n: embedding dim: number of float value within single entry.
// ym: number of elements of input ids.
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
const
auto
&
index_type
=
framework
::
TransToProtoVarType
(
ids_t
->
dtype
());
if
(
index_type
==
framework
::
proto
::
VarType
::
INT32
)
{
int
r
=
xpu
::
embedding
(
dev_ctx
.
x_context
(),
table_data
,
ids_t
->
data
<
int32_t
>
(),
output_data
,
height
,
width
,
ids_t
->
numel
(),
-
1
,
static_cast
<
int32_t
>
(
start_index
));
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"embedding"
);
}
else
if
(
index_type
==
framework
::
proto
::
VarType
::
INT64
)
{
int
r
=
xpu
::
embedding
(
dev_ctx
.
x_context
(),
table_data
,
ids_t
->
data
<
int64_t
>
(),
output_data
,
height
,
width
,
ids_t
->
numel
(),
-
1
,
static_cast
<
int64_t
>
(
start_index
));
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"embedding"
);
}
else
{
PADDLE_THROW
(
platform
::
errors
::
Unavailable
(
"XPU c_embedding ids only support int32 or int64."
));
}
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_XPU_KERNEL
(
c_embedding
,
ops
::
CEmbeddingOpXPUKernel
<
paddle
::
platform
::
XPUDeviceContext
,
float
>
);
paddle/fluid/platform/device/xpu/xpu2_op_list.h
浏览文件 @
ed2ad5d9
...
...
@@ -84,6 +84,7 @@ XPUOpMap& get_kl2_ops() {
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
INT32
,
XPUPlace
())})},
{
"c_embedding"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"c_identity"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
...
...
python/paddle/fluid/tests/unittests/c_embedding_op_base.py
浏览文件 @
ed2ad5d9
...
...
@@ -42,6 +42,8 @@ class TestCEmbeddingCPU(OpTest):
self
.
initcase
()
if
core
.
is_compiled_with_npu
():
self
.
__class__
.
use_npu
=
True
elif
core
.
is_compiled_with_xpu
():
self
.
__class__
.
use_xpu
=
True
elif
core
.
is_compiled_with_cuda
():
self
.
__class__
.
exist_fp64_check_grad
=
True
...
...
@@ -59,6 +61,8 @@ class TestCEmbeddingCPU(OpTest):
self
.
attrs
=
{
'start_index'
:
self
.
start_index
}
if
core
.
is_compiled_with_npu
():
self
.
__class__
.
use_npu
=
True
elif
core
.
is_compiled_with_xpu
():
self
.
__class__
.
use_xpu
=
True
def
test_check_cpu
(
self
):
self
.
check_output_with_place
(
core
.
CPUPlace
())
...
...
@@ -82,12 +86,16 @@ class TestCEmbeddingOpBase(TestCEmbeddingCPU):
self
.
check_output_with_place
(
core
.
CUDAPlace
(
0
))
elif
core
.
is_compiled_with_npu
():
self
.
check_output_with_place
(
core
.
NPUPlace
(
0
))
elif
core
.
is_compiled_with_xpu
():
self
.
check_output_with_place
(
core
.
XPUPlace
(
0
))
def
test_check_grad
(
self
):
if
core
.
is_compiled_with_cuda
():
self
.
check_grad_with_place
(
core
.
CUDAPlace
(
0
),
[
'W'
],
'Out'
)
elif
core
.
is_compiled_with_npu
():
self
.
check_grad_with_place
(
core
.
NPUPlace
(
0
),
[
'W'
],
'Out'
)
elif
core
.
is_compiled_with_xpu
():
self
.
check_grad_with_place
(
core
.
XPUPlace
(
0
),
[
'W'
],
'Out'
)
def
init_dtype
(
self
):
if
core
.
is_compiled_with_cuda
():
...
...
@@ -96,6 +104,9 @@ class TestCEmbeddingOpBase(TestCEmbeddingCPU):
elif
core
.
is_compiled_with_npu
():
self
.
dtype
=
"float32"
self
.
ids_dtype
=
"int32"
elif
core
.
is_compiled_with_xpu
():
self
.
dtype
=
"float32"
self
.
ids_dtype
=
"int64"
class
TestCEmbeddingOpFP32
(
TestCEmbeddingOpBase
):
...
...
@@ -123,6 +134,8 @@ class TestCEmbeddingOpFP32(TestCEmbeddingOpBase):
if
core
.
is_compiled_with_npu
():
self
.
__class__
.
use_npu
=
True
elif
core
.
is_compiled_with_xpu
():
self
.
__class__
.
use_xpu
=
True
elif
core
.
is_compiled_with_cuda
():
self
.
__class__
.
exist_fp64_check_grad
=
True
...
...
python/paddle/fluid/tests/unittests/xpu/test_c_embedding_op_xpu.py
0 → 100644
浏览文件 @
ed2ad5d9
# Copyright (c) 2022 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.
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
sys
sys
.
path
.
append
(
".."
)
import
paddle
from
paddle.fluid.tests.unittests.c_embedding_op_base
import
TestCEmbeddingCPU
,
TestCEmbeddingOpBase
,
TestCEmbeddingOpFP32
paddle
.
enable_static
()
TestCEmbeddingCPU
()
TestCEmbeddingOpBase
()
TestCEmbeddingOpFP32
()
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录