Skip to content
Draft
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
23 changes: 20 additions & 3 deletions src/ldp/alg/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import os
import time
from collections import defaultdict
from collections.abc import Callable, Collection, Iterable, Sequence
from collections.abc import AsyncIterator, Callable, Collection, Iterable, Sequence
from contextlib import asynccontextmanager
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast

Expand Down Expand Up @@ -46,9 +47,11 @@ class Callback:
callback.after_agent_init_state() *
while not done:
callback.before_transition() *
agent.get_asv()
async with callback.during_get_asv():
agent.get_asv()
callback.after_agent_get_asv() *
env.step()
async with callback.during_env_step():
env.step()
callback.after_env_step() *
callback.after_transition() *

Expand Down Expand Up @@ -85,6 +88,13 @@ async def before_transition(
async def after_agent_init_state(self, traj_id: str, init_state: Any) -> None:
"""Invoked by runners after agent.init_state()."""

@asynccontextmanager
async def during_get_asv(
self, traj_id: str, agent: Agent, agent_state: Any
) -> AsyncIterator[None]:
"""Context used by runners during agent.get_asv()."""
yield

async def after_agent_get_asv(
self,
traj_id: str,
Expand All @@ -94,6 +104,13 @@ async def after_agent_get_asv(
) -> None:
"""Invoked by runners after agent.get_asv()."""

@asynccontextmanager
async def during_env_step(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice work.

Do you mind extending test_rollouts.DummyCallback for these in the unit tests?

self, traj_id: str, env: Environment
) -> AsyncIterator[None]:
"""Context used by runners during env.step()."""
yield

async def after_env_reset(
self, traj_id: str, obs: list[Message], tools: list[Tool]
) -> None:
Expand Down
24 changes: 17 additions & 7 deletions src/ldp/alg/rollout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uuid
from collections import Counter
from collections.abc import Callable, Iterator, Sequence
from contextlib import contextmanager, nullcontext
from contextlib import AsyncExitStack, contextmanager, nullcontext
from typing import Any, TypeVar, overload

from aviary.core import Environment, Message
Expand Down Expand Up @@ -426,11 +426,16 @@ async def _take_step(
timer("agent_get_asv"),
reraise_exc_as(AgentError, enabled=self.catch_agent_failures),
):
(
action,
next_agent_state,
value,
) = await self.agent.get_asv(agent_state, obs)
async with AsyncExitStack() as stack:
for callback in self.callbacks:
await stack.enter_async_context(
callback.during_get_asv(traj_id, self.agent, agent_state)
)
(
action,
next_agent_state,
value,
) = await self.agent.get_asv(agent_state, obs)

with timer("after_agent_get_asv"):
await asyncio.gather(*[
Expand All @@ -444,7 +449,12 @@ async def _take_step(
timer("env_step"),
reraise_exc_as(EnvError, enabled=self.catch_env_failures),
):
next_obs, reward, done, trunc = await env.step(action.value)
async with AsyncExitStack() as stack:
for callback in self.callbacks:
await stack.enter_async_context(
callback.during_env_step(traj_id, env)
)
next_obs, reward, done, trunc = await env.step(action.value)
with timer("after_env_step"):
await asyncio.gather(*[
callback.after_env_step(traj_id, next_obs, reward, done, trunc)
Expand Down