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/dotdict #112

Merged
merged 9 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
6 changes: 3 additions & 3 deletions howto/register_new_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def train(
optimizer: torch.optim.Optimizer,
data: TensorDictBase,
aggregator: MetricAggregator,
cfg: DictConfig,
cfg: Dict[str, Any],
):
l1 = loss1(...)
l2 = loss2(...)
Expand All @@ -82,7 +82,7 @@ def train(


@register_algorithm(decoupled=False)
def sota_main(fabric: Fabric, cfg: DictConfig):
def sota_main(fabric: Fabric, cfg: Dict[str, Any]):
rank = fabric.global_rank
world_size = fabric.world_size
device = fabric.device
Expand All @@ -93,7 +93,7 @@ def sota_main(fabric: Fabric, cfg: DictConfig):
logger, log_dir = create_tensorboard_logger(fabric, cfg)
if fabric.is_global_zero:
fabric._loggers = [logger]
fabric.logger.log_hyperparams(OmegaConf.to_container(cfg, resolve=True))
fabric.logger.log_hyperparams(cfg)

# Environment setup
vectorized_env = gym.vector.SyncVectorEnv if cfg.env.sync_env else gym.vector.AsyncVectorEnv
Expand Down
6 changes: 3 additions & 3 deletions sheeprl/algos/dreamer_v1/agent.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any, Dict, Optional, Sequence, Tuple

import gymnasium
import hydra
import numpy as np
import torch
from lightning.fabric import Fabric
from lightning.fabric.wrappers import _FabricModule
from omegaconf import DictConfig
from sympy import Union
from torch import Tensor, nn
from torch.distributions import Normal, OneHotCategorical
Expand Down Expand Up @@ -330,8 +330,8 @@ def build_models(
fabric: Fabric,
actions_dim: Sequence[int],
is_continuous: bool,
cfg: DictConfig,
obs_space: Dict[str, Any],
cfg: Dict[str, Any],
obs_space: gymnasium.spaces.Dict,
world_model_state: Optional[Dict[str, Tensor]] = None,
actor_state: Optional[Dict[str, Tensor]] = None,
critic_state: Optional[Dict[str, Tensor]] = None,
Expand Down
14 changes: 7 additions & 7 deletions sheeprl/algos/dreamer_v1/dreamer_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import pathlib
import warnings
from typing import Dict
from typing import Any, Dict

import gymnasium as gym
import hydra
Expand All @@ -11,7 +11,7 @@
import torch.nn.functional as F
from lightning.fabric import Fabric
from lightning.fabric.wrappers import _FabricModule, _FabricOptimizer
from omegaconf import DictConfig, OmegaConf
from omegaconf import OmegaConf
from tensordict import TensorDict
from tensordict.tensordict import TensorDictBase
from torch.distributions import Bernoulli, Independent, Normal
Expand All @@ -28,7 +28,7 @@
from sheeprl.utils.metric import MetricAggregator
from sheeprl.utils.registry import register_algorithm
from sheeprl.utils.timer import timer
from sheeprl.utils.utils import polynomial_decay
from sheeprl.utils.utils import dotdict, polynomial_decay

# Decomment the following two lines if you cannot start an experiment with DMC environments
# os.environ["PYOPENGL_PLATFORM"] = ""
Expand All @@ -45,7 +45,7 @@ def train(
critic_optimizer: _FabricOptimizer,
data: TensorDictBase,
aggregator: MetricAggregator,
cfg: DictConfig,
cfg: Dict[str, Any],
) -> None:
"""Runs one-step update of the agent.

Expand Down Expand Up @@ -356,7 +356,7 @@ def train(


@register_algorithm()
def main(fabric: Fabric, cfg: DictConfig):
def main(fabric: Fabric, cfg: Dict[str, Any]):
device = fabric.device
rank = fabric.global_rank
world_size = fabric.world_size
Expand All @@ -368,7 +368,7 @@ def main(fabric: Fabric, cfg: DictConfig):
run_name = cfg.run_name
state = fabric.load(cfg.checkpoint.resume_from)
ckpt_path = pathlib.Path(cfg.checkpoint.resume_from)
cfg = OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml")
cfg = dotdict(OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml"))
cfg.checkpoint.resume_from = str(ckpt_path)
cfg.per_rank_batch_size = state["batch_size"] // world_size
cfg.root_dir = root_dir
Expand All @@ -383,7 +383,7 @@ def main(fabric: Fabric, cfg: DictConfig):
logger, log_dir = create_tensorboard_logger(fabric, cfg)
if fabric.is_global_zero:
fabric._loggers = [logger]
fabric.logger.log_hyperparams(OmegaConf.to_container(cfg, resolve=True))
fabric.logger.log_hyperparams(cfg)

# Environment setup
vectorized_env = gym.vector.SyncVectorEnv if cfg.env.sync_env else gym.vector.AsyncVectorEnv
Expand Down
6 changes: 3 additions & 3 deletions sheeprl/algos/dreamer_v2/agent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import copy
from typing import Any, Dict, List, Optional, Sequence, Tuple

import gymnasium
import hydra
import numpy as np
import torch
import torch.nn.functional as F
from lightning.fabric import Fabric
from lightning.fabric.wrappers import _FabricModule
from omegaconf import DictConfig
from torch import Tensor, nn
from torch.distributions import (
Distribution,
Expand Down Expand Up @@ -777,8 +777,8 @@ def build_models(
fabric: Fabric,
actions_dim: Sequence[int],
is_continuous: bool,
cfg: DictConfig,
obs_space: Dict[str, Any],
cfg: Dict[str, Any],
obs_space: gymnasium.spaces.Dict,
world_model_state: Optional[Dict[str, Tensor]] = None,
actor_state: Optional[Dict[str, Tensor]] = None,
critic_state: Optional[Dict[str, Tensor]] = None,
Expand Down
14 changes: 7 additions & 7 deletions sheeprl/algos/dreamer_v2/dreamer_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import pathlib
import warnings
from typing import Dict, Sequence
from typing import Any, Dict, Sequence

import gymnasium as gym
import hydra
Expand All @@ -15,7 +15,7 @@
import torch.nn.functional as F
from lightning.fabric import Fabric
from lightning.fabric.wrappers import _FabricModule
from omegaconf import DictConfig, OmegaConf
from omegaconf import OmegaConf
from tensordict import TensorDict
from tensordict.tensordict import TensorDictBase
from torch import Tensor
Expand All @@ -33,7 +33,7 @@
from sheeprl.utils.metric import MetricAggregator
from sheeprl.utils.registry import register_algorithm
from sheeprl.utils.timer import timer
from sheeprl.utils.utils import polynomial_decay
from sheeprl.utils.utils import dotdict, polynomial_decay

# Decomment the following two lines if you cannot start an experiment with DMC environments
# os.environ["PYOPENGL_PLATFORM"] = ""
Expand All @@ -51,7 +51,7 @@ def train(
critic_optimizer: Optimizer,
data: TensorDictBase,
aggregator: MetricAggregator,
cfg: DictConfig,
cfg: Dict[str, Any],
actions_dim: Sequence[int],
) -> None:
"""Runs one-step update of the agent.
Expand Down Expand Up @@ -375,7 +375,7 @@ def train(


@register_algorithm()
def main(fabric: Fabric, cfg: DictConfig):
def main(fabric: Fabric, cfg: Dict[str, Any]):
device = fabric.device
rank = fabric.global_rank
world_size = fabric.world_size
Expand All @@ -387,7 +387,7 @@ def main(fabric: Fabric, cfg: DictConfig):
run_name = cfg.run_name
state = fabric.load(cfg.checkpoint.resume_from)
ckpt_path = pathlib.Path(cfg.checkpoint.resume_from)
cfg = OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml")
cfg = dotdict(OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml"))
cfg.checkpoint.resume_from = str(ckpt_path)
cfg.per_rank_batch_size = state["batch_size"] // world_size
cfg.root_dir = root_dir
Expand All @@ -402,7 +402,7 @@ def main(fabric: Fabric, cfg: DictConfig):
logger, log_dir = create_tensorboard_logger(fabric, cfg)
if fabric.is_global_zero:
fabric._loggers = [logger]
fabric.logger.log_hyperparams(OmegaConf.to_container(cfg, resolve=True))
fabric.logger.log_hyperparams(cfg)

# Environment setup
vectorized_env = gym.vector.SyncVectorEnv if cfg.env.sync_env else gym.vector.AsyncVectorEnv
Expand Down
5 changes: 2 additions & 3 deletions sheeprl/algos/dreamer_v2/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, Optional, Union

import gymnasium as gym
import numpy as np
import torch
import torch.nn as nn
from lightning import Fabric
from omegaconf import DictConfig
from torch import Tensor
from torch.distributions import Independent, OneHotCategoricalStraightThrough

Expand Down Expand Up @@ -82,7 +81,7 @@ def compute_lambda_values(
def test(
player: Union["PlayerDV2", "PlayerDV1"],
fabric: Fabric,
cfg: DictConfig,
cfg: Dict[str, Any],
test_name: str = "",
sample_actions: bool = False,
):
Expand Down
6 changes: 3 additions & 3 deletions sheeprl/algos/dreamer_v3/agent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import copy
from typing import Any, Dict, List, Optional, Sequence, Tuple

import gymnasium
import hydra
import numpy as np
import torch
import torch.nn.functional as F
from lightning.fabric import Fabric
from lightning.fabric.wrappers import _FabricModule
from omegaconf import DictConfig
from torch import Tensor, device, nn
from torch.distributions import (
Distribution,
Expand Down Expand Up @@ -815,8 +815,8 @@ def build_models(
fabric: Fabric,
actions_dim: Sequence[int],
is_continuous: bool,
cfg: DictConfig,
obs_space: Dict[str, Any],
cfg: Dict[str, Any],
obs_space: gymnasium.spaces.Dict,
world_model_state: Optional[Dict[str, Tensor]] = None,
actor_state: Optional[Dict[str, Tensor]] = None,
critic_state: Optional[Dict[str, Tensor]] = None,
Expand Down
14 changes: 7 additions & 7 deletions sheeprl/algos/dreamer_v3/dreamer_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pathlib
import warnings
from functools import partial
from typing import Dict, Sequence
from typing import Any, Dict, Sequence

import gymnasium as gym
import hydra
Expand All @@ -16,7 +16,7 @@
import torch.nn.functional as F
from lightning.fabric import Fabric
from lightning.fabric.wrappers import _FabricModule
from omegaconf import DictConfig, OmegaConf
from omegaconf import OmegaConf
from tensordict import TensorDict
from tensordict.tensordict import TensorDictBase
from torch import Tensor
Expand All @@ -36,7 +36,7 @@
from sheeprl.utils.metric import MetricAggregator
from sheeprl.utils.registry import register_algorithm
from sheeprl.utils.timer import timer
from sheeprl.utils.utils import polynomial_decay
from sheeprl.utils.utils import dotdict, polynomial_decay

# Decomment the following two lines if you cannot start an experiment with DMC environments
# os.environ["PYOPENGL_PLATFORM"] = ""
Expand All @@ -54,7 +54,7 @@ def train(
critic_optimizer: Optimizer,
data: TensorDictBase,
aggregator: MetricAggregator,
cfg: DictConfig,
cfg: Dict[str, Any],
is_continuous: bool,
actions_dim: Sequence[int],
moments: Moments,
Expand Down Expand Up @@ -328,7 +328,7 @@ def train(


@register_algorithm()
def main(fabric: Fabric, cfg: DictConfig):
def main(fabric: Fabric, cfg: Dict[str, Any]):
device = fabric.device
rank = fabric.global_rank
world_size = fabric.world_size
Expand All @@ -340,7 +340,7 @@ def main(fabric: Fabric, cfg: DictConfig):
run_name = cfg.run_name
state = fabric.load(cfg.checkpoint.resume_from)
ckpt_path = pathlib.Path(cfg.checkpoint.resume_from)
cfg = OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml")
cfg = dotdict(OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml"))
cfg.checkpoint.resume_from = str(ckpt_path)
cfg.per_rank_batch_size = state["batch_size"] // fabric.world_size
cfg.root_dir = root_dir
Expand All @@ -356,7 +356,7 @@ def main(fabric: Fabric, cfg: DictConfig):
logger, log_dir = create_tensorboard_logger(fabric, cfg)
if fabric.is_global_zero:
fabric._loggers = [logger]
fabric.logger.log_hyperparams(OmegaConf.to_container(cfg, resolve=True))
fabric.logger.log_hyperparams(cfg)

# Environment setup
vectorized_env = gym.vector.SyncVectorEnv if cfg.env.sync_env else gym.vector.AsyncVectorEnv
Expand Down
5 changes: 2 additions & 3 deletions sheeprl/algos/dreamer_v3/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import os
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Dict

import gymnasium as gym
import numpy as np
import torch
from lightning import Fabric
from omegaconf import DictConfig
from torch import Tensor

from sheeprl.utils.env import make_env
Expand Down Expand Up @@ -60,7 +59,7 @@ def compute_lambda_values(
def test(
player: "PlayerDV3",
fabric: Fabric,
cfg: DictConfig,
cfg: Dict[str, Any],
test_name: str = "",
sample_actions: bool = False,
):
Expand Down
12 changes: 7 additions & 5 deletions sheeprl/algos/droq/droq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import pathlib
import warnings
from math import prod
from typing import Any, Dict

import gymnasium as gym
import hydra
import numpy as np
import torch
import torch.nn.functional as F
from lightning.fabric import Fabric
from omegaconf import DictConfig, OmegaConf
from omegaconf import OmegaConf
from tensordict import TensorDict, make_tensordict
from torch.optim import Optimizer
from torch.utils.data.distributed import DistributedSampler
Expand All @@ -27,6 +28,7 @@
from sheeprl.utils.metric import MetricAggregator
from sheeprl.utils.registry import register_algorithm
from sheeprl.utils.timer import timer
from sheeprl.utils.utils import dotdict


def train(
Expand All @@ -37,7 +39,7 @@ def train(
alpha_optimizer: Optimizer,
rb: ReplayBuffer,
aggregator: MetricAggregator,
cfg: DictConfig,
cfg: Dict[str, Any],
):
# Sample a minibatch in a distributed way: Line 5 - Algorithm 2
# We sample one time to reduce the communications between processes
Expand Down Expand Up @@ -124,7 +126,7 @@ def train(


@register_algorithm()
def main(fabric: Fabric, cfg: DictConfig):
def main(fabric: Fabric, cfg: Dict[str, Any]):
if "minedojo" in cfg.env.wrapper._target_.lower():
raise ValueError(
"MineDojo is not currently supported by DroQ agent, since it does not take "
Expand All @@ -145,7 +147,7 @@ def main(fabric: Fabric, cfg: DictConfig):
run_name = cfg.run_name
state = fabric.load(cfg.checkpoint.resume_from)
ckpt_path = pathlib.Path(cfg.checkpoint.resume_from)
cfg = OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml")
cfg = dotdict(OmegaConf.load(ckpt_path.parent.parent.parent / ".hydra" / "config.yaml"))
cfg.checkpoint.resume_from = str(ckpt_path)
cfg.per_rank_batch_size = state["batch_size"] // fabric.world_size
cfg.root_dir = root_dir
Expand All @@ -160,7 +162,7 @@ def main(fabric: Fabric, cfg: DictConfig):
logger, log_dir = create_tensorboard_logger(fabric, cfg)
if fabric.is_global_zero:
fabric._loggers = [logger]
fabric.logger.log_hyperparams(OmegaConf.to_container(cfg, resolve=True))
fabric.logger.log_hyperparams(cfg)

# Environment setup
vectorized_env = gym.vector.SyncVectorEnv if cfg.env.sync_env else gym.vector.AsyncVectorEnv
Expand Down
Loading