Skip to content

GPUs for simulations

A campaign runs its simulations on GPUs when CampaignConfig.simulation_gpus names them. The rules, and how the GPUs are shared, are in Placement. This guide shows the settings for the common cases.

On one machine

"""The toy on two GPUs: one pool of spawned workers per GPU, each worker seeing only its GPU."""

import asyncio
from concurrent.futures import ThreadPoolExecutor

from radical.asyncflow import WorkflowEngine
from rhapsody.backends import ConcurrentExecutionBackend

from autonmi import Campaign, CampaignConfig, PopulationConfig
from autonmi.conformance.toy import Toy


async def main() -> bool:
    """Run the campaign on GPUs 0 and 1, and return whether every check passes."""
    backend = await ConcurrentExecutionBackend(ThreadPoolExecutor(4))
    flow = await WorkflowEngine.create(backend=backend, work_dir="runs/toy-gpus")
    config = CampaignConfig(
        run_dir="runs/toy-gpus",
        populations=2,
        simulation_gpus=[0, 1],  # population 0 gets GPU 0, population 1 GPU 1
        population=PopulationConfig(gpu_slots=2),  # two simulations at once on each GPU
    )
    campaign = Campaign(flow, Toy(agents=20), config)
    try:
        await campaign.start_services()
        await campaign.run()
        await campaign.stop_services()
        return campaign.check()
    finally:
        await campaign.close()
        await flow.shutdown()


if __name__ == "__main__":  # the GPU pools spawn processes, which import this module
    print("checks pass:", asyncio.run(main()))
python docs/examples/toy_gpus.py

A run directory holds one run: remove runs/toy-gpus before you run it again.

Each population gets one GPU and a pool of spawned workers that see only it, two simulations at a time. On a machine without GPUs the ids are only labels: the example still runs, each worker with CUDA_VISIBLE_DEVICES set to its id. simulation_gpus="all" takes the GPUs the job sees, from CUDA_VISIBLE_DEVICES or ROCR_VISIBLE_DEVICES, else from PyTorch; it refuses to start where it finds none. On AMD GPUs the same settings work: GPU ids says which variables each worker gets.

The script needs the if __name__ == "__main__": guard. Each spawned worker imports the script's main module: without the guard it runs the campaign again before it takes a task. Here the campaign refuses its run directory, which already holds the run, and the worker dies. Every simulation is then lost, every agent fails ("its worker died in each of its 3 runs"), and the checks fail. With a new run directory each time, each worker would run a second campaign of its own. A population on executor="autonmi.pools:spawn_pool" needs the guard too.

Under Dragon

When the model servers run in a job of their own, give the populations every GPU of each node and no CPU workers:

CampaignConfig(
    run_dir=...,
    runtime="dragon",
    populations=4,
    simulation_gpus="all",
    population=PopulationConfig(workers=0),
)

When the model servers share the job, name the GPUs they leave, the same ids on every node, such as simulation_gpus=[2, 3]. When the nodes differ, give a dict that names every node that runs populations.

  • Dragon must list the GPUs. Where it lists none, put the CUDA runtime's unversioned libcudart.so on LD_LIBRARY_PATH; the repository's job scripts do it with AUTONMI_CUDART in jobs/local.env.
  • With tool-calling agents, give at least as many populations as GPUs on each node: such a population takes one GPU.
  • The campaign checks every id against what Dragon lists on its node before anything starts.

Under Dragon, "all" with workers=0 runs in tests/test_nodes.py's GPU test (AUTONMI_DRAGON_NODE_GPUS=1), and a list as the GPU argument of tests/dragon_nodes.py. The dict is checked only by unit tests, with Dragon faked.

Tests

tests/test_recovery.py runs simulations on GPUs locally, on this machine's or on made-up ids when CUDA sees none. Under Dragon, AUTONMI_DRAGON_GPU=<id> and AUTONMI_DRAGON_GPUS=<id>,<id> add the GPU tests: see Run under Dragon.