# xpixelgroup/hypir architecture

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

- Interactive diagram: https://gitdiagram.com/xpixelgroup/hypir
- Repository: https://github.com/xpixelgroup/hypir

## Overview

1. HYPIR is an image-restoration repo centered on a Stable Diffusion 2.1-based restoration model. The core idea is to apply LoRA weights on top of a pretrained diffusion backbone rather than training a full model from scratch.

2. The repo has three main execution paths:
   - interactive inference via `app.py`
   - batch/offline inference via `test.py` and `predict.py`
   - training via `train.py`

3. Configuration is file-driven. The main runtime behavior is controlled by YAML files in `configs/`, especially `sd2_gradio.yaml` for the demo and `sd2_train.yaml` for training. This makes the architecture mostly “entrypoint + config + modular subsystems.”

4. The model subsystem is split into:
   - `HYPIR/model/backbone.py`: diffusion backbone integration
   - `HYPIR/model/D.py`: an auxiliary discriminator-like component or support module
   - `HYPIR/enhancer/`: higher-level restoration wrappers, including the SD2-specific implementation in `sd2.py`
   The enhancer layer is the user-facing abstraction over the underlying model pieces.

5. Training logic lives in `HYPIR/trainer/`. `base.py` likely defines shared training orchestration, while `sd2.py` specializes it for the Stable Diffusion 2 setup. The training loop is designed around LoRA fine-tuning and is launched with `accelerate`, indicating distributed/multi-GPU support.

6. Data loading and corruption synthesis are in `HYPIR/dataset/`. Key parts are:
   - `realesrgan.py`: dataset class used for restoration training, matching the README’s parquet-based image/prompt metadata flow
   - `batch_transform.py`, `degradation.py`, `diffjpeg.py`: synthetic low-quality generation and degradation pipelines
   - `file_backend.py`: file access abstraction
   This is the main data boundary: clean image/prompt records are transformed into paired low-quality/restoration training inputs.

7. The training data flow is: parquet metadata listing image paths and prompts -> dataset loader -> degradation pipeline -> model/trainer. The README’s example shows prompts can be empty, so text conditioning is optional in the data layer but part of the model interface.

8. The inference flow is: low-quality input image + optional prompt -> enhancer/model -> restored output. In the demo path, prompts can be user-supplied or generated automatically through `HYPIR/utils/captioner.py`, which can call an external GPT API using environment variables. This makes text conditioning a pluggable side path rather than a hard dependency.

9. Memory/performance support is handled by utility code, especially `HYPIR/utils/tiled_vae/`. That subsystem suggests tiled VAE encode/decode hooks to run large-resolution inference on limited VRAM. `ema.py`, `common.py`, and `tabulate.py` provide standard training/runtime helpers.

10. Deployment and packaging are first-class concerns. Besides the local Gradio demo, there are repo-specific deployment files for OpenXLab and Cog (`app_openxlab.py`, `cog.yaml`). The README also points to hosted model weights on HuggingFace/OpenXLab, so the runtime assumes external pretrained LoRA weights are downloaded separately.

11. The project’s main external dependencies that affect architecture are:
   - Stable Diffusion 2.1 as the base generative backbone
   - HuggingFace/OpenXLab for model weight distribution
   - Gradio for interactive UI
   - OpenAI-compatible chat APIs for optional caption generation
   - Accelerate for training orchestration

12. From an architecture diagram perspective, the important boundaries are:
   - user interfaces / deployment wrappers
   - config-driven orchestration
   - dataset/degradation pipeline
   - diffusion backbone + LoRA enhancement layer
   - auxiliary utilities for captions, EMA, and tiled VAE memory management
   The repo is organized more like a model-serving and model-training system than a general application stack.

