Skip to content

[new API] add paddle.kthvalue and paddle.Tensor.kthvalue #38386

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

Merged
merged 10 commits into from
Dec 31, 2021

Conversation

zoooo0820
Copy link
Contributor

@zoooo0820 zoooo0820 commented Dec 23, 2021

PR types

New features

PR changes

OPs

Describe

This pr adds API/OPs paddle.kthvalue and paddle.Tensor.kthvalue.

kthvalue-API文档-PaddlePaddle深度学习平台 - 10 136 157 23

kthvalue-API Document-PaddlePaddle Deep Learning Platform - 10 136 157 23

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.


template <typename T>
bool SortKthvalue(const platform::CUDADeviceContext& ctx,
const framework::Tensor* input_tensor, const int64_t num_cols,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Too many duplicate codes with top_k_function_cuda.h.

indices)) {
return;
} else {
LOG(INFO) << "KthvalueOP: Some errors happened when use cub sorting";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Throw exception if there is any error. Do not just logging.

}
return;
} else {
LOG(INFO) << "KthvalueOP: Some errors happened when use cub sorting";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same above.

const int& dim_size = input_dims.size();
int axis = static_cast<int>(ctx->Attrs().Get<int>("axis"));
PADDLE_ENFORCE_EQ(
(axis < dim_size) && (axis >= (-1 * dim_size)), true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Try to write:

PADDLE_ENFORCE_LT(axis, dim_size)

and

PADDLE_ENFORCE_GE(axis, -dim_size)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just write:
return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

class KthvalueCPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* input = context.Input<framework::Tensor>("X");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Too many duplicate codes in CPU/CUDA kernels.

jeff41404
jeff41404 previously approved these changes Dec 31, 2021
Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@sneaxiy sneaxiy left a comment

Choose a reason for hiding this comment

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

LGTM.

TODO: do code clean to avoid too many duplicate codes. @zoooo0820

Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LG API

@jeff41404 jeff41404 merged commit 538b572 into PaddlePaddle:develop Dec 31, 2021
@zoooo0820 zoooo0820 deleted the kthvalue branch June 16, 2022 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants