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

Suppress env frame errors; serialize logprob #197

Merged
merged 3 commits into from
Dec 30, 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
7 changes: 6 additions & 1 deletion ldp/alg/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from collections import defaultdict
from collections.abc import Collection, Iterable, Sequence
from contextlib import suppress
from pathlib import Path
from typing import Any, cast

Expand Down Expand Up @@ -173,7 +174,11 @@ async def after_transition(
# TODO: make this async?
traj.to_jsonl(self.out_files[traj_id])
if transition.done:
with Path(self.env_files[traj_id]).open("w") as f:
with (
# Do not fail if the environment didn't implement export_frame().
suppress(NotImplementedError),
Path(self.env_files[traj_id]).open("w") as f,
):
f.write(env.export_frame().model_dump_json(exclude={"state"}, indent=2))


Expand Down
8 changes: 7 additions & 1 deletion ldp/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
from collections.abc import Callable, Hashable, Iterable
from contextlib import suppress
from typing import Any, ClassVar, Self, cast
from uuid import UUID

Expand Down Expand Up @@ -121,7 +122,12 @@ def from_jsonl(cls, filename: str | os.PathLike) -> Self:
reader = iter(f)
traj = cls(traj_id=json.loads(next(reader)))
for json_line in reader:
traj.steps.append(Transition(**json.loads(json_line)))
data = json.loads(json_line)
# logprob may have been serialized, but cannot be passed to
# OpResult, so remove it here.
with suppress(KeyError):
data["action"].pop("logprob")
traj.steps.append(Transition(**data))
return traj

def compute_discounted_returns(self, discount: float = 1.0) -> list[float]:
Expand Down
8 changes: 4 additions & 4 deletions ldp/graph/async_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ async def __call__(self, **kwargs):
# Sleep, to let another coroutine take over if it needs to
await asyncio.sleep(0.0)

async def _maybe_process_batch(self):
"""If the buffer is >= batch size or we have been waiting long enough, process the old batch.
async def _maybe_process_batch(self) -> None:
"""If the buffer is >= batch size or we have been waiting long enough, process the oldest batch.

If neither condition is met, do nothing.
"""
Expand All @@ -134,7 +134,7 @@ async def _maybe_process_batch(self):
batch = self._work_buffer[: self.batch_size]
self._work_buffer = self._work_buffer[self.batch_size :]

# Construct the batch tensors
# Construct the batch inputs
sample_kwargs = [x[2] for x in batch]
batch_kwargs = self.collate_fn(sample_kwargs)

Expand All @@ -144,7 +144,7 @@ async def _maybe_process_batch(self):
self._result_buffer.update(zip(request_ids, results, strict=True))

@abstractmethod
async def _batched_call(self, batch_kwargs: dict[str, Any]):
async def _batched_call(self, batch_kwargs: dict[str, Any]) -> Any:
"""Logic to call the worker on a batch of inputs."""


Expand Down
1 change: 1 addition & 0 deletions ldp/graph/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def to_dict(self) -> dict[str, Any]:
"op_name": self.op_name,
"op_class_name": self.op_class_name,
"value": value_dump,
"logprob": self.logprob,
}

@classmethod
Expand Down
Loading