Skip to content

Apply vmap jnp.pad over the batch in REFLECT convolution mode #4640

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sarlinpe
Copy link
Contributor

What does this PR do?

This PR fixes an error occurring when exporting flax models with jax2tf, polymorphic batch size, and REFLECT padding mode in convolutions. This issue can be minimally reproduced as

import jax.numpy as jnp
import tensorflow as tf
from jax.experimental import jax2tf

def fn_jax(x):
  return jnp.pad(x, pad_width=[(0, 0), (1, 1), (1, 1)], mode='reflect')

fn_tf = jax2tf.convert(fn_jax, polymorphic_shapes=["(b, 14, 14)"])
fn_tf = tf.function(fn_tf, autograph=False, jit_compile=True)
fn_tf.get_concrete_function(tf.TensorSpec([None, 14, 14], tf.float32))

which results in

ValueError: Shape polymorphism is supported for jnp.pad with 'reflect' or 'symmetric' padding mode only when it is possible to determine at lowering time that the axis size (= b) is larger than 1 and larger or equal than the padding length (= 0). Error while handling left padding on axis 0.

Applying jax.vmap(functools.partial(jnp.pad, pad_width=[(1, 1), (1, 1)], mode='reflect'))(x) fixes the issue.

Checklist

  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other
    checks if that's the case).
  • This change is discussed in a Github issue/
    discussion (please add a
    link).
  • The documentation and docstrings adhere to the
    documentation guidelines.
  • This change includes necessary high-coverage tests.
    (No quality testing = no merge!)

@sarlinpe sarlinpe force-pushed the sarlinpe/vmap-pad-reflect branch from 893e936 to d448f1a Compare March 26, 2025 23:17
@sarlinpe
Copy link
Contributor Author

@cgarciae It would be great to receive your review, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant