Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix nd.pick large array issue #14082

Merged
merged 4 commits into from
Feb 16, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/operator/tensor/broadcast_reduce_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -1172,18 +1172,18 @@ void L2NormComputeEx(const nnvm::NodeAttrs& attrs,
template<int ndim, bool clip = true>
struct pick {
template<typename DType, typename IType>
MSHADOW_XINLINE static void Map(int i, DType* out, const DType* a,
const IType *idx, int M, int stride,
MSHADOW_XINLINE static void Map(index_t i, DType* out, const DType* a,
const IType *idx, size_t M, int stride,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just use index_t for M so you don't need so many casting in this function?

Copy link
Member

Choose a reason for hiding this comment

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

+1 for using index_t for M.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. i looked at other places where large array support was mentioned. It seemed that
index_t was used where the variable is used as index
and size_t was used otherwise. Hence I stuck to that. But I'll change it to avoid casting if that's what's useful.

mshadow::Shape<ndim> bshape,
mshadow::Shape<ndim> sshape) {
using namespace broadcast;
int j = static_cast<int>(idx[i]);
index_t j = static_cast<index_t>(idx[i]);
if (clip) {
if (j <= 0) j = 0;
else if (j >= M) j = M - 1;
else if (j >= static_cast<index_t>(M)) j = static_cast<index_t>(M) - 1;
} else {
j = j % M;
j += (j < 0) ? M : 0;
j = j % static_cast<index_t>(M);
j += (j < 0) ? static_cast<index_t>(M) : 0;
}
j = ravel(unravel(i, sshape), bshape) + j*stride;
out[i] = a[j];
Expand All @@ -1194,18 +1194,18 @@ struct pick {
template<int ndim, bool clip = true>
struct pick_grad {
template<typename DType, typename IType>
MSHADOW_XINLINE static void Map(int i, DType* igrad, const DType* ograd,
const IType *idx, int M, int stride,
MSHADOW_XINLINE static void Map(index_t i, DType* igrad, const DType* ograd,
const IType *idx, size_t M, int stride,
mshadow::Shape<ndim> bshape,
mshadow::Shape<ndim> sshape) {
using namespace broadcast;
int j = static_cast<int>(idx[i]);
index_t j = static_cast<index_t>(idx[i]);
if (clip) {
if (j <= 0) j = 0;
else if (j >= M) j = M - 1;
else if (j >= static_cast<index_t>(M)) j = static_cast<index_t>(M) - 1;
} else {
j = j % M;
j += (j < 0) ? M : 0;
j = j % static_cast<index_t>(M);
j += (j < 0) ? static_cast<index_t>(M) : 0;
}
j = ravel(unravel(i, sshape), bshape) + j*stride;
igrad[j] += ograd[i];
Expand Down