Skip to content

Commit

Permalink
equal => allclose (#6164)
Browse files Browse the repository at this point in the history
Co-authored-by: oneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com>
  • Loading branch information
jackalcooper and oneflow-ci-bot committed Sep 5, 2021
1 parent 5520c1f commit c194af4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 65 deletions.
56 changes: 28 additions & 28 deletions python/oneflow/test/tensor/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_numpy_and_default_dtype(test_case):
flow.nn.init.ones_(tensor)
test_case.assertTrue(tensor.dtype == flow.float32)
test_case.assertTrue(
np.array_equal(tensor.numpy(), np.ones(shape, dtype=np.float32))
np.allclose(tensor.numpy(), np.ones(shape, dtype=np.float32))
)

@flow.unittest.skip_unless_1n1d()
Expand All @@ -49,43 +49,43 @@ def test_tensor_property(test_case):
def test_copy_to_and_from_numpy(test_case):
np_arr = np.array([4, 6], dtype=np.float32)
tensor = flow.tensor(np_arr, dtype=flow.float32)
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))
test_case.assertEqual(np.float32, tensor.numpy().dtype)
np_arr = np.array([4, 6], dtype=np.int32)
tensor = flow.tensor(np_arr, dtype=flow.int32)
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))
test_case.assertEqual(np.int32, tensor.numpy().dtype)

@flow.unittest.skip_unless_1n1d()
def test_inplace_copy_from_contiguous_numpy(test_case):
np_arr = np.arange(6).reshape(3, 2)
tensor = flow.zeros(3, 2).to(flow.int64)
tensor.copy_(np_arr)
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))

@flow.unittest.skip_unless_1n1d()
def test_inplace_copy_from_non_contiguous_numpy(test_case):
np_arr = np.arange(6).reshape(2, 3).transpose(1, 0)
tensor = flow.zeros(3, 2).to(flow.int64)
tensor.copy_(np_arr)
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))

@flow.unittest.skip_unless_1n1d()
def test_construct_from_numpy_or_list(test_case):
shape = (2, 3, 4, 5)
np_arr = np.random.rand(*shape).astype(np.float32)
tensor = flow.tensor(np_arr)
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))
np_int_arr = np.random.randint(-100, high=100, size=shape, dtype=np.int32)
tensor = flow.tensor(np_int_arr, dtype=flow.int32)
test_case.assertEqual(tensor.dtype, flow.int32)
test_case.assertTrue(np_arr.flags["C_CONTIGUOUS"])
test_case.assertTrue(np.array_equal(tensor.numpy(), np_int_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_int_arr))
np_arr = np.random.random((1, 256, 256, 3)).astype(np.float32)
np_arr = np_arr.transpose(0, 3, 1, 2)
tensor = flow.tensor(np_arr)
test_case.assertFalse(np_arr.flags["C_CONTIGUOUS"])
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))

@flow.unittest.skip_unless_1n1d()
def test_construct_from_another_tensor(test_case):
Expand All @@ -94,7 +94,7 @@ def test_construct_from_another_tensor(test_case):
tensor = flow.tensor(np_arr)
output = flow.tensor(tensor)
test_case.assertEqual(output.dtype, flow.float32)
test_case.assertTrue(np.array_equal(output.numpy(), np_arr))
test_case.assertTrue(np.allclose(output.numpy(), np_arr))

def _test_tensor_init_methods(test_case, tensor_creator, get_numpy):
shape = (2, 3, 4, 5)
Expand All @@ -105,9 +105,9 @@ def _test_tensor_init_methods(test_case, tensor_creator, get_numpy):
x.fill_(random_fill_val)
test_case.assertTrue(np.allclose(get_numpy(x), random_fill_val * np_ones))
flow.nn.init.ones_(x)
test_case.assertTrue(np.array_equal(get_numpy(x), np_ones))
test_case.assertTrue(np.allclose(get_numpy(x), np_ones))
flow.nn.init.zeros_(x)
test_case.assertTrue(np.array_equal(get_numpy(x), np_zeros))
test_case.assertTrue(np.allclose(get_numpy(x), np_zeros))
flow.nn.init.constant_(x, random_fill_val)
test_case.assertTrue(np.allclose(get_numpy(x), random_fill_val * np_ones))
z = tensor_creator(5, 4, 3, 2)
Expand All @@ -123,9 +123,9 @@ def _test_tensor_init_methods(test_case, tensor_creator, get_numpy):
x.fill_(random_fill_val)
test_case.assertTrue(np.allclose(get_numpy(x), random_fill_val * np_ones))
flow.nn.init.ones_(x)
test_case.assertTrue(np.array_equal(get_numpy(x), np_ones))
test_case.assertTrue(np.allclose(get_numpy(x), np_ones))
flow.nn.init.zeros_(x)
test_case.assertTrue(np.array_equal(get_numpy(x), np_zeros))
test_case.assertTrue(np.allclose(get_numpy(x), np_zeros))
flow.nn.init.constant_(x, random_fill_val)
test_case.assertTrue(np.allclose(get_numpy(x), random_fill_val * np_ones))
test_case.assertEqual(flow.nn.init.calculate_gain("conv2d"), 1)
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_tensor_register_hook(test_case):
x.register_hook(lambda _: new_grad)
y = x.sum() + (x * 2).sum()
y.backward()
test_case.assertTrue(np.array_equal(x.grad.numpy(), new_grad.numpy()))
test_case.assertTrue(np.allclose(x.grad.numpy(), new_grad.numpy()))
grad_nonlocal = None

