def test_tl_from_list_of_tensors_different_dtypes(): np_types = [np.float32, np.float16, np.int16, np.int8, np.uint16, np.uint8] for dtypes in np.random.choice(np_types, size=(3, 2), replace=False): t1 = TensorCPU(np.zeros((1), dtype=dtypes[0])) t2 = TensorCPU(np.zeros((1), dtype=dtypes[1])) with assert_raises( TypeError, glob= f"Tensors cannot have different data types. Tensor at position " "1 has type '*' expected to have type '*'."): TensorListCPU([t1, t2])
def test_tl_from_list_of_tensors_different_backends(): t1 = TensorCPU(np.zeros((1))) t2 = TensorCPU(np.zeros((1)))._as_gpu() with assert_raises( TypeError, glob='Object at position 1 cannot be converted to TensorCPU'): TensorListCPU([t1, t2]) with assert_raises( TypeError, glob='Object at position 1 cannot be converted to TensorGPU'): TensorListGPU([t2, t1])
def check_squeeze(shape, dim, in_layout, expected_out_layout): arr = np.random.rand(*shape) t = TensorCPU(arr, in_layout) is_squeezed = t.squeeze(dim) should_squeeze = (len(expected_out_layout) < len(in_layout)) arr_squeeze = arr.squeeze(dim) t_shape = tuple(t.shape()) assert t_shape == arr_squeeze.shape, f"{t_shape} != {arr_squeeze.shape}" assert t.layout( ) == expected_out_layout, f"{t.layout()} != {expected_out_layout}" assert np.allclose(arr_squeeze, np.array(t)) assert is_squeezed == should_squeeze, f"{is_squeezed} != {should_squeeze}"
def test_tl_from_list_of_tensors_different_shapes(): shapes = [(1, 2, 3), (4, 5, 6), (128, 128, 128), (8, 8, 8), (13, 47, 131)] for size in [10, 5, 36, 1]: np_arrays = [ np.random.rand(*shapes[i]) for i in np.random.choice(range(len(shapes)), size=size) ] tl_cpu = TensorListCPU([TensorCPU(a) for a in np_arrays]) tl_gpu = TensorListGPU([TensorCPU(a)._as_gpu() for a in np_arrays]) for arr, tensor_cpu, tensor_gpu in zip(np_arrays, tl_cpu, tl_gpu): np.testing.assert_array_equal(arr, tensor_cpu) np.testing.assert_array_equal(arr, tensor_gpu.as_cpu())
def test_tl_from_list_of_tensors_same_shape(): for shape in [(10, 1), (4, 5, 6), (13, 1), (1, 1)]: arr = np.random.rand(*shape) tl_cpu_from_np = TensorListCPU(arr) tl_cpu_from_tensors = TensorListCPU([TensorCPU(a) for a in arr]) np.testing.assert_array_equal(tl_cpu_from_np.as_array(), tl_cpu_from_tensors.as_array()) tl_gpu_from_np = tl_cpu_from_np._as_gpu() tl_gpu_from_tensors = TensorListGPU( [TensorCPU(a)._as_gpu() for a in arr]) np.testing.assert_array_equal(tl_gpu_from_np.as_cpu().as_array(), tl_gpu_from_tensors.as_cpu().as_array())
def test_empty_tensor_tensorlist(): arr = np.array([], dtype=np.float32) tensor = TensorCPU(arr, "NHWC") tensorlist = TensorListCPU(arr, "NHWC") assert_array_equal(np.array(tensor), tensorlist.as_array()) assert (np.array(tensor).shape == (0, )) assert (tensorlist.as_array().shape == (0, ))
def test_dtype_deprecation_warning(): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") TensorCPU(np.empty((0))).dtype() assert "Calling '.dtype()' is deprecated, please use '.dtype' instead" == str( w[-1].message)
def test_tensorlist_dtype(): dali_types = types._all_types np_types = list(map(dali_type_to_np, dali_types)) for dali_type, np_type in zip(dali_types, np_types): tl = TensorListCPU([TensorCPU(np.zeros((1), dtype=np_type))]) assert tl.dtype == dali_type assert tl._as_gpu().dtype == dali_type
def test_data_ptr_tensor_cpu(): arr = np.random.rand(3, 5, 6) tensor = TensorCPU(arr, "NHWC") from_tensor = py_buffer_from_address(tensor.data_ptr(), tensor.shape(), types.to_numpy_type(tensor.dtype)) assert (np.array_equal(arr, from_tensor))
def test_tensor_str_sample(): arr = np.arange(16) t = TensorCPU(arr) params = [arr, 'DALIDataType.INT64', [16]] _test_str(t, params, _expected_tensor_str)
def test_tensor_str_empty(): t = TensorCPU(np.empty(0)) params = [[], 'DALIDataType.FLOAT64', [0]] _test_str(t, params, _expected_tensor_str)
def test_create_tensor(): arr = np.random.rand(3, 5, 6) tensor = TensorCPU(arr, "NHWC") assert_array_equal(arr, np.array(tensor))
def test_dtype_placeholder_equivalence(): dali_types = types._all_types np_types = list(map(dali_type_to_np, dali_types)) for dali_type, np_type in zip(dali_types, np_types): assert TensorCPU(np.zeros((1), dtype=np_type)).dtype == dali_type
def check_array_types(t): arr = np.array([[-0.39, 1.5], [-1.5, 0.33]], dtype=t) tensor = TensorCPU(arr, "NHWC") assert (np.allclose(np.array(arr), np.asanyarray(tensor)))