docstr: Test whether DreamerV3 can be built with all frameworks.
docstr_tok: ["Test","whether","DreamerV3","can","be","built","with","all","frameworks","."]
code: def test_dreamerv3_compilation(self):
      """Test whether DreamerV3 can be built with all frameworks."""
  
      # Build a DreamerV3Config object.
      config = (
          dreamerv3.DreamerV3Config()
          .training(
              # Keep things simple. Especially the long dream rollouts seem
              # to take an enormous amount of time (initially).
              batch_size_B=4,
              horizon_H=5,
              batch_length_T=16,
              model_size="nano",  # Use a tiny model for testing
              symlog_obs=True,
              use_float16=False,
          )
          .resources(
              num_learner_workers=2,  # Try with 2 Learners.
              num_cpus_per_learner_worker=1,
              num_gpus_per_learner_worker=0,
          )
      )
  
      # TODO (sven): Add a `get_model_config` utility to AlgorithmConfig
      #  that - for now - merges the user provided model_dict (which only
      #  contains settings that only affect the model, e.g. model_size)
      #  with the AlgorithmConfig-wide settings that are relevant for the model
      #  (e.g. `batch_size_B`).
      # config.get_model_config()
  
      num_iterations = 2
  
      for _ in framework_iterator(config, frameworks="tf2"):
          for env in [
              "FrozenLake-v1",
              "CartPole-v1",
              "ALE/MsPacman-v5",
              "Pendulum-v1",
          ]:
              print("Env={}".format(env))
              # Add one-hot observations for FrozenLake env.
              if env == "FrozenLake-v1":
  
                  def env_creator(ctx):
                      import gymnasium as gym
                      from ray.rllib.algorithms.dreamerv3.utils.env_runner import (
                          OneHot,
                      )
  
                      return OneHot(gym.make("FrozenLake-v1"))
  
                  tune.register_env("frozen-lake-one-hot", env_creator)
                  env = "frozen-lake-one-hot"
  
              config.environment(env)
              algo = config.build()
              obs_space = (
                  algo.workers.local_worker().env.single_observation_space
              )
              act_space = (
                  algo.workers.local_worker().env.single_action_space
              )
              rl_module = algo.workers.local_worker().module
  
              for i in range(num_iterations):
                  results = algo.train()
                  print(results)
              # Test dream trajectory w/ recreated observations.
              sample = algo.replay_buffer.sample()
              dream = rl_module.dreamer_model.dream_trajectory_with_burn_in(
                  start_states=rl_module.dreamer_model.get_initial_state(),
                  timesteps_burn_in=5,
                  timesteps_H=45,
                  observations=sample["obs"][:1],  # B=1
                  actions=(
                      one_hot(
                          sample["actions"],
                          depth=act_space.n,
                      )
                      if isinstance(act_space, gym.spaces.Discrete)
                      else sample["actions"]
                  )[
                      :1
                  ],  # B=1
              )
              self.assertTrue(
                  dream["actions_dreamed_t0_to_H_BxT"].shape
                  == (46, 1)
                  + (
                      (act_space.n,)
                      if isinstance(act_space, gym.spaces.Discrete)
                      else tuple(act_space.shape)
                  )
              )
              self.assertTrue(
                  dream["continues_dreamed_t0_to_H_BxT"].shape
                  == (46, 1)
              )
              self.assertTrue(
                  dream["observations_dreamed_t0_to_H_BxT"].shape
                  == [46, 1] + list(obs_space.shape)
              )
              algo.stop()
  
file_path: ray/rllib/algorithms/dreamerv3/tests/test_dreamerv3.py
imports: import unittest
  import gymnasium
  import numpy
  import ray
  from ray.rllib.algorithms.dreamerv3 import dreamerv3
  from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
  from ray.rllib.utils.numpy import one_hot
  from ray.rllib.utils.test_utils import framework_iterator
  from ray import tune
label_desc: Use image node_id 3 for calling the TestDreamerV3 obj's underlying member method code with example usage: obj.test_dreamerv3_compilation() and returns: OneHot