Skip to content

Commit

Permalink
Remove direct tensorflow imports from tests
Browse files Browse the repository at this point in the history
To be fully agnostic, we prefer having utility functions that are
checking the backend use like now `is_in_gpu_mode()`.
  • Loading branch information
nhuet committed Jan 8, 2024
1 parent 6332d77 commit baf51b5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
28 changes: 24 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
from numpy.testing import assert_almost_equal

from decomon.core import ForwardMode, Slope
from decomon.keras_utils import (
BACKEND_JAX,
BACKEND_NUMPY,
BACKEND_PYTORCH,
BACKEND_TENSORFLOW,
)
from decomon.models.utils import ConvertMethod
from decomon.types import Tensor

Expand Down Expand Up @@ -165,10 +171,24 @@ def __call__(self, inputs_: List[np.ndarray]):

class Helpers:
@staticmethod
def tensorflow_in_GPU_mode() -> bool:
import tensorflow

return len(tensorflow.config.list_physical_devices("GPU")) > 0
def in_GPU_mode() -> bool:
backend = keras.config.backend()
if backend == BACKEND_TENSORFLOW:
import tensorflow

return len(tensorflow.config.list_physical_devices("GPU")) > 0
elif backend == BACKEND_PYTORCH:
import torch

return torch.cuda.is_available()
elif backend == BACKEND_NUMPY:
return False
elif backend == BACKEND_JAX:
import jax

return jax.devices()[0].platform != "cpu"
else:
raise NotImplementedError(f"Not implemented for {backend} backend.")

@staticmethod
def is_method_mode_compatible(method, mode):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_backward_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import keras.config as keras_config
import numpy as np
import pytest
from tensorflow.python.keras.backend import _get_available_gpus

from decomon.backward_layers.convert import to_backward
from decomon.layers.decomon_layers import DecomonConv2D


def test_Decomon_conv_box(data_format, padding, use_bias, mode, floatx, decimal, helpers):
if data_format == "channels_first" and not helpers.tensorflow_in_GPU_mode():
if data_format == "channels_first" and not helpers.in_GPU_mode():
pytest.skip("data format 'channels first' is possible only in GPU mode")

odd, m_0, m_1 = 0, 0, 1
Expand Down
3 changes: 1 addition & 2 deletions tests/test_backward_layers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import keras.config as keras_config
import pytest
from keras.layers import Layer, Reshape
from tensorflow.python.keras.backend import _get_available_gpus

from decomon.backward_layers.convert import to_backward
from decomon.core import ForwardMode, Slope
Expand Down Expand Up @@ -105,7 +104,7 @@ def test_Backward_Activation_multiD_box(odd, activation, floatx, decimal, mode,


def test_Backward_Flatten_multiD_box(odd, floatx, decimal, mode, data_format, helpers):
if data_format == "channels_first" and not helpers.tensorflow_in_GPU_mode():
if data_format == "channels_first" and not helpers.in_GPU_mode():
pytest.skip("data format 'channels first' is possible only in GPU mode")

dc_decomp = False
Expand Down
3 changes: 1 addition & 2 deletions tests/test_backward_native_layers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import keras.config as keras_config
import pytest
from keras.layers import Activation, Flatten, Reshape
from tensorflow.python.keras.backend import _get_available_gpus

from decomon.backward_layers.convert import to_backward

Expand Down Expand Up @@ -65,7 +64,7 @@ def test_Backward_NativeActivation_multiD_box(odd, activation, floatx, decimal,


def test_Backward_NativeFlatten_multiD_box(odd, floatx, decimal, mode, data_format, helpers):
if data_format == "channels_first" and not helpers.tensorflow_in_GPU_mode():
if data_format == "channels_first" and not helpers.in_GPU_mode():
pytest.skip("data format 'channels first' is possible only in GPU mode")

dc_decomp = False
Expand Down
3 changes: 1 addition & 2 deletions tests/test_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import numpy as np
import pytest
from keras.layers import Conv2D
from tensorflow.python.keras.backend import _get_available_gpus

from decomon.core import ForwardMode, get_affine, get_ibp
from decomon.layers.convert import to_decomon
from decomon.layers.decomon_layers import DecomonConv2D


def test_Decomon_conv_box(data_format, mode, dc_decomp, floatx, decimal, helpers):
if data_format == "channels_first" and not helpers.tensorflow_in_GPU_mode():
if data_format == "channels_first" and not helpers.in_GPU_mode():
pytest.skip("data format 'channels first' is possible only in GPU mode")

odd, m_0, m_1 = 0, 0, 1
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_toeplitz_from_Keras(channels, filter_size, strides, flatten, data_forma
if floatx == 16:
decimal = 0

if data_format == "channels_first" and not helpers.tensorflow_in_GPU_mode():
if data_format == "channels_first" and not helpers.in_GPU_mode():
pytest.skip("data format 'channels first' is possible only in GPU mode")

dc_decomp = False
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_toeplitz_from_Keras(channels, filter_size, strides, flatten, data_forma
def test_toeplitz_from_Decomon(
floatx, decimal, mode, channels, filter_size, strides, flatten, data_format, padding, helpers
):
if data_format == "channels_first" and not helpers.tensorflow_in_GPU_mode():
if data_format == "channels_first" and not helpers.in_GPU_mode():
pytest.skip("data format 'channels first' is possible only in GPU mode")

odd, m_0, m_1 = 0, 0, 1
Expand Down

0 comments on commit baf51b5

Please sign in to comment.