Debian Codegraph @

test/conftest.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Configuration file for pytest
4
5 # License: MIT License
6
7 import functools
8 import os
9 import pytest
10 import numpy as np
11 from sys import platform
12
13 # set numpy print options : TODO update tests when all release use modern numpy
14 if platform == "linux":
15 np.set_printoptions(legacy="1.25")
16
17 from ot.backend import get_backend_list, jax, tf
18
19
20 if jax:
21 os.environ["XLA_PYTHON_CLIENT_PREALLOCATE"] = "false"
22 from jax import config
23
24 config.update("jax_enable_x64", True)
25
26 if tf:
27 # make sure TF doesn't allocate entire GPU
28 import tensorflow as tf
29
30 physical_devices = tf.config.list_physical_devices("GPU")
31 for device in physical_devices:
32 try:
33 tf.config.experimental.set_memory_growth(device, True)
34 except Exception:
35 pass
36
37 # allow numpy API for TF
38 from tensorflow.python.ops.numpy_ops import np_config
39
40 np_config.enable_numpy_behavior()
41
42
43 backend_list = get_backend_list()
44
45
46 @pytest.fixture(params=backend_list)
47 def nx(request):
48 backend = request.param
49
50 yield backend
51
52
53 def skip_arg(arg, value, reason=None, getter=lambda x: x):
54 if isinstance(arg, (tuple, list)):
55 n = len(arg)
56 else:
57 arg = (arg,)
58 n = 1
59 if n != 1 and isinstance(value, (tuple, list)):
60 pass
61 else:
62 value = (value,)
63 if isinstance(getter, (tuple, list)):
64 pass
65 else:
66 getter = [getter] * n
67
68 if reason is None:
69 reason = f"Param {arg} should be skipped for value {value}"
70
71 def wrapper(function):
72 @functools.wraps(function)
73 def wrapped(*args, **kwargs):
74 if all(
75 arg[i] in kwargs.keys() and getter[i](kwargs[arg[i]]) == value[i]
76 for i in range(n)
77 ):
78 pytest.skip(reason)
79 return function(*args, **kwargs)
80
81 return wrapped
82
83 return wrapper
84
85
86 def pytest_configure(config):
87 pytest.skip_arg = skip_arg
88 pytest.skip_backend = functools.partial(skip_arg, "nx", getter=str)