ComfyUI Extension: ComfyUI-GRAG-ArchAi3D
Advanced GRAG (Guided Region-Adaptive Guidance) implementation for ComfyUI. Features: Simple Controller (3 parameters) for beginners, Unified Controller (25+ parameters) for experts, Advanced Sampler with v2.2.1 contamination fix, Preset Manager with 54 curated presets, per-layer control, adaptive schedules, and multi-resolution support.
Custom Nodes (0)
README
GRAG v3.0 - Advanced Group-Relative Attention Guidance
Professional-grade GRAG implementation for ComfyUI with advanced features and beginner-friendly interface.
π― What is GRAG?
GRAG (Group-Relative Attention Guidance) is a training-free image editing technique that provides fine-grained control over diffusion models by reweighting attention keys. This implementation is based on the research paper arXiv 2510.24657.
Key Formula
kΜ = Ξ» Γ k_mean + Ξ΄ Γ (k - k_mean)
Where:
- Ξ» (lambda): Controls bias strength (>1 enhances, <1 reduces)
 - Ξ΄ (delta): Controls deviation intensity (>1 amplifies, <1 suppresses)
 - k_mean: Group bias (mean of attention keys)
 - (k - k_mean): Token deviation from group
 
β¨ Features
ποΈ Two Controller Options
- Simple Controller: Beginner-friendly with 3 parameters (preset, strength, optional Ξ»/Ξ΄ overrides)
 - Unified Controller: Advanced control with 25+ parameters (per-layer, adaptive, multi-resolution)
 
π¨ Core Capabilities
- β 54 Presets (41 v2.2.1 tested + 13 paper-recommended)
 - β Per-Layer Control - Different Ξ»/Ξ΄ per transformer block
 - β Adaptive Timestep - Vary strength during denoising
 - β Multi-Resolution - Paper's 2-tier system (512px, 4096px)
 - β Architecture Auto-Detection - Works with Qwen, Flux, SD3, etc.
 - β Preset Management - Save/load/share custom configurations
 
π‘οΈ Quality Assurance
- β v2.2.1 Contamination Fix - try/finally restoration prevents global state pollution
 - β PyYAML Optional - Works with 5 hardcoded presets, full library with PyYAML
 - β Debug Mode - Attention analysis and performance metrics
 - β Extensive Documentation - Guides for all skill levels
 
π¦ Installation
Method 1: ComfyUI Manager (Recommended)
- Open ComfyUI Manager
 - Search for "GRAG" or "ArchAi3d"
 - Click Install
 - Restart ComfyUI
 
Method 2: Git Clone
cd ComfyUI/custom_nodes/
git clone https://github.com/amir84ferdos/ComfyUI-GRAG.git GRAG
cd GRAG
pip install -r requirements.txt  # Optional: For full 54-preset library
Method 3: Manual Download
- Download ZIP from GitHub Releases
 - Extract to 
ComfyUI/custom_nodes/GRAG/ - (Optional) Install PyYAML: 
pip install PyYAML - Restart ComfyUI
 
Dependencies
- Required: ComfyUI, PyTorch
 - Optional: PyYAML (for full 54-preset library)
- Without PyYAML: 5 essential presets
 - With PyYAML: All 54 presets
 
 
π Quick Start
Beginner Workflow (Simple Controller)
[Load Image]
    β
[Qwen Encoder V2]
    β
[ποΈ GRAG Simple Controller]
ββ enable_grag: True
ββ preset: "Paper: Balanced"  β Recommended
ββ strength: 1.0              (0.0-2.0)
ββ lambda/delta_override: -1.0 (optional)
    β
[βοΈ GRAG Advanced Sampler]
ββ steps: 20
ββ cfg: 8.0
ββ sampler: euler
    β
[VAE Decode] β [Save Image]
Advanced Workflow (Unified Controller)
[Load Image]
    β
[Qwen Encoder V2]
    β
[ποΈ GRAG Unified Controller]
ββ enable_grag: True
ββ control_mode: "advanced"
ββ per_layer_enabled: True
ββ layer_strategy: "structure_preserving"
ββ lambda/delta ranges: 0.9-1.3
    β
[βοΈ GRAG Advanced Sampler] β [Output]
π Available Nodes
1. ποΈ GRAG Simple Controller v3.0
Beginner-friendly preset-based control
Parameters:
enable_grag(Boolean): On/off togglepreset(Dropdown): Curated presets (Custom, Paper: Balanced, etc.)strength(Float 0-2): Effect intensity- 0.0 = No change
 - 1.0 = Preset's intended effect
 - 2.0 = Very strong
 
lambda_override(Optional Float): Manual Ξ» control (-1.0=auto, 0.1-2.0=custom)delta_override(Optional Float): Manual Ξ΄ control (-1.0=auto, 0.1-2.0=custom)
Best For:
- Beginners new to GRAG
 - Quick experimentation
 - Preset-based workflows
 
2. ποΈ GRAG Unified Controller v3.0
Advanced multi-mode control
Modes:
- Simple: Preset + manual Ξ»/Ξ΄ (like Simple Controller but all 54 presets)
 - Advanced: Per-layer control (different Ξ»/Ξ΄ per transformer block)
 - Expert: Adaptive timestep + Multi-resolution
 
Best For:
- Advanced users
 - Precision editing
 - Research and custom workflows
 
3. βοΈ GRAG Advanced Sampler v3.0
Enhanced sampler with GRAG patches
Features:
- Monkey-patches Qwen attention layers
 - v2.2.1 contamination fix (try/finally restoration)
 - Compatible with both controllers
 - Debug mode for attention analysis
 
4. πΎ GRAG Preset Manager v3.0
Save/load/manage custom presets
Operations:
- Save current parameters as named preset
 - Load user-saved presets
 - Delete user presets
 - Export/share preset configurations
 
π Documentation
Essential Guides
- INSTALL.md - Installation and troubleshooting
 - docs/QUICK_START.md - Getting started guide
 - docs/SIMPLE_VS_UNIFIED.md - Controller comparison
 - docs/STRENGTH_SLIDER_EXPLAINED.md - Strength slider mechanics
 
Technical Documentation
- SIMPLE_CONTROLLER_UPDATES.md - Recent improvements
 - FIXES_APPLIED.md - Import fixes and troubleshooting
 - IMPLEMENTATION_COMPLETE.md - Full implementation details
 
π¨ Preset Library
Categories
- 
Paper-Recommended (5 presets)
- Paper: Subtle (Ξ»=1.02, Ξ΄=1.03)
 - Paper: Balanced (Ξ»=1.05, Ξ΄=1.10) β Recommended
 - Paper: Moderate (Ξ»=1.08, Ξ΄=1.15)
 - Paper: Strong (Ξ»=1.10, Ξ΄=1.20)
 - Paper: Maximum (Ξ»=1.15, Ξ΄=1.25)
 
 - 
v2.2.1 Tested (3 presets)
- v2.2.1: Balanced (Ξ»=1.0, Ξ΄=1.5)
 - v2.2.1: Conservative (Ξ»=1.0, Ξ΄=1.3)
 - v2.2.1: Dramatic (Ξ»=1.2, Ξ΄=1.8)
 
 - 
Clean Room Workflow (3 presets)
- Clean Room: Gentle (Ξ»=1.03, Ξ΄=1.05)
 - Clean Room: Balanced (Ξ»=1.05, Ξ΄=1.10)
 - Clean Room: Strong (Ξ»=1.08, Ξ΄=1.15)
 
 - 
v2.2.1 Experimental (41 presets)
- Preset 01-41 (range: 0.56-0.64)
 - Structure-preserving, tested in production
 
 - 
Conservative Extended (2 presets)
- Conservative: Low (Ξ»=1.02, Ξ΄=1.05)
 - Conservative: High (Ξ»=1.08, Ξ΄=1.12)
 
 
Total: 54 presets (5 without PyYAML, 54 with PyYAML)
π Parameter Ranges
| Parameter | Range | Default | Recommended | |-----------|-------|---------|-------------| | Ξ» (lambda) | 0.1-2.0 | 1.0 | 0.8-1.5 | | Ξ΄ (delta) | 0.1-2.0 | 1.05 | 0.8-1.5 | | Strength | 0.0-2.0 | 1.0 | 0.8-1.5 |
Critical: At Ξ»=1.0, Ξ΄=1.0, the formula produces NO change (neutral point).
π¬ Mathematical Foundation
GRAG Formula
# Decompose attention key into bias and deviation
k_bias = mean(k_1, k_2, ..., k_N)  # Group bias
Ξk_i = k_i - k_bias                 # Token deviation
# Reweight with Ξ» and Ξ΄
kΜ_i = Ξ» Γ k_bias + Ξ΄ Γ Ξk_i
Strength Scaling (Simple Controller)
# Scale from neutral (1.0) based on strength
lambda_deviation = lambda_preset - 1.0
delta_deviation = delta_preset - 1.0
lambda_actual = 1.0 + (lambda_deviation Γ strength)
delta_actual = 1.0 + (delta_deviation Γ strength)
π οΈ Advanced Features
Per-Layer Control
Different Ξ»/Ξ΄ values per transformer block for precision editing.
Strategies:
- Linear: Smooth progression from start to end
 - U-shaped: Lower in middle layers, higher at extremes
 - Bell-curve: Higher in middle layers, lower at extremes
 - Custom: Manual layer-specific values
 