def assign_nonlocal_variable_and_return_none(grad):
Expand All @@ -271,7 +271,7 @@ def assign_nonlocal_variable_and_return_none(grad):
x.register_hook(assign_nonlocal_variable_and_return_none)
y = x.sum() + (x * 2).sum()
y.backward()
test_case.assertTrue(np.array_equal(grad_nonlocal.numpy(), np.ones(shape) * 3))
test_case.assertTrue(np.allclose(grad_nonlocal.numpy(), np.ones(shape) * 3))

@flow.unittest.skip_unless_1n1d()
def test_user_defined_data(test_case):
Expand All @@ -281,9 +281,9 @@ def test_user_defined_data(test_case):
x = flow.Tensor(list_data)
y = flow.Tensor(tuple_data)
z = flow.Tensor(numpy_data)
test_case.assertTrue(np.array_equal(x.numpy(), 5 * np.ones(x.shape)))
test_case.assertTrue(np.array_equal(y.numpy(), 5 * np.ones(y.shape)))
test_case.assertTrue(np.array_equal(z.numpy(), 5 * np.ones(z.shape)))
test_case.assertTrue(np.allclose(x.numpy(), 5 * np.ones(x.shape)))
test_case.assertTrue(np.allclose(y.numpy(), 5 * np.ones(y.shape)))
test_case.assertTrue(np.allclose(z.numpy(), 5 * np.ones(z.shape)))

@flow.unittest.skip_unless_1n1d()
def test_mirrored_tensor_and_op(test_case):
Expand All @@ -303,7 +303,7 @@ def test_mirrored_tensor_and_op(test_case):
)
y = op(x1, x2)[0]
test_case.assertTrue(
np.array_equal(y.numpy(), np.array([[5.0]], dtype=np.float32))
np.allclose(y.numpy(), np.array([[5.0]], dtype=np.float32))
)

@flow.unittest.skip_unless_1n1d()
Expand Down Expand Up @@ -350,7 +350,7 @@ def __getitem__(self, key):

def compare_getitem_with_numpy(tensor, slices):
np_arr = tensor.numpy()
test_case.assertTrue(np.array_equal(np_arr[slices], tensor[slices].numpy()))
test_case.assertTrue(np.allclose(np_arr[slices], tensor[slices].numpy()))

def compare_setitem_with_numpy(tensor, slices, value):
np_arr = tensor.numpy()
Expand All @@ -360,7 +360,7 @@ def compare_setitem_with_numpy(tensor, slices, value):
np_value = value
np_arr[slices] = np_value
tensor[slices] = value
test_case.assertTrue(np.array_equal(np_arr, tensor.numpy()))
test_case.assertTrue(np.allclose(np_arr, tensor.numpy()))

x = flow.Tensor(5, 5)
v = flow.Tensor([[0, 1, 2, 3, 4]])
Expand Down Expand Up @@ -458,7 +458,7 @@ def test_argwhere(test_case):
of_out = input.argwhere()
np_out = np.argwhere(np_input)
test_case.assertTrue(np.allclose(of_out.numpy(), np_out, precision, precision))
test_case.assertTrue(np.array_equal(of_out.numpy().shape, np_out.shape))
test_case.assertTrue(np.allclose(of_out.numpy().shape, np_out.shape))

@flow.unittest.skip_unless_1n1d()
@autotest(n=5, auto_backward=False)
Expand Down Expand Up @@ -723,14 +723,14 @@ def test_zeros_(test_case):
shape = (2, 3)
x = flow.tensor(np.random.randn(*shape), dtype=flow.float32)
x.zeros_()
test_case.assertTrue(np.array_equal(x.numpy(), np.zeros(shape)))
test_case.assertTrue(np.allclose(x.numpy(), np.zeros(shape)))

