Skip to content

Run backends

Backends run a generated model: locally, in Docker or on a SLURM cluster. Each backend has a configuration class, used with ModelRun.run.

rompy.backends.config

Backend configuration classes for ROMPY.

This module provides Pydantic-based configuration classes for different execution backends. These configurations handle transient execution parameters while maintaining type safety and validation.

BackendConfig module-attribute

BaseBackendConfig pydantic-model

Bases: BaseModel, ABC

Base class for all backend configurations.

This class defines common configuration parameters that apply to all backend types, such as timeouts and environment variables.

Config:

  • validate_assignment: True
  • extra: forbid
  • use_enum_values: True

Fields:

Validators:

timeout pydantic-field

timeout: int = 3600

Maximum execution time in seconds (1 minute to 24 hours)

env_vars pydantic-field

env_vars: Dict[str, str]

Additional environment variables to set during execution

working_dir pydantic-field

working_dir: Optional[Path] = None

Working directory for execution (defaults to model output directory)

validate_working_dir pydantic-validator

validate_working_dir(v)

Validate working directory exists if specified.

validate_env_vars pydantic-validator

validate_env_vars(v)

Validate environment variables.

get_backend_class abstractmethod

get_backend_class() -> type

Return the backend class that should handle this configuration.

Returns:

Type Description
type

The backend class to use for execution

LocalConfig pydantic-model

Bases: BaseBackendConfig

Configuration for local execution backend.

This configuration is used when running models directly on the local system using the system's Python interpreter or shell commands.

Config:

  • json_schema_extra: {'examples': [{'timeout': 7200, 'env_vars': {'OMP_NUM_THREADS': '4'}, 'command': 'python run_model.py'}, {'timeout': 3600, 'working_dir': '/path/to/model/dir'}, {'timeout': 3600, 'stream_output': True}]}

Fields:

Validators:

command pydantic-field

command: Optional[str] = None

Optional shell command to run instead of config.run()

shell pydantic-field

shell: bool = True

Whether to execute commands through the shell

capture_output pydantic-field

capture_output: bool = True

Whether to capture stdout and stderr

stream_output pydantic-field

stream_output: bool = False

Whether to stream output in real-time

timeout pydantic-field

timeout: int = 3600

Maximum execution time in seconds (1 minute to 24 hours)

env_vars pydantic-field

env_vars: Dict[str, str]

Additional environment variables to set during execution

working_dir pydantic-field

working_dir: Optional[Path] = None

Working directory for execution (defaults to model output directory)

get_backend_class

get_backend_class()

Return the LocalRunBackend class.

validate_working_dir pydantic-validator

validate_working_dir(v)

Validate working directory exists if specified.

validate_env_vars pydantic-validator

validate_env_vars(v)

Validate environment variables.

DockerConfig pydantic-model

Bases: BaseBackendConfig

Configuration for Docker execution backend.

This configuration is used when running models inside Docker containers with appropriate resource limits and volume mounts.

Config:

  • json_schema_extra: {'examples': [{'image': 'swan:latest', 'cpu': 4, 'memory': '2g', 'timeout': 7200, 'volumes': ['/data:/app/data']}, {'dockerfile': './docker/Dockerfile', 'cpu': 2, 'build_args': {'VERSION': '1.0'}, 'mpiexec': 'mpirun'}]}

Fields:

Validators:

image pydantic-field

image: Optional[str] = None

Docker image to use (required if not building from dockerfile)

dockerfile pydantic-field

dockerfile: Optional[Path] = None

Path to Dockerfile to build (alternative to image)

executable pydantic-field

executable: str = '/usr/local/bin/run.sh'

Path to executable inside the container

cpu pydantic-field

cpu: int = 1

Number of CPU cores to allocate

memory pydantic-field

memory: Optional[str] = None

Memory limit (e.g., '2g', '512m')

mpiexec pydantic-field

mpiexec: str = ''

MPI execution command (if using MPI)

build_args pydantic-field

build_args: Dict[str, str]

Arguments to pass to docker build

build_context pydantic-field

build_context: Optional[Path] = None

Docker build context directory (defaults to dockerfile parent directory)

volumes pydantic-field

volumes: List[str]

Additional volumes to mount (format: 'host_path:container_path')

remove_container pydantic-field

remove_container: bool = True

Whether to remove container after execution

user pydantic-field

user: str = 'root'

User to run as inside the container

timeout pydantic-field

timeout: int = 3600

Maximum execution time in seconds (1 minute to 24 hours)

env_vars pydantic-field

env_vars: Dict[str, str]

Additional environment variables to set during execution

working_dir pydantic-field

working_dir: Optional[Path] = None

Working directory for execution (defaults to model output directory)

validate_image_or_dockerfile pydantic-validator

validate_image_or_dockerfile(v, info)

Validate that either image or dockerfile is provided, but not both.

validate_dockerfile_exists pydantic-validator

validate_dockerfile_exists(v)

Validate dockerfile path format (should be relative).

validate_build_context_exists pydantic-validator

validate_build_context_exists(v)

Validate build context directory exists if specified.

validate_volumes pydantic-validator

validate_volumes(v)

Validate volume mount specifications.

validate_memory_format pydantic-validator

validate_memory_format(v)

Validate memory format.

get_backend_class

get_backend_class()

Return the DockerRunBackend class.

validate_working_dir pydantic-validator

