How Highres Fix Works in AUTOMATIC1111: Generating High-Resolution Images with Two-Stage Sampling
The Highres Fix is a two-stage diffusion pipeline that generates a low-resolution base image, upscales it, and runs a second img2img pass with configurable denoising strength to produce detailed high-resolution output.
The Highres Fix (abbreviated as HR fix) is a core feature of the AUTOMATIC1111 stable-diffusion-webui designed to overcome the resolution limitations of Stable Diffusion models. According to the source code in modules/processing.py, this feature implements a txt2img first pass followed by an img2img refinement pass controlled by the enable_hr flag, allowing users to generate images at resolutions far exceeding the model's native training dimensions.
What Is Highres Fix in AUTOMATIC1111?
The Highres Fix is a built-in two-stage generation pipeline that solves the architectural limitations of diffusion models trained on fixed resolutions (typically 512×512 or 768×768). Instead of generating a large image in one pass—which often produces repetitive patterns or artifacts—the pipeline separates generation into distinct composition and detail phases.
In modules/ui.py (lines 311-324), the web interface exposes this functionality through the "Hires. fix" accordion, providing controls for upscalers, denoising strength, and optional high-resolution prompts. When enabled via the enable_hr boolean flag, the system automatically executes sample_hr_pass after the initial generation completes.
How Highres Fix Generates High-Resolution Images
The implementation follows a strict three-phase architecture defined in modules/processing.py (lines 1360-1645):
Stage 1: Low-Resolution First Pass
The pipeline begins with standard txt2img sampling. The StableDiffusionProcessingTxt2Img class runs the primary sampler at the user-specified base width and height (typically 512×512).
According to the source code at lines 1360-1365, when self.enable_hr evaluates to true, the system stores the intermediate result in self.firstpass_image and immediately invokes self.sample_hr_pass upon completion of the initial sampling.
Stage 2: Upscaling and Latent Preparation
The sample_hr_pass method handles the transition between resolutions through two distinct upscaling paths:
- Latent upscaling: When
self.latent_scale_modeis notNone, the system usestorch.nn.functional.interpolateto resize the latent tensors directly (lines 1388-1392). - Pixel upscaling: For non-latent upscalers like R-ESRGAN, the method decodes low-resolution latents to a PIL image, resizes using
images.resize_image, then re-encodes back to latent space (lines 1401-1414).
Before upscaling, if save_images_before_highres_fix is enabled in modules/shared_options.py (lines 53-55), the system calls save_intermediate to write the pre-HR image with the suffix -before-highres-fix (lines 1372-1383).
Stage 3: Second-Pass Diffusion Refinement
The final stage executes an img2img-style diffusion pass over the upscaled latents. The method self.sampler.sample_img2img processes the upscaled tensor using the HR-specific sampler (self.hr_sampler_name), HR scheduler, and optional HR prompt/negative prompt defined in the UI configuration.
The critical parameter denoising_strength (0.0 to 1.0) controls the balance between preservation and regeneration:
- Values near 0.0 preserve the upscaled image with minimal changes
- Values near 1.0 allow the diffusion model to add substantial new detail, potentially altering composition
This second pass occurs at the target resolution (hr_upscale_to_x, hr_upscale_to_y) and concludes with decode_latent_batch converting the final latent batch to the output image (lines 1619-1625).
Configuration Parameters and Options
The Highres Fix exposes several configuration points across the codebase:
- UI Controls:
modules/ui.pydefines the user-facing parameters including upscaler selection, HR steps, denoising strength, and scale factors (lines 311-324). - Persistence:
modules/shared_options.pycontains the global optionsave_images_before_highres_fixfor saving intermediate low-resolution images (lines 53-55). - Processing:
modules/txt2img.py(lines 14-18) wraps the processing class and forwards HR parameters to the pipeline. - Script Integration: Third-party scripts like
scripts/xyz_grid.pycheckp.enable_hrto adapt behavior when generating comparison grids (lines 657-659).
Practical Implementation Examples
Web UI Configuration
To enable Highres Fix through the graphical interface:
- Navigate to the Txt2Img tab.
- Expand the "Hires. fix" accordion.
- Configure the parameters:
- Upscaler: Select
Latent (nearest-exponential)for fast latent-space upscaling, orR-ESRGAN 4x+for pixel-space enhancement. - Upscale by: Set to
2.0to double the resolution (e.g., 512×512 → 1024×1024). - Denoising strength: Use
0.7for balanced detail addition without losing composition. - Hires steps: Optionally increase from the default (e.g.,
20) for additional refinement.
- Upscaler: Select
- Click Generate. The UI executes the two-stage pipeline automatically, displaying only the final high-resolution output.
API Implementation
For programmatic access via the REST API, include HR parameters in your JSON payload:
{
"prompt": "a majestic castle on a hill, sunrise",
"negative_prompt": "",
"steps": 30,
"width": 512,
"height": 512,
"enable_hr": true,
"denoising_strength": 0.65,
"hr_scale": 2.0,
"hr_upscaler": "R-ESRGAN 4x+",
"hr_second_pass_steps": 20,
"hr_prompt": "",
"hr_negative_prompt": ""
}
POST this to http://localhost:7860/sdapi/v1/txt2img. The server processes both passes server-side and returns the final base64-encoded high-resolution image.
Python Scripting
For direct Python invocation within the AUTOMATIC1111 environment:
from modules import txt2img
# Configure processing with Highres Fix enabled
p = txt2img.txt2img_create_processing(
id_task="1",
request=None,
prompt="a cyberpunk city at night, neon lights",
negative_prompt="low quality, blurry",
prompt_styles=None,
n_iter=1,
batch_size=1,
cfg_scale=7.0,
height=512,
width=512,
enable_hr=True, # Enable Highres Fix
denoising_strength=0.6,
hr_scale=2.0,
hr_upscaler="Latent (nearest-exponential)",
hr_second_pass_steps=15,
hr_resize_x=0,
hr_resize_y=0,
hr_checkpoint_name="Use same checkpoint",
hr_sampler_name="Use same sampler",
hr_scheduler="Use same scheduler",
hr_prompt="",
hr_negative_prompt="",
override_settings_texts={}
)
# Execute the full pipeline
images, info, html = txt2img.txt2img(p, None)
This creates a StableDiffusionProcessingTxt2Img instance with enable_hr=True, triggering the sample_hr_pass method after the initial txt2img generation completes.
Summary
- Highres Fix implements a two-stage pipeline (low-res generation → upscale → img2img refinement) to produce images beyond native model resolutions.
- The
enable_hrflag inmodules/processing.pytriggerssample_hr_pass, which handles upscaling via latent interpolation or pixel-space upscalers. - Denoising strength (0.0-1.0) controls the trade-off between preserving the first-pass composition and adding high-frequency detail in the second pass.
- Configuration spans
modules/ui.py(interface),modules/shared_options.py(persistence options), andmodules/txt2img.py(API wrapper). - Both the Web UI and REST API support full programmatic control over HR parameters including separate prompts for the second pass.
Frequently Asked Questions
What is the difference between using Highres Fix versus generating at high resolution directly?
Generating directly at high resolution (e.g., 1024×1024 in a 512-trained model) often produces repetitive patterns or anatomical errors because the model's attention mechanisms degrade at non-native resolutions. Highres Fix first establishes composition at the model's native resolution, then uses the second pass to intelligently add detail while maintaining structural coherence, resulting in cleaner high-resolution outputs.
How does the denoising strength parameter affect the final image in Highres Fix?
The denoising_strength parameter (specified in modules/processing.py lines 1450-1460) determines how much the second diffusion pass deviates from the upscaled first-pass image. Values below 0.4 preserve most of the original content with minor smoothing, while values above 0.7 allow significant detail generation that may alter fine textures or facial features. Most workflows use 0.5-0.75 for optimal results.
Can I use a different prompt for the Highres Fix second pass?
Yes. The UI configuration in modules/ui.py exposes optional hr_prompt and hr_negative_prompt fields. When provided, these override the original prompts during the second img2img pass in sample_hr_pass, allowing you to adjust style or emphasis specifically for the high-resolution refinement stage without regenerating the base composition.
Why does the first-pass image sometimes get saved with a "-before-highres-fix" suffix?
When the global option save_images_before_highres_fix is enabled in modules/shared_options.py (default False), the pipeline calls save_intermediate during sample_hr_pass (lines 1372-1383) to persist the low-resolution image before upscaling occurs. This allows comparison between the base generation and the final refined output, useful for debugging prompt adherence versus detail quality.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →