-
Notifications
You must be signed in to change notification settings - Fork 46
[CLI] Support agents with custom training loops in handle_dse_job #893
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
Open
rutayan-nv
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
rutayan-nv:rpatro/custom-training-loop-dispatch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |
| import signal | ||
| from contextlib import contextmanager | ||
| from pathlib import Path | ||
| from typing import Callable, List, Optional | ||
| from typing import Callable, List, Optional, Protocol, TypeGuard, runtime_checkable | ||
| from unittest.mock import Mock | ||
|
|
||
| import toml | ||
|
|
@@ -118,6 +118,60 @@ def prepare_installation( | |
| return installables, installer | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class CustomTrainingLoopAgent(Protocol): | ||
| """ | ||
| Agent that drives its own training loop and skips the ``handle_dse_job`` step loop. | ||
|
|
||
| Set ``HAS_CUSTOM_TRAINING_LOOP = True`` on the agent class to opt in. Used by | ||
| agents (e.g. RLlib-based) whose training loops are not modelled as a sequence | ||
| of independent ``select_action`` / ``env.step`` calls. | ||
| """ | ||
|
|
||
| HAS_CUSTOM_TRAINING_LOOP: bool | ||
|
|
||
| def train(self) -> None: ... | ||
|
|
||
|
|
||
| def _has_custom_training_loop(agent: object) -> TypeGuard[CustomTrainingLoopAgent]: | ||
| """ | ||
| Narrow ``agent`` to :class:`CustomTrainingLoopAgent` when it opts into the dispatch path. | ||
|
|
||
| Returning :class:`TypeGuard` (instead of plain ``bool``) lets the type checker | ||
| treat this predicate like ``isinstance``: callers inside the truthy branch see | ||
| ``agent`` as a :class:`CustomTrainingLoopAgent`, so ``agent.train()`` type-checks | ||
| without ``getattr`` or ``cast``. | ||
| """ | ||
| return bool(getattr(agent, "HAS_CUSTOM_TRAINING_LOOP", False)) | ||
|
|
||
|
|
||
| def _run_custom_training_loop(agent: CustomTrainingLoopAgent, agent_type: str) -> int: | ||
| """ | ||
| Drive an agent's self-contained training loop and return a process-style exit code. | ||
|
|
||
| ``shutdown()`` runs inside its own ``try/except`` so a faulty teardown cannot | ||
| suppress the exit code from ``train()`` nor propagate out of this helper: | ||
| ``handle_dse_job`` relies on the returned ``rc`` to accumulate ``err |= rc`` | ||
| and continue with the remaining test runs. | ||
| """ | ||
| logging.info(f"Agent {agent_type} drives its own training loop; delegating to agent.train().") | ||
| rc = 0 | ||
| try: | ||
| agent.train() | ||
| except Exception: | ||
| logging.exception(f"Custom training loop failed for agent {agent_type}.") | ||
| rc = 1 | ||
| finally: | ||
| shutdown = getattr(agent, "shutdown", None) | ||
| if callable(shutdown): | ||
| try: | ||
| shutdown() | ||
| except Exception: | ||
| logging.exception(f"Shutdown failed for agent {agent_type}.") | ||
| rc = 1 | ||
| return rc | ||
|
|
||
|
|
||
| def handle_dse_job(runner: Runner, args: argparse.Namespace) -> int: | ||
| registry = Registry() | ||
|
|
||
|
|
@@ -157,6 +211,10 @@ def handle_dse_job(runner: Runner, args: argparse.Namespace) -> int: | |
|
|
||
| agent = agent_class(env, agent_config) | ||
|
|
||
| if _has_custom_training_loop(agent): | ||
| err |= _run_custom_training_loop(agent, agent_type) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we exit (immediate |
||
| continue | ||
|
|
||
| for step in range(agent.max_steps): | ||
| result = agent.select_action() | ||
| if result is None: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's move this code into base_agent.py. handlers.py is already too long
as for the tests against
_run_custom_training_loop: I'm starting to make the tests folder structure replicate the main code structure. so in this case, I'd place all the relevant tests you added into tests/configurator/test_base_agent.py(not related to tests against
handle_dse_job)