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

Large Vector tests for DGL Ops Part 2 #16497

Merged
merged 8 commits into from
Oct 24, 2019
118 changes: 115 additions & 3 deletions tests/nightly/test_large_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ def test_concat():
a = nd.ones(LARGE_X)
b = nd.zeros(LARGE_X)
c = nd.concat(a, b, dim=0)
assert c[0][0] == 1
assert c[-1][-1] == 0
assert c[0] == 1
assert c[-1] == 0
assert c.shape[0] == (2 * LARGE_X)


Expand Down Expand Up @@ -708,6 +708,70 @@ def test_full():
assert a[-1] == 3


def test_sign():
a = mx.nd.random.normal(-1, 1, shape=LARGE_X)
mx_res = mx.nd.sign(a)
assert_almost_equal(mx_res[-1].asnumpy(), np.sign(a[-1].asnumpy()))


def test_logical():
def check_logical_and(a, b):
mx_res = mx.nd.logical_and(a, b)
assert_almost_equal(mx_res[-1].asnumpy(), np.logical_and(a[-1].asnumpy(), b[-1].asnumpy()))

def check_logical_or(a, b):
mx_res = mx.nd.logical_or(a, b)
assert_almost_equal(mx_res[-1].asnumpy(), np.logical_and(a[-1].asnumpy(), b[-1].asnumpy()))

def check_logical_not(a, b):
mx_res = mx.nd.logical_not(a, b)
assert_almost_equal(mx_res[-1].asnumpy(), np.logical_and(a[-1].asnumpy(), b[-1].asnumpy()))

def check_logical_xor(a, b):
mx_res = mx.nd.logical_xor(a, b)
assert_almost_equal(mx_res[-1].asnumpy(), np.logical_and(a[-1].asnumpy(), b[-1].asnumpy()))
ChaiBapchya marked this conversation as resolved.
Show resolved Hide resolved

a = mx.nd.ones(LARGE_X)
b = mx.nd.zeros(LARGE_X)
check_logical_and(a, b)
check_logical_or(a, b)
check_logical_not(a, b)
check_logical_xor(a, b)


def test_regression():
shape = (LARGE_X, )

def check_regression(symbol, forward, shape):
# init executor
data_s = mx.symbol.Variable('data')
label_s = mx.symbol.Variable('label')
out_s = symbol(data=data_s, label=label_s)
exe = out_s.simple_bind(ctx=mx.cpu(0), data=shape, label=shape)

arg_map = dict(zip(out_s.list_arguments(), exe.arg_arrays))

# init data
data = mx.random.uniform(-1, -1, shape)
arg_map["data"][:] = data
atol = 1e-5
density = 0.5
stype = 'default'
label = arg_map["label"]
label[:] = rand_ndarray(shape, stype, density=density)
exe.forward(is_train=True)
exe.backward()
np_out = forward(data.asnumpy())
assert_almost_equal(exe.outputs[0].asnumpy(), np_out, atol=atol)

check_regression(mx.symbol.LogisticRegressionOutput,
lambda x: 1.0 / (1.0 + np.exp(-x)),
shape)
check_regression(mx.symbol.LinearRegressionOutput,
lambda x: x,
shape)


def test_astype():
x = create_vector(size=LARGE_X//4)
x = nd.tile(x, 4)
Expand Down Expand Up @@ -750,7 +814,7 @@ def assert_correctness_of_rounding_ops(output, mid, expected_vals):

def test_rounding_ops():
x = create_input_for_rounding_ops()

def check_ceil():
y = nd.ceil(x)
# expected ouput for middle 5 values after applying ceil()
Expand Down Expand Up @@ -852,6 +916,48 @@ def check_tan():
expected_output = [-.577, -1, 0, 1, .577]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_arcsinh():
x = create_input_for_trigonometric_ops([-np.pi/2, -np.pi/4, 0, np.pi/4, np.pi/2])
y = nd.arcsinh(x)
# expected ouput for indices=(0, 1, -3, -2, -1) after applying arcsinh()
expected_output = [np.arcsinh(-np.pi/2), np.arcsinh(-np.pi/4), 0, np.arcsinh(np.pi/4), np.arcsinh(np.pi/2)]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_arccosh():
x = create_input_for_trigonometric_ops([1, np.pi/2, 3*np.pi/4, np.pi, 5*np.pi/4])
y = nd.arccosh(x)
# expected ouput for indices=(0, 1, -3, -2, -1) after applying arccosh()
expected_output = [0, np.arccosh(np.pi/2), np.arccosh(3*np.pi/4), np.arccosh(np.pi), np.arccosh(5*np.pi/4)]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_arctanh():
x = create_input_for_trigonometric_ops([-1/4, -1/2, 0, 1/4, 1/2])
y = nd.arctanh(x)
# expected ouput for indices=(0, 1, -3, -2, -1) after applying arctanh()
expected_output = [np.arctanh(-1/4), np.arctanh(-1/2), 0, np.arctanh(1/4), np.arctanh(1/2)]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_sinh():
x = create_input_for_trigonometric_ops([-np.pi/2, -np.pi/4, 0, np.pi/4, np.pi/2])
y = nd.sinh(x)
# expected ouput for indices=(0, 1, -3, -2, -1) after applying sinh()
expected_output = [np.sinh(-np.pi/2), np.sinh(-np.pi/4), 0, np.sinh(np.pi/4), np.sinh(np.pi/2)]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_cosh():
x = create_input_for_trigonometric_ops([0, 1, np.pi/2, 3*np.pi/4, np.pi])
y = nd.cosh(x)
# expected ouput for indices=(0, 1, -3, -2, -1) after applying cosh()
expected_output = [1, np.cosh(1), np.cosh(np.pi/2), np.cosh(3*np.pi/4), np.cosh(np.pi)]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_tanh():
x = create_input_for_trigonometric_ops([-1/4, -1/2, 0, 1/4, 1/2])
y = nd.tanh(x)
# expected ouput for indices=(0, 1, -3, -2, -1) after applying tanh()
expected_output = [np.tanh(-1/4), np.tanh(-1/2), 0, np.tanh(1/4), np.tanh(1/2)]
assert_correctness_of_trigonometric_ops(y, expected_output)

def check_radians():
x = create_input_for_trigonometric_ops([0, 90, 180, 270, 360])
y = nd.radians(x)
Expand All @@ -872,6 +978,12 @@ def check_degrees():
check_sin()
check_cos()
check_tan()
check_arcsinh()
check_arccosh()
check_arctanh()
check_sinh()
check_cosh()
check_tanh()
check_radians()
check_degrees()

Expand Down