Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matmul_v2 kernel in pten #36844

Merged
merged 155 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
155 commits
Select commit Hold shift + click to select a range
3f545c4
initial tensor design & sign kernel demo
chenwhql Jul 9, 2021
1f4ea40
add move constructor for meta & add lodtensor
chenwhql Jul 12, 2021
44bf926
add dirs & sign xpu kernel
chenwhql Jul 12, 2021
b20689d
add mean cpu&cuda kernel impl
chenwhql Jul 15, 2021
79d2a1a
move sign & mean xpu & npu kernel
chenwhql Jul 15, 2021
434136f
add selected_rows basic impl
chenwhql Jul 16, 2021
6c6ee22
refactor design, BaseTensor to DenseTensor, etc.
chenwhql Jul 27, 2021
013c3fb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Jul 27, 2021
33bba06
add scale mkldnn kernel
chenwhql Jul 28, 2021
d895a11
polish xpu & npu impl details
chenwhql Jul 28, 2021
62ebf01
fix mkldnn reuse compile failed
chenwhql Jul 29, 2021
7c09726
change tensor operation lib name
chenwhql Jul 29, 2021
7ae7f2f
resolve conflit with develop
chenwhql Jul 29, 2021
288efc2
rename util filename
chenwhql Jul 29, 2021
be3ddd5
add more comments
chenwhql Jul 29, 2021
3386c49
change TensorImplInterface to TensorInterface
chenwhql Jul 30, 2021
4ef6be5
add kernel key and factory
chenwhql Aug 4, 2021
b69066e
remove MKLDNNTensorMeta, add MKLDNNDenseTensor
chenwhql Aug 4, 2021
1d4f90e
resolve conflict with develop
chenwhql Aug 4, 2021
c732d57
change XXDeviceContext to XXContext
chenwhql Aug 5, 2021
374345f
add base kernel registrar utils & test on sign
chenwhql Aug 16, 2021
bbb6473
resolve conflict with develop
chenwhql Aug 16, 2021
0e18ff4
replace boost::any by paddle::any
chenwhql Aug 16, 2021
805896b
fix several ci failed
chenwhql Aug 17, 2021
fc4442b
fix npu compile error
chenwhql Aug 17, 2021
cefe30a
add ordered map util
chenwhql Aug 17, 2021
aa3e79b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Aug 17, 2021
a1753a0
fix multiple ordered_map compile errors
chenwhql Aug 17, 2021
05a82e7
move dev into include dir
chenwhql Aug 18, 2021
90e9090
support sign op in static op run
chenwhql Aug 19, 2021
a94eefd
fix static op run error
chenwhql Aug 23, 2021
19da152
resolve confilt with develop
chenwhql Aug 23, 2021
021a505
fix new executor compile failed
chenwhql Aug 23, 2021
f24e45e
add dygraph branch & remove sign_op.h
chenwhql Aug 25, 2021
44acc84
fix test_infer_no_need_buffer_slots
chenwhql Aug 26, 2021
2b66ab4
fix rocm compile link error
chenwhql Aug 26, 2021
2a5ce9b
fix unitybuild error & clear glog
chenwhql Aug 26, 2021
39b7d06
fix npu compile failed
chenwhql Aug 26, 2021
d4dec61
skip quant trans test
chenwhql Aug 26, 2021
461f146
fix part windows compile problem
chenwhql Aug 26, 2021
35aee9a
Merge branch 'develop' into op2func_refactor
chenwhql Aug 26, 2021
ddfbbdd
fix xpu enforce error
chenwhql Aug 26, 2021
57bcd67
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 26, 2021
7d82352
fix inference test failed
chenwhql Aug 27, 2021
d55bb4b
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 27, 2021
d9476dd
Merge branch 'develop' into op2func_refactor
chenwhql Aug 27, 2021
193ee9d
remove ordered_map to solve quant failed
chenwhql Aug 30, 2021
f2db581
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Aug 30, 2021
db6ff09
fix part of rcom compile faild
chenwhql Aug 30, 2021
80bf6b8
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 30, 2021
9031ab3
add more register kernels
chenwhql Aug 31, 2021
f7bbaca
revert scale kernel temporarily
chenwhql Sep 3, 2021
cea19d0
Merge branch 'develop' into op2func_refactor
chenwhql Sep 3, 2021
568bebd
fix code format error
chenwhql Sep 6, 2021
0eedc92
add new kernel registrar marco
chenwhql Sep 7, 2021
509d13e
rename top to tcmpt
chenwhql Sep 7, 2021
7146f92
revert xpu, npu, mkldnn impl & remove op def
chenwhql Sep 7, 2021
321b141
add kernel args parse functor to auto parse args
chenwhql Sep 8, 2021
57a14c6
resolve confilt with develop
chenwhql Sep 8, 2021
c3ebfea
revert some change & add scale kernels
chenwhql Sep 9, 2021
b67de9c
add op proto in dygraph kernelcontext building
chenwhql Sep 9, 2021
13c02aa
polish kernel dispatch logic & nameing rule
chenwhql Sep 10, 2021
1987ce9
fix scale kernel match error
chenwhql Sep 10, 2021
33a4c41
fix scale test failed
chenwhql Sep 10, 2021
c32fde9
add mean API and unittest
chenwhql Sep 13, 2021
a4e53ef
test mean api success
chenwhql Sep 17, 2021
1d9f33f
add branch to solve compiled error
chenwhql Sep 18, 2021
b0cf02c
skip clang format error
chenwhql Sep 18, 2021
95a612e
add mean skip rule in op_library
chenwhql Sep 18, 2021
83d6f77
add dot kernel, api and unittest (#6)
MingMingShangTian Sep 18, 2021
dad5e61
remove old kernel and add symbol link
chenwhql Sep 18, 2021
027f0b2
resolve conflit with tianyu
chenwhql Sep 18, 2021
8add5e4
fix dot compiled failed
chenwhql Sep 18, 2021
01b5ded
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Sep 18, 2021
71a3403
add merco for module declare
chenwhql Sep 22, 2021
4663033
fix npu and xpu compile error
chenwhql Sep 22, 2021
be15b02
revert sign, mean, scale, dot kernel removing
chenwhql Sep 23, 2021
8371096
add comment for keeping old kernel impl
chenwhql Sep 23, 2021
f1f6c8e
fix mutable_data error
chenwhql Sep 23, 2021
5547b44
fix bfloat16 conflit
chenwhql Sep 24, 2021
dd3323d
fix inference undef error
chenwhql Sep 24, 2021
65e68c6
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 24, 2021
caaed19
adapt to msvc compile rules
chenwhql Sep 26, 2021
c9a3f38
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 26, 2021
46b7762
polish comment for template inst
chenwhql Sep 26, 2021
4253f49
add cmake template instantiation for win
chenwhql Sep 27, 2021
4e871ea
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Sep 28, 2021
395a50f
Merge branch 'develop' into op2func_refactor
chenwhql Sep 29, 2021
817f052
fix backend to place device id bug
chenwhql Sep 29, 2021
7f640a6
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 29, 2021
bf0f99b
fix ifdef error
chenwhql Sep 29, 2021
73de891
Op2functor (#7)
MingMingShangTian Sep 30, 2021
e9b219d
fill_any_like kernel refactor (#10)
zyfncg Sep 30, 2021
9789890
skip dtype for fill_any_like
chenwhql Oct 11, 2021
9b33270
add attrs for kernel key constrcut
chenwhql Oct 11, 2021
aa6ed57
add use_pt_kernel Flags to control whether to use pt kernel (#13)
MingMingShangTian Oct 12, 2021
9db8e4a
fix mutable_data cuda place error
chenwhql Oct 12, 2021
12c1178
Merge branch 'develop' into op2func_refactor
chenwhql Oct 12, 2021
c882b5c
move high level apis into hapi
chenwhql Oct 13, 2021
e30ca2a
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 13, 2021
46ba70c
remove selectedrows adapting temporarily
chenwhql Oct 14, 2021
073aef3
Support Scalar in Tensor Compute Library (#14)
zyfncg Oct 14, 2021
06789ba
resolve conflit with yunfei
chenwhql Oct 14, 2021
3f5f789
remove mkldnn tensor & polish details
chenwhql Oct 14, 2021
2309149
use flat_hash_map and small_vector in kernel factory
chenwhql Oct 15, 2021
6ce92e5
Refactor flatten kernel (#12)
YuanRisheng Oct 15, 2021
e0322d5
Revert "use flat_hash_map and small_vector in kernel factory"
chenwhql Oct 15, 2021
d3ab655
Move cpu, cuda and other device code into kernels (#15)
zyfncg Oct 15, 2021
ddc7de8
Perfect unitests (#16)
YuanRisheng Oct 18, 2021
37791f7
replace with flat_hash_map, small_vector (#19)
MingMingShangTian Oct 18, 2021
28a6374
Perfect unitests (#20)
YuanRisheng Oct 18, 2021
e3e2b50
refactor execution adapting impl
chenwhql Oct 18, 2021
e0710fd
resolve conflits
chenwhql Oct 18, 2021
ff19bd0
fix insert conflit
chenwhql Oct 19, 2021
1dd0145
Fix CI bug of test_yolov3 (#21)
zyfncg Oct 19, 2021
b77d1ee
add the tensor base class, test=develop (#17)
Shixiaowei02 Oct 19, 2021
320b5f1
[no-verify] commit backend and tensor signature changes
chenwhql Oct 19, 2021
5b2999f
resolve conflit with xiaowei
chenwhql Oct 19, 2021
466ce03
Rename tcmpt to pten (#23)
zyfncg Oct 20, 2021
beec280
remove k of all enum var
chenwhql Oct 20, 2021
a49fd44
resolve conflit with yunfei
chenwhql Oct 20, 2021
373f9c1
Merge branch 'develop' into op2func_refactor
chenwhql Oct 20, 2021
ce210b4
remove kernel_instantiate (#26)
MingMingShangTian Oct 20, 2021
4e71d15
remove symbols and spatial_tensor
chenwhql Oct 20, 2021
04cf058
change common to functions
chenwhql Oct 20, 2021
ab8db2d
readd share tensor impl methods
chenwhql Oct 20, 2021
f1c9661
add a candidate dense tensor class, test=develop (#28)
Shixiaowei02 Oct 20, 2021
d3674e9
change all Pt to Pten
chenwhql Oct 20, 2021
4e2c0dd
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 20, 2021
bbe59bc
resolve conflit with xiaowei
chenwhql Oct 21, 2021
76a588e
Op2functor opt1 (#27)
MingMingShangTian Oct 21, 2021
fb224ab
polish kernel factory and kernel registry
chenwhql Oct 21, 2021
252fb79
fix operator test error msg mismatch
chenwhql Oct 22, 2021
19b1095
remove tensor signature and backend set member
chenwhql Oct 22, 2021
24ef6c5
move scalar and polish enforce
chenwhql Oct 22, 2021
1685b67
revert dtype layout change to fix error
chenwhql Oct 22, 2021
7b7e988
fix enum operator override error
chenwhql Oct 22, 2021
52fead0
add several base unittests
chenwhql Oct 22, 2021
2ff2721
add pten utils tests
chenwhql Oct 23, 2021
e3ed2c6
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Oct 23, 2021
b5c77e5
polish some details
chenwhql Oct 24, 2021
5240ac0
Dev/op2func refactor 3 (#30)
chenwhql Oct 26, 2021
5fb285c
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 26, 2021
3276897
Merge branch 'develop' into op2func_refactor
chenwhql Oct 26, 2021
558a848
polish some details
chenwhql Oct 26, 2021
72910fa
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 26, 2021
8f100da
polish kernel signature details
chenwhql Oct 26, 2021
be9df70
fix a bug about offsets of the tensor, test=develop (#31)
chenwhql Oct 27, 2021
0fb60d0
add matmul kernel in pten
zyfncg Oct 28, 2021
7758f14
add unittest for new matmul_v2 kernel
zyfncg Oct 28, 2021
e7bf067
fix bug of CI compile
zyfncg Oct 28, 2021
2e76b24
fix bug of CI compile
zyfncg Oct 28, 2021
8ee3572
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Nov 1, 2021
2b6a565
merge conflict
zyfncg Nov 1, 2021
0de9aae
remove useless file
zyfncg Nov 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License. */
#include "paddle/fluid/framework/data_type_transform.h"
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/op_call_stack.h"
#include "paddle/fluid/framework/pten_utils.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator.h头文件中已添加,这里不需要重复添加

#include "paddle/fluid/framework/shape_inference.h"
#include "paddle/fluid/framework/transfer_scope_cache.h"
#include "paddle/fluid/framework/unused_var_check.h"
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/imperative/prepared_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/fluid/framework/data_type_transform.h"
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/pten_utils.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

#include "paddle/fluid/imperative/infer_shape_context.h"
#include "paddle/pten/common/scalar.h"
#include "paddle/utils/small_vector.h"
Expand Down
25 changes: 16 additions & 9 deletions paddle/fluid/operators/matmul_v2_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ limitations under the License. */
#include "paddle/fluid/operators/math/complex_functors.h"
#include "paddle/fluid/operators/reduce_ops/reduce_sum_op.h"

// only can include the headers in paddle/pten/api dirs
#include "paddle/pten/api/include/core.h"
#include "paddle/pten/api/include/linalg.h"
#include "paddle/pten/hapi/lib/utils/tensor_utils.h"

#if defined(__NVCC__) || defined(__HIPCC__)
#include "paddle/fluid/operators/reduce_ops/cub_reduce.h"
#endif
Expand Down Expand Up @@ -380,15 +385,17 @@ class MatMulV2Kernel : public framework::OpKernel<T> {
auto* Out = ctx.Output<Tensor>("Out");
bool trans_x = ctx.Attr<bool>("trans_x");
bool trans_y = ctx.Attr<bool>("trans_y");
PADDLE_ENFORCE_NE(framework::product(X->dims()), 0,
platform::errors::InvalidArgument(
"The Input(X) dims size must not be equal 0,"
" but reviced dims size is 0. "));
PADDLE_ENFORCE_NE(framework::product(Y->dims()), 0,
platform::errors::InvalidArgument(
"The Input(Y) dims size must not be equal 0,"
" but reviced dims size is 0. "));
MatMulFunction<DeviceContext, T>(X, Y, Out, trans_x, trans_y, ctx);

auto& dev_ctx = ctx.device_context<DeviceContext>();
Out->mutable_data<T>(X->place());

auto pt_x = paddle::experimental::MakePtenDenseTensor(*X);
auto pt_y = paddle::experimental::MakePtenDenseTensor(*Y);
auto pt_out = paddle::experimental::MakePtenDenseTensor(*Out);

// call new kernel
pten::Matmul<T>(dev_ctx, *pt_x.get(), *pt_y.get(), trans_x, trans_y,
pt_out.get());
}
};

Expand Down
5 changes: 5 additions & 0 deletions paddle/pten/hapi/include/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ namespace experimental {

Tensor dot(const Tensor& x, const Tensor& y);

Tensor matmul(const Tensor& x,
const Tensor& y,
bool transpose_x,
bool transpose_y);

} // namespace experimental
} // namespace paddle
43 changes: 42 additions & 1 deletion paddle/pten/hapi/lib/linalg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ limitations under the License. */
#include "paddle/pten/core/kernel_context.h"
#include "paddle/pten/hapi/lib/kernel_dispatch.h"
#include "paddle/pten/hapi/lib/utils/allocator.h"
#include "paddle/pten/infershape/binary.h"

namespace paddle {
namespace experimental {
Expand Down Expand Up @@ -65,5 +64,47 @@ Tensor dot(const Tensor& x, const Tensor& y) {
return out;
}

Tensor matmul(const Tensor& x,
const Tensor& y,
bool transpose_x,
bool transpose_y) {
// 1. Get kernel signature and kernel
auto kernel_key_set = ParseKernelKeyByInputArgs(x, y);
auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
"matmul_v2", kernel_key);

// 2. Get Device Context
auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
auto kernel_context = pten::KernelContext(*dev_ctx);

// 3. Auto data transform
auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
auto dense_y = std::dynamic_pointer_cast<pten::DenseTensor>(y.impl());
kernel_context.EmplaceBackInput(dense_x);
kernel_context.EmplaceBackInput(dense_y);
kernel_context.EmplaceBackAttr(transpose_x);
kernel_context.EmplaceBackAttr(transpose_y);
// TODO(chenweihang): add transform impl

// 4. InferShape
auto out_meta = MatmulInferShape(
dense_x->meta(), dense_y->meta(), transpose_x, transpose_y);

// 5. Prepare outputs
const auto allocator = std::make_shared<DefaultAllocator>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[TODO] 后续此处我会改为单例

pten::TransToFluidPlace(kernel_key.backend()));
auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
kernel_context.EmplaceBackOutput(dense_out);

Tensor out;
out.set_impl(dense_out);

// 6. Call kernel
kernel(&kernel_context);

return out;
}

} // namespace experimental
} // namespace paddle
70 changes: 70 additions & 0 deletions paddle/pten/infershape/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,74 @@ DenseTensorMeta DotInferShape(const DenseTensorMeta& x_meta,
return return_meta;
}

DenseTensorMeta MatmulInferShape(const DenseTensorMeta& x_meta,
const DenseTensorMeta& y_meta,
bool trans_x,
bool trans_y) {
std::vector<int64_t> dims_x = paddle::framework::vectorize(x_meta.dims);
std::vector<int64_t> dims_y = paddle::framework::vectorize(y_meta.dims);
auto ndims_x = dims_x.size();
auto ndims_y = dims_y.size();
PADDLE_ENFORCE_GT(ndims_x,
0,
paddle::platform::errors::InvalidArgument(
"The Input(x) dims size must be greater than 0,"
" but reviced dims size is 0. "));
PADDLE_ENFORCE_GT(ndims_y,
0,
paddle::platform::errors::InvalidArgument(
"The Input(y) dims size must be greater than 0,"
" but reviced dims size is 0. "));

bool x_broadcasted = false, y_broadcasted = false;
if (ndims_x == 1) {
dims_x.insert(dims_x.begin(), 1);
ndims_x = 2;
x_broadcasted = true;
}

if (ndims_y == 1) {
dims_y.push_back(1);
ndims_y = 2;
y_broadcasted = true;
}

size_t M, N;
if (trans_x) {
M = dims_x[ndims_x - 1];
} else {
M = dims_x[ndims_x - 2];
}
if (trans_y) {
N = dims_y[ndims_y - 2];
} else {
N = dims_y[ndims_y - 1];
}

std::vector<int64_t> new_dims;
if (ndims_x > ndims_y) {
new_dims.assign(dims_x.begin(), dims_x.end() - 2);
} else if (ndims_x < ndims_y) {
new_dims.assign(dims_y.begin(), dims_y.end() - 2);
} else {
new_dims.reserve(ndims_x);
for (size_t i = 0; i < ndims_x - 2; ++i) {
new_dims.push_back(std::max(dims_x[i], dims_y[i]));
}
}
if (!x_broadcasted) {
new_dims.push_back(M);
}
if (!y_broadcasted) {
new_dims.push_back(N);
}
if (x_broadcasted && y_broadcasted) {
new_dims.push_back(1);
}

auto ddim_out = paddle::framework::make_ddim(new_dims);

return {x_meta.type, ddim_out, x_meta.layout};
}

} // namespace pten
5 changes: 5 additions & 0 deletions paddle/pten/infershape/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ namespace pten {
DenseTensorMeta DotInferShape(const DenseTensorMeta& x_meta,
const DenseTensorMeta& y_meta);

DenseTensorMeta MatmulInferShape(const DenseTensorMeta& x_meta,
const DenseTensorMeta& y_meta,
bool trans_x,
bool trans_y);

} // namespace pten
27 changes: 27 additions & 0 deletions paddle/pten/kernels/cpu/linalg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/platform/complex.h"

#include "paddle/pten/kernels/functions/math/matmul_func.h"

namespace pten {

template <typename T>
Expand All @@ -45,6 +47,27 @@ void Dot(const CPUContext& dev_ctx,
}
}

template <typename T>
void Matmul(const CPUContext& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
bool transpose_x,
bool transpose_y,
DenseTensor* out) {
PADDLE_ENFORCE_NE(paddle::framework::product(x.dims()),
0,
paddle::platform::errors::InvalidArgument(
"The Input(X) dims size must not be equal 0,"
" but reviced dims size is 0. "));
PADDLE_ENFORCE_NE(paddle::framework::product(y.dims()),
0,
paddle::platform::errors::InvalidArgument(
"The Input(Y) dims size must not be equal 0,"
" but reviced dims size is 0. "));
math::MatMulFunction<CPUContext, T>(
dev_ctx, x, y, out, transpose_x, transpose_y);
}

} // namespace pten

PT_REGISTER_MODULE(LinalgCPU);
Expand All @@ -62,3 +85,7 @@ PT_REGISTER_KERNEL("dot",
int64_t,
complex64,
complex128) {}

PT_REGISTER_KERNEL(
"matmul_v2", CPU, ANY, pten::Matmul, float, double, complex64, complex128) {
}
2 changes: 1 addition & 1 deletion paddle/pten/kernels/cpu/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void Dot(const CPUContext& dev_ctx,
DenseTensor* out);

template <typename T>
void matmul(const CPUContext& dev_ctx,
void Matmul(const CPUContext& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
bool transpose_x,
Expand Down
33 changes: 33 additions & 0 deletions paddle/pten/kernels/cuda/linalg.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/kernels/functions/eigen/dot.h"
#include "paddle/pten/kernels/functions/math/matmul_func.h"

// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/complex.h"
Expand All @@ -30,10 +31,32 @@ void Dot(const CUDAContext& dev_ctx,
eigen::Dot<CUDAContext, T>(dev_ctx, x, y, out);
}

template <typename T>
void Matmul(const CUDAContext& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
bool transpose_x,
bool transpose_y,
DenseTensor* out) {
PADDLE_ENFORCE_NE(paddle::framework::product(x.dims()),
0,
paddle::platform::errors::InvalidArgument(
"The Input(X) dims size must not be equal 0,"
" but reviced dims size is 0. "));
PADDLE_ENFORCE_NE(paddle::framework::product(y.dims()),
0,
paddle::platform::errors::InvalidArgument(
"The Input(Y) dims size must not be equal 0,"
" but reviced dims size is 0. "));
math::MatMulFunction<CUDAContext, T>(
dev_ctx, x, y, out, transpose_x, transpose_y);
}

} // namespace pten

PT_REGISTER_MODULE(LinalgCUDA);

using float16 = paddle::platform::float16;
using complex64 = ::paddle::platform::complex<float>;
using complex128 = ::paddle::platform::complex<double>;

Expand All @@ -47,3 +70,13 @@ PT_REGISTER_KERNEL("dot",
int64_t,
complex64,
complex128) {}

PT_REGISTER_KERNEL("matmul_v2",
CUDA,
ANY,
pten::Matmul,
float,
double,
float16,
complex64,
complex128) {}
8 changes: 8 additions & 0 deletions paddle/pten/kernels/cuda/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ void Dot(const CUDAContext& dev_ctx,
const DenseTensor& y,
DenseTensor* out);

template <typename T>
void Matmul(const CUDAContext& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
bool transpose_x,
bool transpose_y,
DenseTensor* out);

} // namespace pten

#endif
5 changes: 5 additions & 0 deletions paddle/pten/kernels/cuda/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ limitations under the License. */

#pragma once

// CUDA and HIP use same api
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)

#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_registry.h"

Expand All @@ -26,3 +29,5 @@ using CUDAContext = paddle::platform::CUDADeviceContext;
void Copy(const CUDAContext& dev_ctx, const DenseTensor& src, DenseTensor* dst);

} // namespace pten

#endif
Loading