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 branch 'main' into feat/single-file-clip-encoders

Alexander Eichhorn2ac46cbc0ff3
diff --cc invokeai/frontend/webv2/src/features/upscale/core/settings.ts
index cf44fb243f,6d814e613e..64df555296
--- a/invokeai/frontend/webv2/src/features/upscale/core/settings.ts
+++ b/invokeai/frontend/webv2/src/features/upscale/core/settings.ts
@@@ -27,6 -34,104 +34,106 @@@ import { isSeedMode, SEED_MAX } from '@
import type { SpandrelModelConfig, TileControlNetModelConfig, UpscaleWidgetValues } from './types';
+ // The schedule shifts of the two denoise nodes that clip `denoising_start` by step index, for the
+ // table's `indexClippedShift`; `getIndexClippedDenoisingStart` in graph.ts uses them to start those
+ // nodes where FLUX would. They go away when those nodes clip by value.
+
+ /** `z_image_denoise._calculate_shift`, held at the fit's 4096-token end since the clamp (#378). */
+ const Z_IMAGE_SHIFT_MIN_SEQ_LEN = 256;
+ const Z_IMAGE_SHIFT_MAX_SEQ_LEN = 4096;
+ const Z_IMAGE_BASE_SHIFT = 0.5;
+ const Z_IMAGE_MAX_SHIFT = 1.15;
+
+ /** Z-Image's linear shift for a frame: one token per 16x16 pixels, as `z_image_denoise` patches it. */
+ const zImageScheduleShift = ({ width, height }: { width: number; height: number }): number => {
+ const seqLen = Math.min(Math.floor(width / 16) * Math.floor(height / 16), Z_IMAGE_SHIFT_MAX_SEQ_LEN);
+ const slope = (Z_IMAGE_MAX_SHIFT - Z_IMAGE_BASE_SHIFT) / (Z_IMAGE_SHIFT_MAX_SEQ_LEN - Z_IMAGE_SHIFT_MIN_SEQ_LEN);
+
+ return Math.exp(seqLen * slope + Z_IMAGE_BASE_SHIFT - slope * Z_IMAGE_SHIFT_MIN_SEQ_LEN);
+ };
+
+ /** `ANIMA_SHIFT`: Anima's schedule is shifted by a constant, not by resolution. */
+ const ANIMA_SCHEDULE_SHIFT = 3;
+
+ /** A text encoder the widget can be asked for; each is its own persisted field. */
+ export type UpscaleComponentField = 't5EncoderModel' | 'clipEmbedModel' | 'qwen3EncoderModel' | 'qwen35EncoderModel';
+
+ const UPSCALE_COMPONENT_TYPES: Record<UpscaleComponentField, string> = {
+ clipEmbedModel: 'clip_embed',
+ qwen35EncoderModel: 'qwen3_5_encoder',
+ qwen3EncoderModel: 'qwen3_encoder',
+ t5EncoderModel: 't5_encoder',
+ };
+
+ export const UPSCALE_COMPONENT_FIELDS = Object.keys(UPSCALE_COMPONENT_TYPES) as UpscaleComponentField[];
+
+ export interface UpscaleComponentSlot {
+ readonly field: UpscaleComponentField;
+ /** Which installed models fit. Also the default pick, so it must be as narrow as the loader is. */
+ readonly accepts: (candidate: ModelConfig) => boolean;
+ /** Read in validation messages: "Upscale needs a {label} for this main model." */
+ readonly label: string;
+ }
+
+ const T5_ENCODER_SLOT: UpscaleComponentSlot = {
+ accepts: (candidate) => candidate.type === 't5_encoder',
+ field: 't5EncoderModel',
+ label: 'T5 encoder',
+ };
++// FLUX.1 encodes with CLIP-L; a CLIP-G, which an installed ComfyUI text_encoders folder brings along, fails
++// its text encoder.
+ const CLIP_EMBED_SLOT: UpscaleComponentSlot = {
- accepts: (candidate) => candidate.type === 'clip_embed',
++ accepts: (candidate) => candidate.type === 'clip_embed' && candidate.variant === 'large',
+ field: 'clipEmbedModel',
+ label: 'CLIP Embed model',
+ };
+ // Z-Image and Anima both read a `qwen3_encoder`, but not the same one. Z-Image's caption embedder is
+ // 2560 wide, so it takes the 4B and nothing else: Generate's shared filter also lets FLUX.2 Klein's
+ // 8B through, which fails in the denoise -- here, after the upscale has run, and it would be the
+ // automatic pick wherever its name sorts first. Anima's is the 0.6B.
+ const Z_IMAGE_QWEN3_SLOT: UpscaleComponentSlot = {
+ accepts: (candidate) => candidate.type === 'qwen3_encoder' && candidate.variant === 'qwen3_4b',
+ field: 'qwen3EncoderModel',
+ label: 'Qwen3 4B encoder',
+ };
+ const ANIMA_QWEN3_SLOT: UpscaleComponentSlot = {
+ accepts: isAnimaQwen3Encoder,
+ field: 'qwen3EncoderModel',
+ label: 'Qwen3 0.6B encoder',
+ };
+ const ANIMA_QWEN35_SLOT: UpscaleComponentSlot = {
+ accepts: isAnimaQwen35Encoder,
+ field: 'qwen35EncoderModel',
+ label: 'Qwen3.5 encoder',
+ };
+
+ /** What a main model's loader must be handed. A model that bundles its own parts needs none of it. */
+ export interface UpscaleComponents {
+ readonly slots: readonly UpscaleComponentSlot[];
+ readonly vae: boolean;
+ }
+
+ const NO_COMPONENTS: UpscaleComponents = { slots: [], vae: false };
+
+ export interface UpscaleTileControlNet {
+ /**
+ * SD's multi-diffusion denoises tiles independently, and without a tile ControlNet they drift
+ * apart. Elsewhere the pass runs whole and the ControlNet is an extra anchor it can do without.
+ */
+ readonly required: boolean;
+ /**
+ * Nothing in a checkpoint says which mode it was trained for -- Z-Image's Tile and Union 2.1 are
+ * tensor-for-tensor identical -- so the name decides. Union models tile on SD and SDXL; Z-Image's
+ * Union does Canny, HED, Depth, Pose, MLSD and inpainting, but not tile.
+ */
+ readonly matchesName: (name: string) => boolean;
+ }
+
+ const nameIncludes =
+ (...needles: string[]) =>
+ (name: string): boolean =>
+ needles.some((needle) => name.toLowerCase().includes(needle));
+
/**
* Which main-model architectures the Upscale widget can drive, and what each one needs.
*
diff --cc invokeai/frontend/webv2/src/features/upscale/core/upscaleGraphCoverage.test.ts
index 0000000000,e6216c5b66..4ab283fc84
mode 000000,100644..100644
--- a/invokeai/frontend/webv2/src/features/upscale/core/upscaleGraphCoverage.test.ts
+++ b/invokeai/frontend/webv2/src/features/upscale/core/upscaleGraphCoverage.test.ts
@@@ -1,0 -1,172 +1,172 @@@
+ /** Cover every upscale builder against the backend-validated node/field/literal/connection fixture. */
+
+ import type { BackendGraphContract } from '@features/generation/contracts';
+ import type { ModelConfig } from '@features/models';
+
+ import { seedArchitectureCapabilities } from '@features/generation/core/architectureCapabilities.testing';
+ import { describe, expect, it } from 'vitest';
+
+ import type { UpscaleWidgetValues } from './types';
+
+ import { compileUpscaleGraph } from './graph';
+ import { createDefaultUpscaleWidgetValues, getUpscaleValidationReasons, UPSCALE_ARCHITECTURES } from './settings';
+
+ seedArchitectureCapabilities();
+
+ const model = (key: string, type: string, base: string, extra: Record<string, unknown> = {}): ModelConfig =>
+ ({
+ base,
+ file_size: 1,
+ format: 'checkpoint',
+ hash: `${key}-hash`,
+ key,
+ name: key,
+ path: key,
+ source: key,
+ source_type: 'path',
+ type,
+ ...extra,
+ }) as ModelConfig;
+
+ /** What the widget picks components from, the way it does for a user. */
+ const INSTALLED = [
+ model('spandrel', 'spandrel_image_to_image', 'any'),
+ model('t5', 't5_encoder', 'any'),
- model('clip', 'clip_embed', 'any'),
++ model('clip', 'clip_embed', 'any', { variant: 'large' }),
+ model('qwen3-4b', 'qwen3_encoder', 'any', { variant: 'qwen3_4b' }),
+ model('qwen3-06b', 'qwen3_encoder', 'any', { variant: 'qwen3_06b' }),
+ model('qwen35', 'qwen3_5_encoder', 'any', { variant: 'qwen3_5_4b' }),
+ model('flux-vae', 'vae', 'flux'),
+ model('qwen-vae', 'vae', 'qwen-image'),
+ model('sd1-tile', 'controlnet', 'sd-1', { name: 'sd1 tile' }),
+ model('sdxl-tile', 'controlnet', 'sdxl', { name: 'sdxl tile' }),
+ model('z-tile', 'controlnet', 'z-image', { name: 'Z-Image ControlNet Tile' }),
+ ];
+
+ /**
+ * One case per path a builder can take: CFG above 1 where the negative branch is CFG-gated, the
+ * optional Z-Image ControlNet, a bundled Z-Image install, and Anima-3.8B's second encoder.
+ */
+ const CASES: { label: string; main: ModelConfig; patch?: Partial<UpscaleWidgetValues> }[] = [
+ { label: 'sd-1', main: model('sd1', 'main', 'sd-1') },
+ { label: 'sdxl', main: model('sdxl', 'main', 'sdxl') },
+ { label: 'flux', main: model('flux', 'main', 'flux') },
+ {
+ label: 'z-image',
+ main: model('z', 'main', 'z-image'),
+ patch: { cfgScale: 4, tileControlnetModel: INSTALLED[10] as UpscaleWidgetValues['tileControlnetModel'] },
+ },
+ { label: 'z-image-diffusers', main: model('z-diffusers', 'main', 'z-image', { format: 'diffusers' }) },
+ { label: 'anima', main: model('anima', 'main', 'anima', { variant: 'anima_qwen3' }), patch: { cfgScale: 4.5 } },
+ {
+ label: 'anima-3.8b',
+ main: model('anima-38', 'main', 'anima', { variant: 'anima_qwen35' }),
+ patch: { cfgScale: 6 },
+ },
+ ];
+
+ const compileCase = ({ main, patch }: (typeof CASES)[number]): BackendGraphContract => {
+ const installed = [main, ...INSTALLED, model(`${main.key}-lora`, 'lora', main.base)];
+ const defaults = createDefaultUpscaleWidgetValues(installed);
+ const values: UpscaleWidgetValues = {
+ ...defaults,
+ inputImage: { height: 101, image_name: 'input.png', width: 203 },
+ // Every builder's concept loader, so its node and edges reach the contract too.
+ loras: [{ isEnabled: true, model: installed.at(-1) as UpscaleWidgetValues['loras'][number]['model'], weight: 1 }],
+ ...patch,
+ };
+
+ expect(getUpscaleValidationReasons(values), `${main.key} is not satisfiable`).toEqual([]);
+
+ return compileUpscaleGraph(values, 'gallery', { useCpuNoise: true }).backendGraph;
+ };
+
+ describe('upscale graph coverage', () => {
+ it('covers every architecture the widget supports', () => {
+ expect([...new Set(CASES.map(({ main }) => main.base))].sort()).toEqual(Object.keys(UPSCALE_ARCHITECTURES).sort());
+ });
+
+ it('emits the node types, fields and connections the backend has to provide', async () => {
+ const byBase: Record<string, { nodeTypes: string[] }> = {};
+ const connections = new Set<string>();
+ const fields: Record<
+ string,
+ { inputs: Set<string>; literals: Map<string, Map<string, unknown>>; outputs: Set<string> }
+ > = {};
+ const fieldsFor = (nodeType: string) =>
+ (fields[nodeType] ??= { inputs: new Set(), literals: new Map(), outputs: new Set() });
+
+ for (const testCase of CASES) {
+ const graph = compileCase(testCase);
+ const entry = (byBase[testCase.main.base] ??= { nodeTypes: [] });
+
+ entry.nodeTypes = [
+ ...new Set([...entry.nodeTypes, ...Object.values(graph.nodes).map((node) => node.type)]),
+ ].sort();
+
+ for (const node of Object.values(graph.nodes)) {
+ const { literals } = fieldsFor(node.type);
+
+ for (const [field, value] of Object.entries(node)) {
+ // `id` and `type` address the node; an undefined value is not serialized at all.
+ if (field === 'id' || field === 'type' || value === undefined) {
+ continue;
+ }
+
+ const values = literals.get(field) ?? new Map<string, unknown>();
+ literals.set(field, values);
+
+ // Scalars only: object values are model identifiers and images from this file's fixtures.
+ if (value === null || typeof value !== 'object') {
+ values.set(JSON.stringify(value), value);
+ }
+ }
+ }
+
+ for (const edge of graph.edges) {
+ const source = graph.nodes[edge.source.node_id]!.type;
+ const destination = graph.nodes[edge.destination.node_id]!.type;
+
+ fieldsFor(source).outputs.add(edge.source.field);
+ fieldsFor(destination).inputs.add(edge.destination.field);
+ connections.add([source, edge.source.field, destination, edge.destination.field].join(' '));
+ }
+ }
+
+ const contract = {
+ connections: [...connections].sort(),
+ _comment:
+ 'Generated by src/features/upscale/core/upscaleGraphCoverage.test.ts; regenerate with ' +
+ '`vitest -u`. Excluded from oxfmt so the test alone owns its layout. Consumed by ' +
+ 'tests/app/invocations/test_frontend_graph_node_types.py, which checks every node type, ' +
+ 'edge field and literal input value below against the backend invocation registry, and ' +
+ 'every connection for whether the backend considers the two field types compatible. An ' +
+ 'empty literalInputs value list means the field is set to something other than a scalar, ' +
+ 'so only its name is checked.',
+ byBase,
+ fieldsByNodeType: Object.fromEntries(
+ Object.entries(fields)
+ .sort(([a], [b]) => a.localeCompare(b))
+ .map(([nodeType, { inputs, literals, outputs }]) => [
+ nodeType,
+ {
+ inputs: [...inputs].sort(),
+ literalInputs: Object.fromEntries(
+ [...literals]
+ .sort(([a], [b]) => a.localeCompare(b))
+ .map(([field, values]) => [
+ field,
+ [...values].sort(([a], [b]) => a.localeCompare(b)).map(([, value]) => value),
+ ])
+ ),
+ outputs: [...outputs].sort(),
+ },
+ ])
+ ),
+ };
+
+ await expect(`${JSON.stringify(contract, null, 2)}\n`).toMatchFileSnapshot(
+ './__snapshots__/upscaleGraphNodeTypes.json'
+ );
+ });
+ });