Skip to content

Instantly share code, notes, and snippets.

@ganesh-k13
Last active May 14, 2022 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganesh-k13/6faaf65d418db13012bd403250cca70a to your computer and use it in GitHub Desktop.
Save ganesh-k13/6faaf65d418db13012bd403250cca70a to your computer and use it in GitHub Desktop.
NumPy division overflow tests
import itertools
import numpy as np
from subprocess import Popen, PIPE
from pprint import pprint
from tqdm import tqdm
from tabulate import tabulate
print(np.__version__)
COMMANDS = [
"import numpy as np",
"import warnings",
"warnings.filterwarnings('ignore')"
# "print(np.__version__)",
]
SCALAR_OPERATIONS = [
(
(operation, dividend_dtype, divisor_dtype),
f"{operation}({dividend_dtype}(np.iinfo({dividend_dtype}).min), {divisor_dtype}(-1))",
)
for dividend_dtype, divisor_dtype, operation in itertools.product(
["np.int8", "np.int16", "np.int32", "np.int64"],
["np.int8", "np.int16", "np.int32", "np.int64"],
["np.remainder", "np.fmod", "np.floor_divide"],
)
]
ARRAY_OPERATIONS = [
(
(operation, dividend_dtype, divisor_dtype),
f"[{operation}(np.array([{dividend_dtype}(np.iinfo({dividend_dtype}).min)]*i), {divisor_dtype}(-1)) for i in range(1, 129)]",
)
for dividend_dtype, divisor_dtype, operation in itertools.product(
["np.int8", "np.int16", "np.int32", "np.int64"],
["np.int8", "np.int16", "np.int32", "np.int64"],
["np.remainder", "np.fmod", "np.floor_divide"],
)
]
def main():
failed = {"scalars": [], "arrays": []}
# Scalars
for args, operation in tqdm(SCALAR_OPERATIONS, desc="Scalars"):
process = Popen(["python3", "-c"] + [";".join(COMMANDS + [operation])])
process.communicate()
# print(args, process.returncode)
if process.returncode:
failed["scalars"].append(args)
# Arrays
for args, operation in tqdm(ARRAY_OPERATIONS, desc="Arrays"):
process = Popen(["python3", "-c"] + [";".join(COMMANDS + [operation])])
process.communicate()
# print(args, process.returncode)
if process.returncode:
failed["arrays"].append(args)
print("Scalar Failures Observed:")
print(
tabulate(
failed["scalars"],
tablefmt="grid",
headers=["operation", "dividend_dtype", "divisor_dtype"],
)
)
print("Array Failures Observed:")
print(
tabulate(
failed["arrays"],
tablefmt="grid",
headers=["operation", "dividend_dtype", "divisor_dtype"],
)
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment