Repository navigation
fix(materials): a real negative permittivity takes the decaying root under jax 0.12.1 - #257
Merged
Merged
Conversation
…under jax 0.12.1 refractive_index_from_eps promoted a real array to complex as eps * (1 + 0j). jax and jaxlib 0.12.1 form that real-times-complex product as (x * 1, x * 0), and x * 0 is -0.0 for a negative x: the value then sits on the lower lip of the square root's cut, and principal_sqrt -- which honours the sign of a zero imaginary part -- returns the gain root. refractive_index_from_eps(jnp.asarray(-2.0)) gave -1.4142j, eagerly and under jax.jit, where 0.12.0.1 gave +1.4142j. A user installing the newest jax got a gain medium for a lossless metal written as a real number. The promotion is now eps + 0j: the same width-preserving promotion (float32 -> complex64), and an imaginary part of +0.0 on every backend, since -0.0 + 0.0 and 0.0 + 0.0 are both +0.0. Measured: NumPy, PyTorch, and JAX 0.12.0.1 and 0.12.1, eager and jit, float32 and float64 -- no sign bit set. It is the only such promotion in src/. A new test asks for Im(n) > 0 and the caller's width on NumPy, JAX eager, JAX jit and PyTorch at 32 and 64 bits. Checked: tests/materials/test_material.py with jax 0.12.0.1 (the recorded version; 40 + 8 passed) and with jax 0.12.1 in a separate environment, PYTHONPATH=src, no torch (the jax cases pass, torch skipped). Before the fix, test_refractive_index_from_eps_runs_on_a_jax_array fails there, as it did in CI run 45438350938. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RKzHyBMczzzhKMXEVgazPy
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Under jax and jaxlib 0.12.1,
refractive_index_from_epsreturned the gain root for a real negative permittivity:refractive_index_from_eps(jnp.asarray(-2.0))gave −1.4142j, eagerly and underjax.jit. A user installing the newest jax got a gain medium for a lossless metal written as a real number. This is one of the three failures #251 found on 0.12.1.Cause and fix
eps * (1 + 0j). jax 0.12.1 forms a real-times-complex product as(x * 1, x * 0), so a negativexgets the imaginary part −0.0. That puts the value on the lower lip of the square root's cut, andprincipal_sqrt, which honours the sign of a zero, returns the gain root. jax 0.12.0.1 gave +0.0.eps + 0j. It keeps the caller's width (float32 → complex64), and the imaginary part is +0.0 on every backend, since −0.0 + 0.0 and 0.0 + 0.0 are both +0.0. I measured NumPy, PyTorch, and JAX 0.12.0.1 and 0.12.1, eager and jit, float32 and float64. This is the only such promotion insrc/.Im(n) > 0at the caller's width on NumPy, JAX eager, JAX jit and PyTorch, at 32 and 64 bits.Checks
refractive_index_from_epsgive 828 passed and 36 skipped. The quick profile passes (17), andruff check .passes.PYTHONPATH=src, no torch):tests/materials/test_material.pypasses. Before the fix,test_refractive_index_from_eps_runs_on_a_jax_arrayfails there, as it did in CI run 45438350938.Not done here
.github/constraints/still records jax 0.12.0.1, so this PR's CI runs the fix on that version only; I checked 0.12.1 locally.test_a_layer_with_no_chain_keeps_its_derivative_bitsreproduces locally. The forward value keeps its bits, but the jit gradient with and without the chain capacity differs by 1–2 ulp (3.7e-16 relative on ETM, 1.7e-15 on SMM). The difference goes away when the chain term is computed but not added; selectingLinstead of adding −0 does not restore the bits. Finding the cause needs the compiled backward pass.test_a_traced_uniform_twin_is_the_eager_answer[theta 1.4](1.0168e-13 on CI, against a bound of 1e-13) does not reproduce here. Traced against eager is 3.5e-14 with 0.12.1, and 6.4e-14 with XLA and OpenBLAS limited to AVX2.Generated by Claude Code