# zhanglixuan0720/prelar architecture

> Architecture diagram of the GitHub repository zhanglixuan0720/prelar, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zhanglixuan0720/prelar
- Repository: https://github.com/zhanglixuan0720/prelar

## Overview

##### Project Overview
- The project is a research-oriented implementation in PyTorch focused on world model pre-training. It is designed for learning action representations (PreLAR) using both pretraining (action-free videos) and fine-tuning (using real actions), with baselines provided for DreamerV2 and APV.
- The system handles both data processing (for datasets like Something-Something-V2) and training pipelines for variants of model-based RL in visual control tasks.

##### Main Components to Illustrate

a. Data Preparation and Management
- “data” directory: This houses dataset preparation for Something-Something-V2. Show sub-processes like:
- Frame extraction (extract_frames.py)
- Data processing (process_somethingv2.py)
- Generated lists (train/val video folder text files) as data inputs.

b. Configuration Management
- The “configs” directory holds various YAML configuration files (e.g., apv_finetuning.yaml, prelar_pretraining.yaml, etc.) that define essential hyperparameters and settings for different training regimes.

c. Core Library (wmlib)
- This is the backbone of the project and is divided into several modules:
- Agents (wmlib/agents): Implements different training agents & their variants (standard methods, APV, DreamerV2, PreLAR versions, etc.). It is the central point where model-specific training logic is housed.
- Core (wmlib/core): Implements the training loop driver, replay buffers, distribution utilities, and additional supporting functionalities.
- Networks (wmlib/nets): Contains model building blocks classified into submodules:
- Encoders: (wmlib/nets/encoder) for creating visual representations.
- Decoders: (wmlib/nets/decoder) for reconstruction aspects of the visual world model.
- Dynamics: (wmlib/nets/dynamics) which manage the latent dynamics (using approaches like RSSM or variational RSSM).
- VA Net: Specialized network for action encoding.
- Datasets (wmlib/datasets): Utilities for handling video datasets (including Something-Something-V2) and possibly video-specific processing.
- Environment Interfaces (wmlib/envs): Wrap around external simulated environments (Meta-world and Robodesk) with additional wrappers and utility functions.
- Training Helpers (wmlib/train): Contains the training trainers, pretrainer, and finetuner submodules.

d. Example Scripts
- The “examples” directory contains scripts that act as entry points to launch training for the various modes:
- Pretraining (e.g., train_prelar_pretraining.py, train_apv_pretraining.py)
- Finetuning (e.g., train_prelar_finetuning.py, train_apv_finetuning.py)
- Baseline methods (DreamerV2, APV, and contextualized versions)
- These coordinate the loading of configurations, data, and call upon the agents in wmlib to start the training process.

e. External Dependencies and Environment
- External libraries and tools (like PyTorch, MuJoCo [via Meta-world], and other referenced repositories).
- Mention that the Conda environment is built using environment.yaml, ensuring dependencies are consistent across runs.

##### Relationships and Data Flows
- How configuration files feed into the training scripts, which then initialize the training agents in wmlib.
- Integration between the core modules:
- Data → Datasets Module → Agents/Training Loop (Driver, Replay Buffer)
- Networks (Encoders, Decoders, Dynamics, VA Net) integrate with agents for forward passes and learning.
- Environment Modules (envs) provide simulated scenarios (for Meta-world and Robodesk) that the agents interact with.
- That example scripts act as a wrapper that links configuration, data, and the underlying model/training pipelines in wmlib.

##### Important Architectural Patterns and Design Principles
- Modular design splitting data handling, model architecture, training logic, and configuration management.
- Separation of concerns is emphasized with clear distinctions between:
- Data preprocessing (in the data/ folder)
- Model architecture (in wmlib/nets)
- Training logic and orchestration (in wmlib/core and wmlib/train)
- Agent behavior and different training schemes (in wmlib/agents)
- Extensibility: The system is designed to support multiple variants (PreLAR with/without contrastive loss, APV, DreamerV2) and can be extended by switching configuration and agent modules.

- How example scripts (from the “examples” directory) initiate experiments by selecting particular configurations.
- External services like MuJoCo installation for Meta-world and note dependencies referenced in the README.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Data Ingestion & Preprocessing"
        DP["Data Preprocessing (extract_frames.py, process_somethingv2.py)"]:::data
    end

    subgraph "Configuration Management"
        CM["Configurations (YAML Files)"]:::config
    end

    subgraph "Core Library (wmlib)"
        subgraph "Agents"
            A["Agents (PreLAR, APV, DreamerV2, etc.)"]:::agent
        end
        subgraph "Training Core"
            TC["Training Core (Driver, Replay, Dists)"]:::core
        end
        subgraph "Networks"
            subgraph "Encoders"
                EN["Encoders"]:::net
            end
            subgraph "Decoders"
                DC["Decoders"]:::net
            end
            subgraph "Dynamics"
                DY["Dynamics (RSSM, VA-RSSM)"]:::net
            end
            subgraph "VA Net"
                VA["VA Net (Action Encoding)"]:::net
            end
        end
        subgraph "Datasets"
            DS["Datasets (Video processing)"]:::data
        end
        subgraph "Environment Interfaces"
            ENV["Environment Interfaces (Meta-world, Robodesk)"]:::env
        end
        subgraph "Training Helpers"
            TH["Training Helpers (Pretrainer, Finetuner)"]:::core
        end
    end

    subgraph "Example Scripts"
        EX["Example Scripts (Training entry points)"]:::examples
    end

    subgraph "External Dependencies"
        ED["Conda Environment (environment.yaml)"]:::external
    end

    %% Connections
    DP -->|"processes raw data"| DS
    CM -->|"provides settings"| EX
    CM -->|"feeds configuration"| A
    EX -->|"initiates training"| A
    A -->|"controls training loop"| TC
    TC -->|"uses datasets"| DS
    A -->|"integrates"| EN
    A -->|"integrates"| DC
    A -->|"integrates"| DY
    A -->|"integrates"| VA
    ENV -->|"supplies simulated environment"| A
    A -->|"utilizes helpers"| TH
    ED -->|"dependency for"| A
    ED -->|"dependency for"| TC
    ED -->|"dependency for"| DS

    %% Click Events
    click DP "https://github.com/zhanglixuan0720/prelar/tree/main/data/somethingv2"
    click CM "https://github.com/zhanglixuan0720/prelar/tree/main/configs"
    click A "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/agents"
    click TC "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/core"
    click EN "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/nets/encoder"
    click DC "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/nets/decoder"
    click DY "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/nets/dynamics"
    click VA "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/nets/va_net"
    click DS "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/datasets"
    click ENV "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/envs"
    click TH "https://github.com/zhanglixuan0720/prelar/tree/main/wmlib/train"
    click EX "https://github.com/zhanglixuan0720/prelar/tree/main/examples"
    click ED "https://github.com/zhanglixuan0720/prelar/blob/main/environment.yaml"

    %% Styles
    classDef data fill:#FFDDC1,stroke:#333,stroke-width:2px;
    classDef config fill:#C1FFC1,stroke:#333,stroke-width:2px;
    classDef agent fill:#C1DFFB,stroke:#333,stroke-width:2px;
    classDef core fill:#FFE4E1,stroke:#333,stroke-width:2px;
    classDef net fill:#D1E8FF,stroke:#333,stroke-width:2px;
    classDef env fill:#FFFFB3,stroke:#333,stroke-width:2px;
    classDef examples fill:#FFD1DC,stroke:#333,stroke-width:2px;
    classDef external fill:#E6E6FA,stroke:#333,stroke-width:2px;
```
