ComfyUI Extension: ComfyUI-GRAG-ArchAi3D

Authored by amir84ferdos

Created

Updated

2 stars

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

    License: MIT Python 3.8+ ComfyUI

    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)

    1. Open ComfyUI Manager
    2. Search for "GRAG" or "ArchAi3d"
    3. Click Install
    4. 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

    1. Download ZIP from GitHub Releases
    2. Extract to ComfyUI/custom_nodes/GRAG/
    3. (Optional) Install PyYAML: pip install PyYAML
    4. 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 toggle
    • preset (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

    Technical Documentation

    🎨 Preset Library

    Categories

    1. 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)
    2. 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)
    3. 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)
    4. v2.2.1 Experimental (41 presets)

      • Preset 01-41 (range: 0.56-0.64)
      • Structure-preserving, tested in production
    5. 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

    1. Restart ComfyUI (required after installation)
    2. Check console for error messages
    3. Verify package location: ComfyUI/custom_nodes/GRAG/
    4. Check __init__.py exists

    Only 5 Presets Available

    Cause: PyYAML not installed

    Solution:

    pip install PyYAML
    # Restart ComfyUI
    

    No Visible GRAG Effects

    1. βœ… Using GRAG Advanced Sampler (not standard KSampler)
    2. βœ… enable_grag: True in controller
    3. βœ… Preset with Ξ»β‰ 1.0 or Ξ΄β‰ 1.0
    4. βœ… Console shows "Patched XX Attention layers"

    Import Errors

    See FIXES_APPLIED.md for detailed troubleshooting.

    πŸ“ž Support

    Getting Help

    Contributing

    Contributions welcome! Please:

    1. Fork the repository
    2. Create a feature branch
    3. 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:

    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]