-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-1401] adding more operators to test support for Large Tensor #14944
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,16 @@ def test_ndarray_ones(): | |
assert nd.sum(a).asnumpy() == LARGE_SIZE | ||
|
||
|
||
def test_ndarray_convert(): | ||
a = nd.zeros(shape=(LARGE_X, SMALL_Y)) | ||
b = a.astype(np.int32) | ||
b.wait_to_read() | ||
assert b.dtype == np.int32 | ||
b = a.tostype('row_sparse') | ||
b.wait_to_read() | ||
assert isinstance(b, mx.nd.sparse.RowSparseNDArray) | ||
|
||
|
||
@with_seed() | ||
def test_ndarray_random_uniform(): | ||
a = nd.random.uniform(shape=(LARGE_X, SMALL_Y)) | ||
|
@@ -115,10 +125,9 @@ def test_broadcast(): | |
|
||
|
||
def test_clip(): | ||
a = nd.arange(0, LARGE_X).reshape(LARGE_X, 1) | ||
b = nd.broadcast_to(a, shape=(a.shape[0], SMALL_Y)) | ||
res = nd.clip(b, a_min=100, a_max=1000) | ||
assert np.sum(res[-1].asnumpy() == 1000) == b.shape[1] | ||
a = nd.arange(0, LARGE_X * SMALL_Y).reshape(LARGE_X, SMALL_Y) | ||
res = nd.clip(a, a_min=100, a_max=1000) | ||
assert np.sum(res[-1].asnumpy() == 1000) == a.shape[1] | ||
|
||
|
||
def test_take(): | ||
|
@@ -128,6 +137,32 @@ def test_take(): | |
assert np.sum(res[-1].asnumpy() == 1) == res.shape[1] | ||
|
||
|
||
def test_split(): | ||
a = nd.arange(0, LARGE_X * SMALL_Y).reshape(LARGE_X, SMALL_Y) | ||
outs = nd.split(a, num_outputs=SMALL_Y, axis=1) | ||
result = sum(1 for i, v in enumerate(outs) if i == v[0].asnumpy()) | ||
assert result == a.shape[1] | ||
|
||
|
||
def test_argmin(): | ||
a = nd.arange(0, LARGE_X * SMALL_Y).reshape(LARGE_X, SMALL_Y) | ||
idx = mx.nd.argmin(a, axis=0) | ||
assert idx.shape[0] == SMALL_Y | ||
|
||
|
||
def test_tile(): | ||
a = nd.arange(0, LARGE_X).reshape(LARGE_X, 1) | ||
b = nd.tile(a, reps=(1, SMALL_Y)) | ||
assert np.sum(b[-1].asnumpy() == LARGE_X) == b.shape[1] | ||
|
||
|
||
def test_take(): | ||
a = nd.ones(shape=(LARGE_X, SMALL_Y)) | ||
idx = nd.arange(LARGE_X - 1000, LARGE_X) | ||
res = nd.take(a, idx) | ||
assert np.sum(res[-1].asnumpy() == 1) == res.shape[1] | ||
|
||
|
||
def test_slice(): | ||
a = nd.ones(shape=(LARGE_X, SMALL_Y)) | ||
res = nd.slice(a, begin=(LARGE_X-1000, 1), end=(LARGE_X, SMALL_Y)) | ||
|
@@ -171,14 +206,12 @@ def test_Dense(ctx=mx.cpu(0)): | |
|
||
def test_where(): | ||
a = nd.ones(shape=(LARGE_X, SMALL_Y)) | ||
b = nd.arange(0, LARGE_X).reshape(LARGE_X, 1) | ||
b = nd.broadcast_to(b, shape=(b.shape[0], SMALL_Y)) | ||
b = nd.arange(0, LARGE_X * SMALL_Y).reshape(LARGE_X, SMALL_Y) | ||
res = nd.where(b > 100, a, b) | ||
assert np.sum(res[-1].asnumpy() == 1) == b.shape[1] | ||
|
||
csr_cond = nd.sparse.cast_storage(b < 10, 'csr') | ||
res = nd.sparse.where(csr_cond, a, b) | ||
assert np.sum(res[0].asnumpy() == 1) == b.shape[1] | ||
assert np.sum(res[0].asnumpy() == 1) == 10 | ||
|
||
|
||
def test_pick(): | ||
|
@@ -187,6 +220,7 @@ def test_pick(): | |
res = mx.nd.pick(a,b) | ||
assert res.shape == b.shape | ||
|
||
|
||
def test_depthtospace(): | ||
def numpy_depth_to_space(x, blocksize): | ||
b, c, h, w = x.shape[0], x.shape[1], x.shape[2], x.shape[3] | ||
|
@@ -202,6 +236,7 @@ def numpy_depth_to_space(x, blocksize): | |
output = mx.nd.depth_to_space(data, 2) | ||
assert_almost_equal(output.asnumpy(), expected, atol=1e-3, rtol=1e-3) | ||
|
||
|
||
def test_spacetodepth(): | ||
def numpy_space_to_depth(x, blocksize): | ||
b, c, h, w = x.shape[0], x.shape[1], x.shape[2], x.shape[3] | ||
|
@@ -217,6 +252,33 @@ def numpy_space_to_depth(x, blocksize): | |
output = mx.nd.space_to_depth(data, 2) | ||
assert_almost_equal(output.asnumpy(), expected, atol=1e-3, rtol=1e-3) | ||
|
||
|
||
def test_diag(): | ||
h = np.random.randint(2,9) | ||
w = np.random.randint(2,9) | ||
a_np = np.random.random((LARGE_X, 64)).astype(np.float32) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to convert to np.float32 explicitly here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @apeforest by default
I am trying to keep it consistent with: /~https://github.com/apache/incubator-mxnet/blob/f680255fbff25818ed3eee0d4541d80b3c7b9d9d/tests/python/unittest/test_operator.py#L8029 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. mx.nd.array(a_np) is float32 by default.
I will remove |
||
a = mx.nd.array(a_np) | ||
|
||
# k == 0 | ||
r = mx.nd.diag(a) | ||
assert_almost_equal(r.asnumpy(), np.diag(a_np)) | ||
|
||
# k == 1 | ||
k = 1 | ||
r = mx.nd.diag(a, k=k) | ||
assert_almost_equal(r.asnumpy(), np.diag(a_np, k=k)) | ||
|
||
# k == -1 | ||
k = -1 | ||
r = mx.nd.diag(a, k=k) | ||
assert_almost_equal(r.asnumpy(), np.diag(a_np, k=k)) | ||
|
||
# random k | ||
k = np.random.randint(-min(LARGE_X, 64) + 1, min(h, w)) | ||
r = mx.nd.diag(a, k=k) | ||
assert_almost_equal(r.asnumpy(), np.diag(a_np, k=k)) | ||
|
||
|
||
if __name__ == '__main__': | ||
import nose | ||
nose.runmodule() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use randint? Can we just use deterministic values to test diag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apeforest : using random values is better since we are checking diagonal. Having different values actually ensures that the operation is performed correctly. Checking just the shape is fine but this ensures total correctness. I think if we can perform this check then it ensures total correctness