Skip to content

float32 consistency - #267

Open
b-biswas wants to merge 23 commits into
GalSim-developers:mainfrom
b-biswas:dtype-consistency
Open

b-biswas wants to merge 23 commits into
GalSim-developers:mainfrom
b-biswas:dtype-consistency

Conversation

@b-biswas

@b-biswas b-biswas commented Sep 24, 2026 •

Copy link
Copy Markdown

While trying to run JAX metadetection in float32, I noticed that the FFT kernels were still using float64 internally due to JAX-Galsim. Some small data type fixes seem to solve this issue.
Note: Running on float32 is significantly faster!

TODO:

  • Write an explicit test to check if things are working as expected
  • Check differences wrt original gasim

@codspeed

codspeed Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ 12 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 38 untouched benchmarks


Comparing b-biswas:dtype-consistency (139b9d9) with main (10add4c)

Open in CodSpeed

@ismael-mendoza

Copy link
Copy Markdown
Collaborator

Thanks @b-biswas for this PR! I haven't appreciated that the float32 does not propagate correctly in this case. Let me know if a review would be heplful whenever

@b-biswas

Copy link
Copy Markdown
Author

@ismael-mendoza let's talk about it in the next meeting... I will write some tests meanwhile!

@ismael-mendoza
ismael-mendoza marked this pull request as ready for review October 29, 2026 23:07
@b-biswas

Copy link
Copy Markdown
Author

Some tests are failing with the Interpolated images because galsim.InterpolatedImage converts image arrays to np.float64 even if the input image is float32 and now jax_galsim.InterpolatedImage keeps them as float32 leading to numerical instabilities ~ $10^7$

@beckermr

Copy link
Copy Markdown
Collaborator

We'll need to make sure we run some tests in 64 and have others where we run in 32 and test at a reduced precision.

Comment thread tests/jax/test_image_jax.py Outdated


def test_image_dtype_tracks_storage_with_x64_disabled():
original_x64 = jax.config.read("jax_enable_x64")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. You'll need to use a separate python job for a float32 test.

@ismael-mendoza

Copy link
Copy Markdown
Collaborator

apologies, I accidentally pushed an old branch without rebasing Biswajit's changes. Working on a fix...

Comment thread jax_galsim/image.py
obj._array = children[0]
obj.wcs = children[1]
obj._bounds = children[2]
obj._dtype = aux_data["dtype"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove dtype from aux_data above if we are doing this.

else:
image = self._jax_children[0].copy(dtype=float)
# Preserve the input dtype
image = self._jax_children[0].copy()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do need to cast to float for int input images here.

@beckermr beckermr Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
image = self._jax_children[0].copy()
if self._jax_children[0].dtype == jnp.float32:
image = self._jax_children[0].copy(dtype=jnp.float32)
else:
image = self._jax_children[0].copy(dtype=float)

Comment thread jax_galsim/image.py
dk = jnp.pi / (No2 * dx)

# Use complex64 for float32 inputs, complex128 for float64
if self.dtype == jnp.float32:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block might ask for a type that is not available if jax doesn't have float64 enabled and the dtype of the image is int32. I think we need to add specific handling for all of the types.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For int32 with x64 not enabled, this should raise a warning that x64 is not enabled and use float32 but perhaps we should have something more explicit here.
With int32 images, we should ideally go for float64 ffts because of the dynamic range, right?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes good point on the dynamic range. We shouldn't raise a warning if we don't need to, but I cannot find an alias in jax to the largest width complex type available. Sigh.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is simpler to leave it as is and let the warnings go through.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe jax would accept dtype=complex so that it uses the biggest type it has and doesn't raise a warning if it doesn't need to?

@beckermr beckermr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few casting cases for int images that we missed.

Another comment is that we should add a section in the docs on the specific casting rules we use globally, and make sure we actually use them. Does int32 always cast to float32 when needed? What about uint32, uint64 etc.

This branch has not been deployed

No deployments
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.

3 participants