### View Training Results Source: https://github.com/danijar/dreamerv3/blob/main/README.md Installs the Scope viewer and starts a local server to visualize training results. Results are typically stored in the specified log directory. ```sh pip install -U scope python -m scope.viewer --basedir ~/logdir --port 8000 ``` -------------------------------- ### Configuration Loading Example Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Illustrates the process of configuration loading, showing how named configs and command-line flags override defaults. ```python # Command: # python dreamerv3/main.py --configs size50m atari --batch_size 32 # Results in: # 1. config = elements.Config(configs['defaults']) # 2. config = config.update(configs['size50m']) # 3. config = config.update(configs['atari']) # 4. config.batch_size = 32 ``` -------------------------------- ### Setup Portal for IPC Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Initializes the portal for inter-process communication, error logging, and distributed setup. Supports IPv6. ```python portal.setup( errfile=config.errfile and logdir / 'error', clientkw=dict(logging_color='cyan'), serverkw=dict(logging_color='cyan'), initfns=[init], ipv6=config.ipv6, ) ``` -------------------------------- ### Example Usage of Standard Training Loop Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Example demonstrating how to call the standard train function with bound factories and configuration. Ensure all necessary imports are present. ```python from functools import partial as bind from dreamerv3.main import make_agent, make_replay, make_env, make_stream, make_logger from embodied.run import train import elements config = elements.Config(...) train( bind(make_agent, config), bind(make_replay, config, 'replay'), bind(make_env, config), bind(make_stream, config), bind(make_logger, config), config.run, ) ``` -------------------------------- ### Portal Setup Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Sets up the portal for inter-process communication and error handling. ```APIDOC ## Portal Setup ### Description Sets up the portal for inter-process communication and error handling. ### Method ```python portal.setup( errfile=config.errfile and logdir / 'error', clientkw=dict(logging_color='cyan'), serverkw=dict(logging_color='cyan'), initfns=[init], ipv6=config.ipv6, ) ``` ### Functionality - Error logging to file (if enabled) - Colored logging output - Distributed communication (ipv6 support) ### Example ```python # Assuming config and logdir are defined portal.setup( errfile=config.errfile and logdir / 'error', clientkw=dict(logging_color='cyan'), serverkw=dict(logging_color='cyan'), initfns=[init], ipv6=config.ipv6, ) ``` ``` -------------------------------- ### Make Environment Example Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Example of creating an Atari environment using the make_env factory function with specific configurations. ```python from dreamerv3.main import make_env import elements config = elements.Config() config.task = 'atari_pong' config.seed = 0 config.logdir = '~/logs/dreamer' config.env = { 'atari': {'size': [96, 96], 'repeat': 4, 'sticky': True} } env = make_env(config, index=0) obs = env.step({'action': 0, 'reset': True}) ``` -------------------------------- ### Configuration Inheritance Example Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Demonstrates how to apply multiple configuration files and command-line overrides. Configurations are applied in the order they are listed, with later ones overriding earlier ones. ```bash python dreamerv3/main.py \ --configs defaults size100m atari \ --logdir ~/logs/atari \ --run.train_ratio 64 ``` -------------------------------- ### Configuration Object Usage Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/types.md Demonstrates how to create, access, and update a hierarchical configuration object using dot notation or dictionary access, with an example of getting a value with a default. ```python import elements config = elements.Config({ 'agent': { 'opt': { 'lr': 4e-5, 'beta1': 0.9, }, 'dyn': { 'typ': 'rssm', 'rssm': { 'deter': 4096, }, }, }, }) # Access methods: lr = config.agent.opt.lr # 4e-5 lr = config['agent']['opt']['lr'] # 4e-5 # Update: config = config.update({'agent': {'opt': {'lr': 2e-5}}}) # Get with default: val = config.get('missing_key', 42) ``` -------------------------------- ### Gym Environment Usage Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Example of how to set a Gym environment task and create an environment instance using make_env. ```python config.task = 'gym_CartPole-v1' env = make_env(config, 0) ``` -------------------------------- ### Install Dependencies Source: https://github.com/danijar/dreamerv3/blob/main/README.md Installs the required Python packages for the project. Ensure JAX is installed first. ```sh pip install -U -r requirements.txt ``` -------------------------------- ### RSSM.starts Method Signature Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/rssm.md Prepares imagination starting points by taking the last N timesteps and flattening the batch. It reshapes the state with the batch dimension expanded. ```python def starts(self, entries: dict, carry: dict, nlast: int) -> dict ``` -------------------------------- ### Initialize Driver with Parallel Environments Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Sets up parallel environment runners. When parallel=True, uses multiprocessing with pipes for communication. This example demonstrates creating a Driver with multiple environments. ```python from embodied.core import Driver def make_env(): return MyEnv() driver = Driver([make_env for _ in range(16)], parallel=True) driver.reset(init_policy=agent.init_policy) ``` -------------------------------- ### Start Training Script Source: https://github.com/danijar/dreamerv3/blob/main/README.md Launches the DreamerV3 training script. Customize log directory, configuration, and training ratio as needed. ```sh python dreamerv3/main.py \ --logdir ~/logdir/dreamer/{timestamp} \ --configs crafter \ --run.train_ratio 32 ``` -------------------------------- ### DMLab Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Example training configuration for DMLab, specifying the task and run parameters like steps and train_ratio. ```yaml dmlab: task: dmlab_explore_goal_locations_small run: {steps: 2.6e7, train_ratio: 32} ``` -------------------------------- ### Parallel Replay and Training Setup Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Runs replay buffer and training loop in separate processes. Requires functions to create replay buffers, evaluation replay buffers, and data streams, along with a configuration object. ```python def parallel_replay( make_replay: callable, make_replay_eval: callable, make_stream: callable, config: elements.Config) -> None ``` -------------------------------- ### Replay Buffer Configuration Example Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Configures the replay buffer's capacity, sampling methods, and prioritization settings. 'size' defines the total capacity in steps. ```yaml replay: size: 5e6 online: True fracs: {uniform: 1.0, ...} prio: {exponent: 0.8, maxfrac: 0.5} chunksize: 1024 ``` -------------------------------- ### Mixture Selector Setup Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Configures a mixture selector for prioritized and recency-based sampling during training. Requires replay configuration. ```python recency = 1.0 / np.arange(1, capacity + 1) ** config.replay.recexp selectors = embodied.replay.selectors selector = selectors.Mixture({ 'uniform': selectors.Uniform(), 'priority': selectors.Prioritized(**config.replay.prio), 'recency': selectors.Recency(recency), }, config.replay.fracs) ``` -------------------------------- ### Atari Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Example training configuration for Atari environments, specifying total steps and training ratio. Use this to set up training runs. ```yaml atari: run: {steps: 5.1e7, train_ratio: 32} ``` -------------------------------- ### Parallel Environments Setup Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Sets up multiple environment workers for data collection and evaluation. Requires functions to create environments and a configuration object. ```python def parallel_envs( make_env: callable, make_env_eval: callable, config: elements.Config) -> None ``` -------------------------------- ### Logger Configuration Example Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Specifies output formats, metrics to log, and timing settings for the logger. Adjust the 'filter' regex to control which metrics are displayed. ```yaml logger: outputs: [jsonl, scope] filter: 'score|length|fps|...' timer: True fps: 15 user: '' ``` -------------------------------- ### Environment Configuration Examples Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Defines specific settings for different environments like Atari, DeepMind Control, and Crafter. Common options include image size, action repeat, and grayscale conversion. ```yaml env: atari: {size: [96, 96], repeat: 4, sticky: True, gray: True, actions: all, ...} dmc: {size: [64, 64], repeat: 1, proprio: True, image: True, camera: -1} crafter: {size: [64, 64], logs: False} ``` -------------------------------- ### Initialize Replay Buffer with Prioritized Sampling Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes an experience replay buffer with chunked storage. This example configures the buffer for a sequence length of 64, a capacity of 5 million, disk storage, and prioritized sampling. ```python from embodied.core import Replay from embodied.core.selectors import Prioritized replay = Replay( length=64, capacity=int(5e6), directory='/tmp/replay', chunksize=1024, selector=Prioritized(exponent=0.8), ) ``` -------------------------------- ### Example Atari Action Dictionary Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/types.md Demonstrates the action dictionary format for an Atari environment, specifying the action to take and the reset flag. ```python action = { 'action': np.array(3, dtype=np.int32), # Action index 'reset': np.array(False), } ``` -------------------------------- ### RSSM.starts Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/rssm.md Prepares imagination starting points by taking the last N timesteps and flattening the batch. It reshapes the state with the batch dimension expanded from `[batch, time, ...]` to `[batch*nlast, ...]`. ```APIDOC ## RSSM.starts ### Description Prepares imagination starting points by taking the last N timesteps and flattening the batch. Reshapes state with batch dimension expanded from `[batch, time, ...]` to `[batch*nlast, ...]`. ### Method `starts(self, entries: dict, carry: dict, nlast: int) -> dict` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **entries** (dict) - Required - State entries from observation. * **carry** (dict) - Required - Current state. * **nlast** (int) - Required - Number of final states to extract. ### Request Example ```python # Example usage (conceptual) # entries = {...} # carry = {...} # nlast = 5 # result = rssm.starts(entries, carry, nlast) ``` ### Response #### Success Response (200) * **dict** - Reshaped state with batch dimension expanded. ``` -------------------------------- ### DMC Proprioceptive Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Example training configuration for DMC with proprioceptive observations only. Use this for proprioception-based continuous control tasks. ```yaml dmc_proprio: <<: *size1m task: dmc_walker_walk env.dmc.image: False run: {steps: 1.1e6, train_ratio: 1024} ``` -------------------------------- ### Crafter Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Example training configuration for Crafter environments, specifying task, steps, environments, and training ratio. Use this for setting up Crafter training runs. ```yaml crafter: task: crafter_reward run: {steps: 1.1e6, envs: 1, train_ratio: 512} ``` -------------------------------- ### Example Usage of OneHot Distribution Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-jax.md Demonstrates how to use the OneHot distribution for sampling actions and calculating entropy from logits. It also shows how to aggregate predictions over a time axis using the Agg wrapper. ```python from embodied.jax import outs # Categorical distribution logits = head(features) dist = outs.OneHot(logits, unimix=0.01) action = dist.sample(seed=rng) entropy = dist.entropy() # Aggregated over time axis dist = outs.Agg(dist, axis=1, agg_fn=jnp.sum) ``` -------------------------------- ### Common Space Usage Examples Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/types.md Illustrates how to create Space objects for various types of observations and actions, including images, vectors, discrete actions, continuous actions, and boolean flags. ```python import elements import numpy as np # Image observation: uint8, 64x64x3 image_space = elements.Space(np.uint8, (64, 64, 3)) # Vector observation: float32, 4D vector_space = elements.Space(np.float32, (4,)) # Discrete action: 18 choices action_space = elements.Space(int, (), 0, 18) # Continuous action: float32, 4D in [-1, 1] action_space = elements.Space(np.float32, (4,), -1.0, 1.0) # Binary flag bool_space = elements.Space(bool, ()) ``` -------------------------------- ### Encoder Initialization and Usage Example Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/rssm.md Initializes the Encoder and demonstrates its usage for encoding observations into token representations. The encoder handles image downsampling and vector normalization. ```python from dreamerv3.rssm import Encoder obs_space = { 'image': elements.Space(np.uint8, (64, 64, 3)), 'position': elements.Space(np.float32, (2,)), } encoder = Encoder(obs_space, depth=64, mults=(2, 3, 4, 4)) carry = encoder.initial(batch_size=16) carry, entries, tokens = encoder(carry, obs, reset, training=True) ``` -------------------------------- ### Example DMC Action Dictionary Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/types.md Shows the action dictionary structure for a DeepMind Control (DMC) environment, typically used for continuous control actions like joint torques, along with the reset flag. ```python action = { 'action': np.array([0.5, -0.3, 0.1, 0.0], dtype=np.float32), # Joint torques 'reset': np.array(False), } ``` -------------------------------- ### Command Line and Python Usage Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Demonstrates how to run the main entry point from the command line or programmatically from Python. ```bash # From command line: python dreamerv3/main.py --configs crafter --logdir ~/logs/crafter ``` ```python # Or from Python: from dreamerv3.main import main main(['--configs', 'crafter', '--logdir', '~/logs/dreamer']) ``` -------------------------------- ### main() Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Entry point for DreamerV3 training and evaluation. Loads configuration, parses command-line flags, sets up distributed training, and executes the training script. ```APIDOC ## main() ### Description Entry point for DreamerV3 training and evaluation. Loads configuration, parses command-line flags, sets up distributed training infrastructure, and executes the specified training script. ### Method `main` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **argv** (list) - Optional - Command-line arguments. If None, uses sys.argv[1:]. ### Request Example ```python from dreamerv3.main import main main(['--configs', 'crafter', '--logdir', '~/logs/crafter']) ``` ### Response None #### Success Response (None) None #### Response Example None ``` -------------------------------- ### Create and Use Environment Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This Python snippet demonstrates how to create an environment using the `make_env` function and then perform reset and step operations. It requires a configuration object to specify environment parameters. ```python from dreamerv3.main import make_env import elements config = elements.Config() config.task = 'atari_pong' config.seed = 0 config.env = {'atari': {'size': [96, 96]}} env = make_env(config, index=0) # Reset and step obs = env.step({'action': 0, 'reset': True}) obs = env.step({'action': 1, 'reset': False}) env.close() ``` -------------------------------- ### Run Configuration Parameters Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Configure the training process, including steps, duration, logging, and environment settings. ```yaml run: steps: 1e10 # Total training steps duration: 0 # Training duration in seconds (0=disabled) train_ratio: 32.0 # Training steps per environment step log_every: 120 # Logging interval in seconds report_every: 300 # Evaluation interval in seconds save_every: 900 # Checkpoint interval in seconds envs: 16 # Number of training environments eval_envs: 4 # Number of evaluation environments eval_eps: 1 # Evaluation episodes per task ``` -------------------------------- ### Get RSSM Entry Space Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/rssm.md Retrieves the state space definition for RSSM, useful for checkpointing and state management. ```python dict with keys `deter` and `stoch` mapping to Space objects for checkpointing. ``` -------------------------------- ### Single Environment Worker Process Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Signature for the parallel_env function. This runs a single environment worker process in a distributed setup. ```python def parallel_env( make_env: callable, index: int, config: elements.Config, is_eval: bool) -> None ``` -------------------------------- ### Replay.__init__ Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes the Replay buffer for experience replay with configurable capacity, storage, and sampling strategies. ```APIDOC ## Replay.__init__ ### Description Efficient replay buffer with chunked storage. Supports multiple sampling strategies and online updates. ### Method __init__ ### Parameters #### Path Parameters - **length** (int) - Required - Sequence length for sampling. - **capacity** (int) - Optional - Max number of sequences. If None, unlimited. - **directory** (str) - Optional - Path to save chunks to disk. - **chunksize** (int) - Optional - Chunk size (steps per chunk file). Default: 1024 - **online** (bool) - Optional - Enable online sampling from ongoing episodes. Default: False - **selector** (object) - Optional - Sampling strategy (default: Uniform). - **save_wait** (bool) - Optional - Wait for disk I/O before returning. Default: False - **name** (str) - Optional - Replay buffer name for logging. Default: 'unnamed' - **seed** (int) - Optional - Random seed. Default: 0 ### Request Example ```python from embodied.core import Replay from embodied.core.selectors import Prioritized replay = Replay( length=64, capacity=int(5e6), directory='/tmp/replay', chunksize=1024, selector=Prioritized(exponent=0.8), ) ``` ``` -------------------------------- ### Train on Crafter with Defaults Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This command initiates training on the Crafter environment using the default configuration settings. ```bash python dreamerv3/main.py --configs crafter ``` -------------------------------- ### DMC Vision Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Example training configuration for DMC with visual observations only. Use this for vision-based continuous control tasks. ```yaml dmc_vision: task: dmc_walker_walk env.dmc.proprio: False run: {steps: 1.1e6, train_ratio: 256} ``` -------------------------------- ### Train on Atari with Larger Model Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This command starts training on an Atari environment with a larger model configuration and specifies the Atari Pong task. ```bash python dreamerv3/main.py --configs size100m atari --task atari_pong ``` -------------------------------- ### Dummy Environment Usage (Testing) Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Command-line usage for running the Dummy environment with a specified task and number of steps for testing. ```bash python dreamerv3/main.py --configs defaults --task dummy_disc --run.steps 1000 ``` -------------------------------- ### RSSM.imagine Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/rssm.md Generates imagined trajectories by rolling forward the model with predicted actions. It takes a starting state and a policy to produce future states and actions. ```APIDOC ## RSSM.imagine ### Description Generates imagined trajectories by rolling forward the model with predicted actions. It takes a starting state and a policy to produce future states and actions. ### Parameters #### Path Parameters - **carry** (dict) - Required - Starting state. - **policy** (callable or jax.Array) - Required - Action policy (callable) or action sequence (array). - **length** (int) - Required - Trajectory length. - **training** (bool) - Required - Training mode flag. - **single** (bool) - Optional - Single step vs. sequence. Default: False ### Returns - **tuple**: A tuple of `(final_carry, features, actions)` where features have shape `[batch, length, ...]`. ``` -------------------------------- ### Initialize and Use Replay Buffer Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This Python snippet shows how to initialize a replay buffer using `make_replay`, add transitions, sample batches, and update replay priorities. It requires a configuration object for buffer settings. ```python from dreamerv3.main import make_replay import elements config = elements.Config() config.batch_size = 16 config.batch_length = 64 config.logdir = '/tmp/logs' config.replay = {...} replay = make_replay(config, 'replay', mode='train') # Add transitions replay.add(transition_dict, worker=0) # Sample batch batch = replay.sample(batch_size=16, mode='train') # Update with new info replay.update({'stepid': batch['stepid'], 'priority': priorities}) # Get stats stats = replay.stats() ``` -------------------------------- ### JAX Configuration Parameters Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Configure JAX backend settings, including device platform, compute dtype, and JIT compilation. ```yaml jax: platform: cuda # Device: cuda, cpu, tpu compute_dtype: bfloat16 # Computation dtype: float32, bfloat16 policy_devices: [0] # Policy execution devices train_devices: [0] # Training devices mock_devices: 0 # Mock devices for testing prealloc: True # Preallocate device memory jit: True # Compile with JIT debug: False # JAX debug mode expect_devices: 0 # Expected device count (0=auto) enable_policy: True # Enable policy execution coordinator_address: '' # Multi-machine coordinator ``` -------------------------------- ### Example Atari Observation Dictionary Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/types.md Shows the structure of an observation dictionary for an Atari environment, including image data and essential metadata like episode flags and reward. ```python obs = { 'image': np.zeros((96, 96, 1), dtype=np.uint8), # Grayscale image 'is_first': np.array(True), 'is_last': np.array(False), 'is_terminal': np.array(False), 'reward': np.array(0.0, dtype=np.float32), 'log/episode': np.array(42), } ``` -------------------------------- ### Driver.on_step Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Registers a callback function to process each transition from the environments. ```APIDOC ## Driver.on_step ### Description Register a callback to process each transition. ### Method on_step ### Parameters #### Path Parameters - **callback** (callable) - Required - Function called with `(transition, env_index, **kwargs)`. ### Request Example ```python def on_step(transition, index, **kwargs): replay.add(transition) driver.on_step(on_step) ``` ``` -------------------------------- ### Get Replay Buffer Statistics Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Returns current statistics of the replay buffer, including items, chunks, streams, memory usage, and performance metrics. Resets metric counters. ```python def stats(self) -> dict ``` -------------------------------- ### Example DMC Vision Observation Dictionary Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/types.md Illustrates the observation dictionary format for a DeepMind Control (DMC) vision-based environment, featuring image data and core episode tracking flags. ```python obs = { 'image': np.zeros((64, 64, 3), dtype=np.uint8), 'is_first': np.array(True), 'is_last': np.array(False), 'is_terminal': np.array(False), 'reward': np.array(0.0, dtype=np.float32), } ``` -------------------------------- ### Instantiate and Use Decoder Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/rssm.md Demonstrates how to instantiate a Decoder with a given observation space and use its __call__ method to decode features into reconstructions. Assumes 'features' is pre-defined. ```python from dreamerv3.rssm import Decoder obs_space = { 'image': elements.Space(np.uint8, (64, 64, 3)), 'position': elements.Space(np.float32, (2,)), } decoder = Decoder(obs_space, depth=64, bspace=8) carry = decoder.initial(batch_size=16) carry, entries, recons = decoder(carry, features, reset, training=True) # recons['image'] and recons['position'] are prediction heads ``` -------------------------------- ### Driver.__init__ Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes the Driver to run agents in parallel environments. It takes a list of environment creation functions and an option to run them in parallel. ```APIDOC ## Driver.__init__ ### Description Sets up parallel environment runners. When parallel=True, uses multiprocessing with pipes for communication. ### Method __init__ ### Parameters #### Path Parameters - **make_env_fns** (list) - Required - List of callables that create environments. - **parallel** (bool) - Optional - Run environments in parallel processes. Default: True - **kwargs** (dict) - Optional - Additional kwargs passed to policy. ### Request Example ```python from embodied.core import Driver def make_env(): return MyEnv() driver = Driver([make_env for _ in range(16)], parallel=True) driver.reset(init_policy=agent.init_policy) ``` ``` -------------------------------- ### MLPHead for Single Output Predictions Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-jax.md A multi-layer perceptron head for making predictions for a given space. It supports various output distribution types and can be used to get predictions, means, and compute losses. ```python import elements from embodied.jax import MLPHead space = elements.Space(np.float32, (4,)) head = MLPHead(space, layers=3, units=256, output='bounded_normal') # In forward pass: x = head(features) # Returns distribution object mean = x.pred() # Get mean prediction loss = x.loss(target) # Compute loss vs target ``` -------------------------------- ### Command-Line Configuration Overrides Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/configuration.md Demonstrates how to override default configuration values using command-line arguments. This is useful for quick experimentation and deployment. ```bash python dreamerv3/main.py \ --logdir ~/logdir/dreamer/{timestamp} \ --configs crafter \ --batch_size 32 \ --jax.platform cuda ``` -------------------------------- ### Agent.__init__ Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/agent.md Initializes the agent with encoder, RSSM-based dynamics model, decoder, reward head, continuation head, policy head, and value head. Sets up optimization with learning rate scheduling and auxiliary normalizers. ```APIDOC ## Agent.__init__ ### Description Initializes the agent with encoder, RSSM-based dynamics model, decoder, reward head, continuation head, policy head, and value head. Sets up optimization with learning rate scheduling and auxiliary normalizers. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **obs_space** (dict) - Yes - Observation space dictionary mapping observation names to Space objects. Must contain `is_first`, `is_last`, `is_terminal`, and `reward` keys. - **act_space** (dict) - Yes - Action space dictionary mapping action names to Space objects. Must contain `reset` key. - **config** (elements.Config) - Yes - Configuration object with nested dicts: `enc`, `dyn`, `dec`, `rewhead`, `conhead`, `policy`, `value`, `slowvalue`, `retnorm`, `valnorm`, `advnorm`, `opt`, `loss_scales`, and behavior flags. ### Request Example ```python import embodied from dreamerv3.agent import Agent obs_space = { 'image': elements.Space(np.uint8, (64, 64, 3)), 'is_first': elements.Space(bool, (1,)), 'is_last': elements.Space(bool, (1,)), 'is_terminal': elements.Space(bool, (1,)), 'reward': elements.Space(np.float32, (1,)), } act_space = { 'action': elements.Space(np.float32, (6,)), 'reset': elements.Space(bool, (1,)), } config = elements.Config( enc={'typ': 'simple', 'simple': {...}}, dyn={'typ': 'rssm', 'rssm': {...}}, # ... other required config keys ) agent = Agent(obs_space, act_space, config) ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Agent.__init__ Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Base constructor for RL agents. Implementations should store obs_space, act_space, and config. ```APIDOC ## Agent.__init__ ### Description Base constructor. Implementations should store obs_space, act_space, and config. ### Method __init__ ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **obs_space** (dict) - Yes - Observation space mapping keys to Space objects. - **act_space** (dict) - Yes - Action space mapping keys to Space objects. - **config** (object) - Yes - Configuration object (any type). ### Request Example ```python def __init__(self, obs_space: dict, act_space: dict, config: object): pass ``` ### Response #### Success Response (200) - None #### Response Example - None ``` -------------------------------- ### Command-Line Interface Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Provides instructions and common flags for running the DreamerV3 main script. ```APIDOC ## Command-Line Interface ### Description Provides instructions and common flags for running the DreamerV3 main script. ### Basic Usage ```bash python dreamerv3/main.py \ --logdir ~/logdir/dreamer \ --configs crafter \ --task crafter_reward ``` ### Common Flags | Flag | |--------| | --logdir | | --configs | | --task | | --seed | | --batch_size | | --jax.platform | | --run.train_ratio | ### Flag Details | Flag | Type | Default | Example | |---|---|---|---| | --logdir | path | ~/logdir/{timestamp} | ~/logs/dreamer | | --configs | names | defaults | crafter size50m atari | | --task | str | dummy_disc | atari_pong | | --seed | int | 0 | 42 | | --batch_size | int | 16 | 32 | | --jax.platform | str | cuda | cpu | | --run.train_ratio | float | 32.0 | 64.0 | ### Configuration Override Syntax Use dot notation for nested values: ```bash # Example: --config.nested.value new_value ``` ``` -------------------------------- ### Train Agent with Batch Data Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This Python snippet shows how to initialize the agent for training and then perform a training step using batch data sampled from a replay buffer. It also returns training metrics. ```python # Training train_carry = agent.init_train(batch_size=16) batch_data = {...} # from replay train_carry, outputs, metrics = agent.train(train_carry, batch_data) ``` -------------------------------- ### Initialize and Run Agent Policy Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This Python snippet demonstrates how to initialize a DreamerV3 agent and run its policy for inference. It requires defining observation and action spaces, and a configuration object. ```python from dreamerv3.agent import Agent import elements # Initialize agent obs_space = {...} act_space = {...} config = elements.Config({...}) agent = Agent(obs_space, act_space, config) # Initialize state carry = agent.init_policy(batch_size=1) # Run policy obs = {...} carry, actions, outputs = agent.policy(carry, obs) ``` -------------------------------- ### Checkpoint Management: Save and Load Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Demonstrates how to save an agent's state to a file and load it back. Checkpoints are automatically saved at intervals defined by `run.save_every`. ```python # Save checkpoint = agent.save() logdir.save_file(checkpoint, 'ckpt.pkl') # Load checkpoint = logdir.load_file('ckpt.pkl') agent.load(checkpoint) ``` -------------------------------- ### Create Logger Instance Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Initializes a logger instance with specified output handlers and configuration. Use for tracking metrics during training. ```python def make_logger(config: elements.Config) -> elements.Logger: ``` ```python logger = make_logger(config) logger(key='train/loss', value=0.5) logger(key='episode/score', value=100.0) ``` -------------------------------- ### Basic DreamerV3 Training Script Execution Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Launches the main DreamerV3 training script with essential command-line arguments for logging directory and configuration. ```bash python dreamerv3/main.py \ --logdir ~/logdir/dreamer \ --configs crafter \ --task crafter_reward ``` -------------------------------- ### Create Replay Buffer Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Instantiates a configured Replay buffer instance. Use for training or evaluation data storage. ```python def make_replay(config: elements.Config, folder: str, mode: str = 'train') -> embodied.replay.Replay: ``` ```python replay = make_replay(config, 'replay', mode='train') ``` -------------------------------- ### Seed Handling in make_env Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Illustrates how seeds are derived from the configuration and environment index when 'use_seed' is enabled for an environment suite. ```python seed = hash((config.seed, index)) % (2 ** 32 - 1) ``` -------------------------------- ### make_replay Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Creates and configures a replay buffer instance for storing and sampling experience data. ```APIDOC ## make_replay ### Description Creates and configures a replay buffer instance for storing and sampling experience data. ### Method ```python def make_replay(config: elements.Config, folder: str, mode: str = 'train') -> embodied.replay.Replay ``` ### Parameters #### Path Parameters - **config** (elements.Config) - Required - Configuration. - **folder** (str) - Required - Subfolder name (e.g., 'replay', 'eval_replay'). - **mode** (str) - Optional - Mode: 'train' or 'eval'. Defaults to 'train'. ### Returns Configured Replay buffer instance. ### Configuration - `config.replay.size`: Capacity in steps - `config.replay.chunksize`: Chunk file size - `config.replay.online`: Enable online sampling - `config.replay.fracs`: Sampling strategy fractions - `config.batch_size`: Batch size - `config.batch_length`: Sequence length - `config.logdir`: Save directory ### Example ```python replay = make_replay(config, 'replay', mode='train') ``` ``` -------------------------------- ### Env.step Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Execute one environment step. Returns observations for the next state. ```APIDOC ## Env.step ### Description Execute one environment step. Returns observations for the next state. ### Method step ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **action** (dict) - Yes - Action dict with keys from act_space. ### Request Example ```python def step(self, action: dict) -> dict: pass ``` ### Response #### Success Response (200) - **observations** (dict) - Observation dict with all obs_space keys. #### Response Example - None ``` -------------------------------- ### Multi-Task Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Configure multi-task training by specifying multiple environment configurations and the number of environment instances to run. This allows the agent to learn shared representations across different tasks. ```bash python dreamerv3/main.py \ --configs crafter \ --task crafter_reward \ --run.envs 8 \ --run.steps 5e7 ``` -------------------------------- ### RandomAgent.__init__ Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes the RandomAgent with observation and action spaces, and an optional random seed. ```APIDOC ## RandomAgent.__init__ ### Description Initializes the RandomAgent. ### Method `__init__(self, obs_space: dict, act_space: dict, seed: int = 0)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **obs_space** (dict) - Required - Observation space (unused). - **act_space** (dict) - Required - Action space. - **seed** (int) - Optional - Default: 0 - Random seed. ### Returns None. ``` -------------------------------- ### Logdir Handling in make_env Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Shows how an environment-specific log directory is created when 'use_logdir' is enabled for an environment suite. ```python logdir = elements.Path(config.logdir) / f'env{index}' ``` -------------------------------- ### Implement Custom Environment with Env Interface Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/README.md This Python snippet demonstrates how to implement a custom environment by inheriting from the `Env` class and defining observation and action spaces, along with the `step` method. ```python from embodied.core import Env import elements class CustomEnv(Env): def __init__(self): self._state = None @property def obs_space(self): return { 'image': elements.Space(np.uint8, (64, 64, 3)), 'is_first': elements.Space(bool, () 'is_last': elements.Space(bool, () 'is_terminal': elements.Space(bool, () 'reward': elements.Space(np.float32, () } @property def act_space(self): return { 'action': elements.Space(int, (), 0, 18), 'reset': elements.Space(bool, () } def step(self, action): # Implement step logic return { 'image': image, 'is_first': False, 'is_last': done, 'is_terminal': not truncated, 'reward': reward, } ``` -------------------------------- ### Atari 100K Environment Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Configuration for Atari 100K benchmark, featuring smaller images and no sticky actions. Use this for sample efficiency benchmarks. ```yaml atari100k: size: [64, 64] # Smaller images for 100K benchmark repeat: 4 sticky: False # No sticky actions gray: False # RGB images actions: needed # Only valid actions noops: 30 resize: pillow clip_reward: False ``` -------------------------------- ### Driver Initialization for Data Collection Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Initializes a Driver for data collection, setting up parallel environments and defining a callback for processing each step. The driver is then used to run data collection for a specified number of steps. ```python from embodied.core import Driver def train(make_agent, make_replay, make_env, make_stream, make_logger, config): agent = make_agent() replay = make_replay() env_fns = [lambda: make_env(i) for i in range(config.run.envs)] driver = Driver(env_fns, parallel=True) def on_step(transition, index, **kwargs): replay.add(transition) driver.on_step(on_step) # Run data collection driver(agent.policy, steps=config.run.steps) ``` -------------------------------- ### Replay.add Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Adds a single transition step to the replay buffer. Supports multi-threaded inserts. ```APIDOC ## Replay.add ### Description Add a single step to the buffer. Automatically streams to chunks and inserts complete sequences. ### Method add ### Parameters #### Path Parameters - **step** (dict) - Required - Transition dict. Keys starting with `log/` are filtered. - **worker** (int) - Optional - Worker ID for multi-threaded inserts. Default: 0 ``` -------------------------------- ### Agent Initialization for Reporting Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes the agent's state for reporting. Returns the carry state for a reporting step. ```python def init_report(self, batch_size: int) -> object ``` -------------------------------- ### DreamerV3 Documentation File Structure Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/COMPLETION_REPORT.md Overview of the generated documentation files and their respective line counts and purposes. ```text /workspace/home/output/ ├── README.md (419 lines) - Overview & quick start ├── INDEX.md (334 lines) - File organization index ├── SUMMARY.txt (280 lines) - Generation summary ├── COMPLETION_REPORT.md (this file) - Completion details │ ├── configuration.md (487 lines) - Configuration reference ├── environments.md (629 lines) - Environment specifications ├── types.md (438 lines) - Data structure types │ └── api-reference/ (6 files) ├── agent.md (360 lines) - Agent & loss functions ├── rssm.md (390 lines) - World model components ├── embodied-core.md (667 lines) - Core framework abstractions ├── embodied-jax.md (460 lines) - JAX implementations ├── embodied-run.md (339 lines) - Training loops └── main-entry-point.md (376 lines) - CLI & factories ``` -------------------------------- ### Atari 100K Training Configuration Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/environments.md Training configuration for the Atari 100K benchmark, specifying steps, environments, and training ratio. Use this for sample efficiency benchmarks. ```yaml atari100k: run: {steps: 1.1e5, envs: 1, train_ratio: 256} ``` -------------------------------- ### Agent Initialization for Training Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes the agent's state for training. Must be implemented by subclasses. Returns the carry state for a training step. ```python def init_train(self, batch_size: int) -> object ``` -------------------------------- ### Common Training Configuration Parameters Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-run.md Defines common configuration parameters used across all training scripts, including steps, duration, logging intervals, environment counts, and server addresses. ```python config.run = { 'steps': int, # Total training steps 'duration': int, # Max duration in seconds 'train_ratio': float, # Training steps per env step 'log_every': int, # Log interval (seconds) 'report_every': int, # Eval interval (seconds) 'save_every': int, # Checkpoint interval (seconds) 'envs': int, # Training environment count 'eval_envs': int, # Eval environment count 'eval_eps': int, # Eval episodes per task 'report_batches': int, # Report batch count 'from_checkpoint': str, # Checkpoint to load 'episode_timeout': int, # Max episode length (sec) 'actor_addr': str, # Actor server address 'replay_addr': str, # Replay server address 'logger_addr': str, # Logger server address } ``` -------------------------------- ### Create Data Stream Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Wraps a Replay buffer instance to create a data stream for sampling batches. Use for training or reporting. ```python def make_stream(config: elements.Config, replay: embodied.replay.Replay, mode: str) -> embodied.core.Stream: ``` ```python stream = make_stream(config, replay, mode='train') batch = next(stream) # Get next batch ``` -------------------------------- ### Agent Initialization for Policy Execution Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/embodied-core.md Initializes the agent's state for policy execution. Returns the carry state for policy execution. ```python def init_policy(self, batch_size: int) -> object ``` -------------------------------- ### make_env Source: https://github.com/danijar/dreamerv3/blob/main/_autodocs/api-reference/main-entry-point.md Factory function to create an environment instance with appropriate wrappers. ```APIDOC ## make_env(config: elements.Config, index: int, **overrides) -> embodied.Env ### Description Creates an environment instance with specified wrappers, determined by the configuration. Handles environment selection, seeding, and log directory setup. ### Method `make_env` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **config** (elements.Config) - Required - Configuration object specifying the task and environment settings. - **index** (int) - Required - Environment instance index, used for seeding. - **overrides** (dict) - Optional - Additional keyword arguments to pass to the environment constructor. ### Request Example ```python from dreamerv3.main import make_env import elements config = elements.Config() config.task = 'atari_pong' config.seed = 0 config.logdir = '~/logs/dreamer' config.env = { 'atari': {'size': [96, 96], 'repeat': 4, 'sticky': True} } env = make_env(config, index=0) obs = env.step({'action': 0, 'reset': True}) ``` ### Response Environment instance with appropriate wrappers. #### Success Response (Environment Instance) - **Env** (embodied.Env) - An instance of the configured environment. ```