Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8d181e37
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
8d181e37
编写于
8月 09, 2023
作者:
N
niuliling123
提交者:
GitHub
8月 09, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change index's dtype for int to int64 (#55949)
上级
4eba6478
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
38 addition
and
27 deletion
+38
-27
paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu
paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu
+3
-6
paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu
paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu
+3
-6
paddle/phi/kernels/fusion/gpu/fused_rope_utils.h
paddle/phi/kernels/fusion/gpu/fused_rope_utils.h
+18
-14
python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py
...incubate/nn/functional/fused_rotary_position_embedding.py
+5
-1
test/legacy_test/test_fused_rotary_position_embedding.py
test/legacy_test/test_fused_rotary_position_embedding.py
+9
-0
未找到文件。
paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu
浏览文件 @
8d181e37
...
...
@@ -32,10 +32,9 @@ void FusedRopeGradKernel(const Context& dev_ctx,
DenseTensor
*
dq
,
DenseTensor
*
dk
,
DenseTensor
*
dv
)
{
int
numel
=
dout_q
.
numel
();
int
64_t
numel
=
dout_q
.
numel
();
if
(
numel
<=
0
)
return
;
dev_ctx
.
template
Alloc
<
T
>(
dq
);
dq
->
Resize
(
dout_q
.
dims
());
// small size for broadcast
auto
batch_size
=
dout_q
.
dims
()[
0
];
auto
num_heads
=
dout_q
.
dims
()[
2
];
...
...
@@ -51,8 +50,8 @@ void FusedRopeGradKernel(const Context& dev_ctx,
auto
config
=
phi
::
backends
::
gpu
::
GetGpuLaunchConfig1D
(
dev_ctx
,
numel
,
vec_size
);
int
grid
=
config
.
block_per_grid
.
x
;
int
block
=
config
.
thread_per_block
.
x
;
int
64_t
grid
=
config
.
block_per_grid
.
x
;
int
64_t
block
=
config
.
thread_per_block
.
x
;
auto
stream
=
dev_ctx
.
stream
();
phi
::
Array
<
T
*
,
3
>
outs_data
;
...
...
@@ -65,7 +64,6 @@ void FusedRopeGradKernel(const Context& dev_ctx,
if
(
dout_k
.
get_ptr
())
{
dev_ctx
.
template
Alloc
<
T
>(
dk
);
dk
->
Resize
(
dout_q
.
dims
());
outs_data
[
1
]
=
dk
->
data
<
T
>
();
ins_data
[
1
]
=
dout_k
->
data
<
T
>
();
num_inputs
++
;
...
...
@@ -73,7 +71,6 @@ void FusedRopeGradKernel(const Context& dev_ctx,
if
(
dout_v
.
get_ptr
())
{
dev_ctx
.
template
Alloc
<
T
>(
dv
);
dv
->
Resize
(
dout_q
.
dims
());
outs_data
[
2
]
=
dv
->
data
<
T
>
();
ins_data
[
2
]
=
dout_v
->
data
<
T
>
();
num_inputs
++
;
...
...
paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu
浏览文件 @
8d181e37
...
...
@@ -32,10 +32,9 @@ void FusedRopeKernel(const Context& dev_ctx,
DenseTensor
*
out_q
,
DenseTensor
*
out_k
,
DenseTensor
*
out_v
)
{
int
numel
=
q
.
numel
();
int
64_t
numel
=
q
.
numel
();
if
(
numel
<=
0
)
return
;
dev_ctx
.
template
Alloc
<
T
>(
out_q
);
out_q
->
Resize
(
q
.
dims
());
// small size for broadcast
auto
batch_size
=
q
.
dims
()[
0
];
auto
num_heads
=
q
.
dims
()[
2
];
...
...
@@ -51,8 +50,8 @@ void FusedRopeKernel(const Context& dev_ctx,
auto
config
=
phi
::
backends
::
gpu
::
GetGpuLaunchConfig1D
(
dev_ctx
,
numel
,
vec_size
);
int
grid
=
config
.
block_per_grid
.
x
;
int
block
=
config
.
thread_per_block
.
x
;
int
64_t
grid
=
config
.
block_per_grid
.
x
;
int
64_t
block
=
config
.
thread_per_block
.
x
;
auto
stream
=
dev_ctx
.
stream
();
phi
::
Array
<
T
*
,
3
>
outs_data
;
...
...
@@ -65,7 +64,6 @@ void FusedRopeKernel(const Context& dev_ctx,
if
(
k
.
get_ptr
())
{
dev_ctx
.
template
Alloc
<
T
>(
out_k
);
out_k
->
Resize
(
q
.
dims
());
ins_data
[
1
]
=
k
->
data
<
T
>
();
outs_data
[
1
]
=
out_k
->
data
<
T
>
();
num_inputs
++
;
...
...
@@ -73,7 +71,6 @@ void FusedRopeKernel(const Context& dev_ctx,
if
(
v
.
get_ptr
())
{
dev_ctx
.
template
Alloc
<
T
>(
out_v
);
out_v
->
Resize
(
q
.
dims
());
ins_data
[
2
]
=
v
->
data
<
T
>
();
outs_data
[
2
]
=
out_v
->
data
<
T
>
();
num_inputs
++
;
...
...
paddle/phi/kernels/fusion/gpu/fused_rope_utils.h
浏览文件 @
8d181e37
...
...
@@ -24,16 +24,20 @@ __global__ void VectorizedFusedRopeKernel(phi::Array<const T*, 3> ins_data,
phi
::
Array
<
const
T
*
,
2
>
sin_cos_data
,
bool
flag_sin_cos
,
int
sign
,
int
batch_size
,
int
seq_len
,
int
num_heads
,
int
head_dim
,
int
64_t
batch_size
,
int
64_t
seq_len
,
int
64_t
num_heads
,
int
64_t
head_dim
,
phi
::
Array
<
T
*
,
3
>
outs_data
,
int
num_inputs
,
MPType
div_c
)
{
int
index
=
(
blockIdx
.
x
*
blockDim
.
x
+
threadIdx
.
x
)
*
VecSize
;
int
stride
=
gridDim
.
x
*
blockDim
.
x
*
VecSize
;
int
size
=
batch_size
*
seq_len
*
num_heads
*
head_dim
;
int64_t
index
=
(
static_cast
<
int64_t
>
(
blockIdx
.
x
)
*
static_cast
<
int64_t
>
(
blockDim
.
x
)
+
threadIdx
.
x
)
*
VecSize
;
int64_t
stride
=
static_cast
<
int64_t
>
(
gridDim
.
x
)
*
static_cast
<
int64_t
>
(
blockDim
.
x
)
*
VecSize
;
int64_t
size
=
batch_size
*
seq_len
*
num_heads
*
head_dim
;
MPType
sin_value
[
VecSize
];
MPType
cos_value
[
VecSize
];
MPType
result
[
VecSize
];
...
...
@@ -44,11 +48,11 @@ __global__ void VectorizedFusedRopeKernel(phi::Array<const T*, 3> ins_data,
for
(;
index
<
size
;
index
+=
stride
)
{
if
(
flag_sin_cos
)
{
#pragma unroll
for
(
int
nx
=
0
;
nx
<
VecSize
;
++
nx
)
{
int
index_wc
=
(
index
+
nx
)
%
(
seq_len
*
num_heads
*
head_dim
);
int
pos_seq
=
index_wc
/
(
num_heads
*
head_dim
);
int
pos_head
=
index_wc
%
head_dim
;
int
index_sc
=
pos_seq
*
head_dim
+
pos_head
;
for
(
int
64_t
nx
=
0
;
nx
<
VecSize
;
++
nx
)
{
int
64_t
index_wc
=
(
index
+
nx
)
%
(
seq_len
*
num_heads
*
head_dim
);
int
64_t
pos_seq
=
index_wc
/
(
num_heads
*
head_dim
);
int
64_t
pos_head
=
index_wc
%
head_dim
;
int
64_t
index_sc
=
pos_seq
*
head_dim
+
pos_head
;
const
T
*
sin_input
=
sin_cos_data
[
0
]
+
index_sc
;
const
T
*
cos_input
=
sin_cos_data
[
1
]
+
index_sc
;
...
...
@@ -59,8 +63,8 @@ __global__ void VectorizedFusedRopeKernel(phi::Array<const T*, 3> ins_data,
#pragma unroll
for
(
int
nx
=
0
;
nx
<
VecSize
;
++
nx
)
{
// get sin_index and cos_index
int
index_wc
=
(
index
+
nx
)
%
(
seq_len
*
num_heads
*
head_dim
);
int
pos_seq
=
index_wc
/
(
num_heads
*
head_dim
);
int
64_t
index_wc
=
(
index
+
nx
)
%
(
seq_len
*
num_heads
*
head_dim
);
int
64_t
pos_seq
=
index_wc
/
(
num_heads
*
head_dim
);
MPType
idx
=
static_cast
<
MPType
>
((
index_wc
%
head_dim
)
/
2
*
2.0
);
MPType
indicses
=
static_cast
<
MPType
>
(
1
)
/
...
...
python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py
浏览文件 @
8d181e37
...
...
@@ -17,7 +17,7 @@ from paddle import _C_ops
from
paddle.framework
import
in_dynamic_mode
def
fused_rotary_position_embedding
(
q
,
k
,
v
,
sin
=
None
,
cos
=
None
):
def
fused_rotary_position_embedding
(
q
,
k
=
None
,
v
=
None
,
sin
=
None
,
cos
=
None
):
r
"""
Fused rotary position embedding.
...
...
@@ -53,3 +53,7 @@ def fused_rotary_position_embedding(q, k, v, sin=None, cos=None):
"""
if
in_dynamic_mode
():
return
_C_ops
.
fused_rotary_position_embedding
(
q
,
k
,
v
,
sin
,
cos
)
raise
RuntimeError
(
"This feature is currently supported only in dynamic mode and with CUDAPlace."
)
test/legacy_test/test_fused_rotary_position_embedding.py
浏览文件 @
8d181e37
...
...
@@ -168,6 +168,15 @@ class TestFusedRotaryPositionEmbedding(unittest.TestCase):
p_bw
[
i
].
numpy
(),
f_bw
[
i
].
numpy
(),
rtol
=
1e-05
)
def
test_error
(
self
):
paddle
.
enable_static
()
with
self
.
assertRaises
(
RuntimeError
):
static_q
=
paddle
.
static
.
data
(
name
=
"q"
,
shape
=
self
.
shape
,
dtype
=
self
.
dtype
)
fused_rotary_position_embedding
(
static_q
,
static_q
,
static_q
)
paddle
.
disable_static
()
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录