Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ff2142f2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
ff2142f2
编写于
7月 27, 2023
作者:
Z
zxcd
提交者:
GitHub
7月 27, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add int32/int64 for outer/matmul Kernel. (#55584)
* add int32/int64 for outer/matmul Kernel. * fix by comment. * fix by comment
上级
bd73a57d
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
178 addition
and
9 deletion
+178
-9
paddle/phi/kernels/cpu/matmul_kernel.cc
paddle/phi/kernels/cpu/matmul_kernel.cc
+2
-0
paddle/phi/kernels/gpu/matmul_kernel.cu
paddle/phi/kernels/gpu/matmul_kernel.cu
+2
-0
paddle/phi/kernels/impl/matmul_grad_kernel_impl.h
paddle/phi/kernels/impl/matmul_grad_kernel_impl.h
+8
-7
paddle/phi/kernels/impl/matmul_kernel_impl.h
paddle/phi/kernels/impl/matmul_kernel_impl.h
+34
-1
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+4
-1
test/legacy_test/test_matmul_v2_op.py
test/legacy_test/test_matmul_v2_op.py
+104
-0
test/legacy_test/test_outer.py
test/legacy_test/test_outer.py
+24
-0
未找到文件。
paddle/phi/kernels/cpu/matmul_kernel.cc
浏览文件 @
ff2142f2
...
...
@@ -25,6 +25,8 @@ PD_REGISTER_KERNEL(matmul,
phi
::
MatmulKernel
,
float
,
double
,
int32_t
,
int64_t
,
phi
::
dtype
::
complex
<
float
>
,
phi
::
dtype
::
complex
<
double
>
)
{}
...
...
paddle/phi/kernels/gpu/matmul_kernel.cu
浏览文件 @
ff2142f2
...
...
@@ -25,6 +25,8 @@ PD_REGISTER_KERNEL(matmul,
phi
::
MatmulKernel
,
float
,
double
,
int32_t
,
int64_t
,
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
,
phi
::
dtype
::
complex
<
float
>
,
...
...
paddle/phi/kernels/impl/matmul_grad_kernel_impl.h
浏览文件 @
ff2142f2
...
...
@@ -97,7 +97,8 @@ static DenseTensor FoldHeadAndLastDims(const Context& dev_ctx,
}
template
<
typename
Context
,
typename
T
>
void
MatMul
(
const
Context
&
dev_ctx
,
typename
std
::
enable_if
<!
std
::
is_integral
<
T
>::
value
>::
type
MatMul
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
a
,
bool
trans_a
,
const
DenseTensor
&
b
,
...
...
paddle/phi/kernels/impl/matmul_kernel_impl.h
浏览文件 @
ff2142f2
...
...
@@ -19,6 +19,7 @@ limitations under the License. */
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/autotune/cache_base.h"
#include "paddle/phi/kernels/cast_kernel.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/blas/blaslt_impl.cu.h"
#include "paddle/phi/kernels/funcs/complex_functors.h"
...
...
@@ -1078,6 +1079,38 @@ void MatMulInt8Function(const Context& ctx,
#endif
}
template
<
typename
Context
,
typename
T
>
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
MatmulJudgeDtypeKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
y
,
const
std
::
vector
<
std
::
int64_t
>&
x_dims
,
const
std
::
vector
<
std
::
int64_t
>&
y_dims
,
DenseTensor
*
out
,
bool
transpose_x
,
bool
transpose_y
)
{
auto
x_tmp
=
phi
::
Cast
<
T
,
Context
>
(
ctx
,
x
,
phi
::
DataType
::
FLOAT32
);
auto
y_tmp
=
phi
::
Cast
<
T
,
Context
>
(
ctx
,
y
,
phi
::
DataType
::
FLOAT32
);
DenseTensor
out_tmp
;
MatMulFunction
<
Context
,
float
>
(
ctx
,
x_tmp
,
y_tmp
,
x_dims
,
y_dims
,
&
out_tmp
,
transpose_x
,
transpose_y
);
phi
::
CastKernel
<
float
>
(
ctx
,
out_tmp
,
x
.
dtype
(),
out
);
}
template
<
typename
Context
,
typename
T
>
typename
std
::
enable_if
<!
std
::
is_integral
<
T
>::
value
>::
type
MatmulJudgeDtypeKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
y
,
const
std
::
vector
<
std
::
int64_t
>&
x_dims
,
const
std
::
vector
<
std
::
int64_t
>&
y_dims
,
DenseTensor
*
out
,
bool
transpose_x
,
bool
transpose_y
)
{
MatMulFunction
<
Context
,
T
>
(
ctx
,
x
,
y
,
x_dims
,
y_dims
,
out
,
transpose_x
,
transpose_y
);
}
template
<
typename
T
,
typename
Context
>
void
MatmulKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
...
...
@@ -1097,7 +1130,7 @@ void MatmulKernel(const Context& ctx,
" but reviced dims size is 0. "
));
const
std
::
vector
<
std
::
int64_t
>
x_dims
=
vectorize
(
x
.
dims
());
const
std
::
vector
<
std
::
int64_t
>
y_dims
=
vectorize
(
y
.
dims
());
Mat
MulFunction
<
Context
,
T
>
(
Mat
mulJudgeDtypeKernel
<
Context
,
T
>
(
ctx
,
x
,
y
,
x_dims
,
y_dims
,
out
,
transpose_x
,
transpose_y
);
}
...
...
python/paddle/tensor/math.py
浏览文件 @
ff2142f2
...
...
@@ -2315,7 +2315,10 @@ def outer(x, y, name=None):
var_names
=
{
'x'
:
x
,
'y'
:
y
}
for
name
,
val
in
var_names
.
items
():
check_variable_and_dtype
(
val
,
name
,
[
'float16'
,
'float32'
,
'float64'
],
'inner'
val
,
name
,
[
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'outer'
,
)
__check_input
(
nx
,
ny
)
...
...
test/legacy_test/test_matmul_v2_op.py
浏览文件 @
ff2142f2
...
...
@@ -712,6 +712,110 @@ class TestMatMulTypePromotion(TestComplexMatMulOp):
self
.
out
=
np
.
dot
(
self
.
x
,
self
.
y
)
class
TestInt32MatmulOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"matmul_v2"
self
.
python_api
=
paddle
.
tensor
.
matmul
self
.
init_base_dtype
()
self
.
init_input_output
()
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
x
),
'Y'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
y
),
}
self
.
attrs
=
{
'axis'
:
-
1
,
'use_mkldnn'
:
False
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
def
init_base_dtype
(
self
):
self
.
dtype
=
np
.
int32
def
init_input_output
(
self
):
self
.
x
=
np
.
random
.
random
((
10
,
10
)).
astype
(
self
.
dtype
)
self
.
y
=
np
.
random
.
random
((
10
,
10
)).
astype
(
self
.
dtype
)
self
.
out
=
np
.
matmul
(
self
.
x
,
self
.
y
)
def
test_check_output
(
self
):
self
.
check_output
(
check_cinn
=
False
)
class
TestInt32MatMulOpBroadcast
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"matmul_v2"
self
.
python_api
=
paddle
.
tensor
.
matmul
self
.
init_base_dtype
()
self
.
init_input_output
()
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
x
),
'Y'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
y
),
}
self
.
attrs
=
{
'axis'
:
-
1
,
'use_mkldnn'
:
False
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
def
init_base_dtype
(
self
):
self
.
dtype
=
np
.
int32
def
init_input_output
(
self
):
self
.
x
=
np
.
random
.
random
((
10
,
2
,
5
)).
astype
(
self
.
dtype
)
self
.
y
=
np
.
random
.
random
((
5
,
20
)).
astype
(
self
.
dtype
)
self
.
out
=
np
.
matmul
(
self
.
x
,
self
.
y
)
def
test_check_output
(
self
):
self
.
check_output
(
check_cinn
=
False
)
class
TestInt64MatmulOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"matmul_v2"
self
.
python_api
=
paddle
.
tensor
.
matmul
self
.
init_base_dtype
()
self
.
init_input_output
()
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
x
),
'Y'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
y
),
}
self
.
attrs
=
{
'axis'
:
-
1
,
'use_mkldnn'
:
False
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
def
init_base_dtype
(
self
):
self
.
dtype
=
np
.
int64
def
init_input_output
(
self
):
self
.
x
=
np
.
random
.
random
((
10
,
10
)).
astype
(
self
.
dtype
)
self
.
y
=
np
.
random
.
random
((
10
,
10
)).
astype
(
self
.
dtype
)
self
.
out
=
np
.
matmul
(
self
.
x
,
self
.
y
)
def
test_check_output
(
self
):
self
.
check_output
(
check_cinn
=
False
)
class
TestInt64MatMulOpBroadcast
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"matmul_v2"
self
.
python_api
=
paddle
.
tensor
.
matmul
self
.
init_base_dtype
()
self
.
init_input_output
()
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
x
),
'Y'
:
OpTest
.
np_dtype_to_fluid_dtype
(
self
.
y
),
}
self
.
attrs
=
{
'axis'
:
-
1
,
'use_mkldnn'
:
False
}
self
.
outputs
=
{
'Out'
:
self
.
out
}
def
init_base_dtype
(
self
):
self
.
dtype
=
np
.
int64
def
init_input_output
(
self
):
self
.
x
=
np
.
random
.
random
((
10
,
2
,
5
)).
astype
(
self
.
dtype
)
self
.
y
=
np
.
random
.
random
((
5
,
20
)).
astype
(
self
.
dtype
)
self
.
out
=
np
.
matmul
(
self
.
x
,
self
.
y
)
def
test_check_output
(
self
):
self
.
check_output
(
check_cinn
=
False
)
class
TestMatmulop
(
unittest
.
TestCase
):
def
func_dygraph_matmul
(
self
):
paddle
.
disable_static
()
...
...
test/legacy_test/test_outer.py
浏览文件 @
ff2142f2
...
...
@@ -74,6 +74,18 @@ class TestMultiplyApi(unittest.TestCase):
res
=
self
.
_run_static_graph_case
(
x_data
,
y_data
)
np
.
testing
.
assert_allclose
(
res
,
np
.
outer
(
x_data
,
y_data
),
rtol
=
1e-05
)
# test static computation graph: 1-d int32 array
x_data
=
np
.
random
.
rand
(
50
).
astype
(
np
.
int32
)
y_data
=
np
.
random
.
rand
(
50
).
astype
(
np
.
int32
)
res
=
self
.
_run_static_graph_case
(
x_data
,
y_data
)
np
.
testing
.
assert_allclose
(
res
,
np
.
outer
(
x_data
,
y_data
),
rtol
=
1e-05
)
# test static computation graph: 1-d int64 array
x_data
=
np
.
random
.
rand
(
50
).
astype
(
np
.
int64
)
y_data
=
np
.
random
.
rand
(
50
).
astype
(
np
.
int64
)
res
=
self
.
_run_static_graph_case
(
x_data
,
y_data
)
np
.
testing
.
assert_allclose
(
res
,
np
.
outer
(
x_data
,
y_data
),
rtol
=
1e-05
)
# test dynamic computation graph: 3-d array
x_data
=
np
.
random
.
rand
(
5
,
10
,
10
).
astype
(
np
.
float64
)
y_data
=
np
.
random
.
rand
(
2
,
10
).
astype
(
np
.
float64
)
...
...
@@ -112,6 +124,18 @@ class TestMultiplyApi(unittest.TestCase):
res
=
self
.
_run_dynamic_graph_case
(
x_data
,
y_data
)
np
.
testing
.
assert_allclose
(
res
,
np
.
outer
(
x_data
,
y_data
),
rtol
=
1e-05
)
# test dynamic computation graph: 3-d int32 array
x_data
=
np
.
random
.
rand
(
5
,
10
,
10
).
astype
(
np
.
int32
)
y_data
=
np
.
random
.
rand
(
2
,
10
).
astype
(
np
.
int32
)
res
=
self
.
_run_dynamic_graph_case
(
x_data
,
y_data
)
np
.
testing
.
assert_allclose
(
res
,
np
.
outer
(
x_data
,
y_data
),
rtol
=
1e-05
)
# test dynamic computation graph: 3-d int64 array
x_data
=
np
.
random
.
rand
(
5
,
10
,
10
).
astype
(
np
.
int64
)
y_data
=
np
.
random
.
rand
(
2
,
10
).
astype
(
np
.
int64
)
res
=
self
.
_run_dynamic_graph_case
(
x_data
,
y_data
)
np
.
testing
.
assert_allclose
(
res
,
np
.
outer
(
x_data
,
y_data
),
rtol
=
1e-05
)
class
TestMultiplyError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录