-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Add matmul_v2 kernel in pten #36844
Changes from 154 commits
3f545c4
1f4ea40
44bf926
b20689d
79d2a1a
434136f
6c6ee22
013c3fb
33bba06
d895a11
62ebf01
7c09726
7ae7f2f
288efc2
be3ddd5
3386c49
4ef6be5
b69066e
1d4f90e
c732d57
374345f
bbb6473
0e18ff4
805896b
fc4442b
cefe30a
aa3e79b
a1753a0
05a82e7
90e9090
a94eefd
19da152
021a505
f24e45e
44acc84
2b66ab4
2a5ce9b
39b7d06
d4dec61
461f146
35aee9a
ddfbbdd
57bcd67
7d82352
d55bb4b
d9476dd
193ee9d
f2db581
db6ff09
80bf6b8
9031ab3
f7bbaca
cea19d0
568bebd
0eedc92
509d13e
7146f92
321b141
57a14c6
c3ebfea
b67de9c
13c02aa
1987ce9
33a4c41
c32fde9
a4e53ef
1d9f33f
b0cf02c
95a612e
83d6f77
dad5e61
027f0b2
8add5e4
01b5ded
71a3403
4663033
be15b02
8371096
f1f6c8e
5547b44
dd3323d
65e68c6
caaed19
c9a3f38
46b7762
4253f49
4e871ea
395a50f
817f052
7f640a6
bf0f99b
73de891
e9b219d
9789890
9b33270
aa6ed57
9db8e4a
12c1178
c882b5c
e30ca2a
46ba70c
073aef3
06789ba
3f5f789
2309149
6ce92e5
e0322d5
d3ab655
ddc7de8
37791f7
28a6374
e3e2b50
e0710fd
ff19bd0
1dd0145
b77d1ee
320b5f1
5b2999f
466ce03
beec280
a49fd44
373f9c1
ce210b4
4e71d15
04cf058
ab8db2d
f1c9661
d3674e9
4e2c0dd
bbe59bc
76a588e
fb224ab
252fb79
19b1095
24ef6c5
1685b67
7b7e988
52fead0
2ff2721
e3ed2c6
b5c77e5
5240ac0
5fb285c
3276897
558a848
72910fa
8f100da
be9df70
0fb60d0
7758f14
e7bf067
2e76b24
8ee3572
2b6a565
0de9aae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个文件是不是不小心添加进去的? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已删除 |
||
|
||
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/pten/hapi/include/creation.h" | ||
|
||
#include <memory> | ||
|
||
#include "glog/logging.h" | ||
|
||
#include "paddle/pten/api/include/core.h" | ||
#include "paddle/pten/api/include/infershape.h" | ||
#include "paddle/pten/hapi/lib/kernel_dispatch.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
Tensor full_like(const Tensor& x, | ||
const Scalar& value, | ||
paddle::experimental::DataType dtype) { | ||
// 1. Get kernel signature and kernel | ||
CustomKernelKeyParser kernel_key_parser; | ||
Backend kernel_backend = Backend::UNDEFINED; | ||
DataLayout kernel_layout = DataLayout::UNDEFINED; | ||
DataType kernel_data_type = DataType::UNDEFINED; | ||
|
||
kernel_backend = kernel_key_parser.ParseBackend(backend); | ||
if (kernel_backend == Backend::UNDEFINED) { | ||
kernel_backend = kernel_key_parser.ParseBackend(x); | ||
} | ||
kernel_layout = kernel_key_parser.ParseLayout(layout); | ||
if (kernel_layout == DataLayout::UNDEFINED) { | ||
kernel_layout = kernel_key_parser.ParseLayout(x); | ||
} | ||
DataType kernel_data_type = kernel_key_parser.ParseDataType(dtype); | ||
if (kernel_data_type == DataType::UNDEFINED) { | ||
kernel_data_type = kernel_key_parser.ParseDataType(x); | ||
} | ||
|
||
if (kernel_backend == Backend::UNDEFINED | ||
|| kernel_layout == DataLayout::UNDEFINED | ||
|| kernel_data_type == DataType::UNDEFINED ) { | ||
auto kernel_key_set = ParseKernelKeyByInputArgs(x); | ||
auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey(); | ||
if (kernel_backend == Backend::UNDEFINED) { | ||
kernel_backend = kernel_key.backend(); | ||
} | ||
if (kernel_layout == DataLayout::UNDEFINED) { | ||
kernel_layout = kernel_key.layout(); | ||
} | ||
if (kernel_data_type == DataType::UNDEFINED) { | ||
kernel_data_type = kernel_key.dtype(); | ||
} | ||
} | ||
|
||
auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError( | ||
"fill_any_like", {kernel_backend, kernel_layout, kernel_data_type}); | ||
|
||
// 2. Get Device Context | ||
auto* dev_ctx = GetDeviceContextByBackend(kernel_backend); | ||
auto kernel_context = pten::KernelContext(*dev_ctx); | ||
|
||
// 3. Auto data transform | ||
auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl()); | ||
kernel_context.EmplaceBackInput(dense_x); | ||
kernel_context.EmplaceBackAttr(value); | ||
|
||
// 4. InferShape | ||
auto out_meta = UnchangedInferShape(dense_x->meta()); | ||
|
||
// 5. Prepare outputs | ||
Tensor out; | ||
// InferDataType | ||
if (dtype != pten::DataType::UNDEFINED) { | ||
out_meta.type = dtype; | ||
} | ||
auto dense_out = | ||
std::make_shared<pten::DenseTensor>(out_meta, pten::TensorStatus()); | ||
kernel_context.EmplaceBackOutput(dense_out); | ||
out.set_impl(dense_out); | ||
|
||
// 6. Call kernel | ||
kernel(&kernel_context); | ||
|
||
return out; | ||
} | ||
|
||
Tensor ones_like(const Tensor& x, DataType dtype) { | ||
return full_like(x, 1, dtype); | ||
} | ||
|
||
Tensor zeros_like(const Tensor& x, DataType dtype) { | ||
return full_like(x, 0, dtype); | ||
} | ||
|
||
} // namespace experimental | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
operator.h头文件中已添加,这里不需要重复添加