From b7c31c2642ca7e1a1cf78d7fe1b4a0cbc87fce6d Mon Sep 17 00:00:00 2001 From: sltan19 Date: Mon, 25 May 2026 04:27:33 +0800 Subject: [PATCH 1/7] Add support for Intel XPU --- README.md | 21 +- configs/benchmark_config.json | 1 + configs/config_with_reference.json | 1 + configs/paint_config.json | 1 + configs/stream_processor_config.json | 1 + scripts/install_xpu.bat | 183 ++++++++++++++++++ scripts/install_xpu.sh | 136 +++++++++++++ .../stream_processor/interpolation_model.py | 7 +- .../model_inference_subprocess.py | 10 +- src/fluxrt/stream_processor/pipeline.py | 6 +- src/fluxrt/utils/scan_hardware.py | 43 ++-- 11 files changed, 386 insertions(+), 24 deletions(-) create mode 100644 scripts/install_xpu.bat create mode 100755 scripts/install_xpu.sh diff --git a/README.md b/README.md index c473096..23eb395 100644 --- a/README.md +++ b/README.md @@ -96,9 +96,12 @@ cd FluxRT conda create -n fluxrt python=3.12 pip -y conda activate fluxrt -# Install PyTorch with CUDA support (adjust if needed) +# PyTorch with CUDA support pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128 +# PyTorch with Intel (XPU) support — use this instead of the line above +# pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu + # Install project dependencies pip install -r requirements.txt pip install -e . @@ -110,10 +113,15 @@ pip install -e . # Install uv if you don't have it pip install uv -# Create environment and install PyTorch with CUDA support +# Create environment uv venv --python 3.12 + +# PyTorch with CUDA support uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128 +# PyTorch with Intel (XPU) support — use this instead of the line above +# uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu + # Install project dependencies uv pip install -r requirements.txt uv pip install -e . @@ -225,7 +233,14 @@ FluxRT/ ## 4. Run -To enable `int8` quantization you can either add falg `--int8` when running any script or set `enable_int8_quantization` to `true` in the corresponding config. +To enable `int8` quantization you can either add flag `--int8` when running any script or set `enable_int8_quantization` to `true` in the corresponding config. + +To select the compute device, set `"device"` in the config JSON: + +```json +"device": "cuda" // NVIDIA GPU (default) +"device": "xpu" // Intel GPU +``` Run any script with conda environment activated: diff --git a/configs/benchmark_config.json b/configs/benchmark_config.json index 56b44dc..648f4ab 100644 --- a/configs/benchmark_config.json +++ b/configs/benchmark_config.json @@ -9,6 +9,7 @@ "width" : 576 }, "compile_models": true, + "device": "cuda", "enable_spatial_cache": true, "enable_int8_quantization": false, "target_fps":null, diff --git a/configs/config_with_reference.json b/configs/config_with_reference.json index a79691e..c56d61b 100644 --- a/configs/config_with_reference.json +++ b/configs/config_with_reference.json @@ -9,6 +9,7 @@ "width" : 576 }, "compile_models": true, + "device": "cuda", "enable_spatial_cache": true, "enable_int8_quantization": false, "target_fps":null, diff --git a/configs/paint_config.json b/configs/paint_config.json index 082ca5f..4bb3dad 100644 --- a/configs/paint_config.json +++ b/configs/paint_config.json @@ -9,6 +9,7 @@ "width" : 576 }, "compile_models": false, + "device": "cuda", "enable_spatial_cache": true, "enable_int8_quantization": false, "target_fps":null, diff --git a/configs/stream_processor_config.json b/configs/stream_processor_config.json index 1384d6f..463a8b9 100644 --- a/configs/stream_processor_config.json +++ b/configs/stream_processor_config.json @@ -9,6 +9,7 @@ "width" : 576 }, "compile_models": true, + "device": "cuda", "enable_spatial_cache": true, "enable_int8_quantization": false, "target_fps":null, diff --git a/scripts/install_xpu.bat b/scripts/install_xpu.bat new file mode 100644 index 0000000..55ebb89 --- /dev/null +++ b/scripts/install_xpu.bat @@ -0,0 +1,183 @@ +@echo off +SETLOCAL EnableDelayedExpansion + +:: FluxRT XPU installation script for Windows (Intel GPU). +:: Run from the repository root: scripts\install_xpu.bat + +:: ── sanity-check: running from repo root ────────────────────────────────────── +IF NOT EXIST "pyproject.toml" ( + echo [ERROR] This script must be run from the FluxRT repository root. + exit /b 1 +) + +:: ── prerequisites ───────────────────────────────────────────────────────────── +echo [+] Checking prerequisites... + +where git >nul 2>&1 +IF ERRORLEVEL 1 ( + echo [ERROR] 'git' is not installed. Install it from https://git-scm.com/download/win + exit /b 1 +) + +git lfs version >nul 2>&1 +IF ERRORLEVEL 1 ( + echo [ERROR] 'git-lfs' is not installed. + echo Install with: winget install GitHub.GitLFS + echo Or download from https://git-lfs.com + exit /b 1 +) + +:: ── uv ──────────────────────────────────────────────────────────────────────── +where uv >nul 2>&1 +IF ERRORLEVEL 1 ( + echo [!] 'uv' not found. Installing via official binary installer... + powershell -ExecutionPolicy ByPass -Command "irm https://astral.sh/uv/install.ps1 | iex" + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to install uv. Install it manually: https://docs.astral.sh/uv/ + exit /b 1 + ) + :: Reload PATH so uv is visible in this session + FOR /F "delims=" %%i IN ('powershell -Command "[System.Environment]::GetEnvironmentVariable(\"PATH\",\"User\")"') DO SET "PATH=%%i;%PATH%" + where uv >nul 2>&1 + IF ERRORLEVEL 1 ( + echo [ERROR] uv was installed but is not on PATH. Open a new terminal and re-run. + exit /b 1 + ) +) + +echo [+] All prerequisites found. + +:: ── virtual environment ─────────────────────────────────────────────────────── +SET VENV_DIR=fluxrt + +IF EXIST "%VENV_DIR%\Scripts\activate.bat" ( + echo [+] Virtual environment '%VENV_DIR%' already exists. +) ELSE ( + echo [+] Creating virtual environment '%VENV_DIR%' (python=3.12^) via uv... + uv venv "%VENV_DIR%" --python 3.12 + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to create virtual environment. + exit /b 1 + ) +) + +CALL "%VENV_DIR%\Scripts\activate.bat" +IF ERRORLEVEL 1 ( + echo [ERROR] Failed to activate virtual environment '%VENV_DIR%'. + exit /b 1 +) + +:: ── PyTorch with XPU support ────────────────────────────────────────────────── +python -c "import torch" >nul 2>&1 +IF ERRORLEVEL 1 ( + echo [+] Installing PyTorch with Intel XPU support... + uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to install PyTorch. + exit /b 1 + ) +) ELSE ( + echo [+] PyTorch is already installed. +) + +:: ── Python requirements ─────────────────────────────────────────────────────── +python -c "import diffusers" >nul 2>&1 +IF ERRORLEVEL 1 ( + echo [+] Installing Python requirements from requirements.txt... + uv pip install -r requirements.txt + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to install requirements. + exit /b 1 + ) +) ELSE ( + echo [+] Python requirements already installed. +) + +:: ── fluxrt package ──────────────────────────────────────────────────────────── +python -c "import fluxrt" >nul 2>&1 +IF ERRORLEVEL 1 ( + echo [+] Installing fluxrt package in editable mode... + uv pip install -e . + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to install fluxrt package. + exit /b 1 + ) +) ELSE ( + echo [+] fluxrt package already installed. +) + +:: ── model downloads ─────────────────────────────────────────────────────────── +git lfs install + +:: ── RIFE frame-interpolation model ─────────────────────────────────────────── +SET RIFE_DIR=RIFE-safetensors +SET RIFE_SENTINEL=RIFE-safetensors\flownet.safetensors +IF EXIST "%RIFE_SENTINEL%" ( + echo [+] RIFE frame-interpolation model: already downloaded. +) ELSE IF EXIST "%RIFE_DIR%\.git" ( + echo [!] RIFE: directory exists but looks incomplete -- resuming LFS download... + git -C "%RIFE_DIR%" pull --ff-only + git -C "%RIFE_DIR%" lfs pull +) ELSE IF EXIST "%RIFE_DIR%" ( + echo [!] RIFE: '%RIFE_DIR%' exists but is not a git repository. + echo [!] Remove it and re-run to download the model. +) ELSE ( + echo [+] Downloading RIFE frame-interpolation model... + git clone https://huggingface.co/TensorForger/RIFE-safetensors + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to clone RIFE model. + exit /b 1 + ) +) + +:: ── FLUX.2-klein-4B base model ──────────────────────────────────────────────── +SET FLUX_DIR=FLUX.2-klein-4B +SET FLUX_SENTINEL=FLUX.2-klein-4B\transformer\diffusion_pytorch_model.safetensors +IF EXIST "%FLUX_SENTINEL%" ( + echo [+] FLUX.2-klein-4B base model: already downloaded. +) ELSE IF EXIST "%FLUX_DIR%\.git" ( + echo [!] FLUX.2-klein-4B: directory exists but looks incomplete -- resuming LFS download... + git -C "%FLUX_DIR%" pull --ff-only + git -C "%FLUX_DIR%" lfs pull +) ELSE IF EXIST "%FLUX_DIR%" ( + echo [!] FLUX.2-klein-4B: '%FLUX_DIR%' exists but is not a git repository. + echo [!] Remove it and re-run to download the model. +) ELSE ( + echo [+] Downloading FLUX.2-klein-4B base model... + git clone https://huggingface.co/black-forest-labs/FLUX.2-klein-4B + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to clone FLUX.2-klein-4B model. + exit /b 1 + ) +) + +:: ── FLUX.2-klein-4B-int8 model ──────────────────────────────────────────────── +SET INT8_DIR=FLUX.2-klein-4B-int8 +SET INT8_SENTINEL=FLUX.2-klein-4B-int8\diffusion_pytorch_model.safetensors +IF EXIST "%INT8_SENTINEL%" ( + echo [+] FLUX.2-klein-4B int8 model: already downloaded. +) ELSE IF EXIST "%INT8_DIR%\.git" ( + echo [!] FLUX.2-klein-4B-int8: directory exists but looks incomplete -- resuming LFS download... + git -C "%INT8_DIR%" pull --ff-only + git -C "%INT8_DIR%" lfs pull +) ELSE IF EXIST "%INT8_DIR%" ( + echo [!] FLUX.2-klein-4B-int8: '%INT8_DIR%' exists but is not a git repository. + echo [!] Remove it and re-run to download the model. +) ELSE ( + echo [+] Downloading FLUX.2-klein-4B int8 model... + git clone https://huggingface.co/aydin99/FLUX.2-klein-4B-int8 + IF ERRORLEVEL 1 ( + echo [ERROR] Failed to clone FLUX.2-klein-4B-int8 model. + exit /b 1 + ) +) + +:: ── done ────────────────────────────────────────────────────────────────────── +echo. +echo [+] Installation complete. +echo [!] Note: set "device": "xpu" in your config JSON to use the Intel GPU. +echo [!] Note: the GUI requires OBS to be installed for virtual webcam output. +echo [!] Download from https://obsproject.com/download +echo. +echo [+] Activate the environment and start: %VENV_DIR%\Scripts\activate +echo [+] Then run, for example: python scripts\run_gradio_demo.py diff --git a/scripts/install_xpu.sh b/scripts/install_xpu.sh new file mode 100755 index 0000000..1c99a40 --- /dev/null +++ b/scripts/install_xpu.sh @@ -0,0 +1,136 @@ +#!/bin/bash +# FluxRT XPU installation script. +# Run from the repository root: bash xpu_install.sh +set -euo pipefail + +# -- colours ------------------------------------------------------------------ +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +BOLD='\033[1m' +NC='\033[0m' + +log() { echo -e "${GREEN}[+]${NC} $*"; } +warn() { echo -e "${YELLOW}[!]${NC} $*"; } +die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; } + +# -- sanity-check: running from repo root ------------------------------------- +[ -f "pyproject.toml" ] || die "This script must be run from the FluxRT repository root." + +# -- prerequisites ------------------------------------------------------------- +log "Checking prerequisites..." + +command -v git &>/dev/null || die "'git' is not installed. Install with: sudo apt install git" +if ! command -v uv &>/dev/null; then + warn "'uv' not found. Installing automatically..." + if command -v curl &>/dev/null; then + curl -LsSf https://astral.sh/uv/install.sh | sh + elif command -v wget &>/dev/null; then + wget -qO- https://astral.sh/uv/install.sh | sh + else + die "Neither 'curl' nor 'wget' is installed. Install one of them to bootstrap uv." + fi + + # uv is typically installed under ~/.local/bin by the official installer. + export PATH="$HOME/.local/bin:$PATH" + command -v uv &>/dev/null || die "uv installation failed or uv is not on PATH. Add ~/.local/bin to PATH and re-run." + log "uv installed successfully." +fi +git lfs version &>/dev/null || die "'git-lfs' is not installed. Install with: sudo apt install git-lfs" + +log "All prerequisites found." + +# -- uv virtual environment ---------------------------------------------------- +VENV_DIR="fluxrt" + +if [ -d "${VENV_DIR}" ]; then + log "Virtual environment '${VENV_DIR}' already exists." +else + log "Creating virtual environment '${VENV_DIR}' (python=3.12) via uv..." + uv venv "${VENV_DIR}" --python 3.12 +fi + +# shellcheck source=/dev/null +source "${VENV_DIR}/bin/activate" + +# -- PyTorch ------------------------------------------------------------------- +if python -c "import torch" 2>/dev/null; then + log "PyTorch is already installed." +else + log "Installing PyTorch with XPU support..." + uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu +fi + +# -- Python requirements ------------------------------------------------------- +# Use 'diffusers' as a proxy - it's the heaviest transitive dependency. +if python -c "import diffusers" 2>/dev/null; then + log "Python requirements already installed." +else + log "Installing Python requirements from requirements.txt..." + uv pip install -r requirements.txt +fi + +# -- fluxrt package ------------------------------------------------------------ +if python -c "import fluxrt" 2>/dev/null; then + log "fluxrt package already installed." +else + log "Installing fluxrt package in editable mode..." + uv pip install -e . +fi + +# -- model downloads ----------------------------------------------------------- +# Register LFS hooks for the current user (idempotent). +git lfs install + +# clone_or_resume