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