MCPcopy Create free account
hub / github.com/NVIDIA/cuda-samples / verify_array_result

Function verify_array_result

python/Utilities/cuda_samples_utils.py:66–130  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

64
65
66def 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:

Callers 13

mainFunction · 0.90
run_sampleFunction · 0.90
run_pagerank_benchmarkFunction · 0.90
mainFunction · 0.90
runFunction · 0.90
runFunction · 0.90
run_fft_analysisFunction · 0.90
mainFunction · 0.90
vector_add_cuda_coreFunction · 0.90
runFunction · 0.90
runFunction · 0.90
mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected