# xugao97/attentiondistillation architecture

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

- Interactive diagram: https://gitdiagram.com/xugao97/attentiondistillation
- Repository: https://github.com/xugao97/attentiondistillation

## Overview

#### Purpose and Entry Points
This repository implements Attention Distillation for transferring visual characteristics in diffusion generation: texture synthesis, style/appearance transfer, and style-specific text-to-image generation. Primary interactive entry points are `ad.ipynb`, `app.py`, and the Flux notebook `flux_ad/main.ipynb`. VAE fine-tuning is launched through `train_vae.py`.

#### Diffusion Pipeline Layer
Custom Stable Diffusion pipelines encapsulate the denoising-time attention manipulation used by the method. `pipeline_sd.py` targets SD 1.5-class models; `pipeline_sdxl.py` provides the SDXL variant. These are the main model-runtime boundary over Hugging Face Diffusers and PyTorch.

#### Attention-Distillation Objectives
Loss definitions and shared computation utilities support optimization of visual characteristics from image references. `losses.py` is the central objective layer, while `utils.py` provides shared image/model utilities. The data inputs are organized by characteristic type under `data/content`, `data/style`, and `data/texture`.

#### Generation Workflows
The notebook-driven workflow in `ad.ipynb` exposes the three SD-based use cases: optimize or extract the target visual signal, then invoke the custom pipeline to generate an image matching it. SD and SDXL are selected through the separate pipeline implementations rather than through a single generic runtime.

#### Flux Workflow
A parallel Flux.1-dev implementation supports style-specific text-to-image generation. `flux_ad/mypipeline.py` is its custom pipeline layer, with supporting routines in `flux_ad/utils.py`; `flux_ad/main.ipynb` is its execution entry point. This is a distinct model-family boundary from the Diffusers SD/SDXL paths.

#### VAE Fine-Tuning
`train_vae.py` trains or fine-tunes a VAE from an image path and an existing VAE model path. The resulting VAE can improve reconstruction or generation behavior for the transfer workflow, but is operationally separate from inference pipelines.

#### Web UI Runtime
`app.py` starts a local UI server on port 7860. The UI is composed around task-specific tabs: `webui/tab_texture_synthesis.py`, `webui/tab_style_transfer.py`, and `webui/tab_style_t2i.py`. `webui/runner.py` is the likely execution bridge from UI inputs to model/pipeline calls. Material runtime dependencies are Python 3.10, PyTorch 2.5, and Diffusers 0.32; model weights are external inputs rather than repository-contained services.

