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

Feature request: lingvo.jax.asserts.HasShape #332

Open
drpngx opened this issue Sep 8, 2023 · 1 comment
Open

Feature request: lingvo.jax.asserts.HasShape #332

drpngx opened this issue Sep 8, 2023 · 1 comment

Comments

@drpngx
Copy link
Contributor

drpngx commented Sep 8, 2023

I tried

def AssertShape(x: jnp.array, shape) -> None:
  if not jnp.array_equal(x.shape, shape):
    raise ValueError(f'Shape mismatch: found {x.shape}, expected: {shape}')

and got

jax.errors.TracerBoolConversionError: Attempted boolean conversion of traced array with shape bool[]..

(BTW, note the double period)

@drpngx
Copy link
Contributor Author

drpngx commented Sep 10, 2023

To answer my own question:

def AssertShape(x: jnp.array, expected_shape) -> None:
  # Shape must be a python element with static size.
  xs = x.shape
  if len(xs) != len(expected_shape):
    raise ValueError(
      f'Wrong rank: got [{len(xs)}] {x.shape}, expected [{len(expected_shape)}] {expected_shape}')
  for k, d in enumerate(expected_shape):
    if xs[k] != d:
      raise ValueError(f'Wrong shape at dim {k}: got {x.shape}, expected {expected_shape}')

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

No branches or pull requests

1 participant