From c952d39dfe9e504cb70ded03dc9aa5ecd8f8716a Mon Sep 17 00:00:00 2001 From: Ciyong Chen Date: Thu, 16 Aug 2018 20:01:51 +0800 Subject: [PATCH 1/2] Fix the topk regression issue (#12197) --- src/operator/tensor/ordering_op-inl.h | 46 ++++++++++++++------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/operator/tensor/ordering_op-inl.h b/src/operator/tensor/ordering_op-inl.h index 16e6c0ecd3fc..328851113098 100644 --- a/src/operator/tensor/ordering_op-inl.h +++ b/src/operator/tensor/ordering_op-inl.h @@ -452,20 +452,21 @@ void TopKImpl(RunContext ctx, } else if (param.ret_typ == topk_enum::kReturnIndices) { if (do_transpose) { Tensor ret_indices = ret[0].FlatTo3D(axis, axis, s); - ret_indices = tcast(transpose( - slice<2>(inplace_reshape(indices, - Shape3(ret_indices.shape_[0], - ret_indices.shape_[2], - element_num)), - 0, k), - Shape3(0, 2, 1))); - ret_indices = F(ret_indices, element_num); + ret_indices = tcast(F( + transpose(slice<2>(inplace_reshape(indices, + Shape3(ret_indices.shape_[0], + ret_indices.shape_[2], + element_num)), + 0, k), + Shape3(0, 2, 1)), + element_num)); } else { Tensor ret_indices = ret[0].get_with_shape(Shape2(batch_size, k), s); - ret_indices = tcast(slice<1>( - inplace_reshape(indices, Shape2(batch_size, element_num)), 0, k)); - ret_indices = F(ret_indices, element_num); + ret_indices = tcast(F( + slice<1>(inplace_reshape(indices, Shape2(batch_size, element_num)), + 0, k), + element_num)); } } else { if (do_transpose) { @@ -476,23 +477,24 @@ void TopKImpl(RunContext ctx, Shape3(ret_value.shape_[0], ret_value.shape_[2], element_num)), 0, k), Shape3(0, 2, 1)); - ret_indices = tcast(transpose( - slice<2>(inplace_reshape(indices, - Shape3(ret_indices.shape_[0], - ret_indices.shape_[2], - element_num)), - 0, k), - Shape3(0, 2, 1))); - ret_indices = F(ret_indices, element_num); + ret_indices = tcast(F( + transpose(slice<2>(inplace_reshape(indices, + Shape3(ret_indices.shape_[0], + ret_indices.shape_[2], + element_num)), + 0, k), + Shape3(0, 2, 1)), + element_num)); } else { Tensor ret_value = ret[0].get_with_shape(Shape2(batch_size, k), s); Tensor ret_indices = ret[1].get_with_shape(Shape2(batch_size, k), s); ret_value = slice<1>(inplace_reshape(sorted_dat, Shape2(batch_size, element_num)), 0, k); - ret_indices = tcast(slice<1>( - inplace_reshape(indices, Shape2(batch_size, element_num)), 0, k)); - ret_indices = F(ret_indices, element_num); + ret_indices = tcast(F( + slice<1>(inplace_reshape(indices, Shape2(batch_size, element_num)), + 0, k), + element_num)); } } } From 886e695cd7367acfa05fdf5ca4d5414b68ae6d57 Mon Sep 17 00:00:00 2001 From: Ciyong Chen Date: Thu, 16 Aug 2018 22:25:34 +0800 Subject: [PATCH 2/2] Add comments --- src/operator/tensor/ordering_op-inl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/operator/tensor/ordering_op-inl.h b/src/operator/tensor/ordering_op-inl.h index 328851113098..a6f638e29321 100644 --- a/src/operator/tensor/ordering_op-inl.h +++ b/src/operator/tensor/ordering_op-inl.h @@ -433,6 +433,8 @@ void TopKImpl(RunContext ctx, // 3. Assign results to the ret blob // When returning indices, only update(modulo) required elements instead of full elements // to avoid redundant calculation. + // Cast `ret_indices` from int to real_t could introduce conversion error when the element_num + // is large enough. if (param.ret_typ == topk_enum::kReturnMask) { Tensor ret_mask = ret[0].get_with_shape(Shape2(ret[0].Size(), 1), s);