Implementation:AUTOMATIC1111 Stable diffusion webui Edit Attention
| Knowledge Sources | |
|---|---|
| Domains | UI_Frontend, Prompt_Editing, Attention_Weights |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Enables keyboard-driven attention weight editing in prompt textareas using Ctrl+ArrowUp/ArrowDown to increase or decrease emphasis on selected text.
Description
The keyupEditAttention function intercepts keydown events on prompt textareas and implements attention weight adjustment. When the user presses Ctrl+ArrowUp or Ctrl+ArrowDown (or Cmd on macOS), the function first attempts to auto-select the relevant text if nothing is selected: it tries parenthesis blocks (angle brackets, round parentheses, square brackets in that order), then falls back to selecting the current word based on configurable delimiters (opts.keyedit_delimiters). For text already wrapped in parentheses with a weight value (e.g., "(text:1.0)"), the weight is incremented or decremented by the configured precision (opts.keyedit_precision_attention for round parens, opts.keyedit_precision_extra for angle brackets). For unwrapped text, it wraps the selection in "(text:1.0)" format. The function also handles conversion of old-style stacked parentheses (e.g., "(((text)))") to the explicit weight notation by computing 1.1^n for round parens or (1/1.1)^n for square brackets. When the weight reaches exactly 1.0 for round parentheses, the wrapping is removed entirely.
Usage
In any prompt textarea, select text (or place the cursor inside a word or parenthesized block) and press Ctrl+ArrowUp to increase emphasis or Ctrl+ArrowDown to decrease it. The weight value adjusts by the configured step size. This works for both positive and negative prompts in txt2img and img2img tabs.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: javascript/edit-attention.js
- Lines: 1-156
Signature
function keyupEditAttention(event)
// Internal helpers:
// function selectCurrentParenthesisBlock(OPEN, CLOSE)
// function selectCurrentWord()
Import
// Loaded automatically by the web UI as part of the javascript/ directory
// Registers a global keydown event listener
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| event | KeyboardEvent | Yes | The keydown event; must have ctrlKey or metaKey and ArrowUp or ArrowDown key |
| opts.keyedit_precision_attention | float | No | Step size for attention weight changes in round parentheses (from global opts) |
| opts.keyedit_precision_extra | float | No | Step size for weight changes in angle brackets (from global opts) |
| opts.keyedit_delimiters | string | No | Characters used as word boundary delimiters for auto-selection |
| opts.keyedit_delimiters_whitespace | array | No | Whitespace delimiter names to include in word boundary detection |
Outputs
| Name | Type | Description |
|---|---|---|
| (side effect) | DOM | Modifies the textarea value with updated attention weight syntax and triggers updateInput |
Usage Examples
// User interaction example:
// 1. Type "a beautiful sunset" in the prompt
// 2. Select "beautiful"
// 3. Press Ctrl+ArrowUp -> becomes "(beautiful:1.1)"
// 4. Press Ctrl+ArrowUp again -> becomes "(beautiful:1.2)"
// 5. Press Ctrl+ArrowDown three times -> "(beautiful:0.9)"
// Old-style conversion example:
// 1. Type "(((sunset)))" in the prompt
// 2. Place cursor inside "sunset"
// 3. Press Ctrl+ArrowUp -> converts to "(sunset:1.43)"
// The function is registered globally:
addEventListener('keydown', (event) => {
keyupEditAttention(event);
});