Main entry points: [`HYPIR/enhancer/sd2.py`](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/enhancer/sd2.py) (SD2 enhancer), [`HYPIR/trainer/sd2.py`](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/trainer/sd2.py) (SD2 trainer), [`app.py`](https://github.com/xpixelgroup/hypir/blob/main/app.py) (App), [`HYPIR/dataset/realesrgan.py`](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/realesrgan.py) (Restoration dataset)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_app_py["App<br/>gradio demo<br/>[app.py]"]
  node_app_openxlab_py["OpenXLab app<br/>deployment app<br/>[app_openxlab.py]"]
  node_test_py["Test<br/>batch inference<br/>[test.py]"]
  node_predict_py["Predict<br/>single-image infer<br/>[predict.py]"]
  node_train_py["Train<br/>accelerate launcher<br/>[train.py]"]
end

subgraph group_config["Configuration"]
  node_sd2_gradio_yaml["Demo config<br/>yaml config<br/>[sd2_gradio.yaml]"]
  node_sd2_train_yaml["Train config<br/>yaml config<br/>[sd2_train.yaml]"]
end

subgraph group_core["Core model"]
  node_enhancer_base["Enhancer base<br/>restoration API<br/>[base.py]"]
  node_enhancer_sd2["SD2 enhancer<br/>restoration wrapper<br/>[sd2.py]"]
  node_backbone["Diffusion backbone<br/>sd2 backbone<br/>[backbone.py]"]
  node_model_d["Aux module<br/>support model<br/>[D.py]"]
end

subgraph group_data["Data pipeline"]
  node_dataset_realesrgan["Restoration dataset<br/>paired dataset<br/>[realesrgan.py]"]
  node_dataset_backend["File backend<br/>storage adapter<br/>[file_backend.py]"]
  node_dataset_transform["Degradation pipe<br/>synthetic lq pipeline<br/>[batch_transform.py]"]
  node_dataset_degradation["Degradations<br/>corruption utils<br/>[degradation.py]"]
end

subgraph group_train["Training"]
  node_trainer_base["Trainer base<br/>training orchestration<br/>[base.py]"]
  node_trainer_sd2["SD2 trainer<br/>lora trainer<br/>[sd2.py]"]
end

subgraph group_utils["Utilities"]
  node_captioner["Captioner<br/>optional text source<br/>[captioner.py]"]
  node_tiled_vae["Tiled VAE<br/>memory helper<br/>[__init__.py]"]
  node_ema_common["Training helpers<br/>shared utils<br/>[ema.py]"]
end

node_app_py -->|"loads config"| node_sd2_gradio_yaml
node_app_py -->|"serves"| node_enhancer_sd2
node_app_openxlab_py -->|"serves"| node_enhancer_sd2
node_test_py -->|"runs inference"| node_enhancer_sd2
node_predict_py -->|"runs inference"| node_enhancer_sd2
node_train_py -->|"loads config"| node_sd2_train_yaml
node_train_py -->|"starts"| node_trainer_sd2
node_enhancer_sd2 -->|"extends"| node_enhancer_base
node_enhancer_sd2 -->|"uses"| node_backbone
node_enhancer_sd2 -->|"uses"| node_model_d
node_trainer_sd2 -->|"extends"| node_trainer_base
node_trainer_sd2 -->|"optimizes"| node_enhancer_sd2
node_trainer_sd2 -->|"consumes"| node_dataset_realesrgan
node_dataset_realesrgan -->|"reads via"| node_dataset_backend
node_dataset_realesrgan -->|"applies"| node_dataset_transform
node_dataset_transform -->|"uses"| node_dataset_degradation
node_app_py -.->|"optional prompts"| node_captioner
node_enhancer_sd2 -.->|"accepts prompts"| node_captioner
node_enhancer_sd2 -.->|"memory optimize"| node_tiled_vae
node_trainer_sd2 -->|"uses helpers"| node_ema_common

click node_app_py "https://github.com/xpixelgroup/hypir/blob/main/app.py"
click node_app_openxlab_py "https://github.com/xpixelgroup/hypir/blob/main/app_openxlab.py"
click node_test_py "https://github.com/xpixelgroup/hypir/blob/main/test.py"
click node_predict_py "https://github.com/xpixelgroup/hypir/blob/main/predict.py"
click node_train_py "https://github.com/xpixelgroup/hypir/blob/main/train.py"
click node_sd2_gradio_yaml "https://github.com/xpixelgroup/hypir/blob/main/configs/sd2_gradio.yaml"
click node_sd2_train_yaml "https://github.com/xpixelgroup/hypir/blob/main/configs/sd2_train.yaml"
click node_enhancer_base "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/enhancer/base.py"
click node_enhancer_sd2 "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/enhancer/sd2.py"
click node_backbone "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/model/backbone.py"
click node_model_d "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/model/D.py"
click node_dataset_realesrgan "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/realesrgan.py"
click node_dataset_backend "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/file_backend.py"
click node_dataset_transform "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/batch_transform.py"
click node_dataset_degradation "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/degradation.py"
click node_trainer_base "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/trainer/base.py"
click node_trainer_sd2 "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/trainer/sd2.py"
click node_captioner "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/captioner.py"
click node_tiled_vae "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/tiled_vae/__init__.py"
click node_ema_common "https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/ema.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_py,node_app_openxlab_py,node_test_py,node_predict_py,node_train_py toneBlue
class node_sd2_gradio_yaml,node_sd2_train_yaml toneAmber
class node_enhancer_base,node_enhancer_sd2,node_backbone,node_model_d toneMint
class node_dataset_realesrgan,node_dataset_backend,node_dataset_transform,node_dataset_degradation toneRose
class node_trainer_base,node_trainer_sd2 toneIndigo
class node_captioner,node_tiled_vae,node_ema_common toneTeal
```

## Components

### Entry points

Interactive, batch, and training launchers plus deployment wrappers.

- [App](https://github.com/xpixelgroup/hypir/blob/main/app.py): gradio demo · Interactive restoration UI entrypoint. · `app.py`
- [OpenXLab app](https://github.com/xpixelgroup/hypir/blob/main/app_openxlab.py): deployment app · Deployment wrapper for hosted OpenXLab execution. · `app_openxlab.py`
- [Test](https://github.com/xpixelgroup/hypir/blob/main/test.py): batch inference · Offline evaluation/inference entrypoint. · `test.py`
- [Predict](https://github.com/xpixelgroup/hypir/blob/main/predict.py): single-image infer · Batch or scripted prediction entrypoint. · `predict.py`
- [Train](https://github.com/xpixelgroup/hypir/blob/main/train.py): accelerate launcher · Training entrypoint for LoRA fine-tuning. · `train.py`

### Configuration

YAML-driven runtime settings for demo and training.

- [Demo config](https://github.com/xpixelgroup/hypir/blob/main/configs/sd2_gradio.yaml): yaml config · Runtime settings for the interactive SD2 demo. · `configs/sd2_gradio.yaml`
- [Train config](https://github.com/xpixelgroup/hypir/blob/main/configs/sd2_train.yaml): yaml config · Runtime settings for SD2 training. · `configs/sd2_train.yaml`

### Core model

Enhancement, backbone, and auxiliary model pieces.

- [Enhancer base](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/enhancer/base.py): restoration API · Shared abstraction for restoration interfaces and orchestration. · `HYPIR/enhancer/base.py`
- [SD2 enhancer](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/enhancer/sd2.py): restoration wrapper · Stable Diffusion 2-specific enhancement layer exposed to callers. · `HYPIR/enhancer/sd2.py`
- [Diffusion backbone](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/model/backbone.py): sd2 backbone · Integration with the pretrained Stable Diffusion 2.1 base model. · `HYPIR/model/backbone.py`
- [Aux module](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/model/D.py): support model · Auxiliary discriminator-like or support component used by the model stack. · `HYPIR/model/D.py`

### Data pipeline

Dataset loading, file access, and synthetic degradation.

- [Restoration dataset](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/realesrgan.py): paired dataset · Parquet/image-prompt dataset loader for restoration training. · `HYPIR/dataset/realesrgan.py`
- [File backend](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/file_backend.py): storage adapter · Abstracts file access for dataset loading. · `HYPIR/dataset/file_backend.py`
- [Degradation pipe](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/dataset/batch_transform.py): synthetic lq pipeline · Batch transforms and synthetic low-quality generation. · `HYPIR/dataset/batch_transform.py`
- [Degradations](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/degradation.py): corruption utils · Image degradation operators used to form training pairs. · `HYPIR/utils/degradation.py`

### Training

Orchestration for LoRA fine-tuning with Accelerate.

- [Trainer base](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/trainer/base.py): training orchestration · Shared training loop, scheduling, and state management. · `HYPIR/trainer/base.py`
- [SD2 trainer](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/trainer/sd2.py): lora trainer · Stable Diffusion 2-specific training specialization. · `HYPIR/trainer/sd2.py`

### Utilities

Shared helpers for captions, EMA, and tiled VAE memory handling.

- [Captioner](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/captioner.py): optional text source · Generates prompts via external chat APIs when needed. · `HYPIR/utils/captioner.py`
- [Tiled VAE](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/tiled_vae/__init__.py): memory helper · Tiled encode/decode hooks for large-resolution inference. · `HYPIR/utils/tiled_vae/__init__.py`
- [Training helpers](https://github.com/xpixelgroup/hypir/blob/main/HYPIR/utils/ema.py): shared utils · EMA, runtime helpers, and formatting utilities. · `HYPIR/utils/ema.py`

## Connections

- App → Demo config: loads config · Interactive demo behavior is driven by YAML settings.
- App → SD2 enhancer: serves · The Gradio app invokes the SD2 restoration wrapper.
- OpenXLab app → SD2 enhancer: serves · Hosted deployment uses the same enhancement stack.
- Test → SD2 enhancer: runs inference · Offline testing uses the restoration model.
- Predict → SD2 enhancer: runs inference · Prediction entrypoint calls the enhancement layer.
- Train → Train config: loads config · Training runtime is configured from YAML.
- Train → SD2 trainer: starts · Training entrypoint dispatches to the SD2 trainer.
- SD2 enhancer → Enhancer base: extends · SD2-specific restoration logic builds on the shared enhancer API.
- SD2 enhancer → Diffusion backbone: uses · The enhancer wraps the pretrained diffusion backbone.
- SD2 enhancer → Aux module: uses · The enhancer depends on the auxiliary model component.
- SD2 trainer → Trainer base: extends · SD2 training specializes the shared trainer orchestration.
- SD2 trainer → SD2 enhancer: optimizes · Trainer updates the SD2 restoration model and LoRA weights.
- SD2 trainer → Restoration dataset: consumes · Training reads paired restoration samples from the dataset layer.
- Restoration dataset → File backend: reads via · Dataset loading is abstracted behind a file backend.
- Restoration dataset → Degradation pipe: applies · Samples pass through synthetic degradation transforms.
- Degradation pipe → Degradations: uses · Transform pipeline relies on corruption operators.
- App → Captioner: optional prompts · Demo can auto-generate text prompts when users do not provide them.
- SD2 enhancer → Captioner: accepts prompts · Text conditioning is optional but supported by the model interface.
- SD2 enhancer → Tiled VAE: memory optimize · Large-image inference can use tiled VAE hooks.
- SD2 trainer → Training helpers: uses helpers · Training relies on shared utilities such as EMA and common helpers.
