Skip to content

Validate block sizes in depth-space transforms - #155

Open
sylvesterkaczmarek wants to merge 1 commit into
google-deepmind:masterfrom
sylvesterkaczmarek:fix/depth-space-block-validation
Open

sylvesterkaczmarek wants to merge 1 commit into
google-deepmind:masterfrom
sylvesterkaczmarek:fix/depth-space-block-validation

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown
Contributor

Fixes #154.

Problem

depth_to_space and space_to_depth document a block size of at least 2, but accept values that violate this contract. A zero value reaches integer division and raises ZeroDivisionError; negative values fail during JAX reshaping; and a size of 1 is silently interpreted as an identity operation. Floating-point and boolean block sizes similarly bypass consistent validation. These errors also arise for batched inputs, where the functions map themselves over images.

Change

Introduce one shared block-size validator and call it before shape arithmetic or batching. Accept Python and NumPy integer values of at least 2. Reject booleans, fractions, zero and negative values with a clear ValueError identifying block_size.

For four-dimensional inputs, capture the already validated Python integer in the jax.vmap closure. This avoids converting it into a traced scalar when the per-image function is called recursively, and preserves shape inference for JIT-compiled batches.

Verification

  • On unchanged master, 16 invalid-input regression cases fail: seven invalid sizes for each function and two batched cases.
  • After the change, 19 focused tests pass, including NumPy integer compatibility, an unbatched round-trip, a JIT-compiled batched round-trip and empty-batch shape preservation.
  • Broader API and interpolation selection: 259 passed, 40 skipped on macOS CPU.
  • Python compilation and git diff --check pass.

Scope and limitations

The tensor rearrangement formula, output shape for valid sizes and ordering of pixels are unchanged. The new validation concerns the documented Python integer argument, which must be static for JIT shape construction. TensorFlow-dependent test modules were not executed locally.

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.

Depth/space transforms accept invalid block sizes and fail inside JAX

1 participant