Skip to content

fix: better init of spergel HLR and tabulate xValue#258

Draft
beckermr wants to merge 19 commits into
mainfrom
test-spergel
Draft

fix: better init of spergel HLR and tabulate xValue#258
beckermr wants to merge 19 commits into
mainfrom
test-spergel

Conversation

@beckermr

Copy link
Copy Markdown
Collaborator

This PR has a new way to initialize the spergel HLR and a better way to compute autodiff using implicit differentiation.

@codspeed-hq

codspeed-hq Bot commented Jun 23, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by ×2.6

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 4 improved benchmarks
❌ 3 regressed benchmarks
✅ 29 untouched benchmarks
🆕 2 new benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_benchmark_spergel_init[run] 7.4 µs 40.3 µs -81.77%
Simulation test_benchmark_spergel_init[run] 104.1 µs 267.2 µs -61.02%
Simulation test_benchmarks_lanczos_interp[xval-no_conserve_dc-run] 828.5 µs 1,248.5 µs -33.64%
Simulation test_benchmark_spergel_xvalue[run] 34,862.4 ms 310 ms ×110
WallTime test_benchmark_spergel_xvalue[run] 1,819.6 ms 37.6 ms ×48
WallTime test_benchmark_spergel_calcfluxrad[run] 225.7 µs 121.6 µs +85.6%
Simulation test_benchmark_spergel_calcfluxrad[run] 1,090.6 µs 626.3 µs +74.13%
🆕 Simulation test_benchmark_spergel_init_float[run] N/A 104.2 µs N/A
🆕 WallTime test_benchmark_spergel_init_float[run] N/A 7.5 µs N/A

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing test-spergel (ef1603d) with main (9b25b7d)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (f08967b) during the generation of this report, so 9b25b7d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Comment thread jax_galsim/spergel.py Outdated
@beckermr beckermr changed the title fix: better init of spergel HLR fix: better init of spergel HLR and tabulate xValue Jun 25, 2026
@ismael-mendoza

ismael-mendoza commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

hello - i started looking into what you requested in slack.

Regarding the image artifacts with slen=101 for main and this PR respectively (the plots show the residual between galsim - jax_galsim):

image image

but using slen=201 (again main branch and then this PR):

image image

I used the same code that's here to test. I don't understand why the residual changes so much in this PR after doubling the slen because GalSim reports that the good size is 52. Do you have any insight? (although in both cases it's smaller than what we had before so maybe it's ok anyways? )

@ismael-mendoza

Copy link
Copy Markdown
Collaborator

re the regression when nu is not constant - I could not find any. Perhaps I misunderstood but here is the code I was using to test this in my laptop.

import numpy as np
import jax_galsim as jgs

def _draw_spergel_jgs(
    params: dict, nu: float | Array, *, psf: jgs.GSObject, fft_size: int, slen: int
):
    gsparams = jgs.GSParams(minimum_fft_size=fft_size, maximum_fft_size=fft_size)
    prof = jgs.Spergel(nu=nu, flux=params["flux"], half_light_radius=params["hlr"])
    prof = prof.shear(q=params["q"], beta=params["beta"] * jgs.degrees)
    gal = jgs.Convolve([prof, psf]).withGSParams(gsparams)
    return gal.drawImage(nx=slen, ny=slen, scale=0.2, dtype=jnp.float64).array

# let's average the time over 1000 runs each while varying the parameters
# but fixing the PSF


times1 = []
times2 = []

xpsf = jgs.Gaussian(half_light_radius=0.7, flux=1.0)

draw_jax_nu_float = jit(
    partial(_draw_spergel_jgs, nu=-0.6, psf=xpsf, fft_size=256, slen=201)
)
draw_jax_nu_array = jit(partial(_draw_spergel_jgs, psf=xpsf, fft_size=256, slen=201))


for ii in range(1000):
    params = {
        "flux": np.random.uniform(1, 3, size=()).item(),
        "hlr": np.random.uniform(0.4, 0.5, size=()).item(),
        "q": 1.0,
        "beta": 0.0,
    }
    _nu = np.random.uniform(-0.55, -0.65, size=()).item()
    _nu = device_put(jnp.array(_nu))
    params_jax = device_put(params)

    # compilation
    if ii == 0:
        draw_jax_nu_float(params_jax)
        draw_jax_nu_array(params_jax, _nu)

    # timing
    t1 = time.time()
    with transfer_guard("disallow"):
        _ = block_until_ready(draw_jax_nu_float(params_jax))
    t2 = time.time()
    times1.append(t2 - t1)

    t1 = time.time()
    with transfer_guard("disallow"):
        _ = block_until_ready(draw_jax_nu_array(params_jax, _nu))
    t2 = time.time()
    times2.append(t2 - t1)


t_nu_float = np.mean(times1)
t_nu_array = np.mean(times2)
print(f"Average time nu float: {t_nu_float:.4f}")
print(f"Average time nu array: {t_nu_array:.4f}")

Did I miss something?

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.

2 participants