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

Support more numpy ops. #197

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Support more numpy ops.
PiperOrigin-RevId: 644407715
  • Loading branch information
shaobohou authored and TF2JAXDev committed Jun 18, 2024
commit 3551a0590dcc543ef7ea36c9643480fee97bc33a
5 changes: 5 additions & 0 deletions tf2jax/_src/numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ def get_jax_dtype(dtype: tf.DType):


# Simple numeric ops.
abs_ = lambda x: _get_np(x).abs(x)
add = lambda x, y: _get_np(x, y).add(x, y)
equal = lambda x, y: _get_np(x, y).equal(x, y)
floor_divide = lambda x, y: _get_np(x, y).floor_divide(x, y)
greater = lambda x, y: _get_np(x, y).greater(x, y)
greater_equal = lambda x, y: _get_np(x, y).greater_equal(x, y)
less = lambda x, y: _get_np(x, y).less(x, y)
less_equal = lambda x, y: _get_np(x, y).less_equal(x, y)
logical_and = lambda x, y: _get_np(x, y).logical_and(x, y)
logical_not = lambda x: _get_np(x).logical_not(x)
logical_or = lambda x, y: _get_np(x, y).logical_or(x, y)
maximum = lambda x, y: _get_np(x, y).maximum(x, y)
minimum = lambda x, y: _get_np(x, y).minimum(x, y)
mod = lambda x, y: _get_np(x, y).mod(x, y)
Expand All @@ -117,6 +121,7 @@ def get_jax_dtype(dtype: tf.DType):
negative = lambda x: _get_np(x).negative(x)
not_equal = lambda x, y: _get_np(x, y).not_equal(x, y)
reciprocal = lambda x: _get_np(x).reciprocal(x)
sign = lambda x: _get_np(x).sign(x)
subtract = lambda x, y: _get_np(x, y).subtract(x, y)
true_divide = lambda x, y: _get_np(x, y).true_divide(x, y)

Expand Down
10 changes: 5 additions & 5 deletions tf2jax/_src/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def wrapped(proto):


_jax_ops = {
"Abs": _get_jax_op(jnp.abs, {"T"}),
"Abs": _get_jax_op(anp.abs_, {"T"}),
"Add": _get_jax_op(anp.add, {"T"}),
"AddN": _get_jax_op(
lambda *args: anp.sum_(anp.stack(args, axis=0), axis=0, keepdims=False),
Expand Down Expand Up @@ -141,9 +141,9 @@ def wrapped(proto):
"Lgamma": _get_jax_op(jax.lax.lgamma, {"T"}),
"Log": _get_jax_op(jnp.log, {"T"}),
"Log1p": _get_jax_op(jnp.log1p, {"T"}),
"LogicalAnd": _get_jax_op(jnp.logical_and, {"T"}),
"LogicalNot": _get_jax_op(jnp.logical_not, {"T"}),
"LogicalOr": _get_jax_op(jnp.logical_or, {"T"}),
"LogicalAnd": _get_jax_op(anp.logical_and, {"T"}),
"LogicalNot": _get_jax_op(anp.logical_not, {"T"}),
"LogicalOr": _get_jax_op(anp.logical_or, {"T"}),
"Minimum": _get_jax_op(anp.minimum, {"T"}),
"Maximum": _get_jax_op(anp.maximum, {"T"}),
"Mul": _get_jax_op(anp.multiply, {"T"}),
Expand Down Expand Up @@ -174,7 +174,7 @@ def wrapped(proto):
"Rsqrt": _get_jax_op(jax.lax.rsqrt, {"T"}),
"Shape": _get_jax_op(lambda x: np.array(jnp.shape(x)), {"T", "out_type"}),
"Sigmoid": _get_jax_op(jax.nn.sigmoid, {"T"}),
"Sign": _get_jax_op(jnp.sign, {"T"}),
"Sign": _get_jax_op(anp.sign, {"T"}),
"Sin": _get_jax_op(jnp.sin, {"T"}),
"Sinh": _get_jax_op(jnp.sinh, {"T"}),
"Size": _get_jax_op(lambda x: np.prod(jnp.shape(x), dtype=np.int32),
Expand Down
22 changes: 22 additions & 0 deletions tf2jax/_src/ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ def tf_func(x):
return getattr(tf.raw_ops, op_name)(x=x)
self._test_convert(tf_func, inputs)

@chex.variants(with_jit=True, without_jit=True)
@parameterized.parameters("Abs", "Sign")
def test_unary_numerics_static(self, op_name):
inputs = np.array([4.0, 3.0]).astype(np.float32)

def tf_static():
vals = getattr(tf.raw_ops, op_name)(x=inputs)
return tf.zeros(tf.cast(vals, tf.int32))
self._test_convert(tf_static, [])

@chex.variants(with_jit=True, without_jit=True)
@parameterized.parameters("Atan2", "Atan2")
def test_binary_numerics(self, op_name):
Expand Down Expand Up @@ -238,6 +248,18 @@ def tf_func(x, y):
return getattr(tf.raw_ops, op_name)(**kwargs)
self._test_convert(tf_func, inputs)

# Check static inputs result in static outputs.
def tf_static():
if op_name == "LogicalNot":
vals = tf_func(x=np.array([True, False]), y=None)
else:
vals = tf_func(
x=np.array([True, False, True, False]),
y=np.array([False, False, True, True]),
)
return tf.zeros(tf.cast(vals, tf.int32))
self._test_convert(tf_static, [])

@chex.variants(with_jit=True, without_jit=True)
@parameterized.parameters("LeftShift", "RightShift")
def test_shift(self, op_name):
Expand Down
Loading