Phase space backends#
Introduction#
tensorwaves#599 lets PhaseSpaceGenerator and WeightedPhaseSpaceGenerator generate samples with any backend of phasespace 2.0: NumPy (the default), TensorFlow, or JAX. All backends return NumPy arrays, so they can be swapped without changing the rest of an analysis. This report compares how fast the backends are and checks that they generate the same distributions. The generators below define which backends are compared; all tables and figures loop over them.
initial_state_mass = 3.0969
final_state_masses = {0: 0.1350, 1: 0.1396, 2: 0.4937}
generators = {
"numpy": (
PhaseSpaceGenerator(initial_state_mass, final_state_masses, backend="numpy"),
NumpyUniformRNG(seed=0),
),
"tensorflow": (
PhaseSpaceGenerator(
initial_state_mass, final_state_masses, backend="tensorflow"
),
TFUniformRealNumberGenerator(seed=0),
),
"jax": (
PhaseSpaceGenerator(initial_state_mass, final_state_masses, backend="jax"),
NumpyUniformRNG(seed=0),
),
}
for generator, _ in generators.values():
generator.show_progress = False
Single sample#
Every backend is used through the same PhaseSpaceGenerator.generate method. TensorFlow and JAX compile functions the first time they are called, so we generate two samples per backend and record the time of each. The timings depend on the machine that executed this report.
n_events = 100_000
samples = {}
timings = {}
for name, (generator, rng) in generators.items():
start = time.perf_counter()
samples[name] = generator.generate(n_events, rng)
first_call = time.perf_counter() - start
start = time.perf_counter()
generator.generate(n_events, rng)
timings[name] = first_call, time.perf_counter() - start
| Array type | Shape | First call [s] | Second call [s] | |
|---|---|---|---|---|
| numpy | ndarray | (100000, 4) | 0.270 | 0.253 |
| tensorflow | ndarray | (100000, 4) | 0.887 | 0.134 |
| jax | ndarray | (100000, 4) | 0.794 | 0.138 |
Comparing distributions#
The events of different backends are not identical, not even with the same seed, but they should follow the same distribution. A uniform phase space sample shows up as a flat Dalitz plot within the kinematic boundary, and the invariant mass projections should agree between backends.
Four-body decays#
A three-body decay has two independent kinematic variables, so its phase space fits in a single Dalitz plot. A four-body decay has five, and no single plot shows them all. A common choice is the set of Cabibbo–Maksymowicz variables, which split the decay into two pairs of particles:
the invariant masses \(m_{01}\) and \(m_{23}\) of the two pairs,
the helicity angles \(\theta_{01}\) and \(\theta_{23}\) of particles \(0\) and \(2\) in the rest frames of their pairs,
the angle \(\phi\) between the two decay planes.
For a uniform phase space sample, \(\cos\theta_{01}\), \(\cos\theta_{23}\) and \(\phi\) are distributed uniformly, while the two masses are not. As an example, we generate \(D^0 \to K^-\pi^+\pi^+\pi^-\) with each backend.
four_body_masses = {0: 0.4937, 1: 0.1396, 2: 0.1396, 3: 0.1396}
four_body_samples = {}
for name, (_, rng) in generators.items():
generator = PhaseSpaceGenerator(1.8648, four_body_masses, backend=name)
generator.show_progress = False
four_body_samples[name] = generator.generate(n_events, rng)
The variables are not independent in their ranges: the sum of the two pair masses cannot exceed the mass of the decaying particle. A two-dimensional histogram of \(m_{01}\) against \(m_{23}\) shows this kinematic boundary, which plays a role similar to the boundary of the Dalitz plot.
Benchmark#
The timings above come from a single sample. For a more systematic comparison, we measure how long each backend takes to generate a weighted sample with WeightedPhaseSpaceGenerator, as a function of the sample size and of the number of final state particles. The hit-and-miss step of PhaseSpaceGenerator is left out, because it is the same for all backends. Each measurement is preceded by one call of the same size that is not timed, so that compilation does not count, and the fastest of three repetitions is shown.
Conclusion#
NumPy is the default choice, because it needs no additional dependencies and no compilation. TensorFlow and JAX pay for compilation on the first call, but are several times faster afterwards, as the benchmark shows. They are therefore worth it when generating large or many samples for the same decay. TensorFlow reproduces samples from earlier TensorWaves versions, when combined with a TFUniformRealNumberGenerator. JAX can be useful if the rest of the analysis already uses it.
There are two things to keep in mind:
The same seed gives a different sample on each backend. Samples are only reproducible within one backend.
phasespaceselects its backend per process, and a generator activates its backend each time it generates. Each switch makesphasespacerecompile, so alternating between backends is slower than the timings above suggest, and generators with different backends should not be used concurrently in threads.