Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Increase-test-coverage for keras_tensor and variables+ Fix __invert__ + Fix FloorDivide #944

Closed
8 changes: 4 additions & 4 deletions keras_core/backend/common/keras_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ def __rpow__(self, other):
def __floordiv__(self, other):
from keras_core import ops

return ops.FloorDiv().symbolic_call(self, other)
return ops.FloorDivide().symbolic_call(self, other)

def __rfloordiv__(self, other):
from keras_core import ops

return ops.FloorDiv().symbolic_call(other, self)
return ops.FloorDivide().symbolic_call(other, self)

def __mod__(self, other):
from keras_core import ops
Expand Down Expand Up @@ -270,10 +270,10 @@ def __ror__(self, other):

return ops.LogicalOr().symbolic_call(other, self)

def __invert__(self, other):
def __invert__(self):
from keras_core import ops

return ops.LogicalNot().symbolic_call(other, self)
return ops.LogicalNot().symbolic_call(self)

def __xor__(self, other):
from keras_core import ops
Expand Down
232 changes: 232 additions & 0 deletions keras_core/backend/common/keras_tensor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,235 @@ def _test_binary_op_method(self, mock_method, other, operator):
result = operator(x, other)
mock_method.assert_called_once_with(x, other)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Add.symbolic_call")
def test_radd_method(self, mock_symbolic_call):
"""Test __radd__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y + x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Subtract.symbolic_call")
def test_rsub_method(self, mock_symbolic_call):
"""Test __rsub__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y - x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Multiply.symbolic_call")
def test_rmul_method(self, mock_symbolic_call):
"""Test __rmul__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y * x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Matmul.symbolic_call")
def test_rmatmul_method(self, mock_symbolic_call):
"""Test __rmatmul__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y @ x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Power.symbolic_call")
def test_rpow_method(self, mock_symbolic_call):
"""Test __rpow__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y**x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.FloorDivide.symbolic_call")
def test_floordiv_method(self, mock_symbolic_call):
"""Test __floordiv__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = x // y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.FloorDivide.symbolic_call")
def test_rfloordiv_method(self, mock_symbolic_call):
"""Test __rfloordiv__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y // x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Mod.symbolic_call")
def test_rmod_method(self, mock_symbolic_call):
"""Test __rmod__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y % x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.LessEqual.symbolic_call")
def test_le_method(self, mock_symbolic_call):
"""Test __le__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = x <= y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Greater.symbolic_call")
def test_gt_method(self, mock_symbolic_call):
"""Test __gt__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = x > y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.GreaterEqual.symbolic_call")
def test_ge_method(self, mock_symbolic_call):
"""Test __ge__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = x >= y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.NotEqual.symbolic_call")
def test_ne_method(self, mock_symbolic_call):
"""Test __ne__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = x != y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.LogicalAnd.symbolic_call")
def test_rand_method(self, mock_symbolic_call):
"""Test __rand__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="bool")
y = Mock()
result = y & x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.LogicalOr.symbolic_call")
def test_ror_method(self, mock_symbolic_call):
"""Test __ror__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="bool")
y = Mock()
result = y | x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.LogicalNot.symbolic_call")
def test_invert_method(self, mock_symbolic_call):
"""Test __invert__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="bool")
result = ~x
mock_symbolic_call.assert_called_once_with(x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.LogicalXor.symbolic_call")
def test_xor_method(self, mock_symbolic_call):
"""Test __xor__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="bool")
y = Mock()
result = x ^ y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.LogicalXor.symbolic_call")
def test_rxor_method(self, mock_symbolic_call):
"""Test __rxor__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="bool")
y = Mock()
result = y ^ x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.TrueDivide.symbolic_call")
def test_truediv_method(self, mock_symbolic_call):
"""Test __truediv__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = x / y
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.TrueDivide.symbolic_call")
def test_rtruediv_method(self, mock_symbolic_call):
"""Test __rtruediv__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = Mock()
result = y / x
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Divide.symbolic_call")
def test_div_method(self, mock_symbolic_call):
"""Test __div__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
# to ensure compatibility across Python versions.
result = x.__div__(y)
mock_symbolic_call.assert_called_once_with(x, y)
self.assertEqual(result, mock_tensor)

@patch("keras_core.ops.Divide.symbolic_call")
def test_rdiv_method(self, mock_symbolic_call):
"""Test __rdiv__ method"""
mock_tensor = Mock()
mock_symbolic_call.return_value = mock_tensor
x = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
y = keras_tensor.KerasTensor(shape=(3, 4), dtype="float32")
# to ensure compatibility across Python versions.
result = x.__rdiv__(y)
mock_symbolic_call.assert_called_once_with(y, x)
self.assertEqual(result, mock_tensor)
113 changes: 113 additions & 0 deletions keras_core/backend/common/variables_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from keras_core.backend.common.variables import KerasVariable
from keras_core.backend.common.variables import standardize_shape
from keras_core.testing import test_case
from keras_core.ops.core import convert_to_tensor


