### Build and Run InvokeAI with Docker Compose Source: https://invoke-ai.github.io/InvokeAI/installation/docker This command sequence sets up and runs InvokeAI using Docker Compose. It involves navigating to the docker directory, copying the environment sample, and starting the services. Assumes Docker Desktop is configured for GPU access. ```bash cd docker cp .env.sample .env docker compose up ``` -------------------------------- ### Model Installation Service Initialization Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Demonstrates how to initialize and start the ModelInstallService. ```APIDOC ## Initializing the Model Install Service ### Description This section shows how to initialize and start the `ModelInstallService`, which handles all model installation needs. ### Initialization Pattern ```python from invokeai.app.services.config import get_config from invokeai.app.services.model_records import ModelRecordServiceSQL from invokeai.app.services.model_install import ModelInstallService from invokeai.app.services.download import DownloadQueueService from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase from invokeai.backend.util.logging import InvokeAILogger config = get_config() logger = InvokeAILogger.get_logger(config=config) db = SqliteDatabase(config.db_path, logger) record_store = ModelRecordServiceSQL(db, logger) queue = DownloadQueueService() queue.start() installer = ModelInstallService(app_config=config, record_store=record_store, download_queue=queue ) installer.start() ``` ### Constructor Parameters | Argument | Type | Description | |------------------|------------------------------|------------------------------------------------| | `app_config` | `InvokeAIAppConfig` | InvokeAI app configuration object | | `record_store` | `ModelRecordServiceBase` | Config record storage database | | `download_queue` | `DownloadQueueServiceBase` | Download queue object | | `session` | `Optional[requests.Session]` | Swap in a different Session object (optional) | ``` -------------------------------- ### Install PyPatchMatch Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Installs the PyPatchMatch Python package using pip after environment activation and dependency installation. ```bash pip install pypatchmatch ``` -------------------------------- ### Build InvokeAI Frontend Production Source: https://invoke-ai.github.io/InvokeAI/contributing/dev-environment Navigates to the frontend web directory and installs dependencies using pnpm, then builds the frontend for production. This is required after pulling new changes or for a production-ready UI. ```bash cd /invokeai/frontend/web pnpm i pnpm build ``` -------------------------------- ### Initialize ModelInstallService Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Demonstrates how to initialize the ModelInstallService with necessary dependencies like configuration, record store, and download queue. The installer needs to be started after initialization. ```python from invokeai.app.services.config import get_config from invokeai.app.services.model_records import ModelRecordServiceSQL from invokeai.app.services.model_install import ModelInstallService from invokeai.app.services.download import DownloadQueueService from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase from invokeai.backend.util.logging import InvokeAILogger config = get_config() logger = InvokeAILogger.get_logger(config=config) db = SqliteDatabase(config.db_path, logger) record_store = ModelRecordServiceSQL(db, logger) queue = DownloadQueueService() queue.start() installer = ModelInstallService(app_config=config, record_store=record_store, download_queue=queue ) installer.start() ``` -------------------------------- ### Serve InvokeAI Documentation Locally Source: https://invoke-ai.github.io/InvokeAI/contributing/dev-environment Starts a local development server for the InvokeAI documentation using mkdocs. This server provides hot-reloading, allowing for quick preview of documentation changes. ```bash mkdocs serve ``` -------------------------------- ### Verify PyPatchMatch Installation Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Confirms the successful installation of PyPatchMatch by importing the patch_match module in a Python interpreter. ```python from patchmatch import patch_match ``` -------------------------------- ### Run InvokeAI Web Application (Windows PowerShell) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Starts the InvokeAI web application on Windows. The '--root' parameter points to the installation directory, ensuring InvokeAI can access its necessary files. ```powershell invokeai-web --root $Home/invokeai ``` -------------------------------- ### Install Build Tools on Debian/Ubuntu Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Installs essential build tools on Debian-based Linux distributions, required for compiling C++ extensions like PyPatchMatch. ```bash sudo apt update sudo apt install build-essential ``` -------------------------------- ### Install Models from Various Sources in Python Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Demonstrates how to use `invokeai.app.services.model_install` to install models from local files, local directories, HuggingFace repositories (including subfolders and variants), and URLs. It shows how to create `ModelSource` objects and iterate through them for installation, and how to wait for and check the status of installation jobs. ```python from invokeai.app.services.model_install import \ LocalModelSource, \ HFModelSource, \ URLModelSource source1 = LocalModelSource(path='/opt/models/sushi.safetensors') # a local safetensors file source2 = LocalModelSource(path='/opt/models/sushi_diffusers') # a local diffusers folder source3 = HFModelSource(repo_id='runwayml/stable-diffusion-v1-5') # a repo_id source4 = HFModelSource(repo_id='runwayml/stable-diffusion-v1-5', subfolder='vae') # a subfolder within a repo_id source5 = HFModelSource(repo_id='runwayml/stable-diffusion-v1-5', variant='fp16') # a named variant of a HF model source6 = HFModelSource(repo_id='runwayml/stable-diffusion-v1-5', subfolder='OrangeMix/OrangeMix1.ckpt') # path to an individual model file source7 = URLModelSource(url='https://civitai.com/api/download/models/63006') # model located at a URL source8 = URLModelSource(url='https://civitai.com/api/download/models/63006', access_token='letmein') # with an access token for source in [source1, source2, source3, source4, source5, source6, source7]: install_job = installer.install_model(source) source2job = installer.wait_for_installs(timeout=120) for source in sources: job = source2job[source] if job.complete: model_config = job.config_out model_key = model_config.key print(f"{source} installed as {model_key}") elif job.errored: print(f"{source}: {job.error_type}.\nStack trace:\n{job.error}") ``` -------------------------------- ### Install Base Development Tools on Arch Linux Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Installs the 'base-devel' package group on Arch Linux, which includes essential tools for building software from source. ```bash sudo pacman -Syu sudo pacman -S --needed base-devel ``` -------------------------------- ### Create Installation Directory (Linux/macOS) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Creates a new directory for the InvokeAI installation and changes the current directory to it. This is the first step in setting up the local environment. ```bash mkdir ~/invokeai cd ~/invokeai ``` -------------------------------- ### Install InvokeAI with Development Dependencies Source: https://invoke-ai.github.io/InvokeAI/contributing/dev-environment Installs the InvokeAI package in editable mode from the current directory, including development, testing, and documentation dependencies. It also specifies Python version and PyTorch CUDA version. ```bash uv pip install -e ".[dev,test,docs,xformers]" --python 3.12 --python-preference only-managed --index=https://download.pytorch.org/whl/cu128 --reinstall ``` -------------------------------- ### Create Installation Directory (Windows PowerShell) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Creates a new directory for the InvokeAI installation and changes the current directory to it on Windows systems using PowerShell. This is part of the initial environment setup. ```powershell mkdir $Home/invokeai cd $Home/invokeai ``` -------------------------------- ### InvokeAI Installer: Synchronize Model Records on Startup Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER The `start` method is invoked during API initialization. It ensures that the model record store database is synchronized with the actual model files present on the disk by calling `sync_to_config()`. ```python installer.start(invoker) ``` -------------------------------- ### Install OpenCV on macOS Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Installs the OpenCV library on macOS using Homebrew, a prerequisite for building PyPatchMatch. ```bash brew install opencv ``` -------------------------------- ### Install InvokeAI Package (with Torch Backend) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Installs InvokeAI with a specified PyTorch backend for GPU acceleration. Replace '', '', and '' for the torch backend with appropriate values. ```bash uv pip install == --python 3.12 --python-preference only-managed --torch-backend= --force-reinstall ``` -------------------------------- ### HuggingFace Repo ID with Subfolder Example Source: https://invoke-ai.github.io/InvokeAI/installation/models Demonstrates how to specify a HuggingFace repository ID that includes a subfolder name to install a specific model from a repository containing multiple models. ```text monster-labs/control_v1p_sd15_qrcode_monster:v2 ``` -------------------------------- ### Model Import and Installation Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Demonstrates how to import models from different sources (local, HuggingFace, URL) and manage installation jobs. ```APIDOC ## POST /models/import ### Description Imports a model from a specified source and initiates the installation process. ### Method POST ### Endpoint /models/import ### Parameters #### Request Body - **source** (ModelSource) - Required - The source of the model (LocalModelSource, HFModelSource, URLModelSource). - **config** (Dict[str, Any]) - Optional - Overrides model probing attributes. ### Request Example ```json { "source": { "type": "HFModelSource", "repo_id": "runwayml/stable-diffusion-v1-5" } } ``` ### Response #### Success Response (200) - **job_id** (str) - Identifier for the installation job. - **status** (str) - Current status of the installation job (e.g., 'pending', 'downloading', 'installing', 'completed', 'errored'). #### Response Example ```json { "job_id": "job_12345", "status": "pending" } ``` ``` -------------------------------- ### Run InvokeAI Frontend in Development Mode Source: https://invoke-ai.github.io/InvokeAI/contributing/dev-environment Starts the Vite development server for the InvokeAI frontend. This mode offers hot-reloading for faster UI iteration but is slower than a production build. It uses a different local address (127.0.0.1:5173). ```bash pnpm dev ``` -------------------------------- ### Installing Models via Heuristic Import Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Illustrates how to use the `install` attribute of the ModelManagerService to initiate a model installation process using a heuristic import, typically from a URL. ```python mm = ApiDependencies.invoker.services.model_manager job = mm.install.heuristic_import(`https://civitai.com/models/58390/detail-tweaker-lora-lora`) ``` -------------------------------- ### Install OpenCV on Debian/Ubuntu Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Installs OpenCV and its development headers on Debian-based Linux distributions, necessary for PyPatchMatch compilation. ```bash sudo apt install python3-opencv libopencv-dev ``` -------------------------------- ### Run InvokeAI Web Application (Linux/macOS) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Launches the InvokeAI web interface. The '--root' flag specifies the directory created earlier for the installation, which is where InvokeAI will store its data. ```bash invokeai-web --root ~/invokeai ``` -------------------------------- ### Install InvokeAI Package (Base) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Installs the InvokeAI package using 'uv pip install'. Replace '' with the appropriate version (e.g., 'invokeai[xformers]') and '' with the desired InvokeAI version. Installs Python 3.12 and forces reinstallation. ```bash uv pip install == --python 3.12 --python-preference only-managed --force-reinstall ``` -------------------------------- ### Run InvokeAI Docker Container (TL;DR) Source: https://invoke-ai.github.io/InvokeAI/installation/docker This command pulls and runs the InvokeAI Docker container, exposing it on port 9090. It ensures GPU access for NVIDIA GPUs. Access InvokeAI by navigating to http://localhost:9090 in your browser. ```bash docker run --runtime=nvidia --gpus=all --publish 9090:9090 ghcr.io/invoke-ai/invokeai ``` -------------------------------- ### Nanostores Basic Usage Example Source: https://invoke-ai.github.io/InvokeAI/contributing/frontend/state-management Demonstrates the basic imperative and declarative usage of nanostores for managing a string state. It shows how to get and set the state outside a component and how to access it within a component using a hook. ```typescript export const $myStringOption = atom(null); // Outside a component, or within a callback for performance-critical logic $myStringOption.get(); $myStringOption.set('new value'); // Inside a component const myStringOption = useStore($myStringOption); ``` -------------------------------- ### Test Model Installation and Loading in Python Source: https://invoke-ai.github.io/InvokeAI/contributing/TESTS A sample pytest test function demonstrating how to use `install_and_load_model` to download and load a model for testing. It includes setup for a PyTorch device and model inference, marking the test as 'slow' due to potential model download and processing time. ```python import pytest import torch from invokeai.backend.model_management.models.base import BaseModelType, ModelType from invokeai.backend.util.test_utils import install_and_load_model @pytest.mark.slow def test_model(model_installer, torch_device): model_info = install_and_load_model( model_installer=model_installer, model_path_id_or_url="HF/dummy_model_id", model_name="dummy_model", base_model=BaseModelType.StableDiffusion1, model_type=ModelType.Dummy, ) dummy_input = build_dummy_input(torch_device) with torch.no_grad(), model_info as model: model.to(torch_device, dtype=torch.float32) output = model(dummy_input) # Validate output... ``` -------------------------------- ### Install Documentation Dependencies Source: https://invoke-ai.github.io/InvokeAI/contributing/LOCAL_DEVELOPMENT Installs the necessary Python packages for working with InvokeAI's documentation. This includes dependencies required by mkdocs and the material theme. ```shell pip install ".[docs]" ``` -------------------------------- ### Clean UV Cache Command Source: https://invoke-ai.github.io/InvokeAI/installation/quick_start This command is used to clear the UV cache to potentially fix installation issues. It is executed within the Invoke launcher's development console. ```shell uv cache clean ``` -------------------------------- ### Example Command with Parameters Source: https://invoke-ai.github.io/InvokeAI/help/SAMPLER_CONVERGENCE Demonstrates a typical command-line invocation for generating an anime image with specified dimensions, CFG scale, and seed. This is used as a basis for comparing sampler convergence. ```bash Anime. \"an anime girl\" -W512 -H512 -C7.5 -S3031912972 ``` -------------------------------- ### Install OpenCV and Dependencies on Arch Linux Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Installs OpenCV and related libraries (BLAS, fmt, glew, vtk, hdf5) on Arch Linux. Supports CUDA version if available. ```bash sudo pacman -S opencv blas fmt glew vtk hdf5 ``` ```bash sudo pacman -S opencv-cuda blas fmt glew vtk hdf5 ``` -------------------------------- ### Register or Install Model from Path Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Explains how to directly register or install models from a local filesystem path, bypassing the download queue. These methods allow for specifying model configuration overrides and return a unique ID for the installed model. `install_path` copies the model, while `register_path` only creates a configuration record. ```python key = installer.register_path(model_path, config) ``` ```python key = installer.install_path(model_path, config) ``` -------------------------------- ### Fix Docker Entrypoint Line Endings (WSL2) Source: https://invoke-ai.github.io/InvokeAI/installation/docker Addresses an issue on Windows (WSL2) where the docker-entrypoint.sh script may have incorrect line endings (CRLF instead of LF), causing a "no such file or directory" error. This provides commands to reset the file. ```bash git pull git checkout docker/docker-entrypoint.sh ``` -------------------------------- ### Install Community Node with Git Clone Source: https://invoke-ai.github.io/InvokeAI/nodes/communityNodes The suggested method for installing a community node is to clone its repository using git. This allows for easy updates in the future. ```bash git clone ``` -------------------------------- ### Prompting Examples with Sampler Parameters Source: https://invoke-ai.github.io/InvokeAI/help/SAMPLER_CONVERGENCE Demonstrates how to use prompts with specific parameters like width (-W), height (-H), cfg scale (-C), and seed (-S) for image generation, showcasing different topics and their impact on sampler convergence. ```text "valley landscape wallpaper, d&d art, fantasy, painted, 4k, high detail, sharp focus, washed colors, elaborate excellent painted illustration" -W512 -H512 -C7.5 -S1458228930 ``` ```text "a hamburger with a bowl of french fries" -W512 -H512 -C7.5 -S4053222918 ``` ```text "grown tiger, full body" -W512 -H512 -C7.5 -S3721629802 ``` ```text "Ultra realistic photo, (Miranda Bloom-Kerr), young, stunning model, blue eyes, blond hair, beautiful face, intricate, highly detailed, smooth, art by artgerm and greg rutkowski and alphonse mucha, stained glass" -W512 -H512 -C7.5 -S2131956332 ``` -------------------------------- ### Configure OpenCV Package Name on Arch Linux Source: https://invoke-ai.github.io/InvokeAI/installation/patchmatch Creates a symbolic link for the OpenCV pkg-config file to ensure compatibility with build systems expecting 'opencv.pc'. ```bash cd /usr/lib/pkgconfig/ ln -sf opencv4.pc opencv.pc ``` -------------------------------- ### Build InvokeAI Docker Image (AMD GPU) Source: https://invoke-ai.github.io/InvokeAI/installation/docker Instructions for building the InvokeAI Docker image when using an AMD GPU. This involves setting the GPU_DRIVER environment variable either in the docker-compose.yml file or the .env file before building. ```bash # Option A: Set in docker-compose.yml # GPU_DRIVER=rocm # Option B: Set in .env file # GPU_DRIVER=rocm docker compose build ``` -------------------------------- ### Simplify Output Construction (Python) Source: https://invoke-ai.github.io/InvokeAI/nodes/NODES_MIGRATION_V3_V4 Demonstrates the new builder methods available on core output objects for easier construction. This replaces the need for manual object instantiation or separate utility functions. ```python # Old image_output = ImageOutput( image=ImageField(image_name=image_dto.image_name), width=image_dto.width, height=image_dto.height, ) latents_output = build_latents_output(latents_name=name, latents=latents, seed=None) noise_output = NoiseOutput( noise=LatentsField(latents_name=latents_name, seed=seed), width=latents.size()[3] * 8, height=latents.size()[2] * 8, ) cond_output = ConditioningOutput( conditioning=ConditioningField( conditioning_name=conditioning_name, ), ) # New image_output = ImageOutput.build(image_dto) latents_output = LatentsOutput.build(latents_name=name, latents=noise, seed=self.seed) noise_output = NoiseOutput.build(latents_name=name, latents=noise, seed=self.seed) cond_output = ConditioningOutput.build(conditioning_name) ``` -------------------------------- ### Model Install Events Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Describes the events broadcast to the InvokeAI event bus during model installation. This includes 'model_install_downloading', 'model_install_running', and 'model_install_completed' events, along with their respective payload structures. ```python from typing import List, Dict # Event: model_install_downloading # Issued for remote models during download progress. # Payload keys: 'source', 'local_path', 'bytes', 'total_bytes', 'parts' # 'parts' is a list of dicts with 'source', 'local_path', 'bytes', 'total_bytes' # Event: model_install_running # Issued after downloads complete (if applicable) and probing/copying starts. # Payload key: 'source' # Event: model_install_completed # Issued upon successful installation. # Payload keys: 'source', 'total_bytes', 'key' # Example of listening to events (conceptual): # def handle_model_event(event): # if event.name == "model_install_downloading": # print(f"Downloading {event.payload['source']}: {event.payload['bytes']}/{event.payload['total_bytes']} bytes") # elif event.name == "model_install_running": # print(f"Running install for {event.payload['source']}") # elif event.name == "model_install_completed": # print(f"Successfully installed {event.payload['source']} with key {event.payload['key']}") # Assuming EventServiceBase.model_event is the event type and event_bus.subscribe is used # event_bus.subscribe(EventServiceBase.model_event, handler=handle_model_event) ``` -------------------------------- ### Heuristic Import Method Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Details on using the `heuristic_import` method to install models from various sources. ```APIDOC ## heuristic_import ### Description This method provides a simplified interface to the installer, accepting a source string (local path, URL, or HuggingFace repo ID), an optional model configuration dictionary, and an optional access token. ### Method Signature `install_job = installer.heuristic_import(source, [config], [access_token])` ### Parameters #### `source` Type: `string` Description: A string representing the model's location. Supported formats: 1. Local filesystem path (e.g., `C:\users\fred\model.safetensors`) 2. URL to a downloadable model file (e.g., `https://civitai.com/models/58390/detail-tweaker-lora-lora`) 3. HuggingFace repo_id with formats: - `model/name` (entire model) - `model/name:fp32` (entire model, fp32 variant) - `model/name:fp16:vae` (VAE submodel, fp16 variant) - `model/name::vae` (VAE submodel, default precision) - `model/name:fp16:path/to/model.safetensors` (individual file, fp16 variant) - `model/name::path/to/model.safetensors` (individual file, default variant) #### `config` (Optional) Type: `dict` Description: A dictionary of values to override autoprobed model details (type, base, scheduler, etc.). #### `access_token` (Optional) Type: `string` Description: An access token for authenticated resource access. ### Return Value A `ModelInstallJob` object. ### Notes - The variant defaults to `fp32` for full precision requests and `fp16` otherwise. If a requested variant doesn't exist, the default revision from HuggingFace will be used. - Relative paths within a HuggingFace repo allow downloading arbitrary files. ``` -------------------------------- ### Activate Virtual Environment (Linux/macOS) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Activates the previously created '.venv' virtual environment in Linux or macOS. This makes the installed packages and commands available in the current terminal session. ```bash source .venv/bin/activate ``` -------------------------------- ### Install Development and Testing Dependencies Source: https://invoke-ai.github.io/InvokeAI/contributing/LOCAL_DEVELOPMENT Installs optional groups of Python packages required for InvokeAI's development and testing workflows. These are defined in the pyproject.toml file. ```shell pip install ".[dev,test]" ``` -------------------------------- ### Deactivate and Reactivate Virtual Environment (Linux/macOS) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Deactivates the current virtual environment and then reactivates it. This ensures that InvokeAI-specific commands are properly recognized after installation. ```bash deactivate && source .venv/bin/activate ``` -------------------------------- ### InvokeAI YAML Configuration Example Source: https://invoke-ai.github.io/InvokeAI/configuration This snippet shows a basic structure for the invokeai.yaml configuration file. It includes settings for schema version, host, models directory, and precision. Users can customize these settings to override default behaviors. ```yaml # Internal metadata - do not edit: schema_version: 4.0.2 # Put user settings here - see https://invoke-ai.github.io/InvokeAI/features/CONFIGURATION/: host: 0.0.0.0 # serve the app on your local network models_dir: D:\\invokeai\\models # store models on an external drive precision: float16 # always use fp16 precision ``` -------------------------------- ### Configure Git LFS for InvokeAI Repository Source: https://invoke-ai.github.io/InvokeAI/contributing/dev-environment Sets up Git to automatically fetch LFS (Large File Storage) files for the InvokeAI repository, ensuring all necessary assets are downloaded. ```git git config lfs.fetchinclude "*" ``` -------------------------------- ### Clone InvokeAI Repository Source: https://invoke-ai.github.io/InvokeAI/contributing/contribution_guides/newContributorChecklist Clones the InvokeAI repository from GitHub to your local machine. Assumes you have a fork of the repository under your GitHub username. ```git git clone https://github.com/your-GitHub-username/InvokeAI.git ``` -------------------------------- ### Loading a Model Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Provides an example of loading a model using the `load` attribute of the ModelManagerService, after retrieving its configuration. It also shows the assertion to check if any configurations were found. ```python mm = ApiDependencies.invoker.services.model_manager configs = mm.store.get_model_by_attr(name='stable-diffusion-v1-5') assert len(configs) > 0 loaded_model = mm.load.load_model(configs[0]) ``` -------------------------------- ### Wait for Installation Jobs Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Blocks until all pending installation jobs are completed or have errored. ```APIDOC ## POST /models/wait_for_installs ### Description Waits for a specified duration for all currently queued model installation jobs to complete. ### Method POST ### Endpoint /models/wait_for_installs ### Parameters #### Request Body - **timeout** (int) - Optional, Default: None - The maximum time in seconds to wait for jobs to complete. ### Request Example ```json { "timeout": 120 } ``` ### Response #### Success Response (200) - **results** (dict) - A dictionary mapping the model source to its job status and outcome. - **job_status** (str) - Status of the job ('completed' or 'errored'). - **model_config_out** (object | null) - The configuration of the installed model if completed successfully. - **error_type** (str | null) - The type of error if the job failed. - **error** (str | null) - Detailed error message if the job failed. #### Response Example ```json { "results": { "HFModelSource(repo_id='runwayml/stable-diffusion-v1-5')": { "job_status": "completed", "model_config_out": { "key": "stable-diffusion-v1-5", "name": "Stable Diffusion v1.5", "description": "v1.5 model from Stability AI" }, "error_type": null, "error": null } } } ``` ``` -------------------------------- ### ModelManagerService Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Introduction to the `ModelManagerService` object, providing a unified interface for accessing major model management functionalities like storing, installing, and loading models. ```APIDOC ## ModelManagerService The `ModelManagerService` offers a centralized point of access for core model management operations. It is typically available via `ApiDependencies.invoker.services.model_manager` or `context.services.model_manager` within an invocation context. ### Accessing the Model Manager ```python mm = ApiDependencies.invoker.services.model_manager ``` ### Key Properties and Methods #### `mm.store` Provides access to the `ModelRecordService` for retrieving model information. Example: ```python configs = mm.store.get_model_by_attr(name='stable-diffusion-v1-5') ``` #### `mm.install` Provides access to the `ModelInstallService` for installing new models. Example: ```python job = mm.install.heuristic_import(`https://civitai.com/models/58390/detail-tweaker-lora-lora`) ``` #### `mm.load` Provides access to the `ModelLoaderService` for loading models. Example: ```python configs = mm.store.get_model_by_attr(name='stable-diffusion-v1-5') assert len(configs) > 0 loaded_model = mm.load.load_model(configs[0]) ``` #### `mm.load_model_by_config` A convenience method for loading models, equivalent to `mm.load.load_model()`. Signature: ```python mm.load_model_by_config(model_config, [submodel], [context]) -> LoadedModel ``` ``` -------------------------------- ### Manage Model Installation Jobs Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Details methods for interacting with model installation jobs. This includes listing all jobs, retrieving jobs by source or ID, canceling a specific job, and pruning completed or errored jobs from the list. These functions facilitate job monitoring and cleanup. ```python jobs = installer.list_jobs() ``` ```python jobs = installer.get_job_by_source(source) ``` ```python jobs = installer.get_job_by_id(id) ``` ```python jobs = installer.cancel_job(job) ``` ```python installer.prune_jobs ``` -------------------------------- ### Get All Models Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Retrieves a list of all model configurations stored in the database. ```APIDOC ## GET /models ### Description Retrieves all model configurations from the database. ### Method GET ### Endpoint `/models` ### Response #### Success Response (200) - **List[AnyModelConfig]** (array) - A list of all model configurations. #### Response Example ```json [ { "name": "sd-v1-5", "path": "/tmp/models/ckpts/v1-5-pruned-emaonly.safetensors" }, { "name": "sd-v2-1", "path": "/tmp/models/ckpts/v2-1-base-ema.safetensors" } ] ``` ``` -------------------------------- ### Verify Nvidia Driver and CUDA Installation Source: https://invoke-ai.github.io/InvokeAI/installation/requirements Confirms that Nvidia drivers and CUDA are correctly installed on the system by checking the output of the `nvidia-smi` command. This is crucial for utilizing Nvidia GPUs with InvokeAI. ```bash nvidia-smi ``` -------------------------------- ### Fetch Git LFS Files for InvokeAI Source: https://invoke-ai.github.io/InvokeAI/contributing/dev-environment Manually fetches all files managed by Git LFS. This command only needs to be run once, as subsequent 'git pull' commands will automatically handle LFS file fetching. ```git git lfs pull ``` -------------------------------- ### ModelInstallJob Structure Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Outlines the structure of the ModelInstallJob object, which is returned when creating an install job. It includes attributes for tracking the job's status, configuration, source, local path, and any errors. ```python from pathlib import Path from typing import Any, Dict, List from invokeai.backend.model_manager import InstallStatus, ModelSource, AnyModelConfig class ModelInstallJob: id: int status: InstallStatus config_in: Dict config_out: AnyModelConfig inplace: bool source: ModelSource local_path: Path error_type: str error: str # Assuming event_bus is an optional argument passed during job creation # def __init__(self, ..., event_bus=None): # ... # if event_bus: # # Events will be broadcast to the event bus # pass pass # Example of potential usage (conceptual): # install_job = import_model(model_source) # print(f"Job ID: {install_job.id}") # print(f"Status: {install_job.status}") ``` -------------------------------- ### Stage All Changes Source: https://invoke-ai.github.io/InvokeAI/contributing/contribution_guides/newContributorChecklist Adds all modified files in the current directory and its subdirectories to the Git staging area (index). This prepares them for the next commit. ```git git add -A ``` -------------------------------- ### VSCode Debugger Configuration for Invoke UI Source: https://invoke-ai.github.io/InvokeAI/contributing/frontend A VSCode debugger configuration to launch Chrome and attach to the Invoke UI development server running on localhost:5173. It specifies the webRoot for source mapping. ```json { "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "launch", "name": "Invoke UI", "url": "http://localhost:5173", "webRoot": "${workspaceFolder}/invokeai/frontend/web" } ] } ``` -------------------------------- ### Simplify Image Saving Method (Python) Source: https://invoke-ai.github.io/InvokeAI/nodes/NODES_MIGRATION_V3_V4 Illustrates the refactoring of the image saving method within the Invocation Context. The new API is significantly simpler, reducing the number of parameters required. ```python # Old image_dto = context.services.images.create( image=image, image_origin=ResourceOrigin.INTERNAL, image_category=ImageCategory.GENERAL, node_id=self.id, session_id=context.graph_execution_state_id, is_intermediate=self.is_intermediate, metadata=self.metadata, workflow=context.workflow, ) # New image_dto = context.images.save(image=image) ``` -------------------------------- ### Set Model Hashing Algorithm (YAML) Source: https://invoke-ai.github.io/InvokeAI/configuration Allows setting the hashing algorithm for models during installation. The default is `blake3_single`. Other options include `blake3_multi` for faster SSDs and `random` to skip hashing. Common algorithms like MD5, SHA256, and SHA512 are also supported but are slower. ```yaml hashing_algorithm: blake3_single # default value ``` -------------------------------- ### InvokeAI Custom Node Directory Structure Source: https://invoke-ai.github.io/InvokeAI/contributing/INVOCATIONS This example shows the required directory structure for custom InvokeAI nodes. It includes an __init__.py for loading nodes and subdirectories for individual nodes or collections of nodes. ```tree ├── __init__.py # Invoke-managed custom node loader │ ├── cool_node │ ├── __init__.py # see example below │ └── cool_node.py │ └── my_node_pack ├── __init__.py # see example below ├── tasty_node.py ├── bodacious_node.py ├── utils.py └── extra_nodes └── fancy_node.py ``` -------------------------------- ### Activate Virtual Environment (Windows PowerShell) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Activates the '.venv' virtual environment on Windows using PowerShell. This command makes the InvokeAI tools accessible within the current PowerShell session. ```powershell .venv\Scripts\activate ``` -------------------------------- ### Handle Tensors and Conditioning Data (Python) Source: https://invoke-ai.github.io/InvokeAI/nodes/NODES_MIGRATION_V3_V4 Details the changes in handling tensors and conditioning data within the Invocation Context. Separate, correctly typed methods are introduced for saving and loading, with the service now generating names. ```python # Old # `latents` typed as `torch.Tensor`, but could be `ConditioningFieldData` latents = context.services.latents.get(self.latents.latents_name) # `data` typed as `torch.Tenssor,` but could be `ConditioningFieldData` context.services.latents.save(latents_name, data) # New - separate methods for tensors and conditioning data w/ correct typing # Also, the service generates the names tensor_name = context.tensors.save(tensor) tensor = context.tensors.load(tensor_name) # For conditioning cond_name = context.conditioning.save(cond_data) cond_data = context.conditioning.load(cond_name) ``` -------------------------------- ### Create Portable Virtual Environment with Python 3.12 Source: https://invoke-ai.github.io/InvokeAI/installation/manual Uses 'uv venv' to create a portable Python virtual environment named '.venv' with Python 3.12. This environment is self-contained and doesn't rely on system-installed Python. ```bash uv venv --relocatable --prompt invoke --python 3.12 --python-preference only-managed .venv ``` -------------------------------- ### Deactivate and Reactivate Virtual Environment (Windows PowerShell) Source: https://invoke-ai.github.io/InvokeAI/installation/manual Deactivates the current virtual environment and then reactivates it on Windows using PowerShell. This step is crucial for making InvokeAI commands available in the environment. ```powershell deactivate .venv\Scripts\activate ``` -------------------------------- ### Initialize ModelLoadService Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Initializes the ModelLoadService, which is responsible for loading named models into memory. This process requires configuration details, model caching services, and a model loader registry. It demonstrates a typical setup for the service. ```python from invokeai.app.services.config import InvokeAIAppConfig from invokeai.app.services.model_load import ModelLoadService, ModelLoaderRegistry from invokeai.backend.model_management import ModelCache, ModelConvertCache config = InvokeAIAppConfig.get_config() ram_cache = ModelCache( max_cache_size=config.ram_cache_size, max_vram_cache_size=config.vram_cache_size, logger=logger ) convert_cache = ModelConvertCache( cache_path=config.models_convert_cache_path, max_size=config.convert_cache_size ) loader = ModelLoadService( app_config=config, ram_cache=ram_cache, convert_cache=convert_cache, registry=ModelLoaderRegistry ) ``` -------------------------------- ### Custom Model Loader Implementation Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER This section details how to implement custom model loaders by inheriting from the `ModelLoader` base class and registering them with the `ModelLoaderRegistry`. It also provides an example of a `GenericDiffusersLoader`. ```APIDOC ## Custom Model Loader Implementation Model loaders are custom classes that extend the `ModelLoader` base class. They are typically responsible for implementing the `_load_model()` method, which defines how a specific model type is loaded. ### `_load_model()` Method Signature ```python def _load_model( self, model_path: Path, model_variant: Optional[ModelRepoVariant] = None, submodel_type: Optional[SubModelType] = None, ) -> AnyModel: ``` This method receives the path to the model on disk, an optional repository variant (e.g., `fp16` for diffusers), and an optional `submodel_type` for specific model types. ### Registering a Custom Loader To integrate a new loader, place the class file in `invokeai/backend/model_manager/load/model_loaders`. The loader must inherit from `ModelLoader` and be decorated with `@ModelLoaderRegistry.register()` to specify the model types it handles. A loader can register for multiple model types, but duplicate registrations for the same model type will raise an exception. ### Example: `GenericDiffusersLoader` ```python from pathlib import Path from typing import Optional from invokeai.backend.model_manager import ( AnyModel, BaseModelType, ModelFormat, ModelRepoVariant, ModelType, SubModelType, ) from .. import ModelLoader, ModelLoaderRegistry @ModelLoaderRegistry.register(base=BaseModelType.Any, type=ModelType.CLIPVision, format=ModelFormat.Diffusers) @ModelLoaderRegistry.register(base=BaseModelType.Any, type=ModelType.T2IAdapter, format=ModelFormat.Diffusers) class GenericDiffusersLoader(ModelLoader): """Class to load simple diffusers models.""" def _load_model( self, model_path: Path, model_variant: Optional[ModelRepoVariant] = None, submodel_type: Optional[SubModelType] = None, ) -> AnyModel: model_class = self._get_hf_load_class(model_path) if submodel_type is not None: raise Exception(f"There are no submodels in models of type {model_class}") variant = model_variant.value if model_variant else None result: AnyModel = model_class.from_pretrained(model_path, torch_dtype=self._torch_dtype, variant=variant) # type: ignore return result ``` ``` -------------------------------- ### Backup and Replace Nvidia cuDNN DLLs (Windows) Source: https://invoke-ai.github.io/InvokeAI/installation/requirements Guides through backing up existing cuDNN DLL files and replacing them with updated versions from NVIDIA's developer site. This is recommended for optimizing performance on Nvidia 30-series and 40-series cards. ```powershell # Example path, adjust as necessary $invokeaiPath = "C:\Users\Username\InvokeAI" $venvPath = Join-Path $invokeaiPath ".venv" $torchPath = Join-Path $venvPath "Lib\site-packages\torch" # 1. Backup the existing 'lib' folder $libFolder = Join-Path $torchPath "lib" if (Test-Path $libFolder) { Copy-Item -Path $libFolder -Destination ($libFolder + "_backup") -Recurse Write-Host "Backed up existing lib folder to $($libFolder)_backup" } # 2. Download and extract cuDNN DLLs (Manual Step) # - Go to https://developer.nvidia.com/cudnn # - Download the appropriate version and extract it. # - Locate the 'bin' folder within the extracted cuDNN files. # 3. Copy updated DLLs to the torch 'lib' folder # Replace 'path\to\extracted\cudnn\bin' with the actual path to your cuDNN bin folder $cudnnBinPath = "path\to\extracted\cudnn\bin" if (Test-Path $cudnnBinPath) { Copy-Item -Path (Join-Path $cudnnBinPath "*.dll") -Destination $libFolder -Force Write-Host "Copied updated .dll files to $libFolder" } else { Write-Host "cuDNN bin folder not found at $cudnnBinPath. Please verify the path." } # 4. Restart InvokeAI application after replacement. ``` -------------------------------- ### Download and Cache Model - Python Source: https://invoke-ai.github.io/InvokeAI/nodes/invocation-api Downloads a model file from a given source (URL or Hugging Face repo_id) to the models cache and returns its Path. If the model is already cached, it returns the cached path. This is useful for single-file installations of arbitrary resource types. ```python download_and_cache_model(source: str | AnyHttpUrl) -> Path ``` -------------------------------- ### Creating ModelRecordServiceSQL Instances Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Shows how to create SQL-based model record service instances, either from an existing connection or a database file. ```python from invokeai.app.services.model_records import ModelRecordServiceSQL store = ModelRecordServiceSQL.from_connection(connection, lock) ``` ```python from invokeai.app.services.model_records import ModelRecordServiceSQL store = ModelRecordServiceSQL.from_db_file('/path/to/sqlite_database.db') ``` -------------------------------- ### Creating a Custom Download Queue Instance Source: https://invoke-ai.github.io/InvokeAI/contributing/DOWNLOAD_QUEUE Shows how to instantiate a `DownloadQueueService` with custom parameters, such as an event bus for progress reporting. This allows for isolated download management or integration with system-wide event handling. ```python queue = DownloadQueueService(event_bus=events) ``` -------------------------------- ### Simple Download Queue Usage in Python Source: https://invoke-ai.github.io/InvokeAI/contributing/DOWNLOAD_QUEUE Demonstrates the basic usage of the DownloadQueueService to download multiple files from provided URLs. It initializes the service, adds download jobs, waits for their completion, and then lists the status of each job, including successful downloads and errors. ```python from invokeai.app.services.download import DownloadQueueService, TqdmProgress download_queue = DownloadQueueService() for url in ['https://github.com/invoke-ai/InvokeAI/blob/main/invokeai/assets/a-painting-of-a-fire.png?raw=true', 'https://github.com/invoke-ai/InvokeAI/blob/main/invokeai/assets/birdhouse.png?raw=true', 'https://github.com/invoke-ai/InvokeAI/blob/main/invokeai/assets/missing.png', 'https://civitai.com/api/download/models/152309?type=Model&format=SafeTensor', ]: # urls start downloading as soon as download() is called download_queue.download(source=url, dest='/tmp/downloads', on_progress=TqdmProgress().update ) download_queue.join() # wait for all downloads to finish for job in download_queue.list_jobs(): print(job.model_dump_json(exclude_none=True, indent=4),"\n") ``` -------------------------------- ### Creating ModelRecordServiceBase Instance from File Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Demonstrates using ModelRecordServiceBase.from_db_file to automatically select between SQL and File implementations based on the file extension. ```python from invokeai.app.services.model_records import ModelRecordServiceBase store = ModelRecordServiceBase.from_db_file('/path/to/a/file.{yaml,db}') ``` -------------------------------- ### Install Xcode CLI Tools (macOS) Source: https://invoke-ai.github.io/InvokeAI/installation/requirements Installs the necessary command-line tools for Xcode on macOS, which might be required for certain Python operations or certificate installations related to InvokeAI. ```bash xcode-select --install ``` -------------------------------- ### Wait for All Installs or Specific Job Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Provides methods to pause execution until all pending model installations are complete or a specific job finishes. An optional timeout can be set to prevent indefinite blocking. These methods are crucial for synchronizing operations after initiating installs. ```python jobs = installer.wait_for_installs([timeout]) ``` ```python jobs = installer.wait_for_job(job, [timeout]) ``` -------------------------------- ### Creating ModelRecordServiceFile Instance Source: https://invoke-ai.github.io/InvokeAI/contributing/MODEL_MANAGER Illustrates how to create a file-based (YAML) model record service instance from a database file. ```python from invokeai.app.services.model_records import ModelRecordServiceFile store = ModelRecordServiceFile.from_db_file('/path/to/database.yaml') ```