This repository was archived by the owner on May 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Add the TensorFlow version of some JAX utilities #59
Merged
Merged
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`
c57503f
Add the TensorFlow version of some JAX lax utilities.
7f51350
Add the updated `tf_lax` file and the updated `lax_tests` file
36ae0a3
Remove the docstring at the begining of the file
70924d0
Remove unnecessary imports.
d20c45f
Update Travis CI such that its installation contains the
2fee307
Rename the lax test files and move it under the test folder
34787ba
Adjust the blank lines above the class definition and between the
6aa0154
Remove unused comments as pointed out in https://github.com/google/ne…
42e5b54
Remove the installation of JAX in Travis CI
7dc72d6
Revert back to the original Travis CI file as mentioned in https://gi…
e0fabe3
Remove the `_non_batched_matmul` as suggested in
4dbc0b3
Remove the extra blank line in Travis CI
412eedc
Give it a try on the direct file import from the `tf_helpers` folder
96c0ff1
Remove the extra blank line and revise the import typo.
23d998d
Add the TF version of `ostax` purely for the use in Neural Tangents.
c04e7b8
Rename `tf_jax_stax` to `stax`
70257b9
Remove the unused print statement
d92e0fc
Remove the unused `tf nn` import in lax tests.
9432296
Rename the lax tests
51f78a4
Remove the unused comments
9357a24
Add an extra `batch` dimension and an extra `channel` dimension to pass
27a6982
Add the window_shape and strides dimension expansion that appear in JAX
a020275
Revert the changes in Travis CI.
7df8a23
Replace the vanilla NumPy support with TF NumPy support.
d730456
Remove the unused lines and unused `moveaxis`.
588b748
Remove the extra 2 dimensions in the output of TF `reduce_window`.
ab17374
Move `np.asarray` wrapper to TF `pool`.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
DarrenZhang01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.