validate_working_dir(v)

Validate working directory exists if specified.

validate_env_vars pydantic-validator

validate_env_vars(v)

Validate environment variables.

SlurmConfig pydantic-model

Bases: BaseBackendConfig

Configuration for SLURM cluster execution.

Config:

  • json_schema_extra: {'examples': [{'queue': 'general', 'command': 'python run_model.py', 'nodes': 1, 'ntasks': 1, 'cpus_per_task': 4, 'time_limit': '02:00:00', 'account': 'myproject', 'timeout': 7200}, {'queue': 'gpu', 'command': 'python run_model.py --gpu', 'nodes': 2, 'ntasks': 8, 'cpus_per_task': 2, 'time_limit': '24:00:00', 'reservation': 'special_reservation', 'additional_options': ['--gres=gpu:v100:2']}]}

Fields:

Validators:

model_type pydantic-field

model_type: Literal['slurm'] = 'slurm'

The backend type.

queue pydantic-field

queue: Optional[str] = None

SLURM partition name (equivalent to queue)

command pydantic-field

command: str

Shell command to run in the workspace directory

nodes pydantic-field

nodes: int = 1

Number of nodes to allocate

ntasks pydantic-field

ntasks: int = 1

Number of tasks (processes) to run

cpus_per_task pydantic-field

cpus_per_task: int = 1

Number of CPU cores per task

time_limit pydantic-field

time_limit: str = '1:00:00'

Time limit in format HH:MM:SS

account pydantic-field

account: Optional[str] = None

Account for billing/resource tracking

qos pydantic-field

qos: Optional[str] = None

Quality of Service for the job

reservation pydantic-field

reservation: Optional[str] = None

Reservation name to run job under

output_file pydantic-field

output_file: Optional[str] = None

Output file path for job output

error_file pydantic-field

error_file: Optional[str] = None

Error file path for job errors

job_name pydantic-field

job_name: Optional[str] = None

Name for the SLURM job

mail_type pydantic-field

mail_type: Optional[str] = None

Type of mail to send (BEGIN, END, FAIL, ALL, etc.)

mail_user pydantic-field

mail_user: Optional[str] = None

Email address for notifications

additional_options pydantic-field

additional_options: List[str]

Additional SLURM options (e.g., '--gres=gpu:1')

timeout pydantic-field

timeout: int = 3600

Maximum execution time in seconds (1 minute to 24 hours)

env_vars pydantic-field

env_vars: Dict[str, str]

Additional environment variables to set during execution

working_dir pydantic-field

working_dir: Optional[Path] = None

Working directory for execution (defaults to model output directory)

validate_time_limit pydantic-validator

validate_time_limit(v)

Validate time limit format (HH:MM:SS).

get_backend_class

get_backend_class()

Return the SlurmRunBackend class.

validate_working_dir pydantic-validator

validate_working_dir(v)

Validate working directory exists if specified.

validate_env_vars pydantic-validator

validate_env_vars(v)

Validate environment variables.

rompy.run

Local execution backend for model runs.

This module provides the local run backend implementation.

logger module-attribute

logger = logging.getLogger(__name__)

LocalRunBackend

Execute models locally using the system's Python interpreter.

This is the simplest backend that just runs the model directly on the local system.

run

run(
    model_run: ModelRun,
    config: LocalConfig,
    workspace_dir: Optional[str] = None,
) -> bool

Run the model locally.

Parameters:

Name Type Description Default
model_run ModelRun

The ModelRun instance to execute

required
config LocalConfig

LocalConfig instance with execution parameters

required
workspace_dir Optional[str]

Path to the generated workspace directory (if None, will generate)

None

Returns:

Type Description
bool

True if execution was successful, False otherwise

Raises:

Type Description
ValueError

If model_run is invalid

TimeoutError

If execution exceeds timeout

rompy.run.docker

Docker backend for running models.

This module provides a Docker-based execution backend for rompy models.

logger module-attribute

logger = logging.getLogger(__name__)

DockerRunBackend

Execute models inside Docker containers.

This backend builds Docker images if needed and runs models inside containers with appropriate volume mounts.

run

run(
    model_run: ModelRun,
    config: DockerConfig,
    workspace_dir: Optional[str] = None,
) -> bool

Run the model inside a Docker container.

Parameters:

Name Type Description Default
model_run ModelRun

The ModelRun instance to execute

required
config DockerConfig

DockerConfig instance with execution parameters

required
workspace_dir Optional[str]

Path to the generated workspace directory (if None, will generate)

None

Returns:

Type Description
bool

True if execution was successful, False otherwise

rompy.run.slurm

SLURM backend for running models.

This module provides a SLURM-based execution backend for rompy models.

logger module-attribute

logger = logging.getLogger(__name__)

SlurmRunBackend

Execute models on SLURM clusters.

This backend submits model runs to a SLURM-managed HPC cluster for execution.

run

run(
    model_run: ModelRun,
    config: SlurmConfig,
    workspace_dir: Optional[str] = None,
) -> bool

Submit model run to SLURM queue.

Parameters:

Name Type Description Default
model_run ModelRun

The ModelRun instance to execute

required
config SlurmConfig

SlurmConfig instance with execution parameters

required
workspace_dir Optional[str]

Path to the generated workspace directory (if None, will generate)

None

Returns:

Type Description
bool

True if execution was successful, False otherwise