Skip to content

Commit

Permalink
chore: skipping tests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
ariG23498 committed Jul 7, 2023
1 parent 17a5dda commit 512c441
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
6 changes: 6 additions & 0 deletions keras_core/models/cloning_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest
from absl.testing import parameterized

from keras_core import backend
from keras_core import layers
from keras_core import models
from keras_core import testing
Expand Down Expand Up @@ -41,6 +43,10 @@ def call(self, x):
return ExampleModel()


@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
class CloneModelTest(testing.TestCase, parameterized.TestCase):
@parameterized.named_parameters(
("functional", get_functional_model),
Expand Down
57 changes: 57 additions & 0 deletions keras_core/models/functional_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

import numpy as np
import pytest

from keras_core import backend
from keras_core import layers
Expand All @@ -12,6 +13,10 @@


class FunctionalTest(testing.TestCase):
@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_basic_flow_multi_input(self):
input_a = Input(shape=(3,), batch_size=2, name="input_a")
input_b = Input(shape=(3,), batch_size=2, name="input_b")
Expand All @@ -37,6 +42,10 @@ def test_basic_flow_multi_input(self):
out_val = model(in_val)
self.assertEqual(out_val.shape, (2, 4))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_scalar_input(self):
input_a = Input(shape=(3,), batch_size=2, name="input_a")
input_b = Input(shape=(), batch_size=2, name="input_b")
Expand All @@ -48,6 +57,10 @@ def test_scalar_input(self):
out_val = model(in_val)
self.assertAllClose(out_val, np.ones((2, 3)))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_basic_flow_multi_output(self):
inputs = Input(shape=(3,), batch_size=2, name="input")
x = layers.Dense(5)(inputs)
Expand All @@ -70,6 +83,10 @@ def test_basic_flow_multi_output(self):
self.assertEqual(out_val[0].shape, (2, 4))
self.assertEqual(out_val[1].shape, (2, 5))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_basic_flow_dict_io(self):
input_a = Input(shape=(3,), batch_size=2, name="a")
input_b = Input(shape=(3,), batch_size=2, name="b")
Expand Down Expand Up @@ -101,6 +118,10 @@ def test_basic_flow_dict_io(self):
out_val = model(in_val)
self.assertEqual(out_val.shape, (2, 4))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_named_input_dict_io(self):
input_a = Input(shape=(3,), batch_size=2, name="a")
x = layers.Dense(5)(input_a)
Expand All @@ -119,6 +140,10 @@ def test_named_input_dict_io(self):
out_val = model(in_val)
self.assertEqual(out_val.shape, (2, 4))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_input_dict_with_extra_field(self):
input_a = Input(shape=(3,), batch_size=2, name="a")
x = input_a * 5
Expand All @@ -145,6 +170,10 @@ def test_input_dict_with_extra_field(self):
out_val = model(in_val)
self.assertEqual(out_val.shape, (2, 3))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_layer_getters(self):
# Test mixing ops and layers
input_a = Input(shape=(3,), batch_size=2, name="input_a")
Expand All @@ -162,6 +191,10 @@ def test_layer_getters(self):
self.assertEqual(model.get_layer(index=3).name, "dense_2")
self.assertEqual(model.get_layer(name="dense_1").name, "dense_1")

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_training_arg(self):
class Canary(layers.Layer):
def call(self, x, training=False):
Expand All @@ -180,6 +213,10 @@ def test_mask_arg(self):
# TODO
pass

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_passing_inputs_by_name(self):
input_a = Input(shape=(3,), batch_size=2, name="input_a")
input_b = Input(shape=(3,), batch_size=2, name="input_b")
Expand All @@ -203,6 +240,10 @@ def test_passing_inputs_by_name(self):
out_val = model(in_val)
self.assertEqual(out_val.shape, (2, 4))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_rank_standardization(self):
# Downranking
inputs = Input(shape=(3,), batch_size=2)
Expand All @@ -218,6 +259,10 @@ def test_rank_standardization(self):
out_val = model(np.random.random((2, 3)))
self.assertEqual(out_val.shape, (2, 3, 3))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_dtype_standardization(self):
float_input = Input(shape=(2,), dtype="float16")
int_input = Input(shape=(2,), dtype="int32")
Expand All @@ -229,6 +274,10 @@ def test_dtype_standardization(self):
self.assertEqual(backend.standardize_dtype(float_data.dtype), "float16")
self.assertEqual(backend.standardize_dtype(int_data.dtype), "int32")

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_serialization(self):
# Test basic model
inputs = Input(shape=(3,), batch_size=2)
Expand Down Expand Up @@ -269,6 +318,10 @@ def test_serialization(self):
model = Functional({"a": input_a, "b": input_b}, outputs)
self.run_class_serialization_test(model)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_bad_input_spec(self):
# Single input
inputs = Input(shape=(4,))
Expand Down Expand Up @@ -303,6 +356,10 @@ def test_bad_input_spec(self):
):
model({"a": np.zeros((2, 3)), "b": np.zeros((2, 4))})

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_manual_input_spec(self):
inputs = Input(shape=(None, 3))
outputs = layers.Dense(2)(inputs)
Expand Down
74 changes: 74 additions & 0 deletions keras_core/models/model_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest
from absl.testing import parameterized