class VariablesTest(test_case.TestCase):
Expand Down Expand Up @@ -125,3 +126,115 @@ def test_autocast_scope_with_non_float_dtype(self):
"`AutocastScope` can only be used with a floating-point",
):
_ = AutocastScope("int32")

def test_variable_initialization_with_non_callable(self):
v = backend.Variable(initializer=np.ones((2, 2)))
self.assertAllClose(v.value, np.ones((2, 2)))

def test_variable_path_creation(self):
v = backend.Variable(initializer=np.ones((2, 2)), name="test_var")
self.assertEqual(v.path, "test_var")

def test_variable_initialization_with_non_trainable(self):
v = backend.Variable(initializer=np.ones((2, 2)), trainable=False)
self.assertFalse(v.trainable)

def test_variable_initialization_with_dtype(self):
v = backend.Variable(initializer=np.ones((2, 2)), dtype="int32")
self.assertEqual(v.dtype, "int32")
self.assertEqual(backend.standardize_dtype(v.value.dtype), "int32")

def test_variable_initialization_without_shape(self):
with self.assertRaisesRegex(
ValueError,
"When creating a Variable from an initializer, the `shape` ",
):
backend.Variable(initializer=initializers.RandomNormal())

def test_deferred_initialize_already_initialized(self):
v = backend.Variable(initializer=np.ones((2, 2)))
with self.assertRaisesRegex(
ValueError, f"Variable {v.path} is already initialized."
):
v._deferred_initialize()

def test_deferred_initialize_within_stateless_scope(self):
with backend.StatelessScope():
v = backend.Variable(
initializer=initializers.RandomNormal(), shape=(2, 2)
)
with self.assertRaisesRegex(
ValueError,
"You are attempting to initialize a variable "
"while in a stateless scope. This is disallowed.",
):
v._deferred_initialize()

def test_variable_as_boolean(self):
v = backend.Variable(initializer=np.ones((2, 2)))
with self.assertRaises(TypeError):
bool(v)

def test_variable_negation(self):
v = backend.Variable(initializer=np.array([-1, 2]))
neg_v = -v
self.assertAllClose(neg_v, np.array([1, -2]))

def test_variable_pos(self):
v = backend.Variable(initializer=np.array([-1, 2]))
pos_v = v
self.assertAllClose(pos_v, np.array([-1, 2]))

def test_variable_abs(self):
v = backend.Variable(initializer=np.array([-1, 2]))
abs_v = abs(v)
self.assertAllClose(abs_v, np.array([1, 2]))

def test_variable_invert(self):
v = backend.Variable(initializer=np.array([0, -1]), dtype="int32")
inv_v = ~v
self.assertAllClose(inv_v, np.array([-1, 0]))

def test_variable_lt_numpy_array(self):
v = backend.Variable(initializer=np.array([1, 2, 3]))
arr = np.array([2, 2, 2])

lt_result = v < arr
self.assertAllClose(lt_result.numpy(), np.array([True, False, False]))

def test_variable_div_numpy_array(self):
v = backend.Variable(initializer=np.array([2, 4, 8]))
arr = np.array([2, 8, 16])

div_result = arr / v
self.assertAllClose(div_result, np.array([1, 2, 2]))

def test_variable_rdiv_numpy_array(self):
v = backend.Variable(initializer=np.array([2, 4, 8]))
arr = np.array([16, 32, 64])

rdiv_result = arr / v
self.assertAllClose(rdiv_result, np.array([8, 8, 8]))

def test_variable_rsub_numpy_array(self):
v = backend.Variable(initializer=np.array([1, 2, 3]))
arr = np.array([2, 2, 2])

rsub_result = arr - v
self.assertAllClose(rsub_result, np.array([1, 0, -1]))

def test_variable_rfloordiv(self):
v = backend.Variable(initializer=np.array([3, 4, 6]))
result = np.array([9, 12, 18]) // v
self.assertAllClose(result.numpy(), np.array([3, 3, 3]))

def test_variable_rfloordiv(self):
v = backend.Variable(initializer=np.array([3, 4, 6]))
result = np.array([9, 12, 18]) // v
self.assertAllClose(result, np.array([3, 3, 3]))

def test_variable_rmatmul(self):
v = backend.Variable(initializer=np.array([[2, 3], [4, 5]]))
other = np.array([[1, 2], [3, 4]])
rmatmul_result = other @ v
self.assertAllClose(rmatmul_result, np.array([[10, 13], [22, 29]]))
Loading