Skip to content

rats_e2e.aml.basic

A minimal example application that can be submitted as an aml job.

We can run the examples with the cli commands in the rats_e2e.aml.cli module.

$ python -m rats_e2e.aml.cli --help
Usage: python -m rats_e2e.aml.cli [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  basic-job  Submit the [rats_e2e.aml.basic][] application as an aml job

The rats_e2e.aml.basic.Application class contains a small amount of code we want to execute within an aml job. It outputs information provided to it by the job submitter. We can submit this job through the rats-aml submit cli command, or by using the rats.aml.submit function in python.

Note

The examples will all run rats-ci build-image before any of the example commands to make sure the aml job uses the most recent version of our code.

Submit the basic job, providing additional context defined in a yaml file.

$ cd rats-devtools
$ rats-ci build-image && \
    rats-aml submit rats_e2e.aml.basic --wait \
    --context-file src/rats_resources/aml/example-context.yaml

Instead of relying on a static yaml file, we can submit aml jobs with additional control using rats.aml.submit. Along with a rats.app_context.Collection instance for the remote job, we can specify additional configuration for the creation of the aml job, like the aml job environment variables.

from collections.abc import Iterator

from rats import aml as aml
from rats import app_context, apps, logs
from rats_e2e.aml import basic

class Application(apps.AppContainer, cli.Container, apps.PluginMixin):
    def execute(self) -> None:
        def envs() -> Iterator[dict[str, str]]:
            yield {"RATS_AML_E2E_EXAMPLE": "this env is attached to the remote job"}

        aml.submit(
            "rats_e2e.aml.basic",
            container_plugin=lambda app: apps.StaticContainer(
                apps.static_group(aml.AppConfigs.CLI_ENVS, envs)
            ),
            context=app_context.Collection.make(
                app_context.Context.make(
                    basic.AppServices.EXAMPLE_DATA,
                    basic.ExampleData("example data name", "example data value"),
                    basic.ExampleData("another example name", "another example value"),
                ),
            ),
            wait=True,
        )


def main() -> None:
    apps.run_plugin(Application)

Note

The rats.aml.AppConfigs class contains the complete list of options you can provide through the container_plugin argument.

We can pass a yaml file to rats-aml submit to add [rats.app_context.Context] values to the remote aml job, using the --context-file argument.

items:
  - service_id: ["rats_e2e.aml._example_job:JobServices[example-data]"]
    values:
      - name: example data name
        value: example data name
      - name: another example data name
        value: another example data name

__all__ = ['AppServices', 'Application', 'ExampleData'] module-attribute

Application(app)

Bases: apps.AppContainer, apps.PluginMixin

Shows how to read information passed into the application by the aml job submitter.

Source code in rats/apps/_app_containers.py
def __init__(self, app: Container) -> None:
    self._app = app

execute()

Print all the environment and context information in the aml job.

Source code in src/rats_e2e/aml/basic/_app.py
def execute(self) -> None:
    """Print all the environment and context information in the aml job."""
    context_collection = self._app.get(runtime.AppServices.CONTEXT)
    print("loaded context:")
    for item in context_collection.items:
        print(f"{item.service_id} -> {item.values}")

    job_context = context_collection.decoded_values(
        aml.AmlJobContext,
        aml.AppConfigs.JOB_CONTEXT,
    )
    print(f"aml job context that is always available: {job_context}")

    example_data = context_collection.decoded_values(ExampleData, AppServices.EXAMPLE_DATA)
    print(f"{len(example_data)} example data item(s) in context {AppServices.EXAMPLE_DATA}")
    for x in example_data:
        print(x)

    print("rats envs:")
    for k, v in os.environ.items():
        if k.startswith("RATS_"):
            print(f"{k}{v}")

AppServices

EXAMPLE_DATA = apps.ServiceId[ExampleData]('example-data') class-attribute instance-attribute

ExampleData(name, value) dataclass

name instance-attribute

value instance-attribute