@flow.unittest.skip_unless_1n1d()
def test_construct_small_tensor(test_case):
shape = (2, 3, 4, 5)
np_arr = np.random.rand(*shape).astype(np.float32)
tensor = flow.tensor(np_arr)
test_case.assertTrue(np.array_equal(tensor.numpy(), np_arr))
test_case.assertTrue(np.allclose(tensor.numpy(), np_arr))
test_case.assertEqual(tensor.dtype, flow.float32)
np_int_arr = np.random.randint(-100, high=100, size=shape, dtype=np.int32)
tensor = flow.tensor(np_int_arr, dtype=flow.int32)
Expand All @@ -744,7 +744,7 @@ def test_construct_small_tensor(test_case):
tuple_data = ((1, 2, 5), (4, 3, 10))
tensor = flow.tensor(tuple_data)
test_case.assertEqual(tensor.dtype, flow.int64)
test_case.assertTrue(np.array_equal(tensor.numpy(), np.array(tuple_data)))
test_case.assertTrue(np.allclose(tensor.numpy(), np.array(tuple_data)))
scalar = 5.5
tensor = flow.tensor(scalar)
test_case.assertEqual(tensor.dtype, flow.float32)
Expand Down Expand Up @@ -773,7 +773,7 @@ def _test_tensor_reshape(test_case):
input = flow.tensor(x)
of_shape = input.reshape(2, 2, 2, -1).numpy().shape
np_shape = (2, 2, 2, 2)
test_case.assertTrue(np.array_equal(of_shape, np_shape))
test_case.assertTrue(np.allclose(of_shape, np_shape))

@autotest()
def test_reshape_tensor_with_random_data(test_case):
Expand Down Expand Up @@ -835,7 +835,7 @@ def test_tensor_equal(test_case):
other = flow.tensor(arr2, dtype=flow.float32)
of_out = input.eq(other)
np_out = np.equal(arr1, arr2)
test_case.assertTrue(np.array_equal(of_out.numpy(), np_out))
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))

@flow.unittest.skip_unless_1n1d()
def test_tensor_detach(test_case):
Expand All @@ -854,7 +854,7 @@ def _test_cast_tensor_function(test_case):
input = flow.tensor(np_arr, dtype=flow.float32)
output = input.cast(flow.int8)
np_out = np_arr.astype(np.int8)
test_case.assertTrue(np.array_equal(output.numpy(), np_out))
test_case.assertTrue(np.allclose(output.numpy(), np_out))

def _test_sin_tensor_function(test_case, shape, device):
input = flow.Tensor(np.random.randn(2, 3, 4, 5))
Expand Down
70 changes: 33 additions & 37 deletions python/oneflow/test/tensor/test_tensor_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,79 +26,77 @@
def test_basic_slice(test_case, numpy_x):
x = flow.tensor(numpy_x)