from keras_core import backend
from keras_core import layers
from keras_core import testing
from keras_core.layers.core.input_layer import Input
Expand Down Expand Up @@ -65,16 +67,28 @@ def _get_model_multi_outputs_dict():


class ModelTest(testing.TestCase, parameterized.TestCase):
@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_rerouting(self):
model = _get_model()
self.assertTrue(isinstance(model, Functional))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_json_serialization(self):
model = _get_model()
json_string = model.to_json()
new_model = model_from_json(json_string)
self.assertEqual(json_string, new_model.to_json())

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_tuple_input_model_subclass(self):
# https://github.com/keras-team/keras-core/issues/324

Expand All @@ -94,6 +108,10 @@ def call(self, inputs):
out = model((x1, x2))
self.assertEqual(out.shape, (3, 6))

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_reviving_functional_from_config_custom_layer(self):
class CustomDense(layers.Layer):
def __init__(self, units, **kwargs):
Expand Down Expand Up @@ -127,6 +145,10 @@ def call(self, x):
("single_dict_output_3", _get_model_single_output_dict, "dict"),
("single_dict_output_4", _get_model_single_output_dict, "dict_list"),
)
@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_single_output(self, model_fn, loss_type):
model = model_fn()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -168,6 +190,10 @@ def test_functional_single_output(self, model_fn, loss_type):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_list_losses(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -199,6 +225,10 @@ def test_functional_list_outputs_list_losses(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_list_losses_abbr(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -233,6 +263,10 @@ def test_functional_list_outputs_list_losses_abbr(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_nested_list_losses(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -264,6 +298,10 @@ def test_functional_list_outputs_nested_list_losses(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_dict_outputs_dict_losses(self):
model = _get_model_multi_outputs_dict()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -310,6 +348,10 @@ def test_functional_dict_outputs_dict_losses(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_dict_losses_metrics(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -350,6 +392,10 @@ def test_functional_list_outputs_dict_losses_metrics(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_dict_losses_metrics_uniq_weighted(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -391,6 +437,10 @@ def test_functional_list_outputs_dict_losses_metrics_uniq_weighted(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_dict_losses_partial_metrics(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down Expand Up @@ -422,6 +472,10 @@ def test_functional_list_outputs_dict_losses_partial_metrics(self):
)
self.assertListEqual(hist_keys, ref_keys)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_dict_losses_invalid_keys(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand All @@ -443,6 +497,10 @@ def test_functional_list_outputs_dict_losses_invalid_keys(self):
):
model.fit(x, (y1, y2), batch_size=2, epochs=1, verbose=0)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_dict_losses_no_output_names(self):
model = _get_model_multi_outputs_list_no_output_names()
self.assertTrue(isinstance(model, Functional))
Expand All @@ -461,6 +519,10 @@ def test_functional_list_outputs_dict_losses_no_output_names(self):
):
model.fit(x, (y1, y2), batch_size=2, epochs=1, verbose=0)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_dict_metrics_invalid_keys(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand All @@ -485,6 +547,10 @@ def test_functional_list_outputs_dict_metrics_invalid_keys(self):
):
model.fit(x, (y1, y2), batch_size=2, epochs=1, verbose=0)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_dict_outputs_dict_losses_invalid_keys(self):
model = _get_model_multi_outputs_dict()
self.assertTrue(isinstance(model, Functional))
Expand All @@ -506,6 +572,10 @@ def test_functional_dict_outputs_dict_losses_invalid_keys(self):
):
model.fit(x, (y1, y2), batch_size=2, epochs=1, verbose=0)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_dict_outputs_dict_metrics_invalid_keys(self):
model = _get_model_multi_outputs_dict()
self.assertTrue(isinstance(model, Functional))
Expand All @@ -530,6 +600,10 @@ def test_functional_dict_outputs_dict_metrics_invalid_keys(self):
):
model.fit(x, (y1, y2), batch_size=2, epochs=1, verbose=0)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented from NumPy backend.",
)
def test_functional_list_outputs_invalid_nested_list_losses(self):
model = _get_model_multi_outputs_list()
self.assertTrue(isinstance(model, Functional))
Expand Down
Loading

0 comments on commit 512c441

Please sign in to comment.