access2rohit commented on a change in pull request #15948: Fix a memory
misalignment in topk operator
URL: https://github.com/apache/incubator-mxnet/pull/15948#discussion_r315777314
##########
File path: src/operator/tensor/ordering_op-inl.h
##########
@@ -414,30 +414,23 @@ void TopKImpl(const RunContext &ctx,
<< element_num << ", but the selected index_t can only represent "
<< mxnet::common::MaxIntegerValue<index_t>() << " elements";
Tensor<xpu, 3, DType> dat = src.FlatTo3D<xpu, DType>(axis, axis, s);
- size_t temp_size = 0;
- // Temp space needed by the gpu-based full sorts.
- temp_size = std::max(temp_size,
- mxnet::op::SortByKeyWorkspaceSize<int, int, xpu>(src.Size()));
- temp_size = std::max(temp_size,
- mxnet::op::SortByKeyWorkspaceSize<int, DType, xpu>(src.Size()));
- temp_size = std::max(temp_size,
- mxnet::op::SortByKeyWorkspaceSize<DType, int, xpu>(src.Size()));
- // Additional temp space for gpu full sorts for batch ids.
- temp_size += PadBytes(sizeof(index_t) * src.Size(), alignment);
- // Temp space for cpu sorts.
- temp_size = std::max(temp_size, static_cast<size_t>(sizeof(DType) *
src.Size()));
+ // Temp space needed by the full sorts.
+ size_t temp_size = std::max(
+ mxnet::op::SortByKeyWorkspaceSize<index_t, DType, xpu>(src.Size()),
+ mxnet::op::SortByKeyWorkspaceSize<DType, index_t, xpu>(src.Size()));
+
size_t workspace_size = temp_size + PadBytes(sizeof(DType) * src.Size(),
alignment)
+ PadBytes(sizeof(index_t) * src.Size(),
alignment);
if (param.ret_typ == topk_enum::kReturnMask) {
- workspace_size += PadBytes(sizeof(int) * batch_size * k, alignment);
+ workspace_size += PadBytes(sizeof(index_t) * batch_size * k, alignment);
}
workspace = resource.get_space_typed<xpu, 1, char>(Shape1(workspace_size),
s);
char* workspace_curr_ptr = workspace.dptr_;
sorted_dat = Tensor<xpu, 1,
DType>(reinterpret_cast<DType*>(workspace_curr_ptr),
- Shape1(src.Size()), s); // contain
sorted dat
+ Shape1(src.Size()), s); // contain sorted dat
Review comment:
nit: indentation ?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services