-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Modified RandomKernel with Kernel Primitive API #39666
Merged
AnnaTrainingG
merged 4 commits into
PaddlePaddle:develop
from
AnnaTrainingG:IndexRandom
Feb 22, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* 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 <thrust/device_vector.h> | ||
#include <thrust/host_vector.h> | ||
#include <thrust/random.h> | ||
#include "paddle/fluid/framework/generator.h" | ||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "paddle/fluid/framework/operator.h" | ||
#include "paddle/fluid/operators/amp/fp16_type_traits.h" | ||
#include "paddle/fluid/operators/distribution_helper.h" | ||
#include "paddle/fluid/operators/fill_constant_op.h" | ||
#include "paddle/fluid/platform/aligned_vector.h" | ||
#include "paddle/phi/backends/gpu/gpu_launch_config.h" | ||
#include "paddle/phi/core/hostdevice.h" | ||
#include "paddle/phi/kernels/primitive/kernel_primitives.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
namespace kps = phi::kps; | ||
template <typename T, typename Functor, int VecSize> | ||
__global__ void VectorizedIndexKernel(T *out, int numel, int main_offset, | ||
Functor func) { | ||
int data_offset = BLOCK_ID_X * BLOCK_NUM_X * VecSize; | ||
int stride = BLOCK_NUM_X * GRID_NUM_X * VecSize; | ||
int args[VecSize]; | ||
T result[VecSize]; | ||
for (; data_offset < main_offset; data_offset += stride) { | ||
kps::InitWithDataIndex<int, VecSize, 1, 1>(&args[0], data_offset); | ||
kps::ElementwiseUnary<int, T, VecSize, 1, 1, Functor>(&result[0], &args[0], | ||
func); | ||
kps::WriteData<T, VecSize, 1, 1, false>(out + data_offset, &result[0], | ||
BLOCK_NUM_X * VecSize); | ||
} | ||
int num = numel - data_offset; | ||
if (numel > 0) { | ||
kps::InitWithDataIndex<int, VecSize, 1, 1>(&args[0], data_offset); | ||
kps::ElementwiseUnary<int, T, VecSize, 1, 1, Functor>(&result[0], &args[0], | ||
func); | ||
kps::WriteData<T, VecSize, 1, 1, true>(out + data_offset, &result[0], num); | ||
} | ||
} | ||
|
||
template <typename T, typename Functor> | ||
void IndexKernel(const KPDevice &dev_ctx, Tensor *out, Functor func) { | ||
int numel = out->numel(); | ||
T *out_data = out->mutable_data<T>(dev_ctx.GetPlace()); | ||
if (numel <= 0) return; | ||
int vec_size = paddle::platform::GetVectorizedSize((out->data<T>())); | ||
#ifdef PADDLE_WITH_XPU_KP | ||
int block = 64; | ||
int grid = 8; | ||
auto stream = dev_ctx.x_context()->xpu_stream; | ||
#else | ||
auto config = | ||
phi::backends::gpu::GetGpuLaunchConfig1D(dev_ctx, numel, vec_size); | ||
int grid = config.block_per_grid.x; | ||
int block = config.thread_per_block.x; | ||
auto stream = dev_ctx.stream(); | ||
#endif | ||
|
||
int main_offset = (numel / (vec_size * block)) * vec_size * block; | ||
switch (vec_size) { | ||
case 4: | ||
VectorizedIndexKernel<T, Functor, 4><<<grid, block, 0, stream>>>( | ||
out_data, numel, main_offset, func); | ||
break; | ||
case 2: | ||
VectorizedIndexKernel<T, Functor, 2><<<grid, block, 0, stream>>>( | ||
out_data, numel, main_offset, func); | ||
break; | ||
case 1: | ||
VectorizedIndexKernel<T, Functor, 1><<<grid, block, 0, stream>>>( | ||
out_data, numel, main_offset, func); | ||
break; | ||
default: { | ||
PADDLE_THROW(paddle::platform::errors::Unimplemented( | ||
"Unsupported vectorized size: %d !", vec_size)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} // namespace operators | ||
} // namespace paddle |
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
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.
XPU的线程配置是否合并到GetGpuLaunchConfig1D里面更好?这里线程配置就不用写分支了。包括stream的获取。可以考虑优化下
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.
可以的, 后面会加进去