test_case.assertTrue(np.array_equal(numpy_x[1], x[1].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[-2], x[-2].numpy()))
test_case.assertTrue(np.allclose(numpy_x[1], x[1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[-2], x[-2].numpy()))

test_case.assertTrue(np.array_equal(numpy_x[0, 1], x[0, 1].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[(0, 1)], x[(0, 1)].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[((0, 1))], x[((0, 1))].numpy()))
test_case.assertTrue(np.allclose(numpy_x[0, 1], x[0, 1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[(0, 1)], x[(0, 1)].numpy()))
test_case.assertTrue(np.allclose(numpy_x[((0, 1))], x[((0, 1))].numpy()))

test_case.assertTrue(np.array_equal(numpy_x[None], x[None].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[True], x[True].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[1, None], x[1, None].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[1, None, 1], x[1, None, 1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[None], x[None].numpy()))
test_case.assertTrue(np.allclose(numpy_x[True], x[True].numpy()))
test_case.assertTrue(np.allclose(numpy_x[1, None], x[1, None].numpy()))
test_case.assertTrue(np.allclose(numpy_x[1, None, 1], x[1, None, 1].numpy()))
test_case.assertTrue(
np.array_equal(numpy_x[1, None, None, 1], x[1, None, None, 1].numpy())
np.allclose(numpy_x[1, None, None, 1], x[1, None, None, 1].numpy())
)

test_case.assertTrue(np.array_equal(numpy_x[:], x[:].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[:1], x[:1].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[0:1], x[0:1].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[-2:-1], x[-2:-1].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[2:100:200], x[2:100:200].numpy()))
test_case.assertTrue(np.allclose(numpy_x[:], x[:].numpy()))
test_case.assertTrue(np.allclose(numpy_x[:1], x[:1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[0:1], x[0:1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[-2:-1], x[-2:-1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[2:100:200], x[2:100:200].numpy()))

test_case.assertTrue(np.array_equal(numpy_x[0:2, ...], x[0:2, ...].numpy()))
test_case.assertTrue(np.array_equal(numpy_x[0:2, ..., 1], x[0:2, ..., 1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[0:2, ...], x[0:2, ...].numpy()))
test_case.assertTrue(np.allclose(numpy_x[0:2, ..., 1], x[0:2, ..., 1].numpy()))
test_case.assertTrue(
np.array_equal(numpy_x[0:2, ..., 1, 1], x[0:2, ..., 1, 1].numpy())
np.allclose(numpy_x[0:2, ..., 1, 1], x[0:2, ..., 1, 1].numpy())
)

test_case.assertTrue(np.array_equal(numpy_x[0:4:2, ...], x[0:4:2, ...].numpy()))
test_case.assertTrue(np.allclose(numpy_x[0:4:2, ...], x[0:4:2, ...].numpy()))
test_case.assertTrue(
np.array_equal(numpy_x[0:2, None, ..., True], x[0:2, None, ..., True].numpy())
np.allclose(numpy_x[0:2, None, ..., True], x[0:2, None, ..., True].numpy())
)
test_case.assertTrue(
np.array_equal(
numpy_x[None, ..., 0:4:2, True], x[None, ..., 0:4:2, True].numpy()
)
np.allclose(numpy_x[None, ..., 0:4:2, True], x[None, ..., 0:4:2, True].numpy())
)


def test_advanced_indexing(test_case, numpy_x):
x = flow.tensor(numpy_x)

test_case.assertTrue(np.array_equal(numpy_x[[0, 1]], x[[0, 1]].numpy()))
test_case.assertTrue(np.allclose(numpy_x[[0, 1]], x[[0, 1]].numpy()))
test_case.assertTrue(
np.array_equal(numpy_x[[0, 1], [1, 0]], x[[0, 1], [1, 0]].numpy())
np.allclose(numpy_x[[0, 1], [1, 0]], x[[0, 1], [1, 0]].numpy())
)
test_case.assertTrue(
np.array_equal(
np.allclose(
numpy_x[[[0, 1], [0, 1], [1, 0]]], x[[[0, 1], [0, 1], [1, 0]]].numpy()
)
)
test_case.assertTrue(np.array_equal(numpy_x[[[0], [1]]], x[[[0], [1]]].numpy()))
test_case.assertTrue(np.allclose(numpy_x[[[0], [1]]], x[[[0], [1]]].numpy()))
test_case.assertTrue(
np.array_equal(
np.allclose(
numpy_x[[[[0], [1]], [[0], [1]], [0, 1]]],
x[[[[0], [1]], [[0], [1]], [0, 1]]].numpy(),
)
)
test_case.assertTrue(
np.array_equal(
np.allclose(
numpy_x[[[[0, 1], [1, 1]], [[0, 0], [1, 1]], [0, 1]]],
x[[[[0, 1], [1, 1]], [[0, 0], [1, 1]], [0, 1]]].numpy(),
)
)

# Tensor index
test_case.assertTrue(
np.array_equal(
np.allclose(
numpy_x[np.array([0, 1]), np.array([1, 0])],
x[flow.tensor([0, 1]), flow.tensor([1, 0])].numpy(),
)
)
test_case.assertTrue(
np.array_equal(
np.allclose(
numpy_x[:, np.array([[0, 1], [1, 1]]), np.array([[1, 0], [1, 1]])],
x[:, flow.tensor([[0, 1], [1, 1]]), flow.tensor([[1, 0], [1, 1]]),].numpy(),
)
Expand All @@ -109,16 +107,14 @@ def test_combining_indexing(test_case, numpy_x):
x = flow.tensor(numpy_x)

test_case.assertTrue(
np.array_equal(numpy_x[[0, 1], 1:2, [1, 0]], x[[0, 1], 1:2, [1, 0]].numpy())
np.allclose(numpy_x[[0, 1], 1:2, [1, 0]], x[[0, 1], 1:2, [1, 0]].numpy())
)
test_case.assertTrue(
np.array_equal(numpy_x[:, [0, 1], [1, 0]], x[:, [0, 1], [1, 0]].numpy())
np.allclose(numpy_x[:, [0, 1], [1, 0]], x[:, [0, 1], [1, 0]].numpy())
)
test_case.assertTrue(np.array_equal(numpy_x[:, [0, 1], 1], x[:, [0, 1], 1].numpy()))
test_case.assertTrue(np.allclose(numpy_x[:, [0, 1], 1], x[:, [0, 1], 1].numpy()))
test_case.assertTrue(
np.array_equal(
numpy_x[..., [0, 1], 1, [1, 0]], x[..., [0, 1], 1, [1, 0]].numpy()
)
np.allclose(numpy_x[..., [0, 1], 1, [1, 0]], x[..., [0, 1], 1, [1, 0]].numpy())
)


Expand Down

0 comments on commit c194af4

Please sign in to comment.