# stepfun-ai/step1x-edit architecture

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

- Interactive diagram: https://gitdiagram.com/stepfun-ai/step1x-edit
- Repository: https://github.com/stepfun-ai/step1x-edit

## Overview

##### Project Type and Purpose
- This is an open-source, full-stack image-editing framework (Step1X-Edit). It provides:
- A multimodal LLM + diffusion decoder model for guided image editing
- Training and fine-tuning pipelines (including LoRA support)
- Inference scripts and a Gradio demo
- An evaluation benchmark (GEdit-Bench)

##### Main Components
A. Model Definition
- `modules/`
- `model_edit.py`: orchestrates the multimodal encoder (e.g., Gemini2, Qwen2.5) with a diffusion decoder
- `attention.py`, `autoencoder.py`, `conditioner.py`, etc.: building blocks of the diffusion network and embedding fusion
B. Core Utilities & Infrastructure
- `library/`
- Configuration & device management (`config_util.py`, `device_utils.py`)
- HF & Diffusers integration (`huggingface_util.py`, `diffusers` helpers)
- Training strategies and custom functions (Deepspeed via `deepspeed_utils.py`, IPEX overrides)
- Data-loading and LoRA support (`data_configs/`, `lora_module.py`, `step1x_edit_train_utils.py`)
C. Entry-Point Scripts
- Training: `finetuning.py`, `scripts/finetuning.sh`
- Inference: `inference.py`, `sampling.py`, `scripts/run_examples*.sh`
- Gradio UI: `gradio_app.py`
D. Evaluation Benchmark (GEdit-Bench)
- `GEdit-Bench/`: scripts to generate prompts, run model on benchmark, compute metrics (`calculate_statistics.py`, `test_gedit_score.py`)
E. Assets & Examples
- `assets/`: logos, demo GIFs, tech report PDF
- `examples/`: sample JSON prompts for CN/EN instructions

##### Relationships & Data Flow
1) Training Pipeline
- Input: JSON metadata → `library/data_configs` loader → `finetuning.py` uses `modules/` model
- Optimizer strategies in `library/` (Deepspeed, IPEX) → checkpoint outputs (LoRA or full weights)
2) Inference Pipeline
- Input: source image + instruction JSON → `inference.py` (calls `modules.model_edit`, `library/huggingface_util`)
- Optional flags: `--quantized`, `--offload` trigger accelerate/offload utilities
- Output: edited images in `output_dir`
3) Gradio Demo
- `gradio_app.py` wraps inference logic with a web UI on `localhost:32800`
4) Benchmark Evaluation
- `GEdit-Bench/generate_image_example.py` runs inference on benchmark prompts
- `calculate_statistics.py` & `test_gedit_score.py` produce evaluation metrics

##### Architectural Patterns & Design Principles
- Modular design: clear separation between model components (`modules/`) and training/inference utilities (`library/`).
- Plug-and-play: support for multiple LLM backends (OpenAI, Gemini, Qwen) via adapter pattern in `mllm_tools/`.
- Hardware optimization: optional integration with Flash-Attention, Deepspeed, IPEX, CPU offload.
- Extensibility: LoRA fine-tuning mechanism abstracted in `lora_module.py`.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Training Pipeline
    subgraph "Training Pipeline"
        direction TB
        Data["JSON Metadata"]:::gray
        DataConfig["library/data_configs"]:::green
        TrainScript["finetuning.py"]:::orange
        ModelEdit["modules/model_edit.py"]:::blue
        Attention["modules/attention.py"]:::blue
        Autoencoder["modules/autoencoder.py"]:::blue
        Conditioner["modules/conditioner.py"]:::blue
        Layers["modules/layers.py"]:::blue
        Connector["modules/connector_edit.py"]:::blue
        DeepspeedUtil["library/deepspeed_utils.py"]:::green
        CustomOffload["library/custom_offloading_utils.py"]:::green
        IPEXDir["library/ipex/"]:::green
        Checkpoints["Checkpoints (LoRA/Full)"]:::gray

        Data -->|load| DataConfig
        DataConfig -->|feeds| TrainScript
        TrainScript -->|uses| ModelEdit
        ModelEdit --> Attention
        ModelEdit --> Autoencoder
        ModelEdit --> Conditioner
        ModelEdit --> Layers
        ModelEdit --> Connector
        TrainScript -->|optimizes| DeepspeedUtil
        DeepspeedUtil --> CustomOffload
        DeepspeedUtil --> IPEXDir
        TrainScript -->|outputs| Checkpoints
    end

    %% Inference Pipeline
    subgraph "Inference Pipeline"
        direction TB
        Img["Source Image"]:::gray
        Instr["Instruction JSON"]:::gray
        Inference["inference.py"]:::orange
        HFUtil["library/huggingface_util.py"]:::green
        OffloadUtil["library/custom_offloading_utils.py"]:::green
        Edited["Edited Images"]:::gray

        Img -->|input| Inference
        Instr -->|input| Inference
        Inference -->|calls| ModelEdit
        Inference -->|calls| HFUtil
        Inference -->|flags --quantized/offload| OffloadUtil
        Inference -->|output| Edited
    end

    %% Evaluation Pipeline
    subgraph "Evaluation Pipeline"
        direction TB
        BenchPrompts["GEdit-Bench/generate_image_example.py"]:::yellow
        Stats["GEdit-Bench/calculate_statistics.py"]:::yellow
        Score["GEdit-Bench/test_gedit_score.py"]:::yellow

        BenchPrompts -->|runs inference| Inference
        BenchPrompts --> Stats
        Stats --> Score
    end

    %% Gradio UI
    subgraph "Gradio UI"
        direction TB
        UI["gradio_app.py"]:::orange
        Browser["Gradio Web UI"]:::purple

        UI -->|wraps| Inference
        UI -->|serves| Browser
    end

    %% Assets & Examples
    subgraph "Assets & Examples"
        direction TB
        Assets["assets/"]:::gray
        Examples["examples/"]:::gray
    end

    %% Click Events
    click ModelEdit "https://github.com/stepfun-ai/step1x-edit/blob/main/modules/model_edit.py"
    click Attention "https://github.com/stepfun-ai/step1x-edit/blob/main/modules/attention.py"
    click Autoencoder "https://github.com/stepfun-ai/step1x-edit/blob/main/modules/autoencoder.py"
    click Conditioner "https://github.com/stepfun-ai/step1x-edit/blob/main/modules/conditioner.py"
    click Layers "https://github.com/stepfun-ai/step1x-edit/blob/main/modules/layers.py"
    click Connector "https://github.com/stepfun-ai/step1x-edit/blob/main/modules/connector_edit.py"
    click DataConfig "https://github.com/stepfun-ai/step1x-edit/blob/main/library/data_configs/step1x_edit.toml"
    click DeepspeedUtil "https://github.com/stepfun-ai/step1x-edit/blob/main/library/deepspeed_utils.py"
    click CustomOffload "https://github.com/stepfun-ai/step1x-edit/blob/main/library/custom_offloading_utils.py"
    click IPEXDir "https://github.com/stepfun-ai/step1x-edit/tree/main/library/ipex/"
    click HFUtil "https://github.com/stepfun-ai/step1x-edit/blob/main/library/huggingface_util.py"
    click UI "https://github.com/stepfun-ai/step1x-edit/blob/main/gradio_app.py"
    click TrainScript "https://github.com/stepfun-ai/step1x-edit/blob/main/finetuning.py"
    click BenchPrompts "https://github.com/stepfun-ai/step1x-edit/blob/main/GEdit-Bench/generate_image_example.py"
    click Stats "https://github.com/stepfun-ai/step1x-edit/blob/main/GEdit-Bench/calculate_statistics.py"
    click Score "https://github.com/stepfun-ai/step1x-edit/blob/main/GEdit-Bench/test_gedit_score.py"

    %% Styles
    classDef blue fill:#cce5ff,stroke:#0366d6,color:#0366d6;
    classDef green fill:#d4edda,stroke:#155724,color:#155724;
    classDef orange fill:#ffe5b4,stroke:#d39e00,color:#d39e00;
    classDef yellow fill:#fff3cd,stroke:#856404,color:#856404;
    classDef gray fill:#f8f9fa,stroke:#6c757d,color:#6c757d;
    classDef purple fill:#e2e3e5,stroke:#383d41,color:#383d41;
```
