diff --git a/paddle/phi/kernels/cpu/top_k_kernel.cc b/paddle/phi/kernels/cpu/top_k_kernel.cc index 3e94680366076818313ffe418627a6ea7a9f4f29..cebe292d33951ed02536cc36a8adec81a2da5d94 100644 --- a/paddle/phi/kernels/cpu/top_k_kernel.cc +++ b/paddle/phi/kernels/cpu/top_k_kernel.cc @@ -31,6 +31,14 @@ static void FullTopK(Type input_height, const int& k, const bool& largest, const bool& sorted) { + PADDLE_ENFORCE_LE( + k, + input_width, + errors::InvalidArgument("The rank (%d) of the input 'k' for " + "topk op must be less than or equal to %d.", + k, + input_width)); + // when the k is small, will the partial sort bool partial_sort_flag = (k * 64) < input_width;