-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[Fluid] move assign_pos to phi #55794
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2eea06a
move assign_pos to phi
AndSonder 8727c0d
add sig file
AndSonder a52059d
fix
AndSonder e7ab627
Update assign_pos_sig.cc
AndSonder efb5251
fix
AndSonder 092ad1a
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AndSonder 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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) 2023 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/phi/core/dense_tensor.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void AssignPosKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& cum_count, | ||
const DenseTensor& eff_num_len, | ||
DenseTensor* out); | ||
|
||
} // namespace phi |
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) 2023 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/phi/kernels/assign_pos_kernel.h" | ||
#include "paddle/phi/core/errors.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void AssignPosKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& cum_count, | ||
const DenseTensor& eff_num_len, | ||
DenseTensor* out) { | ||
PADDLE_THROW(phi::errors::Unavailable( | ||
"Do not support assign pos op for cpu kernel now.")); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL( | ||
assign_pos, CPU, ALL_LAYOUT, phi::AssignPosKernel, int, int64_t) {} |
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,89 @@ | ||
// Copyright (c) 2023 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/phi/kernels/assign_pos_kernel.h" | ||
#include "paddle/phi/backends/gpu/gpu_primitives.h" | ||
#include "paddle/phi/core/dense_tensor.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/core/tensor_utils.h" | ||
|
||
namespace phi { | ||
|
||
static constexpr int kNumCUDAThreads = 512; | ||
static constexpr int kNumMaxinumNumBlocks = 4096; | ||
|
||
static inline int NumBlocks(const int N) { | ||
return std::min((N + kNumCUDAThreads - 1) / kNumCUDAThreads, | ||
kNumMaxinumNumBlocks); | ||
} | ||
|
||
template <typename T> | ||
__global__ void AssignPos(T* cum_count, | ||
const T* numbers, | ||
T* out, | ||
int64_t limit) { | ||
CUDA_KERNEL_LOOP(i, limit) { | ||
int number_idx = numbers[i]; | ||
if (number_idx > -1) { | ||
int p = phi::CudaAtomicAdd(cum_count + number_idx, -1); | ||
out[p - 1] = i; | ||
} | ||
} | ||
} | ||
|
||
template <typename T, typename Context> | ||
void AssignPosKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& cum_count, | ||
const DenseTensor& eff_num_len, | ||
DenseTensor* out) { | ||
// assign pos decides which tokens should be fetched belong to specially | ||
// counter orderingly. | ||
auto cum_count_ptr = &cum_count; // (counter number) int32 | int64 | ||
auto numbers = &x; // (batch_size * seq_len, topk) int32 | ||
auto eff_num_len_ptr = &eff_num_len; // (sum(cum_count)) | ||
auto out_ptr = &out; // (cum_count) value ranges | ||
// from 0 to batch_size * | ||
// seq_len * topk | ||
auto numel = numbers->numel(); | ||
T* cum_data = const_cast<T*>(cum_count_ptr->data<T>()); | ||
auto cum_size = cum_count_ptr->numel(); | ||
|
||
phi::DenseTensor cpu_eff_num_len; | ||
int64_t cpu_eff_num_len_data = 0; | ||
bool is_cpu_place = eff_num_len_ptr->place() == phi::CPUPlace(); | ||
if (is_cpu_place) { | ||
cpu_eff_num_len_data = eff_num_len_ptr->data<T>()[0]; | ||
} else { | ||
phi::Copy(dev_ctx, eff_num_len, phi::CPUPlace(), false, &cpu_eff_num_len); | ||
cpu_eff_num_len_data = cpu_eff_num_len.data<T>()[0]; | ||
} | ||
|
||
phi::DDim out_dims = phi::make_ddim({cpu_eff_num_len_data}); | ||
out->Resize(out_dims); | ||
auto out_data = dev_ctx.template Alloc<T>(out); | ||
|
||
const T* num_data = numbers->data<T>(); | ||
|
||
int blocks = NumBlocks(numel); | ||
int threads = kNumCUDAThreads; | ||
|
||
AssignPos<T><<<blocks, threads, 0, dev_ctx.stream()>>>( | ||
cum_data, num_data, out_data, numel); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(assign_pos, GPU, ALL_LAYOUT, phi::AssignPosKernel, int64_t) { | ||
} |
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,27 @@ | ||
/* Copyright (c) 2023 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/phi/core/compat/op_utils.h" | ||
|
||
namespace phi { | ||
|
||
KernelSignature AssignPosOpArgumentMapping( | ||
const ArgumentMappingContext& ctx UNUSED) { | ||
return KernelSignature( | ||
"assign_pos", {"X", "cum_count", "eff_num_len"}, {}, {"Out"}); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_ARG_MAPPING_FN(assign_pos, phi::AssignPosOpArgumentMapping); |
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
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.
为啥改成测静态图?
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.
@AndSonder
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.
review意见一直在pending,忘记点submit了...抱歉
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.
这个算子没有对应的python API,迁移之后会报类似下面的这个错误,所以这里就改成只测试静态图的了: