Skip to content

Commit 53006f6

Browse files
sbodensteinTorax team
authored andcommitted
Switch while_loop_bounded to use implementation='while_loop'.
PiperOrigin-RevId: 950859512
1 parent 02f6997 commit 53006f6

5 files changed

Lines changed: 52 additions & 56 deletions

File tree

torax/_src/orchestration/jit_run_loop.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""JITted run_loop for iterating over the simulation step function."""
16+
1617
import chex
1718
import jax
1819
import jax.numpy as jnp
@@ -62,7 +63,7 @@ def _step_fn(inputs):
6263
_step_fn,
6364
(initial_state, initial_post_processed_outputs),
6465
max_steps,
65-
implementation='scan',
66+
implementation='while_loop',
6667
)
6768

6869
# Prepend initial state to give (max_steps + 1, ...) output.
@@ -119,6 +120,8 @@ def run_loop(
119120
runtime_params_overrides: (
120121
build_runtime_params.RuntimeParamsProvider | None
121122
) = None,
123+
log_timestep_info: bool = False,
124+
progress_bar: bool = True,
122125
max_steps: int | None = None,
123126
) -> tuple[
124127
list[sim_state.SimState],
@@ -135,6 +138,9 @@ def run_loop(
135138
is). The state_history that run_simulation() outputs comes from these
136139
ToraxSimState objects.
137140
runtime_params_overrides: Optional runtime params overrides to use.
141+
log_timestep_info: If True, logs basic timestep info, like time, dt, on
142+
every step.
143+
progress_bar: If True, displays a progress bar.
138144
max_steps: Optional maximum number of steps to take. If not provided, then
139145
the maximum number of steps will be determined by the numerics.t_final and
140146
numerics.min_dt.
@@ -154,6 +160,16 @@ def run_loop(
154160
the last valid timestep.
155161
- The sim error state.
156162
"""
163+
164+
if progress_bar:
165+
raise NotImplementedError(
166+
'Progress bar is not supported with the jitted run loop.'
167+
)
168+
if log_timestep_info:
169+
raise NotImplementedError(
170+
'Log timestep info is not supported with the jitted run loop.'
171+
)
172+
157173
numerics = step_fn.runtime_params_provider.numerics
158174
if max_steps is None:
159175
max_steps = int(

torax/_src/orchestration/run_simulation.py

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def run_simulation(
105105
log_timestep_info: bool = False,
106106
progress_bar: bool = True,
107107
max_steps: int | None = None,
108+
_use_jitted_run_loop: bool = False, # pylint: disable=invalid-name
108109
) -> tuple[xr.DataTree, output.StateHistory]:
109110
"""Runs a TORAX simulation using the config and returns the outputs.
110111
@@ -114,6 +115,8 @@ def run_simulation(
114115
progress_bar: Whether to show a progress bar.
115116
max_steps: The maximum number of steps to take, if not provided, then the
116117
simulation will run until the maximum time is reached.
118+
_use_jitted_run_loop: If True, then a jitted run loop will be used. A
119+
temporary private argument used for testing.
117120
118121
Returns:
119122
A tuple of the simulation outputs in the form of a DataTree and the state
@@ -128,14 +131,27 @@ def run_simulation(
128131
step_fn,
129132
) = prepare_simulation(torax_config)
130133

131-
state_history, post_processed_outputs_history, sim_error = run_loop.run_loop(
132-
initial_state=initial_state,
133-
initial_post_processed_outputs=post_processed_outputs,
134-
step_fn=step_fn,
135-
log_timestep_info=log_timestep_info,
136-
progress_bar=progress_bar,
137-
max_steps=max_steps,
138-
)
134+
if _use_jitted_run_loop:
135+
136+
state_history, post_processed_outputs_history, sim_error = (
137+
jit_run_loop.run_loop(
138+
step_fn,
139+
max_steps=max_steps,
140+
log_timestep_info=log_timestep_info,
141+
progress_bar=progress_bar,
142+
)
143+
)
144+
else:
145+
state_history, post_processed_outputs_history, sim_error = (
146+
run_loop.run_loop(
147+
initial_state=initial_state,
148+
initial_post_processed_outputs=post_processed_outputs,
149+
step_fn=step_fn,
150+
log_timestep_info=log_timestep_info,
151+
progress_bar=progress_bar,
152+
max_steps=max_steps,
153+
)
154+
)
139155

140156
state_history = output.StateHistory(
141157
state_history=state_history,
@@ -148,45 +164,3 @@ def run_simulation(
148164
state_history.simulation_output_to_xr(),
149165
state_history,
150166
)
151-
152-
153-
def run_simulation_jitted(
154-
torax_config: model_config.ToraxConfig,
155-
max_steps: int | None = None,
156-
) -> tuple[xr.DataTree, output.StateHistory]:
157-
"""Runs a TORAX simulation using the config and returns the outputs.
158-
159-
NOTE: This function doesn't guarantee that the simulation will complete. If
160-
the simulation does not complete successfully, then the state history will
161-
contain the error state `SimError.DID_NOT_REACH_T_FINAL` and a truncated
162-
simulation history.
163-
164-
Args:
165-
torax_config: The TORAX config to use for the simulation.
166-
max_steps: The maximum number of steps to take, if not provided, then the
167-
maximum number of steps will be determined by the numerics.t_final and
168-
numerics.min_dt.
169-
170-
Returns:
171-
A tuple of the simulation outputs in the form of a DataTree and the state
172-
history which is intended for helpful use with debugging as it contains
173-
the `CoreProfiles`, `CoreTransport`, `CoreSources`, `Geometry`, and
174-
`PostProcessedOutputs` dataclasses for each step of the simulation.
175-
"""
176-
step_fn = make_step_fn(torax_config)
177-
states_history, post_processed_outputs_history, sim_error = (
178-
jit_run_loop.run_loop(
179-
step_fn,
180-
max_steps=max_steps,
181-
)
182-
)
183-
state_history = output.StateHistory(
184-
state_history=states_history,
185-
post_processed_outputs_history=post_processed_outputs_history,
186-
sim_error=sim_error,
187-
torax_config=torax_config,
188-
)
189-
return (
190-
state_history.simulation_output_to_xr(),
191-
state_history,
192-
)

torax/_src/simulation_app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ def run(_):
5050
logging.set_verbosity(logging.INFO)
5151
app.run(run)
5252
"""
53+
5354
from collections.abc import Sequence
5455
import datetime
5556
import enum
5657
import os
58+
5759
import shutil
5860
import sys
5961
from typing import Callable, Final
@@ -154,6 +156,7 @@ def main(
154156
log_sim_output: bool = False,
155157
plot_sim_progress: bool = False,
156158
log_sim_progress_bar: bool = True,
159+
_use_jitted_run_loop: bool = False,
157160
) -> str:
158161
"""Runs a simulation obtained via `get_config`.
159162
@@ -175,6 +178,8 @@ def main(
175178
plot_sim_progress: If True, then a plotting spectator will be attached to
176179
the sim.
177180
log_sim_progress_bar: If True, then a progress bar will be logged.
181+
_use_jitted_run_loop: If True, then a jitted run loop will be used. A
182+
temporary private argument used for testing.
178183
179184
Returns:
180185
The output state file path.
@@ -187,6 +192,7 @@ def main(
187192
torax_config,
188193
log_sim_progress,
189194
progress_bar=log_sim_progress_bar,
195+
_use_jitted_run_loop=_use_jitted_run_loop,
190196
)
191197

192198
# Check if simulation encountered an error

torax/experimental/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from torax._src.orchestration.initial_state import get_initial_state_and_post_processed_outputs
2222
from torax._src.orchestration.jit_run_loop import run_loop_jit
2323
from torax._src.orchestration.run_simulation import make_step_fn
24-
from torax._src.orchestration.run_simulation import run_simulation_jitted
2524
from torax._src.orchestration.sim_state import SimState
2625
from torax._src.orchestration.step_function import SimulationStepFn
2726
from torax._src.plotting.plotruns_lib import create_plotly_figure
@@ -44,5 +43,4 @@
4443
'get_initial_state_and_post_processed_outputs',
4544
'SimState',
4645
'ExtendedLengyelConfig',
47-
'run_simulation_jitted',
4846
]

torax/tests/jit_sim_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ def test_run_simulation_jitted(
275275
torax_config = config_loader.build_torax_config_from_file(config_path)
276276
reference_file = output.load_state_file(data_path)
277277

278-
xr_data_tree, _ = run_simulation.run_simulation_jitted(torax_config)
278+
xr_data_tree, _ = run_simulation.run_simulation(
279+
torax_config, progress_bar=False, _use_jitted_run_loop=True
280+
)
279281

280282
# Allow for small numerical differences due to the change in the order of
281283
# operations in the jitted versus non-jitted case.
@@ -290,8 +292,8 @@ def test_did_not_reach_t_final_error_when_max_steps_too_low(self):
290292
torax_config = config_loader.build_torax_config_from_file(config_path)
291293

292294
# Use max_steps=1 which is far too few to reach t_final=1.
293-
_, state_history = run_simulation.run_simulation_jitted(
294-
torax_config, max_steps=1
295+
_, state_history = run_simulation.run_simulation(
296+
torax_config, max_steps=1, progress_bar=False, _use_jitted_run_loop=True
295297
)
296298

297299
self.assertEqual(

0 commit comments

Comments
 (0)