# zfyre/placerl architecture

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

- Interactive diagram: https://gitdiagram.com/zfyre/placerl
- Repository: https://github.com/zfyre/placerl

## Overview

##### Project type and purpose
- This repository implements a reinforcement learning (RL) based chip placement method (“PlaceRL”) that uses visual representation learning.
- The README explains how to run the placement via PPO2 (or alternative via DDPG) and provides benchmarks (adaptec1 and ariane) as well as performance comparisons with other methods.
- The project is organized as a full-stack research tool focusing on RL, simulation (environment), model training, and benchmark circuit evaluation.

##### File structure and map components
- Entry Points (Driver Scripts):
- “maskplace/PPO2.py”: Main script to start training or testing the RL agent.
- “maskplace/DDPG.py”: Alternative RL algorithm for chip placement.
(These form the “Controller/Entry” layer of the system.)

- RL Algorithm Core:
- “maskplace/PPO2.py” & “maskplace/DDPG.py” include the RL code, training loop, and hyper-parameter handling.
- “maskplace/nnet.py”: Contains neural network definitions potentially used by the RL agents.
- “maskplace/model/pretrained_model.pkl”: A serialized file holding pretrained model weights for inference or warm-start training.

- Simulation Environment:
- “maskplace/place_env/place_env.py”: Defines the custom gym-like environment used for chip placement simulation.
- “maskplace/place_env/__init__.py”: Initializes the environment module.

- Benchmark and Data Components:
- “maskplace/adaptec1/…” (includes adaptec1.aux, adaptec1.nets, adaptec1.nodes, adaptec1.pl, adaptec1.scl, adaptec1.wts): Provides benchmark circuit definitions and associated data files for the “adaptec1” benchmark.
- “maskplace/ariane/…” (includes __init__.py, graph.proto, laiyao.proto, laiyao_pb2.py, netlist.pb.txt, read_info.py): Offers benchmark files and protobuffer definitions for the “ariane” benchmark.

- Database / Data Processing Components:
- “maskplace/place_db.py” & “maskplace/place_db_proto.py”: Likely handle database interactions or protocol definitions that manage placement data.
- “maskplace/comp_res.py”: Possibly computes and compares placement results (e.g., cost metrics).

- Utility / Additional Modules:
- “maskplace/prim.py”: Could provide core placement primitives or helper methods.
- “_config.yml”: Configuration file for setting system parameters.

- Assets and Visual Representations:
- “README.md”: Provides high-level instructions and benchmark performance visualizations.

a. Main components and add explicit file names:
- Entry/Controller Component:
- Node: “PPO2 Controller” (Points to maskplace/PPO2.py)
- Node: “DDPG Controller” (Points to maskplace/DDPG.py)
- RL Core and Model:
- Node: “Neural Network Module” (Points to maskplace/nnet.py)
- Node: “Pretrained Model” (Points to maskplace/model/pretrained_model.pkl)
- Environment:
- Node: “Placement Environment” (Points to maskplace/place_env/place_env.py and maskplace/place_env/__init__.py)
- Benchmark Data:
- Node: “Adaptec1 Benchmark” (Points to files in maskplace/adaptec1: adaptec1.aux, adaptec1.nets, adaptec1.nodes, adaptec1.pl, adaptec1.scl, adaptec1.wts)
- Node: “Ariane Benchmark” (Points to files in maskplace/ariane: graph.proto, laiyao.proto, laiyao_pb2.py, netlist.pb.txt, read_info.py)
- Data Handling and Processing:
- Node: “Placement Database Manager” (Points to maskplace/place_db.py and maskplace/place_db_proto.py)
- Node: “Computation / Evaluation” (Points to maskplace/comp_res.py)
- Node: “Placement Primitives” (Points to maskplace/prim.py)
- Configuration and Assets:
- Node: “Configuration” (Points to _config.yml)
- Node: “Visual Assets” (Points to imgs/ directory with its subfolders)
- Node: “Documentation” (Points to README.md)

b. Relationships and Interactions:
- The controller scripts (PPO2.py or DDPG.py) initialize the RL process, access the neural network module (nnet.py) and load model weights (pretrained_model.pkl).
- The RL agent interacts with the simulation environment (place_env/place_env.py) to perform actions and receive state information.
- Depending on the benchmark being used, the agent loads data from the benchmark directories (adaptec1 or ariane). For instance, the “Ariane Benchmark” node may feed data into the environment.
- Placement data and results are passed to the database manager (place_db.py and place_db_proto.py) and evaluated via comp_res.py.
- Configuration parameters from _config.yml govern various aspects of the training and simulation.

c. Important Architectural Patterns:
- The design follows a modular pattern where the RL algorithm, environment, benchmark data, and helper modules are maintained in separate components.
- It leverages a separation of concerns: controllers handle the execution flow, the environment encapsulates simulation details, benchmark directories provide data, and additional modules handle evaluation and database operations.
- Use of protobuf files in the ariane module indicates a well-defined data interchange structure for benchmark definition.

