Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

Merge remote-tracking branch 'upstream/main' into feat/krea2-edit-references

Alexander Eichhorn791d76f61799
diff --cc invokeai/app/invocations/krea2/krea2_denoise.py
index fb43808ec5,5a6543d3e8..cee18f57ed
--- a/invokeai/app/invocations/krea2/krea2_denoise.py
+++ b/invokeai/app/invocations/krea2/krea2_denoise.py
@@@ -45,26 -39,14 +45,22 @@@ from invokeai.backend.krea2.regional_pr
Krea2TextConditioning,
)
from invokeai.backend.krea2.sampling_utils import (
- KREA2_BASE_IMAGE_SEQ_LEN,
- KREA2_BASE_SHIFT,
KREA2_DISTILLED_MU,
- KREA2_MAX_IMAGE_SEQ_LEN,
- KREA2_MAX_SHIFT,
+ KREA2_SCHEDULER_CONFIG,
build_sigmas,
- calculate_shift,
pack_latents,
prepare_position_ids,
+ prepare_position_ids_with_references,
+ prepare_reference_position_ids,
unpack_latents,
)
-from invokeai.backend.krea2.style_reference_extension import Krea2StyleReferenceExtension
+from invokeai.backend.krea2.style_reference import KREA2_NUM_BLOCKS
+from invokeai.backend.krea2.style_reference_extension import (
+ KREA2_HEAD_DIM,
+ KREA2_NUM_KV_HEADS,
+ Krea2StyleReferenceExtension,
+)
+from invokeai.backend.krea2.text_encoding import KREA2_MAX_REFERENCE_IMAGES
from invokeai.backend.model_manager.taxonomy import BaseModelType
from invokeai.backend.patches.layer_patcher import LayerPatcher, PatchSpec
from invokeai.backend.patches.lora_conversions.krea2_lora_constants import KREA2_LORA_TRANSFORMER_PREFIX
diff --cc invokeai/backend/krea2/sampling_utils.py
index 490c48e421,d95db2d1be..582638a391
--- a/invokeai/backend/krea2/sampling_utils.py
+++ b/invokeai/backend/krea2/sampling_utils.py
@@@ -77,65 -78,13 +87,42 @@@ def prepare_position_ids(text_seq_len:
coordinates. Matches diffusers ``Krea2Pipeline.prepare_position_ids``.
"""
text_ids = torch.zeros(text_seq_len, 3, device=device)
- image_ids = torch.zeros(grid_height, grid_width, 3, device=device)
- image_ids[..., 1] = torch.arange(grid_height, device=device)[:, None]
- image_ids[..., 2] = torch.arange(grid_width, device=device)[None, :]
- image_ids = image_ids.reshape(grid_height * grid_width, 3)
- return torch.cat([text_ids, image_ids], dim=0)
+ return torch.cat([text_ids, _grid_position_ids(0, grid_height, grid_width, device)], dim=0)
+
+
+def prepare_reference_position_ids(reference_grids: Sequence[tuple[int, int]], device: torch.device) -> torch.Tensor:
+ """Rotary coordinates of the reference tokens alone: reference ``i`` (1-based) carries ``(i, h, w)`` over its
+ own ``(grid_h, grid_w)``, starting from 0."""
+ reference_ids = [
+ _grid_position_ids(index, ref_height, ref_width, device)
+ for index, (ref_height, ref_width) in enumerate(reference_grids, start=1)
+ ]
+ return torch.cat(reference_ids, dim=0) if reference_ids else torch.zeros(0, 3, device=device)
+
+
+def prepare_position_ids_with_references(
+ text_seq_len: int,
+ grid_height: int,
+ grid_width: int,
+ reference_grids: Sequence[tuple[int, int]],
+ device: torch.device,
+) -> torch.Tensor:
+ """Rotary coordinates for ``[text | target | reference 1 | reference 2 | ...]``.
+
+ Text and target are laid out as in ``prepare_position_ids``. Reference ``i`` (1-based) carries
+ ``(i, h, w)`` over its own ``(grid_h, grid_w)``, starting from 0: the first axis is what tells the
+ transformer the reference tokens apart from the target and from each other. This is the layout the
+ ai-toolkit edit LoRAs were trained with (ComfyUI's "index_timestep_zero" reference method).
+ """
+ return torch.cat(
+ [
+ prepare_position_ids(text_seq_len, grid_height, grid_width, device),
+ prepare_reference_position_ids(reference_grids, device),
+ ],
+ dim=0,
+ )
- def calculate_shift(
- image_seq_len: int,
- base_image_seq_len: int = KREA2_BASE_IMAGE_SEQ_LEN,
- max_image_seq_len: int = KREA2_MAX_IMAGE_SEQ_LEN,
- base_shift: float = KREA2_BASE_SHIFT,
- max_shift: float = KREA2_MAX_SHIFT,
- ) -> float:
- """Resolution-aware mu: a line through the two config points, held at the last one.
-
- Past ``max_image_seq_len`` (6400 tokens, 1280x1280 of area) the line would keep climbing: mu 2.2 at
- 2048x2048 puts about half of 28 steps above sigma 0.9, and mu 7.4 at 4096x4096 puts all of them
- there, so an img2img start has no step near the sigma it asks for. Held with the stock config, Raw
- ends at Turbo's fixed mu, which ComfyUI also uses for Krea-2 at every size; diffusers'
- ``Krea2Pipeline`` extrapolates.
-
- NOTE: mu is fed straight into ``FlowMatchEulerDiscreteScheduler.set_timesteps(..., mu=mu)``;
- the exponential time-shift happens inside the scheduler. Do NOT ``exp()`` this value.
- """
- m = (max_shift - base_shift) / (max_image_seq_len - base_image_seq_len)
- b = base_shift - m * base_image_seq_len
- return min(image_seq_len, max_image_seq_len) * m + b
-
-
def build_sigmas(steps: int) -> List[float]:
"""Krea-2 sigma schedule: linspace(1.0, 1/steps, steps)."""
return np.linspace(1.0, 1.0 / steps, steps).tolist()
diff --cc tests/app/invocations/test_krea2_denoise.py
index cf8a50bd96,1e0198b7a3..68c28f4493
--- a/tests/app/invocations/test_krea2_denoise.py
+++ b/tests/app/invocations/test_krea2_denoise.py
@@@ -1,4 -1,4 +1,5 @@@
+import dataclasses
+ import json
import math
from contextlib import contextmanager, nullcontext
from types import SimpleNamespace