-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrun_simulation_main.py
545 lines (497 loc) · 17.5 KB
/
run_simulation_main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# Copyright 2024 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""Main entrypoint for running transport simulation.
Example command with a configuration defined in Python:
python3 run_simulation_main.py \
--config='torax.tests.test_data.default_config' \
--log_progress
"""
from collections.abc import Sequence
import enum
import functools
import time
from absl import app
from absl import flags
from absl import logging
import jax
import torax
from torax import simulation_app
from torax.config import config_loader
from torax.plotting import plotruns_lib
from torax.torax_pydantic import model_config
# String used when prompting the user to make a choice of command
CHOICE_PROMPT = 'Your choice: '
# String used when prompting the user to make a yes / no choice
Y_N_PROMPT = 'y/n: '
# String used when printing how long the simulation took
SIMULATION_TIME = 'simulation time'
_PYTHON_CONFIG_MODULE = flags.DEFINE_string(
'config',
None,
'Module from which to import a python-based config. This program expects a '
'`get_sim()` function to be implemented in this module. Can either be '
'an absolute or relative path. See importlib.import_module() for more '
'information on how to use this flag and --config_package.',
)
_PYTHON_CONFIG_PACKAGE = flags.DEFINE_string(
'config_package',
None,
'If provided, it is the base package the --config is imported from. '
'This is required if --config is a relative path.',
)
_LOG_SIM_PROGRESS = flags.DEFINE_bool(
'log_progress',
False,
'If true, logs the time of each timestep as the simulation runs.',
)
_PLOT_SIM_PROGRESS = flags.DEFINE_bool(
'plot_progress',
False,
'If true, plots the time of each timestep as the simulation runs.'
' Note: this is temporarily disabled.',
)
_LOG_SIM_OUTPUT = flags.DEFINE_bool(
'log_output',
False,
'If True, logs extra information to stdout/stderr.',
)
_REFERENCE_RUN = flags.DEFINE_string(
'reference_run',
None,
'If provided, after the simulation is run, we can compare the last run to'
' the reference run.',
)
_USE_JAX_PROFILER = flags.DEFINE_bool(
'use_jax_profiler',
False,
'If True, use Jax profiler. Processing will stop and a URL to visualise'
' results will be generated. Processing will resume when the URL is opened.'
'See https://jax.readthedocs.io/en/latest/profiling.html.',
)
_QUIT = flags.DEFINE_bool(
'quit',
False,
'If True, quits after the first operation (no interactive mode).',
)
_OUTPUT_DIR = flags.DEFINE_string(
'output_dir',
None,
'If provided, overrides the default output directory.',
)
_PLOT_CONFIG_MODULE = flags.DEFINE_string(
'plot_config',
'torax.plotting.configs.default_plot_config', # Default
'Module path to the plot config.',
)
jax.config.parse_flags_with_absl()
@enum.unique
class _UserCommand(enum.Enum):
"""Options to do on every iteration of the script."""
RUN = ('RUN SIMULATION', 'r', simulation_app.AnsiColors.GREEN)
MODIFY_CONFIG = ('modify the existing config and reload it', 'mc')
CHANGE_CONFIG = ('provide a new config file to load', 'cc')
TOGGLE_LOG_SIM_PROGRESS = ('toggle --log_progress', 'tlp')
TOGGLE_PLOT_SIM_PROGRESS = ('toggle --plot_progress', 'tpp')
TOGGLE_LOG_SIM_OUTPUT = ('toggle --log_output', 'tlo')
PLOT_RUN = ('plot previous run(s) or against reference if provided', 'pr')
QUIT = ('quit', 'q', simulation_app.AnsiColors.RED)
def _prompt_user(config_module_str: str) -> _UserCommand:
"""Prompts the user for the next thing to do."""
simulation_app.log_to_stdout('\n')
simulation_app.log_to_stdout(
f'Using the config: {config_module_str}',
color=simulation_app.AnsiColors.YELLOW,
)
user_command = None
simulation_app.log_to_stdout('\n')
while user_command is None:
simulation_app.log_to_stdout(
'What would you like to do next?',
color=simulation_app.AnsiColors.BLUE,
)
for uc in _UserCommand:
if len(uc.value) == 3:
color = uc.value[2]
else:
color = simulation_app.AnsiColors.BLUE
simulation_app.log_to_stdout(f'{uc.value[1]}: {uc.value[0]}', color)
input_text = input(CHOICE_PROMPT)
text_to_uc = {uc.value[1]: uc for uc in _UserCommand}
input_text = input_text.lower().strip()
if input_text in text_to_uc:
user_command = text_to_uc[input_text]
else:
simulation_app.log_to_stdout(
'Unrecognized input. Try again.',
color=simulation_app.AnsiColors.YELLOW,
)
return user_command
def _maybe_update_config_module(
config_module_str: str,
) -> str:
"""Returns a possibly-updated config module to import."""
simulation_app.log_to_stdout(
f'Existing module: {config_module_str}',
color=simulation_app.AnsiColors.BLUE,
)
simulation_app.log_to_stdout(
'Would you like to change which file to import?',
color=simulation_app.AnsiColors.BLUE,
)
should_change = _get_yes_or_no()
if should_change:
logging.info('Updating the module.')
return input('Enter the new module to use: ').strip()
else:
logging.info('Continuing with %s', config_module_str)
return config_module_str
def _modify_config(
config_module_str: str,
) -> tuple[model_config.ToraxConfig, str] | None:
"""Returns a new ToraxConfig from the modified config module."""
simulation_app.log_to_stdout(
f'Change {config_module_str} to include new values.',
color=simulation_app.AnsiColors.BLUE,
)
simulation_app.log_to_stdout(
'Modify the config with new values, then enter "y".',
color=simulation_app.AnsiColors.BLUE,
)
simulation_app.log_to_stdout(
'Enter "n" to go back to the main menu without loading any changes.',
color=simulation_app.AnsiColors.BLUE,
)
proceed_with_run = _get_yes_or_no()
if not proceed_with_run:
return None
config_module = config_loader.import_module(
config_module_str, _PYTHON_CONFIG_PACKAGE.value
)
if not hasattr(config_module, 'CONFIG'):
raise ValueError(
f'Config module {config_module_str} does not have a CONFIG attribute.'
' Please use the basic config dict to build Sim.'
)
return model_config.ToraxConfig.from_dict(config_module.CONFIG)
def _change_config(
config_module_str: str,
) -> tuple[model_config.ToraxConfig, str]:
"""Returns a ToraxConfig from the new config module and the config file."""
config_module_str = _maybe_update_config_module(config_module_str)
simulation_app.log_to_stdout(
f'Change {config_module_str} to include new values. Any changes to '
'CONFIG will be picked up.',
color=simulation_app.AnsiColors.BLUE,
)
input('Press Enter when done changing the module.')
config_module = config_loader.import_module(
config_module_str, _PYTHON_CONFIG_PACKAGE.value
)
if not hasattr(config_module, 'CONFIG'):
raise ValueError(
f'Config module {config_module_str} does not have a CONFIG attribute.'
' Please use the basic config dict to build Sim.'
)
torax_config = model_config.ToraxConfig.from_dict(config_module.CONFIG)
return torax_config, config_module_str
def _get_yes_or_no() -> bool:
"""Returns a boolean indicating yes depending on user input."""
input_text = None
while input_text is None:
input_text = input(Y_N_PROMPT)
input_text = input_text.lower().strip()
if input_text not in ('y', 'n'):
simulation_app.log_to_stdout(
'Unrecognized input. Try again.',
color=simulation_app.AnsiColors.YELLOW,
)
input_text = None
else:
return input_text == 'y'
def _toggle_log_progress(log_sim_progress: bool) -> bool:
"""Toggles the --log_progress flag."""
log_sim_progress = not log_sim_progress
simulation_app.log_to_stdout(
f'--log_progress is now {log_sim_progress}.',
color=simulation_app.AnsiColors.GREEN,
)
if log_sim_progress:
simulation_app.log_to_stdout(
'Each time step will be logged to stdout.',
color=simulation_app.AnsiColors.GREEN,
)
else:
simulation_app.log_to_stdout(
'No time steps will be logged to stdout.',
color=simulation_app.AnsiColors.GREEN,
)
return log_sim_progress
def _toggle_plot_progress(plot_sim_progress: bool) -> bool:
"""Toggles the --plot_progress flag."""
plot_sim_progress = not plot_sim_progress
simulation_app.log_to_stdout(
f'--plot_progress is now {plot_sim_progress}.',
color=simulation_app.AnsiColors.GREEN,
)
if plot_sim_progress:
simulation_app.log_to_stdout(
'Each time step will be shown in a plot.',
color=simulation_app.AnsiColors.GREEN,
)
else:
simulation_app.log_to_stdout(
'No plots will be shown.',
color=simulation_app.AnsiColors.GREEN,
)
return plot_sim_progress
def _toggle_log_output(log_sim_output: bool) -> bool:
"""Toggles the --log_output flag."""
log_sim_output = not log_sim_output
simulation_app.log_to_stdout(
f'--log_output is now {log_sim_output}.',
color=simulation_app.AnsiColors.GREEN,
)
if log_sim_output:
simulation_app.log_to_stdout(
'Extra simulation info will be logged to stdout at the end of the run.',
color=simulation_app.AnsiColors.GREEN,
)
else:
simulation_app.log_to_stdout(
'No extra information will be logged to stdout.',
color=simulation_app.AnsiColors.GREEN,
)
return log_sim_output
def _post_run_plotting(
output_files: Sequence[str],
) -> None:
"""Helper to produce plots after a simulation run."""
input_text = input(
'Plot the last run (0), the last two runs (1), the last run against a'
' reference run (2): '
)
if not output_files:
simulation_app.log_to_stdout(
'No output files found, skipping plotting.',
color=simulation_app.AnsiColors.RED,
)
return
try:
plot_config = config_loader.import_module(
_PLOT_CONFIG_MODULE.value
).PLOT_CONFIG
except (ModuleNotFoundError, AttributeError) as e:
logging.exception(
'Error loading plot config module %s: %s', _PLOT_CONFIG_MODULE.value, e
)
return
match input_text:
case '0':
return plotruns_lib.plot_run(plot_config, output_files[-1])
case '1':
if len(output_files) == 1:
simulation_app.log_to_stdout(
'Only one output run file found, only plotting the last run.',
color=simulation_app.AnsiColors.RED,
)
return plotruns_lib.plot_run(plot_config, output_files[-1])
return plotruns_lib.plot_run(
plot_config, output_files[-1], output_files[-2]
)
case '2':
reference_run = _REFERENCE_RUN.value
if reference_run is None:
simulation_app.log_to_stdout(
'No reference run provided, only plotting the last run.',
color=simulation_app.AnsiColors.RED,
)
return plotruns_lib.plot_run(plot_config, output_files[-1], reference_run)
case _:
raise ValueError('Unknown command')
def main(_):
torax.set_jax_precision()
config_module_str = _PYTHON_CONFIG_MODULE.value
if config_module_str is None:
raise ValueError(f'--{_PYTHON_CONFIG_MODULE.name} must be specified.')
log_sim_progress = _LOG_SIM_PROGRESS.value
plot_sim_progress = _PLOT_SIM_PROGRESS.value
log_sim_output = _LOG_SIM_OUTPUT.value
torax_config = None
output_files = []
try:
start_time = time.time()
torax_config = (
config_loader.build_torax_config_from_config_module(
config_module_str, _PYTHON_CONFIG_PACKAGE.value
)
)
output_dir = (
_OUTPUT_DIR.value
if _OUTPUT_DIR.value is not None
else torax_config.runtime_params.output_dir
)
build_time = time.time() - start_time
start_time = time.time()
output_file = _call_sim_app_main(
torax_config,
output_dir=output_dir,
log_sim_progress=log_sim_progress,
plot_sim_progress=plot_sim_progress,
log_sim_output=log_sim_output,
)
simulation_time = time.time() - start_time
output_files.append(output_file)
simulation_app.log_to_stdout(
f'Sim and params build time: {build_time:.2f}s, {SIMULATION_TIME}:'
f' {simulation_time:.2f}s',
color=simulation_app.AnsiColors.GREEN,
)
except ValueError as ve:
simulation_app.log_to_stdout(
f'Error occurred: {ve}',
color=simulation_app.AnsiColors.RED,
exc_info=True,
)
simulation_app.log_to_stdout(
'Not running sim. Update config and try again.',
color=simulation_app.AnsiColors.RED,
)
if _QUIT.value:
return
user_command = _prompt_user(config_module_str)
while user_command != _UserCommand.QUIT:
match user_command:
case _UserCommand.QUIT:
# This line shouldn't get hit, but is here for pytype.
return # Exit the function.
case _UserCommand.RUN:
if torax_config is None:
simulation_app.log_to_stdout(
'Need to reload the simulation.',
color=simulation_app.AnsiColors.RED,
)
else:
start_time = time.time()
output_file = _call_sim_app_main(
torax_config,
output_dir=torax_config.runtime_params.output_dir,
log_sim_progress=log_sim_progress,
plot_sim_progress=plot_sim_progress,
log_sim_output=log_sim_output,
)
output_files.append(output_file)
simulation_time = time.time() - start_time
simulation_app.log_to_stdout(
f'Simulation time: {simulation_time:.2f}s'
)
case _UserCommand.MODIFY_CONFIG:
# See docstring for detailed info on what recompiles.
if torax_config is None:
simulation_app.log_to_stdout(
'Need to reload the simulation.',
color=simulation_app.AnsiColors.RED,
)
else:
try:
start_time = time.time()
torax_config_or_none = _modify_config(
config_module_str
)
if torax_config_or_none is not None:
torax_config = torax_config_or_none
config_change_time = time.time() - start_time
simulation_app.log_to_stdout(
f'Config change time: {config_change_time:.2f}s'
)
except ValueError as ve:
simulation_app.log_to_stdout(
f'Error occurred: {ve}',
color=simulation_app.AnsiColors.RED,
)
simulation_app.log_to_stdout(
'Update config and try again.',
color=simulation_app.AnsiColors.RED,
)
case _UserCommand.CHANGE_CONFIG:
# See docstring for detailed info on what recompiles.
if torax_config is None:
simulation_app.log_to_stdout(
'Need to reload the simulation.',
color=simulation_app.AnsiColors.RED,
)
else:
try:
start_time = time.time()
torax_config_or_none = _change_config(
config_module_str
)
if torax_config_or_none is not None:
torax_config, config_module_str = torax_config_or_none
config_change_time = time.time() - start_time
simulation_app.log_to_stdout(
f'Config change time: {config_change_time:.2f}s'
)
except ValueError as ve:
simulation_app.log_to_stdout(
f'Error occurred: {ve}',
color=simulation_app.AnsiColors.RED,
)
simulation_app.log_to_stdout(
'Update config and try again.',
color=simulation_app.AnsiColors.RED,
)
case _UserCommand.TOGGLE_LOG_SIM_PROGRESS:
log_sim_progress = _toggle_log_progress(log_sim_progress)
case _UserCommand.TOGGLE_PLOT_SIM_PROGRESS:
plot_sim_progress = _toggle_plot_progress(plot_sim_progress)
case _UserCommand.TOGGLE_LOG_SIM_OUTPUT:
log_sim_output = _toggle_log_output(log_sim_output)
case _UserCommand.PLOT_RUN:
_post_run_plotting(output_files)
case _:
raise ValueError('Unknown command')
user_command = _prompt_user(config_module_str)
def use_jax_profiler_if_enabled(f):
"""Decorator that runs `func` with profiling if the flag is enabled."""
@functools.wraps(f)
def decorated(*args, **kwargs):
if _USE_JAX_PROFILER.value:
with jax.profiler.trace('/tmp/torax-jax-trace'):
result = f(*args, **kwargs)
simulation_app.log_to_stdout(
'Profiling: to display results go to'
' https://ui.perfetto.dev/#!/viewer and open the trace file shown'
' below.'
)
return result
else:
return f(*args, **kwargs)
return decorated
@use_jax_profiler_if_enabled
def _call_sim_app_main(
torax_config,
output_dir: str,
log_sim_progress: bool,
plot_sim_progress: bool,
log_sim_output: bool,
):
return simulation_app.main(
lambda: torax_config,
output_dir=output_dir,
log_sim_progress=log_sim_progress,
plot_sim_progress=plot_sim_progress,
log_sim_output=log_sim_output,
)
if __name__ == '__main__':
app.run(main)