Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
fb80048d
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看板
未验证
提交
fb80048d
编写于
7月 26, 2022
作者:
B
BiynXu
提交者:
GitHub
7月 26, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move bmm OP from fluid to phi (#44496)
上级
ff216f18
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
411 addition
and
209 deletion
+411
-209
paddle/fluid/operators/bmm_op.cc
paddle/fluid/operators/bmm_op.cc
+14
-91
paddle/fluid/operators/bmm_op.cu
paddle/fluid/operators/bmm_op.cu
+0
-29
paddle/fluid/operators/bmm_op.h
paddle/fluid/operators/bmm_op.h
+0
-89
paddle/phi/infermeta/backward.cc
paddle/phi/infermeta/backward.cc
+11
-0
paddle/phi/infermeta/backward.h
paddle/phi/infermeta/backward.h
+6
-0
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+47
-0
paddle/phi/infermeta/binary.h
paddle/phi/infermeta/binary.h
+2
-0
paddle/phi/kernels/bmm_grad_kernel.h
paddle/phi/kernels/bmm_grad_kernel.h
+29
-0
paddle/phi/kernels/bmm_kernel.h
paddle/phi/kernels/bmm_kernel.h
+41
-0
paddle/phi/kernels/cpu/bmm_grad_kernel.cc
paddle/phi/kernels/cpu/bmm_grad_kernel.cc
+22
-0
paddle/phi/kernels/cpu/bmm_kernel.cc
paddle/phi/kernels/cpu/bmm_kernel.cc
+21
-0
paddle/phi/kernels/gpu/bmm_grad_kernel.cu
paddle/phi/kernels/gpu/bmm_grad_kernel.cu
+27
-0
paddle/phi/kernels/gpu/bmm_kernel.cu
paddle/phi/kernels/gpu/bmm_kernel.cu
+27
-0
paddle/phi/kernels/impl/bmm_grad_kernel_impl.h
paddle/phi/kernels/impl/bmm_grad_kernel_impl.h
+96
-0
paddle/phi/kernels/impl/bmm_kernel_impl.h
paddle/phi/kernels/impl/bmm_kernel_impl.h
+42
-0
paddle/phi/ops/compat/bmm_sig.cc
paddle/phi/ops/compat/bmm_sig.cc
+26
-0
未找到文件。
paddle/fluid/operators/bmm_op.cc
浏览文件 @
fb80048d
...
@@ -16,6 +16,11 @@
...
@@ -16,6 +16,11 @@
#include <vector>
#include <vector>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/binary.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -24,62 +29,6 @@ class BmmOp : public framework::OperatorWithKernel {
...
@@ -24,62 +29,6 @@ class BmmOp : public framework::OperatorWithKernel {
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"X"
),
true
,
platform
::
errors
::
NotFound
(
"Input(X) of BmmOp should not be null"
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"Y"
),
true
,
platform
::
errors
::
NotFound
(
"Input(Y) of BmmOp should not be null"
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasOutput
(
"Out"
),
true
,
platform
::
errors
::
NotFound
(
"Output(Out) of BmmOp should not be null."
));
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
3
,
platform
::
errors
::
InvalidArgument
(
"Input(X) of BmmOp must be 3-dimensional in BmmOp, "
"but received X's shape: [%s]."
,
x_dims
));
PADDLE_ENFORCE_EQ
(
y_dims
.
size
(),
3
,
platform
::
errors
::
InvalidArgument
(
"Input(Y) of BmmOp must be 3-dimensional in BmmOp, "
"but received Y's shape: [%s]."
,
y_dims
));
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
y_dims
[
0
],
platform
::
errors
::
InvalidArgument
(
"Input(X) and Input(Y) must have the same batch size in BmmOp, "
"but received X's batch size: [%s],"
"Y's batch size [%s]"
,
x_dims
[
0
],
y_dims
[
0
]));
PADDLE_ENFORCE_EQ
(
x_dims
[
2
],
y_dims
[
1
],
platform
::
errors
::
InvalidArgument
(
"Input(X)'s width must be equal with Input(Y)'s height in BmmOp,"
"but receive X's width: [%s],"
"Y's height: [%s]."
,
x_dims
[
2
],
y_dims
[
1
]));
std
::
vector
<
int64_t
>
dim_out
;
dim_out
.
push_back
(
x_dims
[
0
]);
dim_out
.
push_back
(
x_dims
[
1
]);
dim_out
.
push_back
(
y_dims
[
2
]);
ctx
->
SetOutputDim
(
"Out"
,
phi
::
make_ddim
(
dim_out
));
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
framework
::
OpKernelType
GetExpectedKernelType
(
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
data_type
=
OperatorWithKernel
::
IndicateVarDataType
(
ctx
,
"X"
);
auto
data_type
=
OperatorWithKernel
::
IndicateVarDataType
(
ctx
,
"X"
);
...
@@ -110,33 +59,6 @@ class BmmOpGrad : public framework::OperatorWithKernel {
...
@@ -110,33 +59,6 @@ class BmmOpGrad : public framework::OperatorWithKernel {
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"X"
),
true
,
platform
::
errors
::
NotFound
(
"Input(X) of BmmOp should not be null"
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"Y"
),
true
,
platform
::
errors
::
NotFound
(
"Input(Y) of BmmOp should not be null"
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
true
,
platform
::
errors
::
NotFound
(
"Output(Out@GRAD) of BmmOp should not be null."
));
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
auto
x_grad_name
=
framework
::
GradVarName
(
"X"
);
auto
y_grad_name
=
framework
::
GradVarName
(
"Y"
);
if
(
ctx
->
HasOutput
(
x_grad_name
))
{
ctx
->
SetOutputDim
(
x_grad_name
,
x_dims
);
}
if
(
ctx
->
HasOutput
(
y_grad_name
))
{
ctx
->
SetOutputDim
(
y_grad_name
,
y_dims
);
}
}
framework
::
OpKernelType
GetExpectedKernelType
(
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
framework
::
OpKernelType
(
OperatorWithKernel
::
IndicateVarDataType
(
return
framework
::
OpKernelType
(
OperatorWithKernel
::
IndicateVarDataType
(
...
@@ -166,15 +88,16 @@ class BmmOpGradMaker : public framework::SingleGradOpMaker<T> {
...
@@ -166,15 +88,16 @@ class BmmOpGradMaker : public framework::SingleGradOpMaker<T> {
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
bmm
,
BmmInferShapeFunctor
,
PD_INFER_META
(
phi
::
BmmInferMeta
));
DECLARE_INFER_SHAPE_FUNCTOR
(
bmm_grad
,
BmmGradInferShapeFunctor
,
PD_INFER_META
(
phi
::
BmmGradInferMeta
));
REGISTER_OPERATOR
(
bmm
,
REGISTER_OPERATOR
(
bmm
,
ops
::
BmmOp
,
ops
::
BmmOp
,
ops
::
BmmOpMaker
,
ops
::
BmmOpMaker
,
ops
::
BmmOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
BmmOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
BmmOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
BmmOpGradMaker
<
paddle
::
imperative
::
OpBase
>
,
REGISTER_OPERATOR
(
bmm_grad
,
ops
::
BmmOpGrad
);
BmmInferShapeFunctor
);
REGISTER_OP_CPU_KERNEL
(
bmm
,
REGISTER_OPERATOR
(
bmm_grad
,
ops
::
BmmOpGrad
,
BmmGradInferShapeFunctor
);
ops
::
BmmKernel
<
phi
::
CPUContext
,
float
>
,
ops
::
BmmKernel
<
phi
::
CPUContext
,
double
>
);
REGISTER_OP_CPU_KERNEL
(
bmm_grad
,
ops
::
BmmGradKernel
<
phi
::
CPUContext
,
float
>
,
ops
::
BmmGradKernel
<
phi
::
CPUContext
,
double
>
);
paddle/fluid/operators/bmm_op.cu
已删除
100644 → 0
浏览文件 @
ff216f18
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
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/bmm_op.h"
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
bmm
,
ops
::
BmmKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
BmmKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
BmmKernel
<
paddle
::
platform
::
CUDADeviceContext
,
paddle
::
platform
::
float16
>
);
REGISTER_OP_CUDA_KERNEL
(
bmm_grad
,
ops
::
BmmGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
BmmGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
BmmGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
paddle
::
platform
::
float16
>
);
#endif
paddle/fluid/operators/bmm_op.h
浏览文件 @
fb80048d
...
@@ -58,95 +58,6 @@ static void ReshapeXYOutIntoMatrixSequence(framework::Tensor *x,
...
@@ -58,95 +58,6 @@ static void ReshapeXYOutIntoMatrixSequence(framework::Tensor *x,
ReshapeTensorIntoMatrixSequence
(
y
,
mat_dim_y
);
ReshapeTensorIntoMatrixSequence
(
y
,
mat_dim_y
);
}
}
template
<
typename
DeviceContext
,
typename
T
>
class
BmmKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
const
Tensor
&
x
=
*
context
.
Input
<
Tensor
>
(
"X"
);
const
Tensor
&
y
=
*
context
.
Input
<
Tensor
>
(
"Y"
);
Tensor
*
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
if
(
x
.
numel
()
==
0
||
y
.
numel
()
==
0
)
{
return
;
}
auto
blas
=
phi
::
funcs
::
GetBlas
<
DeviceContext
,
T
>
(
context
);
auto
mat_dim_a
=
phi
::
funcs
::
CreateMatrixDescriptor
(
x
.
dims
(),
0
,
false
);
auto
mat_dim_b
=
phi
::
funcs
::
CreateMatrixDescriptor
(
y
.
dims
(),
0
,
false
);
// auto scale = static_cast<T>(context.Attr<float>("alpha"));
blas
.
MatMul
(
x
,
mat_dim_a
,
y
,
mat_dim_b
,
T
(
1
),
out
,
T
(
0
));
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
BmmGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
MatMul
(
const
framework
::
ExecutionContext
&
context
,
const
framework
::
Tensor
&
a
,
bool
trans_a
,
const
framework
::
Tensor
&
b
,
bool
trans_b
,
framework
::
Tensor
*
out
)
const
{
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
blas
=
phi
::
funcs
::
GetBlas
<
DeviceContext
,
T
>
(
context
);
auto
mat_dim_a
=
phi
::
funcs
::
CreateMatrixDescriptor
(
a
.
dims
(),
0
,
trans_a
);
auto
mat_dim_b
=
phi
::
funcs
::
CreateMatrixDescriptor
(
b
.
dims
(),
0
,
trans_b
);
blas
.
MatMul
(
a
,
mat_dim_a
,
b
,
mat_dim_b
,
T
(
1
),
out
,
T
(
0
));
}
void
CalcInputGrad
(
const
framework
::
ExecutionContext
&
context
,
const
framework
::
Tensor
&
a
,
bool
trans_a
,
const
framework
::
Tensor
&
b
,
bool
trans_b
,
framework
::
Tensor
*
out
)
const
{
if
(
out
==
nullptr
)
return
;
MatMul
(
context
,
a
,
trans_a
,
b
,
trans_b
,
out
);
}
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
x
=
*
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
y
=
*
context
.
Input
<
framework
::
Tensor
>
(
"Y"
);
auto
dout
=
*
context
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dx
=
context
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dy
=
context
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
ReshapeXYOutIntoMatrixSequence
(
&
x
,
&
y
,
&
dout
,
false
,
false
);
framework
::
DDim
dx_dims
;
if
(
dx
)
{
dx_dims
=
dx
->
dims
();
if
(
dx_dims
!=
x
.
dims
())
{
dx
->
Resize
(
x
.
dims
());
}
}
framework
::
DDim
dy_dims
;
if
(
dy
)
{
dy_dims
=
dy
->
dims
();
if
(
dy_dims
!=
y
.
dims
())
{
dy
->
Resize
(
y
.
dims
());
}
}
CalcInputGrad
(
context
,
dout
,
false
,
y
,
true
,
dx
);
CalcInputGrad
(
context
,
x
,
true
,
dout
,
false
,
dy
);
if
(
dx
)
{
if
(
dx_dims
!=
x
.
dims
())
{
dx
->
Resize
(
dx_dims
);
}
}
if
(
dy
)
{
if
(
dy_dims
!=
y
.
dims
())
{
dy
->
Resize
(
dy_dims
);
}
}
}
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
#endif // PADDLE_FLUID_OPERATORS_BMM_OP_H_
#endif // PADDLE_FLUID_OPERATORS_BMM_OP_H_
paddle/phi/infermeta/backward.cc
浏览文件 @
fb80048d
...
@@ -73,6 +73,17 @@ void BilinearTensorProductGradInferMeta(const MetaTensor& x,
...
@@ -73,6 +73,17 @@ void BilinearTensorProductGradInferMeta(const MetaTensor& x,
}
}
}
}
void
BmmGradInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
const
MetaTensor
&
out_grad
,
MetaTensor
*
x_grad
,
MetaTensor
*
y_grad
)
{
x_grad
->
set_dims
(
x
.
dims
());
y_grad
->
set_dims
(
y
.
dims
());
x_grad
->
set_dtype
(
x
.
dtype
());
y_grad
->
set_dtype
(
y
.
dtype
());
}
void
ChannelShuffleGradInferMeta
(
const
MetaTensor
&
out_grad
,
void
ChannelShuffleGradInferMeta
(
const
MetaTensor
&
out_grad
,
int
groups
,
int
groups
,
const
std
::
string
&
data_format
,
const
std
::
string
&
data_format
,
...
...
paddle/phi/infermeta/backward.h
浏览文件 @
fb80048d
...
@@ -41,6 +41,12 @@ void BilinearTensorProductGradInferMeta(const MetaTensor& x,
...
@@ -41,6 +41,12 @@ void BilinearTensorProductGradInferMeta(const MetaTensor& x,
MetaTensor
*
dweight
,
MetaTensor
*
dweight
,
MetaTensor
*
dbias
);
MetaTensor
*
dbias
);
void
BmmGradInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
const
MetaTensor
&
out_grad
,
MetaTensor
*
x_grad
,
MetaTensor
*
y_grad
);
void
ChannelShuffleGradInferMeta
(
const
MetaTensor
&
out_grad
,
void
ChannelShuffleGradInferMeta
(
const
MetaTensor
&
out_grad
,
int
groups
,
int
groups
,
const
std
::
string
&
data_format
,
const
std
::
string
&
data_format
,
...
...
paddle/phi/infermeta/binary.cc
浏览文件 @
fb80048d
...
@@ -260,6 +260,53 @@ void BincountInferMeta(const MetaTensor& x,
...
@@ -260,6 +260,53 @@ void BincountInferMeta(const MetaTensor& x,
out
->
share_lod
(
x
);
out
->
share_lod
(
x
);
}
}
void
BmmInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
MetaTensor
*
out
)
{
std
::
vector
<
int64_t
>
x_dims
=
phi
::
vectorize
(
x
.
dims
());
std
::
vector
<
int64_t
>
y_dims
=
phi
::
vectorize
(
y
.
dims
());
std
::
size_t
x_ndims
=
x_dims
.
size
();
std
::
size_t
y_ndims
=
y_dims
.
size
();
PADDLE_ENFORCE_EQ
(
x_ndims
,
3
,
phi
::
errors
::
InvalidArgument
(
"Input(X) of BmmOp must be 3-dimensional in BmmOp, "
"but received X's shape: [%s]."
,
x_ndims
));
PADDLE_ENFORCE_EQ
(
y_ndims
,
3
,
phi
::
errors
::
InvalidArgument
(
"Input(Y) of BmmOp must be 3-dimensional in BmmOp, "
"but received Y's shape: [%s]."
,
y_ndims
));
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
y_dims
[
0
],
phi
::
errors
::
InvalidArgument
(
"Input(X) and Input(Y) must have the same batch size in BmmOp, "
"but received X's batch size: [%s],"
"Y's batch size [%s]"
,
x_dims
[
0
],
y_dims
[
0
]));
PADDLE_ENFORCE_EQ
(
x_dims
[
2
],
y_dims
[
1
],
phi
::
errors
::
InvalidArgument
(
"Input(X)'s width must be equal with Input(Y)'s height in BmmOp,"
"but receive X's width: [%s],"
"Y's height: [%s]."
,
x_dims
[
2
],
y_dims
[
1
]));
std
::
vector
<
int64_t
>
dim_out
;
dim_out
.
push_back
(
x_dims
[
0
]);
dim_out
.
push_back
(
x_dims
[
1
]);
dim_out
.
push_back
(
y_dims
[
2
]);
out
->
set_dims
(
phi
::
make_ddim
(
dim_out
));
out
->
share_lod
(
x
);
out
->
set_dtype
(
x
.
dtype
());
out
->
set_layout
(
x
.
layout
());
}
void
CholeskySolveInferMeta
(
const
MetaTensor
&
x
,
void
CholeskySolveInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
const
MetaTensor
&
y
,
bool
upper
,
bool
upper
,
...
...
paddle/phi/infermeta/binary.h
浏览文件 @
fb80048d
...
@@ -60,6 +60,8 @@ void BincountInferMeta(const MetaTensor& x,
...
@@ -60,6 +60,8 @@ void BincountInferMeta(const MetaTensor& x,
int
minlength
,
int
minlength
,
MetaTensor
*
out
);
MetaTensor
*
out
);
void
BmmInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
MetaTensor
*
out
);
void
CholeskySolveInferMeta
(
const
MetaTensor
&
x
,
void
CholeskySolveInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
const
MetaTensor
&
y
,
bool
upper
,
bool
upper
,
...
...
paddle/phi/kernels/bmm_grad_kernel.h
0 → 100644
浏览文件 @
fb80048d
// 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.
#pragma once
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
BmmGradKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
y
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
x_grad
,
DenseTensor
*
y_grad
);
}
// namespace phi
paddle/phi/kernels/bmm_kernel.h
0 → 100644
浏览文件 @
fb80048d
// 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.
#pragma once
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
/**
* @brief Bmm Kernel.
* Applies batched matrix multiplication to two tensors.
*
* Both of the two input tensors must be three-dementional
* and share the same batch size.
* if x is a (b, m, k) tensor, y is a (b, k, n) tensor,
* the output will be a (b, m, n) tensor.
*
* @param ctx device context
* @param x The input tensor
* @param y The input tensor
* @param out The product Tensor
*/
template
<
typename
T
,
typename
Context
>
void
BmmKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
y
,
DenseTensor
*
out
);
}
// namespace phi
paddle/phi/kernels/cpu/bmm_grad_kernel.cc
0 → 100644
浏览文件 @
fb80048d
// 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/phi/kernels/bmm_grad_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/bmm_grad_kernel_impl.h"
PD_REGISTER_KERNEL
(
bmm_grad
,
CPU
,
ALL_LAYOUT
,
phi
::
BmmGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/cpu/bmm_kernel.cc
0 → 100644
浏览文件 @
fb80048d
// 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/phi/kernels/bmm_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/bmm_kernel_impl.h"
PD_REGISTER_KERNEL
(
bmm
,
CPU
,
ALL_LAYOUT
,
phi
::
BmmKernel
,
float
,
double
)
{}
paddle/phi/kernels/gpu/bmm_grad_kernel.cu
0 → 100644
浏览文件 @
fb80048d
// 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/phi/kernels/bmm_grad_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/bmm_grad_kernel_impl.h"
PD_REGISTER_KERNEL
(
bmm_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
BmmGradKernel
,
float
,
double
,
paddle
::
platform
::
float16
)
{}
paddle/phi/kernels/gpu/bmm_kernel.cu
0 → 100644
浏览文件 @
fb80048d
// 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/phi/kernels/bmm_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/bmm_kernel_impl.h"
PD_REGISTER_KERNEL
(
bmm
,
GPU
,
ALL_LAYOUT
,
phi
::
BmmKernel
,
float
,
double
,
paddle
::
platform
::
float16
)
{}
paddle/phi/kernels/impl/bmm_grad_kernel_impl.h
0 → 100644
浏览文件 @
fb80048d
// 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.
#pragma once
#include "paddle/phi/kernels/bmm_grad_kernel.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/impl/matmul_grad_kernel_impl.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
MatMul
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
a
,
bool
trans_a
,
const
DenseTensor
&
b
,
bool
trans_b
,
DenseTensor
*
out
)
{
dev_ctx
.
template
Alloc
<
T
>(
out
);
auto
blas
=
phi
::
funcs
::
GetBlas
<
Context
,
T
>
(
dev_ctx
);
auto
mat_dim_a
=
phi
::
funcs
::
CreateMatrixDescriptor
(
a
.
dims
(),
0
,
trans_a
);
auto
mat_dim_b
=
phi
::
funcs
::
CreateMatrixDescriptor
(
b
.
dims
(),
0
,
trans_b
);
blas
.
MatMul
(
a
,
mat_dim_a
,
b
,
mat_dim_b
,
T
(
1
),
out
,
T
(
0
));
}
template
<
typename
T
,
typename
Context
>
void
CalcInputGrad
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
a
,
bool
trans_a
,
const
DenseTensor
&
b
,
bool
trans_b
,
DenseTensor
*
out
)
{
if
(
out
==
nullptr
)
return
;
MatMul
<
T
,
Context
>
(
dev_ctx
,
a
,
trans_a
,
b
,
trans_b
,
out
);
}
template
<
typename
T
,
typename
Context
>
void
BmmGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
y
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
x_grad
,
DenseTensor
*
y_grad
)
{
DenseTensor
x_help
=
x
;
DenseTensor
y_help
=
y
;
DenseTensor
out_grad_help
=
out_grad
;
ReshapeXYOutIntoMatrixSequence
(
&
x_help
,
&
y_help
,
&
out_grad_help
,
false
,
false
);
phi
::
DDim
dx_dims
;
if
(
x_grad
)
{
dx_dims
=
x_grad
->
dims
();
if
(
dx_dims
!=
x_help
.
dims
())
{
x_grad
->
Resize
(
x_help
.
dims
());
}
}
phi
::
DDim
dy_dims
;
if
(
y_grad
)
{
dy_dims
=
y_grad
->
dims
();
if
(
dy_dims
!=
y_help
.
dims
())
{
y_grad
->
Resize
(
y_help
.
dims
());
}
}
CalcInputGrad
<
T
,
Context
>
(
dev_ctx
,
out_grad_help
,
false
,
y_help
,
true
,
x_grad
);
CalcInputGrad
<
T
,
Context
>
(
dev_ctx
,
x_help
,
true
,
out_grad_help
,
false
,
y_grad
);
if
(
x_grad
)
{
if
(
dx_dims
!=
x_help
.
dims
())
{
x_grad
->
Resize
(
dx_dims
);
}
}
if
(
y_grad
)
{
if
(
dy_dims
!=
y_help
.
dims
())
{
y_grad
->
Resize
(
dy_dims
);
}
}
}
}
// namespace phi
paddle/phi/kernels/impl/bmm_kernel_impl.h
0 → 100644
浏览文件 @
fb80048d
// 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.
#pragma once
#include "paddle/phi/kernels/bmm_kernel.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
BmmKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
y
,
DenseTensor
*
out
)
{
dev_ctx
.
template
Alloc
<
T
>(
out
);
if
(
x
.
numel
()
==
0
||
y
.
numel
()
==
0
)
{
return
;
}
auto
blas
=
phi
::
funcs
::
GetBlas
<
Context
,
T
>
(
dev_ctx
);
auto
mat_dim_a
=
phi
::
funcs
::
CreateMatrixDescriptor
(
x
.
dims
(),
0
,
false
);
auto
mat_dim_b
=
phi
::
funcs
::
CreateMatrixDescriptor
(
y
.
dims
(),
0
,
false
);
blas
.
MatMul
(
x
,
mat_dim_a
,
y
,
mat_dim_b
,
T
(
1
),
out
,
T
(
0
));
}
}
// namespace phi
paddle/phi/ops/compat/bmm_sig.cc
0 → 100644
浏览文件 @
fb80048d
// 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/phi/core/compat/op_utils.h"
namespace
phi
{
KernelSignature
BmmGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"bmm_grad"
,
{
"X"
,
"Y"
,
"Out@GRAD"
},
{},
{
"X@GRAD"
,
"Y@GRAD"
});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
bmm_grad
,
phi
::
BmmGradOpArgumentMapping
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录