Using the Python API¶
In addition to the CLI functionality, AFL-Sim supports launching new simulations or resuming existing ones from disk using Python scripts. To do this, import AFL-Sim as a Python library using the command import afl_sim.
For the full list of modules that can be imported from AFL-Sim and their descriptions, check the API Reference.
Tip
If you installed AFL-Sim with uv, prepend uv run when running a Python script that imports AFL-Sim:
Launching a New Simulation¶
Launching simulations from a Python script provides greater flexibility than using the CLI. In addition to providing a path to a YAML configuration file, users can manually configure an AppConfig object whose attributes correspond directly to the parameters in a YAML configuration.
To launch a new simulation with the Python API, use the run_simulation function from the afl_sim library. Configuration parameters can be supplied to run_simulation using either AppConfig objects or paths to YAML configuration files.
Tip
- For a detailed description of
afl_sim.AppConfig,afl_sim.run_simulationand their attributes, see the dedicated sections in the API Reference. - Check the Parameter Reference section in Setting Up a YAML Configuration for a deep dive into the configuration parameters.
Using AppConfig Objects¶
An annotated example of a Python script that launches a new simulation by constructing and supplying an AppConfig object is provided below.
| launch_new_from_app_config.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 | |
Info
- The inputs
output_dir,data_dirandcheckpoint_dirtorun_simulationare optional. If they are not provided, AFL-Sim will default to./outputs,./data, and./checkpoints. AFL-Sim will automatically create non-existing directories. - For a complete list of supported options for
ModelType,DeviceType,DatasetTypeandMemoryType, check the corresponding sections in the API Reference.
Danger
The learning_rate argument of run_simulation permits overriding the learning_rate parameter of a YAML configuration (see the next section for launching new simulations using YAML files). This behavior is disabled when AppConfig objects are provided to run_simulation, and attempting to override the learning_rate of an AppConfig object (config.optimization.learning_rate in the script above) will raise a RuntimeError. To modify the learning rate, edit the AppConfig object directly.
Using YAML Configurations¶
An example script launching a new simulation from a YAML configuration file can be found below. Note that learning_rate overrides are allowed in this instance.
| launch_new_from_yaml.py | |
|---|---|
Info
Check Setting Up a YAML Configuration for guidance on creating YAML configurations.
Resuming a Simulation¶
AFL-Sim assigns a unique identifier to every simulation (e.g., 2026-08-08_12-31-42_936aed) and creates a folder with that name in the output directory specified when the simulation was launched (e.g., outputs/). To resume an existing simulation from a checkpoint saved to disk, import the resume_simulation function from the afl_sim library and provide it with the path to this output folder:
| resume_existing.py | |
|---|---|
Tip
- For a description of
afl_sim.resume_simulationand its arguments, check the corresponding section in the API Reference. - The section Launching a New Simulation in the Execution Guide details AFL-Sim directory organization, including output directories and simulation artifact storage.