-
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
【PTen】Add empty and empty_like kernel in pten #38334
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c6acc6
add empty and empty_like kernel in pten
zyfncg ed88333
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg d5146fb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 3c2aa39
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 3852fff
add empty dev_api
zyfncg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* 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/pten/kernels/empty_kernel.h" | ||
|
||
#include "paddle/pten/backends/cpu/cpu_context.h" | ||
#include "paddle/pten/core/kernel_registry.h" | ||
#include "paddle/pten/kernels/impl/empty_kernel_impl.h" | ||
|
||
PT_REGISTER_CTX_KERNEL(empty, | ||
CPU, | ||
ALL_LAYOUT, | ||
pten::Empty, | ||
bool, | ||
int, | ||
int64_t, | ||
float, | ||
double, | ||
paddle::platform::float16) {} | ||
|
||
PT_REGISTER_CTX_KERNEL(empty_like, | ||
CPU, | ||
ALL_LAYOUT, | ||
pten::EmptyLike, | ||
bool, | ||
int, | ||
int64_t, | ||
float, | ||
double, | ||
paddle::platform::float16) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
if(WITH_GPU) | ||
nv_library(math_cuda SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_cuda) | ||
nv_library(linalg_cuda SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) | ||
nv_library(utils_cuda SRCS utils.cu DEPS dense_tensor kernel_context kernel_factory memory convert_utils) | ||
nv_library(manipulation_cuda SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory utils_cuda unary) | ||
nv_library(scale_kernel_cuda SRCS scale_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function) | ||
nv_library(full_kernel_cuda SRCS full_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function) | ||
nv_library(conj_kernel_cuda SRCS conj_kernel.cu DEPS dense_tensor kernel_context kernel_factory) | ||
nv_library(empty_kernel_cuda SRCS empty_kernel.cu DEPS dense_tensor kernel_context kernel_factory) | ||
elseif(WITH_ROCM) | ||
hip_library(math_cuda SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_cuda) | ||
hip_library(linalg_cuda SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) | ||
hip_library(utils_cuda SRCS utils.cu DEPS dense_tensor kernel_context kernel_factory memory convert_utils) | ||
hip_library(manipulation_cuda SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory utils_cuda unary) | ||
hip_library(scale_kernel_cuda SRCS scale_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function) | ||
hip_library(full_kernel_cuda SRCS full_kernel.cu DEPS dense_tensor kernel_context kernel_factory eigen_function) | ||
hip_library(conj_kernel_cuda SRCS conj_kernel.cu DEPS dense_tensor kernel_context kernel_factory) | ||
hip_library(empty_kernel_cuda SRCS empty_kernel.cu DEPS dense_tensor kernel_context kernel_factory) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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/pten/common/scalar_array.h" | ||
#include "paddle/pten/core/dense_tensor.h" | ||
|
||
namespace pten { | ||
|
||
template <typename T, typename ContextT> | ||
void Empty(const ContextT& dev_ctx, const ScalarArray& shape, DenseTensor* out); | ||
|
||
template <typename T, typename ContextT> | ||
void EmptyLike(const ContextT& dev_ctx, DenseTensor* out); | ||
|
||
} // namespace pten |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* 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/pten/kernels/empty_kernel.h" | ||
|
||
#include "paddle/pten/backends/gpu/gpu_context.h" | ||
#include "paddle/pten/core/kernel_registry.h" | ||
#include "paddle/pten/kernels/impl/empty_kernel_impl.h" | ||
|
||
PT_REGISTER_CTX_KERNEL(empty, | ||
GPU, | ||
ALL_LAYOUT, | ||
pten::Empty, | ||
bool, | ||
int, | ||
int64_t, | ||
float, | ||
double, | ||
paddle::platform::float16) {} | ||
|
||
PT_REGISTER_CTX_KERNEL(empty_like, | ||
GPU, | ||
ALL_LAYOUT, | ||
pten::EmptyLike, | ||
bool, | ||
int, | ||
int64_t, | ||
float, | ||
double, | ||
paddle::platform::float16) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* 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/pten/common/scalar_array.h" | ||
#include "paddle/pten/core/dense_tensor.h" | ||
|
||
namespace pten { | ||
|
||
template <typename T, typename ContextT> | ||
void Empty(const ContextT& dev_ctx, | ||
const ScalarArray& shape, | ||
DenseTensor* out) { | ||
out->Resize(paddle::framework::make_ddim(shape.GetData())); | ||
} | ||
|
||
template <typename T, typename ContextT> | ||
void EmptyLike(const ContextT& dev_ctx, DenseTensor* out) { | ||
out->mutable_data<T>(); | ||
} | ||
|
||
} // namespace pten |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* 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 <gtest/gtest.h> | ||
#include <memory> | ||
|
||
#include "paddle/pten/api/include/api.h" | ||
|
||
#include "paddle/pten/api/lib/utils/allocator.h" | ||
#include "paddle/pten/core/dense_tensor.h" | ||
#include "paddle/pten/core/kernel_registry.h" | ||
|
||
namespace paddle { | ||
namespace tests { | ||
|
||
namespace framework = paddle::framework; | ||
using DDim = paddle::framework::DDim; | ||
|
||
// TODO(chenweihang): Remove this test after the API is used in the dygraph | ||
TEST(API, empty_like) { | ||
// 1. create tensor | ||
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>( | ||
paddle::platform::CPUPlace()); | ||
auto dense_x = std::make_shared<pten::DenseTensor>( | ||
alloc, | ||
pten::DenseTensorMeta(pten::DataType::FLOAT32, | ||
framework::make_ddim({3, 2}), | ||
pten::DataLayout::NCHW)); | ||
|
||
paddle::experimental::Tensor x(dense_x); | ||
|
||
// 2. test API | ||
auto out = paddle::experimental::empty_like(x, pten::DataType::FLOAT32); | ||
|
||
// 3. check result | ||
ASSERT_EQ(out.dims().size(), 2); | ||
ASSERT_EQ(out.dims()[0], 3); | ||
ASSERT_EQ(out.numel(), 6); | ||
ASSERT_EQ(out.is_cpu(), true); | ||
ASSERT_EQ(out.type(), pten::DataType::FLOAT32); | ||
ASSERT_EQ(out.layout(), pten::DataLayout::NCHW); | ||
ASSERT_EQ(out.initialized(), true); | ||
} | ||
|
||
TEST(API, empty1) { | ||
// 1. create tensor | ||
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>( | ||
paddle::platform::CPUPlace()); | ||
|
||
auto dense_shape = std::make_shared<pten::DenseTensor>( | ||
alloc, | ||
pten::DenseTensorMeta(pten::DataType::INT64, | ||
framework::make_ddim({2}), | ||
pten::DataLayout::NCHW)); | ||
auto* shape_data = dense_shape->mutable_data<int64_t>(); | ||
shape_data[0] = 2; | ||
shape_data[1] = 3; | ||
|
||
paddle::experimental::Tensor tensor_shape(dense_shape); | ||
|
||
// 2. test API | ||
auto out = paddle::experimental::empty(tensor_shape, pten::DataType::FLOAT32); | ||
|
||
// 3. check result | ||
ASSERT_EQ(out.shape().size(), 2UL); | ||
ASSERT_EQ(out.shape()[0], 2); | ||
ASSERT_EQ(out.numel(), 6); | ||
ASSERT_EQ(out.is_cpu(), true); | ||
ASSERT_EQ(out.type(), pten::DataType::FLOAT32); | ||
ASSERT_EQ(out.layout(), pten::DataLayout::NCHW); | ||
ASSERT_EQ(out.initialized(), true); | ||
} | ||
|
||
TEST(API, empty2) { | ||
const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>( | ||
paddle::platform::CPUPlace()); | ||
|
||
auto dense_scalar = std::make_shared<pten::DenseTensor>( | ||
alloc, | ||
pten::DenseTensorMeta(pten::DataType::INT32, | ||
framework::make_ddim({1}), | ||
pten::DataLayout::NCHW)); | ||
dense_scalar->mutable_data<int32_t>()[0] = 2; | ||
|
||
paddle::experimental::Tensor shape_scalar1(dense_scalar); | ||
paddle::experimental::Tensor shape_scalar2(dense_scalar); | ||
std::vector<paddle::experimental::Tensor> list_shape{shape_scalar1, | ||
shape_scalar2}; | ||
|
||
auto out = paddle::experimental::empty(list_shape, pten::DataType::FLOAT32); | ||
|
||
ASSERT_EQ(out.shape().size(), 2UL); | ||
ASSERT_EQ(out.shape()[0], 2); | ||
ASSERT_EQ(out.numel(), 4); | ||
ASSERT_EQ(out.is_cpu(), true); | ||
ASSERT_EQ(out.type(), pten::DataType::FLOAT32); | ||
ASSERT_EQ(out.layout(), pten::DataLayout::NCHW); | ||
ASSERT_EQ(out.initialized(), true); | ||
} | ||
|
||
TEST(API, empty3) { | ||
std::vector<int64_t> vector_shape{2, 3}; | ||
|
||
auto out = paddle::experimental::empty(vector_shape, pten::DataType::INT32); | ||
|
||
ASSERT_EQ(out.shape().size(), 2UL); | ||
ASSERT_EQ(out.shape()[0], 2); | ||
ASSERT_EQ(out.numel(), 6); | ||
ASSERT_EQ(out.is_cpu(), true); | ||
ASSERT_EQ(out.type(), pten::DataType::INT32); | ||
ASSERT_EQ(out.layout(), pten::DataLayout::NCHW); | ||
ASSERT_EQ(out.initialized(), true); | ||
} | ||
|
||
} // namespace tests | ||
} // namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
empty是不是个设备无关的kernel?
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.
按理说是的,不过目前框架只提供了cpu和gpu的kernel,这里先按照设备无关来写