Adaptive Timestep Scheduling
Vary GRAG strength during denoising process.
Schedules:
- Linear: Constant progression
 - Exponential: Gradual then rapid change
 - Sine/Cosine: Smooth transitions
 - Diffusion-aligned: Matches noise schedule
 
Multi-Resolution Tiers
Paper's 2-tier system for hierarchical control.
Configuration:
- Tier 1 (512px): Coarse structure (typically Ξ»=1.0, Ξ΄=1.0)
 - Tier 2 (4096px): Fine details (custom Ξ»/Ξ΄)
 
π Troubleshooting
Nodes Not Appearing
- Restart ComfyUI (required after installation)
 - Check console for error messages
 - Verify package location: 
ComfyUI/custom_nodes/GRAG/ - Check 
__init__.pyexists 
Only 5 Presets Available
Cause: PyYAML not installed
Solution:
pip install PyYAML
# Restart ComfyUI
No Visible GRAG Effects
- β Using GRAG Advanced Sampler (not standard KSampler)
 - β
 
enable_grag: Truein controller - β Preset with Ξ»β 1.0 or Ξ΄β 1.0
 - β Console shows "Patched XX Attention layers"
 
Import Errors
See FIXES_APPLIED.md for detailed troubleshooting.
π Support
Getting Help
- Issues: GitHub Issues
 - Email: [email protected]
 - LinkedIn: ArchAi3d
 
Contributing
Contributions welcome! Please:
- Fork the repository
 - Create a feature branch
 - Submit a pull request with clear description
 
π License
MIT License - Free for all uses (personal and commercial)
Copyright (c) 2025 Amir Ferdos (ArchAi3d)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
π Credits
Based on:
- arXiv 2510.24657 - GRAG-Image-Editing paper
 - little-misfit's GRAG repository
 - v2.2.1 implementation learnings (ArchAi3d-Qwen package)
 
Author: Amir Ferdos (ArchAi3d) Email: [email protected] LinkedIn: https://www.linkedin.com/in/archai3d/ GitHub: https://github.com/amir84ferdos
πΊοΈ Roadmap
- [x] Core algorithms (attention_v3, per-layer, adaptive, multi-res)
 - [x] 54-preset library (v2.2.1 + paper + proven)
 - [x] Simple Controller (beginner-friendly)
 - [x] Unified Controller (advanced/expert modes)
 - [x] Advanced Sampler (v2.2.1 contamination fix)
 - [x] Preset Manager (save/load/share)
 - [x] Comprehensive documentation
 - [ ] Attention visualizer (future)
 - [ ] Example workflows repository
 - [ ] Video tutorials
 
π Version History
v3.0.0 (2025-11-03)
- β Complete rewrite with modular architecture
 - β Simple Controller for beginners
 - β Unified Controller with 3 modes
 - β 54 presets (41 v2.2.1 + 13 new)
 - β Per-layer, adaptive, multi-resolution support
 - β v2.2.1 contamination fix enhanced
 - β PyYAML optional dependency
 - β Comprehensive documentation
 
v2.2.1 (Previous)
- Basic GRAG implementation
 - Contamination fix (try/finally restoration)
 - 41 experimental presets (0.56-0.64 range)
 - Proven parameter ranges (0.1-2.0)
 
π― Use Cases
Interior Design (Clean Room Workflow)
- Material changes
 - Furniture adjustments
 - Color variations
 - Style transfers
 
General Image Editing
- Object manipulation
 - Attribute modification
 - Composition adjustments
 - Fine-grained control
 
Research & Experimentation
- Attention mechanism analysis
 - Parameter exploration
 - Custom preset development
 - Model behavior study
 
β Star this repository if you find it useful!
π Report issues on GitHub Issues
π§ Contact: [email protected]