Verify that computed result matches expected result. Automatically detects whether arrays are NumPy or CuPy and uses the appropriate library without unnecessary data transfers. Parameters ---------- result : numpy.ndarray or cupy.ndarray Computed result array.
(
result, expected, rtol: float = 1e-5, atol: float = 1e-8, verbose: bool = True
)
| 64 | |
| 65 | |
| 66 | def verify_array_result( |
| 67 | result, expected, rtol: float = 1e-5, atol: float = 1e-8, verbose: bool = True |
| 68 | ) -> bool: |
| 69 | """ |
| 70 | Verify that computed result matches expected result. |
| 71 | |
| 72 | Automatically detects whether arrays are NumPy or CuPy and uses the |
| 73 | appropriate library without unnecessary data transfers. |
| 74 | |
| 75 | Parameters |
| 76 | ---------- |
| 77 | result : numpy.ndarray or cupy.ndarray |
| 78 | Computed result array. |
| 79 | expected : numpy.ndarray or cupy.ndarray |
| 80 | Expected result array. |
| 81 | rtol : float |
| 82 | Relative tolerance (default: 1e-5) |
| 83 | atol : float |
| 84 | Absolute tolerance (default: 1e-8) |
| 85 | verbose : bool |
| 86 | Whether to print verification result (default: True). |
| 87 | |
| 88 | Returns |
| 89 | ------- |
| 90 | bool |
| 91 | True if results match, False otherwise. |
| 92 | |
| 93 | Raises |
| 94 | ------ |
| 95 | TypeError |
| 96 | If arrays are not both NumPy or both CuPy, or if CuPy is needed |
| 97 | but not available. |
| 98 | """ |
| 99 | import numpy as np |
| 100 | |
| 101 | is_np = isinstance(result, np.ndarray) and isinstance(expected, np.ndarray) |
| 102 | |
| 103 | if is_np: |
| 104 | allclose = np.allclose |
| 105 | abs_ = np.abs |
| 106 | max_ = np.max |
| 107 | else: |
| 108 | import cupy as cp |
| 109 | |
| 110 | is_cp = isinstance(result, cp.ndarray) and isinstance(expected, cp.ndarray) |
| 111 | |
| 112 | if not is_cp: |
| 113 | raise TypeError( |
| 114 | "verify_array_result expects both arrays to be either " |
| 115 | "numpy.ndarray or cupy.ndarray" |
| 116 | ) |
| 117 | |
| 118 | allclose = cp.allclose |
| 119 | abs_ = cp.abs |
| 120 | max_ = cp.max |
| 121 | |
| 122 | if allclose(result, expected, rtol=rtol, atol=atol): |
| 123 | if verbose: |
no outgoing calls
no test coverage detected