heat.testing.basic_test
Module for TestCases used for heat
Module Contents
- class TestCase(methodName='runTest')[source]
Bases:
unittest.TestCaseHelper functions for unit tests
- __comm
- device :heat.core.Device
- _hostnames :Optional[list[str]] = None
- other_device :Optional[heat.core.Device] = None
- envar :Optional[str] = None
- setUpClass() None[source]
Read the environment variable ‘HEAT_TEST_USE_DEVICE’ and return the requested devices. Supported values
cpu: Use CPU only (default)
gpu: Use GPU only
- Raises:
RuntimeError if value of 'HEAT_TEST_USE_DEVICE' is not recognized –
- assert_array_equal(heat_array: heat.DNDarray, expected_array: numpy.ndarray | torch.Tensor, rtol: float = 1e-05, atol: float = 1e-08) None[source]
Check if the heat_array is equivalent to the expected_array. Therefore first the split heat_array is compared to the corresponding expected_array slice locally and second the heat_array is combined and fully compared with the expected_array. Note if the heat array is split it also needs to be balanced.
- Parameters:
heat_array (heat.DNDarray) – The heat array which should be checked.
expected_array (numpy.ndarray or torch.Tensor) – The array against which the heat_array should be checked.
rtol (float) – The relative tolerance parameter.
atol (float) – The absolute tolerance parameter.
- Raises:
AssertionError if the arrays do not equal. –
Examples
>>> import numpy as np >>> import heat as ht >>> a = ht.ones((5, 5), split=1, dtype=ht.int32) >>> b = np.ones((5, 5), dtype=np.int32) >>> self.assert_array_equal(a, b)
>>> c = np.ones((5, 5), dtype=np.int64) >>> self.assert_array_equal(a, c) AssertionError: [...] >>> c = np.zeros((5, 5), dtype=np.int32) >>> self.assert_array_equal(a, c) AssertionError: [...]
- assert_func_equal(shape: tuple[Any, Ellipsis] | list[Any], heat_func: Callable[Ellipsis, Any], numpy_func: Callable[Ellipsis, Any], distributed_result: bool = True, heat_args: dict[str, Any] | None = None, numpy_args: dict[str, Any] | None = None, data_types: tuple[type, Ellipsis] = (np.int32, np.int64, np.float32, np.float64), low: int = -10000, high: int = 10000) None[source]
Creates random tensors of the given shape with different data types. All of these tensors will be tested with ht.assert_func_equal_for_tensor.
- Parameters:
shape (tuple or list) – The shape of which a random tensors will be created and tested against
heat_func (function) – The function that is to be tested
numpy_func (function) – The numpy implementation of an equivalent function to test against
heat_args (dictionary, optional) – The keyword arguments that will be passed to the heat function. Array and split function don’t need to be specified. Default is {}.
numpy_args (dictionary, optional) – The keyword arguments that will be passed to the numpy function. Array doesn’t need to be specified. Default is {}.
distributed_result (bool, optional) – Specify whether the result of the heat function is distributed across all nodes or all nodes have the full result. Default is True.
data_types (list of numpy dtypes, optional) – Tensors with all of these dtypes will be created and tested. Each type must to be a numpy dtype. Default is [numpy.int32, numpy.int64, numpy.float32, numpy.float64]
low (int, optional) – In case one of the data_types has integer types, this is the lower bound for the random values. Default is -10000
high (int, optional) – In case one of the data_types has integer types, this is the upper bound for the random values. Default is 10000
- Raises:
AssertionError if the functions do not perform equally. –
Examples
>>> import numpy as np >>> import heat as ht >>> self.assert_func_equal((2, 2), ht.exp, np.exp)
>>> self.assert_func_equal((2, 2), ht.exp, np.log) AssertionError: [...] >>> self.assert_func_equal((1, 3, 5), ht.any, np.any, distributed_result=False)
>>> heat_args = {"sorted": True, "axis": 0} >>> numpy_args = {"axis": 0} >>> self.assert_func_equal( ... [5, 5, 5, 5], ht.unique, np.unique, heat_arg=heat_args, numpy_args=numpy_args ... )
- assert_func_equal_for_tensor(tensor: numpy.ndarray | torch.Tensor, heat_func: Callable[Ellipsis, Any], numpy_func: Callable[Ellipsis, Any], heat_args: dict[str, Any] | None = None, numpy_args: dict[str, Any] | None = None, distributed_result: bool = True) None[source]
Tests if the heat function and the numpy function create the equal result on the given tensor.
- Parameters:
tensor (torch.Tensor or numpy.ndarray) – The tensor on which the heat function will be executed.
heat_func (function) – The function that is to be tested
numpy_func (function) – The numpy implementation of an equivalent function to test against
heat_args (dictionary, optional) – The keyword arguments that will be passed to the heat function. Array and split function don’t need to be specified. Default is {}.
numpy_args (dictionary, optional) – The keyword arguments that will be passed to the numpy function. Array doesn’t need to be specified. Default is {}.
distributed_result (bool, optional) – Specify whether the result of the heat function is distributed across all nodes or all nodes have the full result. Default is True.
- Raises:
AssertionError if the functions to not perform equally. –
Examples
>>> import numpy as np >>> import heat as ht >>> a = np.arange(10) >>> self.assert_func_equal_for_tensor(a, ht.exp, np.exp)
>>> self.assert_func_equal_for_tensor(a, ht.exp, np.log) AssertionError: [...] >>> self.assert_func_equal_for_tensor(a, ht.any, np.any, distributed_result=False)
>>> a = torch.ones([5, 5, 5, 5]) >>> heat_args = {"sorted": True, "axis": 0} >>> numpy_args = {"axis": 0} >>> self.assert_func_equal_for_tensor( ... a, ht.unique, np.unique, heat_arg=heat_args, numpy_args=numpy_args ... )
- assertTrue_memory_layout(tensor: heat.DNDarray, order: str) None[source]
Checks that the memory layout of a given heat tensor is as specified by argument order.
- Parameters:
tensor (DNDarray) – The input array.
order (str) – ‘C’ for C-like (row-major), ‘F’ for Fortran-like (column-major) memory layout.
- __create_random_np_array(shape: list[Any] | tuple[Any], dtype: type = np.float32, low: int = -10000, high: int = 10000) numpy.ndarray
Creates a random array based on the input parameters. The used seed will be printed to stdout for debugging purposes.
- Parameters:
shape (list or tuple) – The shape of the random array to be created.
dtype (np.dtype, optional) – The datatype of the resulting array. If dtype is subclass of np.floating then numpy.random.randn is used to create random values. If dtype is subclass of np.integer then numpy.random.randint is called with the low and high argument to create the random values. Default is numpy.float64
low (int, optional) – In case dtype is an integer type, this is the lower bound for the random values. Default is -10000
high (int, optional) – In case dtype is an integer type, this is the upper bound for the random values. Default is 10000
- Returns:
res – An array of random values with the specified shape and dtype.
- Return type:
numpy.ndarray
- Raises:
ValueError if the dtype is not a subtype of numpy.integer or numpy.floating –
- get_tmpdir()[source]
Create a temporary directory in the current working directory. When running with multiple tasks, only rank 0 creates and destroys the directory, but the path is communicated across all ranks. The directory is cleaned up / deleted when the variable tmpdir is garbage collected on rank 0.
- Returns:
path (str) – Path to the temporary directory
tmpdir (object or None) – On rank 0: A Python object administering the temporary directory, on other ranks, None.