Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e2e1c57b
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看板
未验证
提交
e2e1c57b
编写于
7月 12, 2021
作者:
Y
Yuang Liu
提交者:
GitHub
7月 12, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
softmax mask fuse upper triangle (#33981)
* softmax mask fuse upper triangle * cover not implemented cpu code
上级
bfbea8fd
变更
8
展开全部
显示空白变更内容
内联
并排
Showing
8 changed file
with
860 addition
and
1 deletion
+860
-1
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.cc
...le/fluid/operators/softmax_mask_fuse_upper_triangle_op.cc
+107
-0
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.cu
...le/fluid/operators/softmax_mask_fuse_upper_triangle_op.cu
+546
-0
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.h
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.h
+30
-0
python/paddle/fluid/tests/unittests/test_softmax_mask_fuse_upper_triangle_op.py
...sts/unittests/test_softmax_mask_fuse_upper_triangle_op.py
+117
-0
python/paddle/incubate/__init__.py
python/paddle/incubate/__init__.py
+2
-1
python/paddle/incubate/operators/__init__.py
python/paddle/incubate/operators/__init__.py
+15
-0
python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py
...le/incubate/operators/softmax_mask_fuse_upper_triangle.py
+42
-0
python/setup.py.in
python/setup.py.in
+1
-0
未找到文件。
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.cc
0 → 100644
浏览文件 @
e2e1c57b
/* Copyright (c) 2021 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/softmax_mask_fuse_upper_triangle_op.h"
#include "paddle/fluid/framework/generator.h"
#include "paddle/fluid/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
using
framework
::
Tensor
;
class
SoftmaxMaskFuseUpperTriangleOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"SoftmaxMaskFuseUpperTriangle"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"SoftmaxMaskFuseUpperTriangle"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
4
,
platform
::
errors
::
InvalidArgument
(
"Input x must be in 4D dimension but "
"received the dimension of X is %d"
,
x_dims
.
size
()));
ctx
->
SetOutputDim
(
"Out"
,
x_dims
);
ctx
->
ShareLoD
(
"X"
,
"Out"
);
}
};
class
SoftmaxMaskFuseUpperTriangleOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"The input of softmax_mask_fuse_upper_triangle op, "
"which is the result of matmul(QK)/sqrt(dk)."
);
AddOutput
(
"Out"
,
"The result of softmax_mask_fuse_upper_triangle op."
);
AddComment
(
R"DOC(
Softmax Mask Fuse Operator.
product = matmul(QK)/sqrt(dk)
output = softmax_mask_fuse_upper_triangle(product)
to get the final output.
)DOC"
);
}
};
class
SoftmaxMaskFuseUpperTriangleOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input"
,
framework
::
GradVarName
(
"Out"
),
"SoftmaxMaskFuseUpperTriangleGrad"
);
auto
out_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Out"
));
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
out_dims
);
ctx
->
ShareLoD
(
framework
::
GradVarName
(
"Out"
),
framework
::
GradVarName
(
"X"
));
}
};
template
<
typename
T
>
class
SoftmaxMaskFuseUpperTriangleGradOpMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
protected:
void
Apply
(
GradOpPtr
<
T
>
op
)
const
override
{
op
->
SetType
(
"softmax_mask_fuse_upper_triangle_grad"
);
op
->
SetInput
(
"Softmax"
,
this
->
Output
(
"Out"
));
op
->
SetInput
(
framework
::
GradVarName
(
"Out"
),
this
->
OutputGrad
(
"Out"
));
op
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
this
->
InputGrad
(
"X"
));
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
softmax_mask_fuse_upper_triangle
,
ops
::
SoftmaxMaskFuseUpperTriangleOp
,
ops
::
SoftmaxMaskFuseUpperTriangleOpMaker
,
ops
::
SoftmaxMaskFuseUpperTriangleGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
SoftmaxMaskFuseUpperTriangleGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
softmax_mask_fuse_upper_triangle_grad
,
ops
::
SoftmaxMaskFuseUpperTriangleOpGrad
);
REGISTER_OP_CPU_KERNEL
(
softmax_mask_fuse_upper_triangle
,
ops
::
SoftmaxMaskFuseUpperTriangleCPUKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
SoftmaxMaskFuseUpperTriangleCPUKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
);
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.cu
0 → 100644
浏览文件 @
e2e1c57b
此差异已折叠。
点击以展开。
paddle/fluid/operators/softmax_mask_fuse_upper_triangle_op.h
0 → 100644
浏览文件 @
e2e1c57b
/* Copyright (c) 2021 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/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
SoftmaxMaskFuseUpperTriangleCPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_EQ
(
platform
::
is_gpu_place
(
ctx
.
GetPlace
()),
true
,
platform
::
errors
::
Unimplemented
(
"Softmax mask fuse op only supports GPU now."
));
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/tests/unittests/test_softmax_mask_fuse_upper_triangle_op.py
0 → 100644
浏览文件 @
e2e1c57b
# Copyright (c) 2021 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
unittest
import
numpy
as
np
import
paddle.fluid.core
as
core
from
op_test
import
OpTest
import
paddle
import
paddle.fluid
as
fluid
import
paddle.incubate
as
incubate
paddle
.
enable_static
()
def
_get_softmax_upper
(
x
,
fp16
=
True
):
x_lower
=
np
.
tril
(
x
)
masked_x
=
np
.
where
(
x_lower
==
0
,
-
10000.0
,
x_lower
).
astype
(
"float32"
)
max_value
=
np
.
max
(
masked_x
,
axis
=-
1
,
keepdims
=
True
)
before_exp
=
masked_x
-
max_value
exp
=
np
.
exp
(
before_exp
)
exp_sum
=
np
.
sum
(
exp
,
axis
=-
1
,
keepdims
=
True
)
rst
=
exp
/
exp_sum
if
fp16
:
rst
=
rst
.
astype
(
"float16"
)
return
rst
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
(),
"core is not compiled with CUDA"
)
class
TestSoftmaxMaskFuseOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"softmax_mask_fuse_upper_triangle"
x
=
np
.
random
.
random
((
1
,
1
,
32
,
32
)).
astype
(
"float16"
)
self
.
inputs
=
{
'X'
:
x
}
rst
=
_get_softmax_upper
(
x
)
self
.
outputs
=
{
'Out'
:
rst
}
def
test_check_output
(
self
):
self
.
check_output_with_place
(
core
.
CUDAPlace
(
0
))
def
test_check_grad
(
self
):
self
.
check_grad_with_place
(
core
.
CUDAPlace
(
0
),
[
"X"
],
"Out"
)
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
(),
"core is not compiled with CUDA"
)
class
TestSoftmaxMaskFuseOp1
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"softmax_mask_fuse_upper_triangle"
x
=
np
.
random
.
random
((
1
,
1
,
32
,
32
))
self
.
inputs
=
{
'X'
:
x
}
rst
=
_get_softmax_upper
(
x
)
self
.
outputs
=
{
'Out'
:
rst
}
def
test_check_output
(
self
):
try
:
self
.
check_output_with_place
(
core
.
CPUPlace
())
except
NotImplementedError
:
pass
def
test_check_grad
(
self
):
try
:
self
.
check_grad_with_place
(
core
.
CPUPlace
(),
[
"X"
],
"Out"
)
except
NotImplementedError
:
pass
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
(),
"core is not compiled with CUDA"
)
class
TestDropoutBiasFuseOp2
(
unittest
.
TestCase
):
# test the python side API for softmax_mask_fuse op
def
setUp
(
self
):
np
.
random
.
seed
(
123
)
self
.
dtypes
=
[
'float16'
,
'float32'
]
def
test_static
(
self
):
for
dtype
in
self
.
dtypes
:
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
input_x
=
fluid
.
data
(
name
=
"x"
,
shape
=
[
1
,
1
,
32
,
32
],
dtype
=
dtype
)
rst
=
incubate
.
softmax_mask_fuse_upper_triangle
(
input_x
)
x_in_np
=
np
.
random
.
random
((
1
,
1
,
32
,
32
)).
astype
(
dtype
)
rst_np
=
_get_softmax_upper
(
x_in_np
,
dtype
==
'float16'
)
exe
=
fluid
.
Executor
(
fluid
.
CUDAPlace
(
0
))
fetches
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"x"
:
x_in_np
},
fetch_list
=
[
rst
])
self
.
assertTrue
(
np
.
allclose
(
fetches
[
0
],
rst_np
))
def
test_dygraph
(
self
):
for
dtype
in
self
.
dtypes
:
with
fluid
.
dygraph
.
guard
(
fluid
.
CUDAPlace
(
0
)):
x_in_np
=
np
.
random
.
random
((
1
,
1
,
32
,
32
)).
astype
(
dtype
)
rst_np
=
_get_softmax_upper
(
x_in_np
,
dtype
==
'float16'
)
input_x
=
fluid
.
dygraph
.
to_variable
(
x_in_np
)
rst
=
incubate
.
softmax_mask_fuse_upper_triangle
(
input_x
)
self
.
assertTrue
(
np
.
allclose
(
rst
,
rst_np
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/incubate/__init__.py
浏览文件 @
e2e1c57b
...
@@ -16,7 +16,8 @@ from .optimizer import LookAhead # noqa: F401
...
@@ -16,7 +16,8 @@ from .optimizer import LookAhead # noqa: F401
from
.optimizer
import
ModelAverage
# noqa: F401
from
.optimizer
import
ModelAverage
# noqa: F401
from
.checkpoint
import
auto_checkpoint
# noqa: F401
from
.checkpoint
import
auto_checkpoint
# noqa: F401
from
..fluid.layer_helper
import
LayerHelper
# noqa: F401
from
..fluid.layer_helper
import
LayerHelper
# noqa: F401
from
.operators
import
softmax_mask_fuse_upper_triangle
# noqa: F401
__all__
=
[
# noqa
__all__
=
[
# noqa
'LookAhead'
,
'ModelAverage'
'LookAhead'
,
'ModelAverage'
,
'softmax_mask_fuse_upper_triangle'
]
]
python/paddle/incubate/operators/__init__.py
0 → 100644
浏览文件 @
e2e1c57b
# Copyright (c) 2021 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
.softmax_mask_fuse_upper_triangle
import
softmax_mask_fuse_upper_triangle
# noqa: F401
python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py
0 → 100644
浏览文件 @
e2e1c57b
# Copyright (c) 2021 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
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.framework
import
in_dygraph_mode
from
paddle.fluid
import
core
def
softmax_mask_fuse_upper_triangle
(
x
):
"""
Fuse softmax mask together without even give a mask.
Under GPT model, the mask is always be a upper triangle
so we can simply mask the upper triangle part of x to get the mask result
:param x: the input x (rst of QK)
:return: the result of softmax mask fuse (upper triangle)
"""
if
in_dygraph_mode
():
out
=
core
.
ops
.
softmax_mask_fuse_upper_triangle
(
x
)
return
out
helper
=
LayerHelper
(
'softmax_mask_fuse_upper_triangle'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
helper
.
append_op
(
type
=
'softmax_mask_fuse_upper_triangle'
,
inputs
=
{
'X'
:
[
x
]},
outputs
=
{
'Out'
:
[
out
]})
return
out
python/setup.py.in
浏览文件 @
e2e1c57b
...
@@ -146,6 +146,7 @@ packages=['paddle',
...
@@ -146,6 +146,7 @@ packages=['paddle',
'paddle.incubate',
'paddle.incubate',
'paddle.incubate.optimizer',
'paddle.incubate.optimizer',
'paddle.incubate.checkpoint',
'paddle.incubate.checkpoint',
'paddle.incubate.operators',
'paddle.distributed.fleet',
'paddle.distributed.fleet',
'paddle.distributed.fleet.base',
'paddle.distributed.fleet.base',
'paddle.distributed.fleet.meta_optimizers',
'paddle.distributed.fleet.meta_optimizers',
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录