From 4446c8416eb935a49250125d6e8067a265784cda Mon Sep 17 00:00:00 2001 From: zha0q1 Date: Tue, 22 Sep 2020 21:05:53 +0000 Subject: [PATCH] fix seg fault --- src/operator/nn/moments-inl.h | 2 +- tests/nightly/test_np_large_array.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/operator/nn/moments-inl.h b/src/operator/nn/moments-inl.h index 6a9bdc54b905..a15e0260106a 100644 --- a/src/operator/nn/moments-inl.h +++ b/src/operator/nn/moments-inl.h @@ -77,7 +77,7 @@ inline bool MomentsType(const nnvm::NodeAttrs& attrs, struct VarBroadcastKernel { template - MSHADOW_XINLINE static void Map(int i, + MSHADOW_XINLINE static void Map(index_t i, DType *out, const DType *data, const DType *mean, diff --git a/tests/nightly/test_np_large_array.py b/tests/nightly/test_np_large_array.py index 780fa12e8f45..1d17b139736d 100644 --- a/tests/nightly/test_np_large_array.py +++ b/tests/nightly/test_np_large_array.py @@ -665,6 +665,37 @@ def test_subtract(): assert B.grad.shape == (INT_OVERFLOW, 2) assert B.grad[0][0] == -1 +@use_np +def test_std(): + N = 2*20 + inp = np.zeros((2, INT_OVERFLOW)) + inp[-1, -1] = N + inp.attach_grad() + with mx.autograd.record(): + out = np.std(inp, axis=1) + out.backward() + assert out.shape == (2, ) + ref = ((float(N)/INT_OVERFLOW)**2 * (INT_OVERFLOW-1))**0.5 + assert_almost_equal(out[1], ref, rtol=1e-5, atol=1e-5) + assert inp.grad.shape == inp.shape + assert inp.grad[-1, -1] == 0 + +@use_np +def test_var(): + N = 2*20 + inp = np.zeros((2, INT_OVERFLOW)) + inp[-1, -1] = N + inp.attach_grad() + with mx.autograd.record(): + out = np.var(inp, axis=1) + out.backward() + assert out.shape == (2, ) + ref = (float(N)/INT_OVERFLOW)**2 * (INT_OVERFLOW-1) + assert_almost_equal(out[1], ref, rtol=1e-5, atol=1e-5) + assert inp.grad.shape == inp.shape + assert inp.grad[-1, -1] == 0 + + ''' _ _ _ _ _ _ _ __ _ __ _ _ _____ _| |_ ___ _ _ __(_)___ _ _