Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Add the TensorFlow version of some JAX utilities #59

Merged
merged 28 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d49ffb7
Add the TensorFlow version of general dot operation to `tf_hlpers`
Aug 24, 2020
c57503f
Add the TensorFlow version of some JAX lax utilities.
Aug 25, 2020
7f51350
Add the updated `tf_lax` file and the updated `lax_tests` file
Aug 25, 2020
36ae0a3
Remove the docstring at the begining of the file
Aug 25, 2020
70924d0
Remove unnecessary imports.
Aug 25, 2020
d20c45f
Update Travis CI such that its installation contains the
Aug 25, 2020
2fee307
Rename the lax test files and move it under the test folder
Aug 25, 2020
34787ba
Adjust the blank lines above the class definition and between the
Aug 25, 2020
6aa0154
Remove unused comments as pointed out in https://github.com/google/ne…
Aug 25, 2020
42e5b54
Remove the installation of JAX in Travis CI
Aug 25, 2020
7dc72d6
Revert back to the original Travis CI file as mentioned in https://gi…
Aug 25, 2020
e0fabe3
Remove the `_non_batched_matmul` as suggested in
Aug 25, 2020
4dbc0b3
Remove the extra blank line in Travis CI
Aug 25, 2020
412eedc
Give it a try on the direct file import from the `tf_helpers` folder
Aug 25, 2020
96c0ff1
Remove the extra blank line and revise the import typo.
Aug 25, 2020
23d998d
Add the TF version of `ostax` purely for the use in Neural Tangents.
Aug 26, 2020
c04e7b8
Rename `tf_jax_stax` to `stax`
Aug 26, 2020
70257b9
Remove the unused print statement
Aug 26, 2020
d92e0fc
Remove the unused `tf nn` import in lax tests.
Aug 26, 2020
9432296
Rename the lax tests
Aug 26, 2020
51f78a4
Remove the unused comments
Aug 26, 2020
9357a24
Add an extra `batch` dimension and an extra `channel` dimension to pass
Aug 26, 2020
27a6982
Add the window_shape and strides dimension expansion that appear in JAX
Aug 26, 2020
a020275
Revert the changes in Travis CI.
Aug 26, 2020
7df8a23
Replace the vanilla NumPy support with TF NumPy support.
Aug 26, 2020
d730456
Remove the unused lines and unused `moveaxis`.
Aug 26, 2020
588b748
Remove the extra 2 dimensions in the output of TF `reduce_window`.
Aug 26, 2020
ab17374
Move `np.asarray` wrapper to TF `pool`.
Aug 26, 2020
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
102 changes: 102 additions & 0 deletions tests/lax_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================


import tensorflow as tf
from tf_helpers import lax
from tensorflow.python.platform import test
from absl.testing import parameterized
import itertools
import numpy as onp
from tensorflow.python.ops import numpy_ops as tfnp
from jax import numpy as jnp
import jax
import sys


class TFLaxTest(tf.test.TestCase, parameterized.TestCase):

@parameterized.parameters(
{"lhs_np": onp.ones((5, 3)), "rhs_np": onp.ones((3, 2)),
"dims": (((1,), (0,)), ((), ()))},
{"lhs_np": onp.ones((5, 3)), "rhs_np": onp.ones((5, 3)),
"dims": (((0, 1), (0, 1)), ((), ()))},
{"lhs_np": onp.ones((5, 3, 2)), "rhs_np": onp.ones((2, 3, 2)),
"dims": (((1, 2), (1, 0)), ((), ()))},
{"lhs_np": onp.ones((6, 5, 3)), "rhs_np": onp.ones((6, 3, 2)),
"dims": (((2,), (1,)), ((0,), (0,)))},
{"lhs_np": onp.ones((6, 3, 5)), "rhs_np": onp.ones((6, 3, 2)),
"dims": (((1,), (1,)), ((0,), (0,)))},
{"lhs_np": onp.ones((5, 3, 2, 2)), "rhs_np": onp.ones((5, 2, 2, 6)),
"dims": (((2, 3), (1, 2)), ((0,), (0,)))},
{"lhs_np": onp.ones((2, 2, 5, 3)), "rhs_np": onp.ones((2, 2, 3, 2)),
"dims": (((3,), (2,)), ((0, 1), (0, 1)))},
{"lhs_np": onp.ones((2, 2, 5, 2)), "rhs_np": onp.ones((2, 2, 3, 2)),
"dims": (((3,), (1,)), ((0,), (0,)))},
{"lhs_np": onp.ones((2, 2, 5, 3, 3)), "rhs_np": onp.ones((2, 3, 2, 3, 2)),
"dims": (((4,), (1,)), ((0,), (0,)))},
)
def test_tf_dot_general(self, lhs_np, rhs_np, dims):
ans = jax.lax.dot_general(lhs_np, rhs_np, dims)
result = lax.dot_general(lhs_np, rhs_np, dims)
self.assertAllClose(result, tfnp.array(ans))

@parameterized.named_parameters([
("_lhs_shape={}_rhs_shape={}_strides={}_padding={}"
"_lhs_dilation={}_rhs_dilation={}"
"_feature_group_count={}_batch_group_count={}_dims={}"
"_perms={}".format(lhs_shape, rhs_shape,
strides, padding, lhs_dilation, rhs_dilation,
feature_group_count, batch_group_count, ",".join(dimension_numbers), perms),
lhs_shape, rhs_shape, strides, padding, lhs_dilation, rhs_dilation,
feature_group_count, batch_group_count, dimension_numbers, perms)
for batch_group_count, feature_group_count in [(1, 1)]
for lhs_shape, rhs_shape in [
((b * batch_group_count, i * feature_group_count, 9, w),
(j * feature_group_count * batch_group_count, i, 4, 5))
for w in [0, 10]
for b, i, j in itertools.product([2, 3], repeat=3)]
for strides in [(1, 1), (2, 1)]
for padding in ['SAME']
for lhs_dilation, rhs_dilation in [
(None, (1, 1))
]
for dimension_numbers, perms in [
(("NHWC", "HWIO", "NHWC"), ([0, 2, 3, 1], [2, 3, 1, 0]))
]])
def testConvGeneralDilated(self, lhs_shape, rhs_shape, strides,
padding, lhs_dilation, rhs_dilation,
feature_group_count, batch_group_count,
dimension_numbers, perms):
tf.print("dimension_numbers: {}".format(dimension_numbers), output_stream=sys.stdout)
lhs_perm, rhs_perm = perms # permute to compatible shapes

lhs_tf = tfnp.transpose(tfnp.ones(lhs_shape), lhs_perm)
rhs_tf = tfnp.transpose(tfnp.ones(rhs_shape), rhs_perm)

lhs_jax = jnp.transpose(jnp.ones(lhs_shape), lhs_perm)
rhs_jax = jnp.transpose(jnp.ones(rhs_shape), rhs_perm)

jax_conv = jax.lax.conv_general_dilated(lhs_jax, rhs_jax, strides, padding, lhs_dilation,
rhs_dilation, dimension_numbers, feature_group_count, batch_group_count)

tf_conv = lax.conv_general_dilated(lhs_tf, rhs_tf, strides, padding, jax_conv.shape, lhs_dilation,
rhs_dilation, dimension_numbers, feature_group_count, batch_group_count)

self.assertAllEqual(tf_conv, tfnp.asarray(jax_conv))


if __name__ == "__main__":
test.main()
Empty file added tf_helpers/__init__.py
Empty file.
Loading