Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/jobmon_resources #118

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions src/onemod/backend/jobmon_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,55 @@

Examples
--------
Resources yaml file requires tool resources:
Compute resources can be passed as a dictionary or a path to a resources
file (e.g., json, toml, yaml).

Required tool resources:

.. code-block:: yaml

tool_resources:
cluster_name:
cores: 1
memory: 1G
{cluster_name}:
project: proj_name
queue: queue_name.q
runtime: 00:01:00

Optional stage resources can be specified at the stage or method level:
Optional stage resources can be specified at the stage or stage + method
level:

task_template_resources:
stage_name:
cluster_name:
runtime: 00:10:00
stage_name_collect:
cluster_name:
runtime: 00:05:00
{stage_name}:
{cluster_name}:
...
{stage_name}_{collect}:
{cluster_name}:
...

In the above example, the stage's `collect` method requests five
minutes, while all other methods request ten minutes.
See Jobmon documentation for additional optional resources and default
values.

"""

import sys
from pathlib import Path
from typing import Literal

import yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highly recommend ruff pre-commit
https://github.com/astral-sh/ruff-pre-commit
it will automatically detect the unused import and fix it :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had formatting on save but it wasn't working today for some reason. I had to update a vscode extension and now it is working again :)

from jobmon.client.api import Tool
from jobmon.client.task import Task
from jobmon.client.task_template import TaskTemplate
from pydantic import validate_call

from onemod.fsutils.config_loader import ConfigLoader
from onemod.pipeline import Pipeline
from onemod.stage import ModelStage, Stage


def get_tool(name: str, cluster: str, resources: Path | str) -> Tool:
def get_tool(name: str, cluster: str, resources: dict) -> Tool:
"""Get tool."""
tool = Tool(name=f"{name}")
tool.set_default_cluster_name(cluster)
tool.set_default_compute_resources_from_yaml(cluster, str(resources))
tool.set_default_compute_resources_from_dict(
cluster, resources["tool_resources"][cluster]
)
return tool


Expand All @@ -66,7 +69,7 @@ def run_workflow(name: str, tool: Tool, tasks: list[Task]) -> None:

def get_tasks(
tool: Tool,
resources: Path | str,
resources: dict,
stage: Stage,
method: str,
task_args: dict[str, str],
Expand Down Expand Up @@ -110,7 +113,7 @@ def get_tasks(

def get_task_template(
tool: Tool,
resources: Path | str,
resources: dict,
stage_name: str,
method: str,
node_args: list[str],
Expand Down Expand Up @@ -152,15 +155,21 @@ def get_command_template(


def get_task_resources(
resources: Path | str, cluster: str, stage_name: str, method: str
resources: dict, cluster: str, stage_name: str, method: str
) -> dict | None:
"""Get task-specific resources."""
with open(resources, "r") as f:
resource_dict = yaml.safe_load(f)["task_template_resources"]
if f"{stage_name}_{method}" in resource_dict:
return resource_dict[f"{stage_name}_{method}"][cluster]
if stage_name in resource_dict:
return resource_dict[stage_name][cluster]
task_resources = resources.get("task_template_resources")
if task_resources is not None:
stage_resources = task_resources.get(stage_name, {})
method_resources = task_resources.get(f"{stage_name}_{method}", {})
if method_resources:
return stage_resources.get(cluster)
if stage_resources:
return method_resources.get(cluster)
return {
**stage_resources.get(cluster, {}),
**method_resources.get(cluster, {}),
}
return None


Expand Down Expand Up @@ -215,7 +224,7 @@ def get_upstream_tasks(
def evaluate_with_jobmon(
model: Pipeline | Stage,
cluster: str,
resources: Path | str,
resources: Path | str | dict,
python: Path | str | None = None,
method: Literal["run", "fit", "predict"] = "run",
stages: set[str] | None = None,
Expand All @@ -228,8 +237,8 @@ def evaluate_with_jobmon(
Pipeline or stage instance.
cluster : str
Cluster name.
resources : Path or str
Path to resources yaml file.
resources : Path, str, or dict
Dictionary of compute resources or path to resources file.
python : Path, str, or None, optional
Path to Python environment. If None, use sys.executable.
Default is None.
Expand All @@ -244,8 +253,16 @@ def evaluate_with_jobmon(
TODO: Could dependencies be method specific?

"""
# Get compute resources
resources_dict: dict
if isinstance(resources, (Path, str)):
config_loader = ConfigLoader()
resources_dict = config_loader.load(Path(resources))
else:
resources_dict = resources

# Get tool
tool = get_tool(model.name, cluster, resources)
tool = get_tool(model.name, cluster, resources_dict)

# Set config
if isinstance(model, Stage):
Expand All @@ -270,11 +287,16 @@ def evaluate_with_jobmon(
stage, method, model.stages, task_dict, stages
)
task_dict[stage_name] = get_tasks(
tool, resources, stage, method, task_args, upstream_tasks
tool,
resources_dict,
stage,
method,
task_args,
upstream_tasks,
)
tasks.extend(task_dict[stage_name])
else:
tasks = get_tasks(tool, resources, model, method, task_args)
tasks = get_tasks(tool, resources_dict, model, method, task_args)

# Create and run workflow
run_workflow(model.name, tool, tasks)
6 changes: 3 additions & 3 deletions src/onemod/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ def evaluate(
----------------
cluster : str, optional
Cluster name. Required if `backend` is 'jobmon'.
resources : Path or str, optional
Path to resources yaml file. Required if `backend` is
'jobmon'.
resources : Path, str, or dict, optional
Dictionary of compute resources or path to resources file.
Required if `backend` is 'jobmon'.

"""
if method == "collect":
Expand Down
12 changes: 6 additions & 6 deletions src/onemod/stage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ def evaluate(
----------------
cluster : str, optional
Cluster name. Required if `backend` is 'jobmon'.
resources : Path or str, optional
Path to resources yaml file. Required if `backend` is
'jobmon'.
resources : Path, str, or dict, optional
Dictionary of compute resources or path to resources file.
Required if `backend` is 'jobmon'.

Notes
-----
Expand Down Expand Up @@ -539,9 +539,9 @@ def evaluate(
or method is `collect`.
cluster : str, optional
Cluster name. Required if `backend` is 'jobmon'.
resources : Path or str, optional
Path to resources yaml file. Required if `backend` is
'jobmon'.
resources : Path, str, or dict, optional
Dictionary of compute resources or path to resources file.
Required if `backend` is 'jobmon'.

Notes
-----
Expand Down
Loading