Main entry points: [`losses.py`](https://github.com/xugao97/attentiondistillation/blob/main/losses.py) (Attention objectives), [`webui/runner.py`](https://github.com/xugao97/attentiondistillation/blob/main/webui/runner.py) (UI runner), [`ad.ipynb`](https://github.com/xugao97/attentiondistillation/blob/main/ad.ipynb) (AD notebook), [`app.py`](https://github.com/xugao97/attentiondistillation/blob/main/app.py) (UI server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_sd["SD Attention Distillation"]
  node_ad_notebook["AD notebook<br/>interactive entry<br/>[ad.ipynb]"]
  node_sd_pipeline["SD 1.5 pipeline<br/>custom diffusion pipeline<br/>[pipeline_sd.py]"]
  node_sdxl_pipeline["SDXL pipeline<br/>custom diffusion pipeline<br/>[pipeline_sdxl.py]"]
  node_objectives["Attention objectives<br/>loss layer<br/>[losses.py]"]
  node_shared_utils["Shared utilities<br/>model/image utilities<br/>[utils.py]"]
  node_content_data[("Content references<br/>image inputs")]
  node_style_data[("Style references<br/>image inputs")]
  node_texture_data[("Texture references<br/>image inputs")]
end

subgraph group_ui["Web UI"]
  node_app["UI server<br/>application entry<br/>[app.py]"]
  node_texture_tab["Texture synthesis tab<br/>UI task"]
  node_style_transfer_tab["Style transfer tab<br/>UI task"]
  node_style_t2i_tab["Style T2I tab<br/>UI task<br/>[tab_style_t2i.py]"]
  node_ui_runner["UI runner<br/>execution bridge<br/>[runner.py]"]
end

subgraph group_flux["Flux Workflow"]
  node_flux_notebook["Flux notebook<br/>interactive entry<br/>[main.ipynb]"]
  node_flux_pipeline["Flux custom pipeline<br/>Flux runtime<br/>[mypipeline.py]"]
  node_flux_utils["Flux utilities<br/>support routines<br/>[utils.py]"]
end

subgraph group_training["VAE Fine-Tuning"]
  node_vae_training["VAE training<br/>training entry<br/>[train_vae.py]"]
end

node_model_weights[("External model weights<br/>external models")]

node_ad_notebook -->|"optimizes"| node_objectives
node_ad_notebook -->|"generates with"| node_sd_pipeline
node_ad_notebook -->|"generates with"| node_sdxl_pipeline
node_objectives -->|"uses"| node_shared_utils
node_objectives -->|"reads"| node_content_data
node_objectives -->|"reads"| node_style_data
node_objectives -->|"reads"| node_texture_data
node_sd_pipeline -->|"loads"| node_model_weights
node_sdxl_pipeline -->|"loads"| node_model_weights
node_app -->|"hosts"| node_texture_tab
node_app -->|"hosts"| node_style_transfer_tab
node_app -->|"hosts"| node_style_t2i_tab
node_texture_tab -->|"submits to"| node_ui_runner
node_style_transfer_tab -->|"submits to"| node_ui_runner
node_style_t2i_tab -->|"submits to"| node_ui_runner
node_ui_runner -->|"invokes"| node_sd_pipeline
node_ui_runner -->|"invokes"| node_sdxl_pipeline
node_flux_notebook -->|"runs"| node_flux_pipeline
node_flux_pipeline -->|"uses"| node_flux_utils
node_flux_pipeline -->|"loads"| node_model_weights
node_vae_training -->|"loads and produces"| node_model_weights

click node_ad_notebook "https://github.com/xugao97/attentiondistillation/blob/main/ad.ipynb"
click node_sd_pipeline "https://github.com/xugao97/attentiondistillation/blob/main/pipeline_sd.py"
click node_sdxl_pipeline "https://github.com/xugao97/attentiondistillation/blob/main/pipeline_sdxl.py"
click node_objectives "https://github.com/xugao97/attentiondistillation/blob/main/losses.py"
click node_shared_utils "https://github.com/xugao97/attentiondistillation/blob/main/utils.py"
click node_content_data "https://github.com/xugao97/attentiondistillation/tree/main/data/content"
click node_style_data "https://github.com/xugao97/attentiondistillation/tree/main/data/style"
click node_texture_data "https://github.com/xugao97/attentiondistillation/tree/main/data/texture"
click node_app "https://github.com/xugao97/attentiondistillation/blob/main/app.py"
click node_texture_tab "https://github.com/xugao97/attentiondistillation/blob/main/webui/tab_texture_synthesis.py"
click node_style_transfer_tab "https://github.com/xugao97/attentiondistillation/blob/main/webui/tab_style_transfer.py"
click node_style_t2i_tab "https://github.com/xugao97/attentiondistillation/blob/main/webui/tab_style_t2i.py"
click node_ui_runner "https://github.com/xugao97/attentiondistillation/blob/main/webui/runner.py"
click node_flux_notebook "https://github.com/xugao97/attentiondistillation/blob/main/flux_ad/main.ipynb"
click node_flux_pipeline "https://github.com/xugao97/attentiondistillation/blob/main/flux_ad/mypipeline.py"
click node_flux_utils "https://github.com/xugao97/attentiondistillation/blob/main/flux_ad/utils.py"
click node_vae_training "https://github.com/xugao97/attentiondistillation/blob/main/train_vae.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_ad_notebook,node_sd_pipeline,node_sdxl_pipeline,node_objectives,node_shared_utils,node_content_data,node_style_data,node_texture_data toneBlue
class node_app,node_texture_tab,node_style_transfer_tab,node_style_t2i_tab,node_ui_runner toneAmber
class node_flux_notebook,node_flux_pipeline,node_flux_utils toneMint
class node_vae_training toneRose
class node_model_weights toneNeutral
```

## Components

### SD Attention Distillation

Stable Diffusion and SDXL optimization and inference.

- [AD notebook](https://github.com/xugao97/attentiondistillation/blob/main/ad.ipynb): interactive entry · Runs texture, style transfer, and style-specific T2I workflows. · `ad.ipynb`
- [SD 1.5 pipeline](https://github.com/xugao97/attentiondistillation/blob/main/pipeline_sd.py): custom diffusion pipeline · Denoising-time attention manipulation for SD 1.5-class models. · `pipeline_sd.py`
- [SDXL pipeline](https://github.com/xugao97/attentiondistillation/blob/main/pipeline_sdxl.py): custom diffusion pipeline · SDXL-specific attention-distillation runtime. · `pipeline_sdxl.py`
- [Attention objectives](https://github.com/xugao97/attentiondistillation/blob/main/losses.py): loss layer · Central visual-characteristic optimization objectives. · `losses.py`
- [Shared utilities](https://github.com/xugao97/attentiondistillation/blob/main/utils.py): model/image utilities · `utils.py`
- [Content references](https://github.com/xugao97/attentiondistillation/tree/main/data/content): image inputs · `data/content`
- [Style references](https://github.com/xugao97/attentiondistillation/tree/main/data/style): image inputs · `data/style`
- [Texture references](https://github.com/xugao97/attentiondistillation/tree/main/data/texture): image inputs · `data/texture`

### Web UI

Local task-oriented interface.

- [UI server](https://github.com/xugao97/attentiondistillation/blob/main/app.py): application entry · Starts the local interface on port 7860. · `app.py`
- [Texture synthesis tab](https://github.com/xugao97/attentiondistillation/blob/main/webui/tab_texture_synthesis.py): UI task · `webui/tab_texture_synthesis.py`
- [Style transfer tab](https://github.com/xugao97/attentiondistillation/blob/main/webui/tab_style_transfer.py): UI task · `webui/tab_style_transfer.py`
- [Style T2I tab](https://github.com/xugao97/attentiondistillation/blob/main/webui/tab_style_t2i.py): UI task · `webui/tab_style_t2i.py`
- [UI runner](https://github.com/xugao97/attentiondistillation/blob/main/webui/runner.py): execution bridge · Routes UI task inputs to model workflows. · `webui/runner.py`

### Flux Workflow

Separate Flux.1-dev model-family path.

- [Flux notebook](https://github.com/xugao97/attentiondistillation/blob/main/flux_ad/main.ipynb): interactive entry · Runs Flux style-specific text-to-image generation. · `flux_ad/main.ipynb`
- [Flux custom pipeline](https://github.com/xugao97/attentiondistillation/blob/main/flux_ad/mypipeline.py): Flux runtime · `flux_ad/mypipeline.py`
- [Flux utilities](https://github.com/xugao97/attentiondistillation/blob/main/flux_ad/utils.py): support routines · `flux_ad/utils.py`

### VAE Fine-Tuning

Independent VAE training path.

- [VAE training](https://github.com/xugao97/attentiondistillation/blob/main/train_vae.py): training entry · Trains or fine-tunes a VAE from images and an existing VAE. · `train_vae.py`

### Other components

- **External model weights**: external models · Diffusion and VAE weights are supplied outside the repository.

## Connections

- AD notebook → Attention objectives: optimizes
- AD notebook → SD 1.5 pipeline: generates with
- AD notebook → SDXL pipeline: generates with
- Attention objectives → Shared utilities: uses
- Attention objectives → Content references: reads
- Attention objectives → Style references: reads
- Attention objectives → Texture references: reads
- SD 1.5 pipeline → External model weights: loads
- SDXL pipeline → External model weights: loads
- UI server → Texture synthesis tab: hosts
- UI server → Style transfer tab: hosts
- UI server → Style T2I tab: hosts
- Texture synthesis tab → UI runner: submits to
- Style transfer tab → UI runner: submits to
- Style T2I tab → UI runner: submits to
- UI runner → SD 1.5 pipeline: invokes
- UI runner → SDXL pipeline: invokes
- Flux notebook → Flux custom pipeline: runs
- Flux custom pipeline → Flux utilities: uses
- Flux custom pipeline → External model weights: loads
- VAE training → External model weights: loads and produces · Consumes an existing VAE and produces fine-tuned weights.
