-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspherical.py
151 lines (134 loc) · 3.82 KB
/
spherical.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Benchmarks for on-the-fly spherical transforms."""
import numpy as np
from benchmarking import (
BenchmarkSetup,
benchmark,
parse_args_collect_and_run_benchmarks,
skip,
)
import s2fft
from s2fft.recursions.price_mcewen import generate_precomputes_jax
L_VALUES = [8, 16, 32, 64, 128, 256]
L_LOWER_VALUES = [0]
SPIN_VALUES = [0]
L_TO_NSIDE_RATIO_VALUES = [2]
SAMPLING_VALUES = ["mw"]
METHOD_VALUES = ["numpy", "jax"]
REALITY_VALUES = [True]
SPMD_VALUES = [False]
N_ITER_VALUES = [None]
def _jax_arrays_to_numpy(precomps):
return [np.asarray(p) for p in precomps]
def _get_nside(sampling, L, L_to_nside_ratio):
return None if sampling != "healpix" else L // L_to_nside_ratio
def setup_forward(
method, L, L_lower, sampling, spin, L_to_nside_ratio, reality, spmd, n_iter
):
if reality and spin != 0:
skip("Reality only valid for scalar fields (spin=0).")
if spmd and method != "jax":
skip("GPU distribution only valid for JAX.")
rng = np.random.default_rng()
flm = s2fft.utils.signal_generator.generate_flm(rng, L, spin=spin, reality=reality)
nside = _get_nside(sampling, L, L_to_nside_ratio)
f = s2fft.transforms.spherical.inverse(
flm,
L=L,
spin=spin,
nside=nside,
sampling=sampling,
method=method,
reality=reality,
spmd=spmd,
L_lower=L_lower,
)
precomps = generate_precomputes_jax(
L, spin, sampling, nside=nside, forward=True, L_lower=L_lower
)
if method == "numpy":
precomps = _jax_arrays_to_numpy(precomps)
return BenchmarkSetup({"f": f, "precomps": precomps}, flm, "jax" in method)
@benchmark(
setup_forward,
method=METHOD_VALUES,
L=L_VALUES,
L_lower=L_LOWER_VALUES,
sampling=SAMPLING_VALUES,
spin=SPIN_VALUES,
L_to_nside_ratio=L_TO_NSIDE_RATIO_VALUES,
reality=REALITY_VALUES,
spmd=SPMD_VALUES,
n_iter=N_ITER_VALUES,
)
def forward(
f,
precomps,
method,
L,
L_lower,
sampling,
spin,
L_to_nside_ratio,
reality,
spmd,
n_iter,
):
return s2fft.transforms.spherical.forward(
f=f,
L=L,
L_lower=L_lower,
precomps=precomps,
spin=spin,
nside=_get_nside(sampling, L, L_to_nside_ratio),
sampling=sampling,
reality=reality,
method=method,
spmd=spmd,
iter=n_iter,
)
def setup_inverse(method, L, L_lower, sampling, spin, L_to_nside_ratio, reality, spmd):
if reality and spin != 0:
skip("Reality only valid for scalar fields (spin=0).")
if spmd and method != "jax":
skip("GPU distribution only valid for JAX.")
rng = np.random.default_rng()
flm = s2fft.utils.signal_generator.generate_flm(rng, L, spin=spin, reality=reality)
precomps = generate_precomputes_jax(
L,
spin,
sampling,
nside=_get_nside(sampling, L, L_to_nside_ratio),
forward=False,
L_lower=L_lower,
)
if method == "numpy":
precomps = _jax_arrays_to_numpy(precomps)
return BenchmarkSetup({"flm": flm, "precomps": precomps}, None, "jax" in method)
@benchmark(
setup_inverse,
method=METHOD_VALUES,
L=L_VALUES,
L_lower=L_LOWER_VALUES,
sampling=SAMPLING_VALUES,
spin=SPIN_VALUES,
L_to_nside_ratio=L_TO_NSIDE_RATIO_VALUES,
reality=REALITY_VALUES,
spmd=SPMD_VALUES,
)
def inverse(
flm, precomps, method, L, L_lower, sampling, spin, L_to_nside_ratio, reality, spmd
):
return s2fft.transforms.spherical.inverse(
flm=flm,
L=L,
L_lower=L_lower,
precomps=precomps,
spin=spin,
nside=_get_nside(sampling, L, L_to_nside_ratio),
sampling=sampling,
reality=reality,
method=method,
spmd=spmd,
)
if __name__ == "__main__":
results = parse_args_collect_and_run_benchmarks()