d. Relevant Technologies:
- Python (entry-level code in PPO2.py, DDPG.py, etc.)
- PyTorch (for the deep learning components found in nnet.py)
- OpenAI Gym (for the custom environment in place_env)
- Protocol Buffers (in ariane directory: graph.proto, laiyao.proto, etc.)
- Supporting libraries: matplotlib, tqdm

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Entry/Controller Layer
    subgraph "Entry/Controller"
        ppo["PPO2 Controller (maskplace/PPO2.py)"]:::controller
        ddpg["DDPG Controller (maskplace/DDPG.py)"]:::controller
    end

    %% RL Core & Model
    subgraph "RL Core & Model"
        nn["Neural Network Module (maskplace/nnet.py)"]:::core
        pre["Pretrained Model (maskplace/model/pretrained_model.pkl)"]:::core
    end

    %% Simulation Environment
    subgraph "Simulation Environment"
        env["Placement Environment (maskplace/place_env/place_env.py & __init__.py)"]:::env
    end

    %% Benchmark & Data Components
    subgraph "Benchmark & Data"
        adaptec["Adaptec1 Benchmark (maskplace/adaptec1/...)"]:::benchmark
        ariane["Ariane Benchmark (maskplace/ariane/...)"]:::benchmark
    end

    %% Database & Data Processing
    subgraph "Database & Processing"
        db["Placement Database Manager (maskplace/place_db.py & place_db_proto.py)"]:::db
        comp["Computation/Evaluation (maskplace/comp_res.py)"]:::db
    end

    %% Utility Modules
    subgraph "Utility Modules"
        prim["Placement Primitives (maskplace/prim.py)"]:::utils
    end

    %% Configuration and Assets
    config["Configuration (_config.yml)"]:::config
    assets["Visual Assets (imgs/...)"]:::assets
    doc["Documentation (README.md)"]:::doc

    %% Relationships

    %% Controllers to RL Core
    ppo -->|"calls"| nn
    ppo -->|"loads"| pre
    ddpg -->|"calls"| nn
    ddpg -->|"loads"| pre

    %% Controllers to Environment
    ppo -->|"interacts"| env
    ddpg -->|"interacts"| env

    %% Environment to Benchmarks
    env -->|"configuredWith"| adaptec
    env -->|"configuredWith"| ariane

    %% Environment to Database and Evaluation
    env -->|"sendsResults"| db
    db -->|"evaluates"| comp

    %% Configuration feeds into Controllers, Environment and DB Manager
    config -->|"configures"| ppo
    config -->|"configures"| ddpg
    config -->|"configures"| env
    config -->|"configures"| db

    %% Neural Network Module feedback to Controller
    nn -->|"triggersUpdate"| ppo

    %% Utility usage in Environment
    prim -->|"supports"| env

    %% Click Events
    click ppo "https://github.com/zfyre/placerl/blob/main/maskplace/PPO2.py"
    click ddpg "https://github.com/zfyre/placerl/blob/main/maskplace/DDPG.py"
    click nn "https://github.com/zfyre/placerl/blob/main/maskplace/nnet.py"
    click pre "https://github.com/zfyre/placerl/blob/main/maskplace/model/pretrained_model.pkl"
    click env "https://github.com/zfyre/placerl/blob/main/maskplace/place_env/place_env.py"
    click adaptec "https://github.com/zfyre/placerl/tree/main/maskplace/adaptec1/"
    click ariane "https://github.com/zfyre/placerl/tree/main/maskplace/ariane/"
    click db "https://github.com/zfyre/placerl/blob/main/maskplace/place_db.py"
    click comp "https://github.com/zfyre/placerl/blob/main/maskplace/comp_res.py"
    click prim "https://github.com/zfyre/placerl/blob/main/maskplace/prim.py"
    click config "https://github.com/zfyre/placerl/blob/main/_config.yml"
    click assets "https://github.com/zfyre/placerl/tree/main/imgs/"
    click doc "https://github.com/zfyre/placerl/blob/main/README.md"

    %% Styles
    classDef controller fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef core fill:#B8F2B8,stroke:#333,stroke-width:2px;
    classDef env fill:#FFF4B7,stroke:#333,stroke-width:2px;
    classDef benchmark fill:#FFC2B3,stroke:#333,stroke-width:2px;
    classDef db fill:#D1C4E9,stroke:#333,stroke-width:2px;
    classDef utils fill:#F8BBD0,stroke:#333,stroke-width:2px;
    classDef config fill:#E0E0E0,stroke:#333,stroke-width:2px;
    classDef assets fill:#FFE082,stroke:#333,stroke-width:2px;
    classDef doc fill:#C8E6C9,stroke:#333,stroke-width:2px;
```
