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

Symbolic Args test #536

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
wrote fill in tests
Also wrote comments + fixes
  • Loading branch information
adi-kmt committed Jul 20, 2023

Verified

This commit was signed with the committer’s verified signature.
adamdehaven Adam DeHaven
commit 16cd94b3af5dfc395a98d7fb88849477619aaffd
84 changes: 81 additions & 3 deletions keras_core/ops/symbolic_arguments_test.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@


class SymbolicArgumentsTest(testing.TestCase):
# Testing multiple args and empty kwargs
def test_args(self):
shape = (2, 3, 4)
a = KerasTensor(shape=shape)
@@ -19,18 +20,51 @@ def test_args(self):
)

self.assertEqual(args.keras_tensors, [a, b])
self.assertEqual(args.__dict__["_flat_arguments"], [a, b])
self.assertEqual(args._flat_arguments, [a, b])
self.assertEqual(args._single_positional_tensor, None)

# Testing single arg and single position tensor
def test_args_single_arg(self):
shape = (2, 3, 4)
a = KerasTensor(shape=shape)
args = SymbolicArguments((a))

self.assertEqual(args.keras_tensors, [a])
self.assertEqual(args._flat_arguments, [a])
self.assertEqual(len(args.kwargs), 0)
self.assertEqual(isinstance(args.args[0], KerasTensor), True)
self.assertEqual(args._single_positional_tensor, a)

# Testing kwargs
def test_kwargs(self):
shape = (2, 3, 4)
a = KerasTensor(shape=shape)
b = KerasTensor(shape=shape)
c = KerasTensor(shape=shape)
args = SymbolicArguments(
(
a,
b,
),
{1: c},
)

self.assertEqual(args.keras_tensors, [a, b, c])
self.assertEqual(args._flat_arguments, [a, b, c])
self.assertEqual(args._single_positional_tensor, None)

# Testing conversion function with args and kwargs
def test_conversion_fn(self):
shape = (2, 3, 4)
a = KerasTensor(shape=shape)
b = KerasTensor(shape=shape)
c = KerasTensor(shape=shape)
sym_args = SymbolicArguments(
(
a,
b,
),
{},
{1: c},
)

(value, _) = sym_args.convert(lambda x: x**2)
@@ -45,5 +79,49 @@ def test_conversion_fn(self):
self.assertEqual(mapped_value.shape, args1.shape)
self.assertEqual(mapped_value.dtype, args1.dtype)

# Testing fill in function with single args only
def test_fill_in_single_arg(self):
shape = (2, 3, 4)
a = KerasTensor(shape=shape)

tensor_dict = {id(a): 3}
sym_args = SymbolicArguments((a))

# Call the method to be tested
result, _ = sym_args.fill_in(tensor_dict)

self.assertEqual(result, (3,))

# Testing fill in function with multiple args
def test_fill_in_multiple_arg(self):
shape = (2, 3, 4)
a = KerasTensor(shape=shape)
b = KerasTensor(shape=shape)

tensor_dict = {id(b): 2}
sym_args = SymbolicArguments((a, b))

# Call the method to be tested
result, _ = sym_args.fill_in(tensor_dict)

self.assertEqual(result, ((a, 2),))

# Testing fill in function for args and kwargs
def test_fill_in(self):
adi-kmt marked this conversation as resolved.
Show resolved Hide resolved
pass
shape1 = (2, 3, 4)
shape2 = (3, 2, 4)
a = KerasTensor(shape=shape1)
b = KerasTensor(shape=shape2)
c = KerasTensor(shape=shape2)
dictionary = {id(a): 3, id(c): 2}
sym_args = SymbolicArguments(
(
a,
b,
),
{1: c},
)

(values, _) = sym_args.fill_in(dictionary)

self.assertEqual(values, ((3, b), {1: 2}))