2023-10-11 19:31:26 +02:00
|
|
|
import logging
|
2023-09-27 14:10:45 +02:00
|
|
|
import os
|
2023-10-11 19:31:26 +02:00
|
|
|
from abc import ABC, abstractmethod
|
2023-10-12 17:40:16 +02:00
|
|
|
from collections.abc import Callable
|
2023-10-12 15:01:49 +02:00
|
|
|
from enum import Enum
|
2023-10-12 17:40:16 +02:00
|
|
|
from typing import TYPE_CHECKING, Protocol, Self, runtime_checkable
|
2023-10-11 19:31:26 +02:00
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from tianshou.highlevel.world import World
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from tianshou.highlevel.module.core import TDevice
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2023-09-27 14:10:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@runtime_checkable
|
|
|
|
class PersistableConfigProtocol(Protocol):
|
|
|
|
@classmethod
|
|
|
|
def load(cls, path: os.PathLike[str]) -> Self:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def save(self, path: os.PathLike[str]) -> None:
|
|
|
|
pass
|
2023-10-11 19:31:26 +02:00
|
|
|
|
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
class PersistEvent(Enum):
|
2023-10-12 17:40:16 +02:00
|
|
|
"""Enumeration of persistence events that Persistence objects can react to."""
|
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
PERSIST_POLICY = "persist_policy"
|
|
|
|
"""Policy neural network is persisted (new best found)"""
|
|
|
|
|
|
|
|
|
|
|
|
class RestoreEvent(Enum):
|
2023-10-12 17:40:16 +02:00
|
|
|
"""Enumeration of restoration events that Persistence objects can react to."""
|
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
RESTORE_POLICY = "restore_policy"
|
|
|
|
"""Policy neural network parameters are restored"""
|
2023-10-11 19:31:26 +02:00
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
|
|
|
|
class Persistence(ABC):
|
2023-10-11 19:31:26 +02:00
|
|
|
@abstractmethod
|
2023-10-12 15:01:49 +02:00
|
|
|
def persist(self, event: PersistEvent, world: World) -> None:
|
2023-10-11 19:31:26 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-10-12 15:01:49 +02:00
|
|
|
def restore(self, event: RestoreEvent, world: World):
|
2023-10-11 19:31:26 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PersistenceGroup(Persistence):
|
2023-10-12 17:40:16 +02:00
|
|
|
def __init__(self, *p: Persistence, enabled=True):
|
2023-10-11 19:31:26 +02:00
|
|
|
self.items = p
|
2023-10-12 17:40:16 +02:00
|
|
|
self.enabled = enabled
|
2023-10-11 19:31:26 +02:00
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
def persist(self, event: PersistEvent, world: World) -> None:
|
2023-10-12 17:40:16 +02:00
|
|
|
if not self.enabled:
|
|
|
|
return
|
2023-10-11 19:31:26 +02:00
|
|
|
for item in self.items:
|
2023-10-12 15:01:49 +02:00
|
|
|
item.persist(event, world)
|
2023-10-11 19:31:26 +02:00
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
def restore(self, event: RestoreEvent, world: World):
|
2023-10-11 19:31:26 +02:00
|
|
|
for item in self.items:
|
2023-10-12 15:01:49 +02:00
|
|
|
item.restore(event, world)
|
2023-10-11 19:31:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyPersistence:
|
|
|
|
FILENAME = "policy.dat"
|
|
|
|
|
2023-10-12 17:40:16 +02:00
|
|
|
def __init__(self, additional_persistence: Persistence | None = None, enabled=True):
|
|
|
|
""":param additional_persistence: a persistence instance which is to be envoked whenever
|
|
|
|
this object is used to persist/restore data
|
|
|
|
:param enabled: whether persistence is enabled (restoration is always enabled)
|
|
|
|
"""
|
2023-10-12 15:01:49 +02:00
|
|
|
self.additional_persistence = additional_persistence
|
2023-10-12 17:40:16 +02:00
|
|
|
self.enabled = enabled
|
2023-10-12 15:01:49 +02:00
|
|
|
|
|
|
|
def persist(self, policy: torch.nn.Module, world: World) -> None:
|
2023-10-12 17:40:16 +02:00
|
|
|
if not self.enabled:
|
|
|
|
return
|
2023-10-12 15:01:49 +02:00
|
|
|
path = world.persist_path(self.FILENAME)
|
2023-10-11 19:31:26 +02:00
|
|
|
log.info(f"Saving policy in {path}")
|
|
|
|
torch.save(policy.state_dict(), path)
|
2023-10-12 15:01:49 +02:00
|
|
|
if self.additional_persistence is not None:
|
|
|
|
self.additional_persistence.persist(PersistEvent.PERSIST_POLICY, world)
|
2023-10-11 19:31:26 +02:00
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
def restore(self, policy: torch.nn.Module, world: World, device: "TDevice") -> None:
|
|
|
|
path = world.restore_path(self.FILENAME)
|
2023-10-11 19:31:26 +02:00
|
|
|
log.info(f"Restoring policy from {path}")
|
|
|
|
state_dict = torch.load(path, map_location=device)
|
|
|
|
policy.load_state_dict(state_dict)
|
2023-10-12 15:01:49 +02:00
|
|
|
if self.additional_persistence is not None:
|
|
|
|
self.additional_persistence.restore(RestoreEvent.RESTORE_POLICY, world)
|
2023-10-11 19:31:26 +02:00
|
|
|
|
2023-10-12 15:01:49 +02:00
|
|
|
def get_save_best_fn(self, world) -> Callable[[torch.nn.Module], None]:
|
2023-10-11 19:31:26 +02:00
|
|
|
def save_best_fn(pol: torch.nn.Module) -> None:
|
2023-10-12 15:01:49 +02:00
|
|
|
self.persist(pol, world)
|
2023-10-11 19:31:26 +02:00
|
|
|
|
|
|
|
return save_best_fn
|