Backend internals¶
This page is for developers adding a way to run models, to postprocess their outputs or to chain the steps. Backends, postprocessors and pipelines describes how to use the built-in ones.
Each extension is a pair: a Pydantic configuration class that users create and validate, and a plain class that does the work.
| Extension | Configuration class | Worker class | Called by |
|---|---|---|---|
| Run backend | subclass of BaseBackendConfig |
run(model_run, config, workspace_dir=None) -> bool |
ModelRun.run |
| Postprocessor | subclass of BasePostprocessorConfig |
process(model_run, **fields) -> dict |
ModelRun.postprocess |
| Pipeline backend | none, keyword arguments | execute(model_run, **kwargs) -> dict |
ModelRun.pipeline |
Run backends¶
How a run finds its backend¶
ModelRun.run(backend, workspace_dir=None):
- checks that
backendis aBaseBackendConfiginstance, and raisesTypeErrorotherwise; - calls
backend.get_backend_class()and creates the class with no arguments; - calls its
run(model_run, config=backend, workspace_dir=workspace_dir)and returns the result.
The configuration decides the backend, so no registry is consulted. The built-in pairs are:
| Configuration | Backend |
|---|---|
LocalConfig |
LocalRunBackend |
DockerConfig |
DockerRunBackend |
SlurmConfig |
SlurmRunBackend |
The contract¶
A backend's run() method:
- Uses the workspace it is given. If
workspace_dirisNone, it callsmodel_run.generate()first; the built-in backends do this so thatrun()works on an ungenerated run. - Returns
TrueorFalse. ReturnFalsewhen the model fails, and log why. The local backend raisesTimeoutErrorwhen the timeout is exceeded. - Reads its settings from
config. The common ones aretimeout,env_varsandworking_dir; the default working directory is the workspace.
Writing one¶
The configuration class adds the fields the backend needs and returns the backend class. Configuration classes forbid unknown fields, and validators check values when the object is created. This backend writes a file instead of running a model:
from rompy.backends.config import BaseBackendConfig
from rompy.core.time import TimeRange
from rompy.model import ModelRun
class EchoConfig(BaseBackendConfig):
"""Write a message into the workspace."""
message: str = "hello"
def get_backend_class(self):
return EchoRunBackend
class EchoRunBackend:
def run(self, model_run, config, workspace_dir=None):
workspace = Path(workspace_dir or model_run.generate())
(workspace / "echo.txt").write_text(config.message)
return True
run = ModelRun(
run_id="echo",
period=TimeRange(start="2023-01-01", end="2023-01-02", interval="1h"),
output_dir=OUT_DIR,
)
print(run.run(EchoConfig(message="model finished", timeout=600)))
print((run.staging_dir / "echo.txt").read_text())
Register the backend class so that rompy backends list shows it:
The list is keyed by the class name, lower-cased, without RunBackend (echo here).
Backend files on the command line
rompy run --backend-config maps the file's type key to a class with a fixed table in rompy.cli (local, docker, slurm). A new backend configuration is used from Python, or through a pipeline backend that builds it.
Postprocessors¶
How a postprocessor is called¶
ModelRun.postprocess(processor, **kwargs):
- checks that
processoris aBasePostprocessorConfiginstance; - calls
processor.get_postprocessor_class()and creates the class with no arguments; - dumps the configuration, drops
timeout,env_vars,working_dirandtype, updates the rest withkwargs; - calls
process(model_run, **fields)and returns the dictionary it gives back.
So the configuration's own fields arrive as keyword arguments. By convention the result has a success key; the pipeline logs a warning when it is False.
Writing one¶
The configuration has a type literal, used to pick the class when a YAML file is loaded:
from typing import Literal
from rompy.postprocess.config import BasePostprocessorConfig
class ListFilesConfig(BasePostprocessorConfig):
"""List the files in the workspace."""
type: Literal["list_files"] = "list_files"
pattern: str = "*"
def get_postprocessor_class(self):
return ListFilesPostprocessor
class ListFilesPostprocessor:
def process(self, model_run, pattern="*", **kwargs):
files = sorted(p.name for p in model_run.staging_dir.glob(pattern))
return {"success": True, "run_id": model_run.run_id, "files": files}
print(run.postprocess(ListFilesConfig(pattern="*.txt")))
print(run.postprocess(ListFilesConfig(), pattern="*"))
{'success': True, 'run_id': 'echo', 'files': ['echo.txt']}
{'success': True, 'run_id': 'echo', 'files': ['INPUT', 'datasets', 'echo.txt', 'outputs']}
Register both classes. The name in rompy.postprocess.config must equal type: the command line uses it to find the configuration class for a file with that type.
[project.entry-points."rompy.postprocess.config"]
list_files = "mypackage.postprocess:ListFilesConfig"
[project.entry-points."rompy.postprocess"]
list_files = "mypackage.postprocess:ListFilesPostprocessor"
rompy.postprocess feeds rompy backends list, keyed by class name without Postprocessor.
Pipeline backends¶
ModelRun.pipeline(pipeline_backend="local", **kwargs) looks up the pipeline backend by name, creates it with no arguments and returns execute(model_run, **kwargs). The built-in LocalPipelineBackend takes backend_config, processor, process_kwargs, cleanup_on_failure and validate_stages, and calls generate(), run() and postprocess() in this process.
A pipeline backend that submits the steps elsewhere, for example to a workflow engine, implements execute() and returns a dictionary with at least success:
class WorkflowPipelineBackend:
def execute(self, model_run, backend_config=None, processor=None, **kwargs):
workflow = build_workflow(model_run, backend_config, processor)
job = workflow.submit()
return {"success": job.wait(), "run_id": model_run.run_id}
The name pipeline() accepts is taken from the class name, lower-cased, without PipelineBackend (workflow here), not from the entry point name; keep the two the same. rompy pipeline always uses the local pipeline backend.
Testing¶
- Test the configuration's validators both ways, and that
get_backend_class()returns the right class. - Test the worker with the external tool mocked:
subprocess.runfor command-line tools,docker.from_envfor Docker,sbatchandscontrolfor SLURM.tests/backends/has examples for the built-in backends. - Run a real model only in integration tests that skip when the tool is missing, as
tests/integration/does for Docker.
See Testing.
See also¶
- Reference: run backends, postprocessors, pipelines.