Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions invokeai/app/invocations/compel.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def _lora_loader():

with ModelPatcher.apply_lora_text_encoder(text_encoder_info.context.model, _lora_loader()),\
ModelPatcher.apply_ti(tokenizer_info.context.model, text_encoder_info.context.model, ti_list) as (tokenizer, ti_manager),\
ModelPatcher.apply_clip_skip(text_encoder_info.context.model, self.clip.skipped_layers),\
text_encoder_info as text_encoder:

compel = Compel(
Expand Down Expand Up @@ -134,6 +135,24 @@ def _lora_loader():
),
)

class ClipSkipInvocationOutput(BaseInvocationOutput):
"""Clip skip node output"""
type: Literal["clip_skip_output"] = "clip_skip_output"
clip: ClipField = Field(None, description="Clip with skipped layers")

class ClipSkipInvocation(BaseInvocation):
"""Skip layers in clip text_encoder model."""
type: Literal["clip_skip"] = "clip_skip"

clip: ClipField = Field(None, description="Clip to use")
skipped_layers: int = Field(0, description="Number of layers to skip in text_encoder")

def invoke(self, context: InvocationContext) -> ClipSkipInvocationOutput:
self.clip.skipped_layers += self.skipped_layers
return ClipSkipInvocationOutput(
clip=self.clip,
)


def get_max_token_count(
tokenizer, prompt: Union[FlattenedPrompt, Blend, Conjunction],
Expand Down
2 changes: 2 additions & 0 deletions invokeai/app/invocations/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class UNetField(BaseModel):
class ClipField(BaseModel):
tokenizer: ModelInfo = Field(description="Info to load tokenizer submodel")
text_encoder: ModelInfo = Field(description="Info to load text_encoder submodel")
skipped_layers: int = Field(description="Number of skipped layers in text_encoder")
loras: List[LoraInfo] = Field(description="Loras to apply on model loading")


Expand Down Expand Up @@ -154,6 +155,7 @@ def invoke(self, context: InvocationContext) -> ModelLoaderOutput:
submodel=SubModelType.TextEncoder,
),
loras=[],
skipped_layers=0,
),
vae=VaeField(
vae=ModelInfo(
Expand Down
18 changes: 18 additions & 0 deletions invokeai/backend/model_management/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,24 @@ def _get_trigger(ti, index):
text_encoder.resize_token_embeddings(init_tokens_count)


@classmethod
@contextmanager
def apply_clip_skip(
cls,
text_encoder: CLIPTextModel,
clip_skip: int,
):
skipped_layers = []
try:
for i in range(clip_skip):
skipped_layers.append(text_encoder.text_model.encoder.layers.pop(-1))

yield

finally:
while len(skipped_layers) > 0:
text_encoder.text_model.encoder.layers.append(skipped_layers.pop())

class TextualInversionModel:
name: str
embedding: torch.Tensor # [n, 768]|[n, 1280]
Expand Down
6 changes: 4 additions & 2 deletions invokeai/frontend/web/public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@
"showOptionsPanel": "Show Options Panel",
"hidePreview": "Hide Preview",
"showPreview": "Show Preview",
"controlNetControlMode": "Control Mode"
"controlNetControlMode": "Control Mode",
"clipSkip": "Clip Skip"
},
"settings": {
"models": "Models",
Expand All @@ -551,7 +552,8 @@
"generation": "Generation",
"ui": "User Interface",
"favoriteSchedulers": "Favorite Schedulers",
"favoriteSchedulersPlaceholder": "No schedulers favorited"
"favoriteSchedulersPlaceholder": "No schedulers favorited",
"showAdvancedOptions": "Show Advanced Options"
},
"toast": {
"serverError": "Server Error",
Expand Down
4 changes: 2 additions & 2 deletions invokeai/frontend/web/src/common/components/IAISlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '@chakra-ui/react';
import { clamp } from 'lodash-es';

import { useTranslation } from 'react-i18next';
import { roundDownToMultiple } from 'common/util/roundDownToMultiple';
import {
FocusEvent,
memo,
Expand All @@ -36,9 +36,9 @@ import {
useMemo,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { BiReset } from 'react-icons/bi';
import IAIIconButton, { IAIIconButtonProps } from './IAIIconButton';
import { roundDownToMultiple } from 'common/util/roundDownToMultiple';

const SLIDER_MARK_STYLES: ChakraProps['sx'] = {
mt: 1.5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { addDynamicPromptsToGraph } from './addDynamicPromptsToGraph';
import { addLoRAsToGraph } from './addLoRAsToGraph';
import { addVAEToGraph } from './addVAEToGraph';
import {
CLIP_SKIP,
IMAGE_TO_IMAGE_GRAPH,
IMAGE_TO_LATENTS,
LATENTS_TO_IMAGE,
Expand Down Expand Up @@ -40,6 +41,7 @@ export const buildCanvasImageToImageGraph = (
scheduler,
steps,
img2imgStrength: strength,
clipSkip,
iterations,
seed,
shouldRandomizeSeed,
Expand Down Expand Up @@ -82,6 +84,11 @@ export const buildCanvasImageToImageGraph = (
id: MAIN_MODEL_LOADER,
model,
},
[CLIP_SKIP]: {
type: 'clip_skip',
id: CLIP_SKIP,
skipped_layers: clipSkip,
},
[LATENTS_TO_IMAGE]: {
type: 'l2i',
id: LATENTS_TO_IMAGE,
Expand Down Expand Up @@ -109,14 +116,24 @@ export const buildCanvasImageToImageGraph = (
node_id: MAIN_MODEL_LOADER,
field: 'clip',
},
destination: {
node_id: CLIP_SKIP,
field: 'clip',
},
},
{
source: {
node_id: CLIP_SKIP,
field: 'clip',
},
destination: {
node_id: POSITIVE_CONDITIONING,
field: 'clip',
},
},
{
source: {
node_id: MAIN_MODEL_LOADER,
node_id: CLIP_SKIP,
field: 'clip',
},
destination: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { modelIdToMainModelField } from '../modelIdToMainModelField';
import { addLoRAsToGraph } from './addLoRAsToGraph';
import { addVAEToGraph } from './addVAEToGraph';
import {
CLIP_SKIP,
INPAINT,
INPAINT_GRAPH,
ITERATE,
Expand Down Expand Up @@ -49,6 +50,7 @@ export const buildCanvasInpaintGraph = (
seamStrength,
tileSize,
infillMethod,
clipSkip,
} = state.generation;

// The bounding box determines width and height, not the width and height params
Expand Down Expand Up @@ -108,6 +110,11 @@ export const buildCanvasInpaintGraph = (
id: MAIN_MODEL_LOADER,
model,
},
[CLIP_SKIP]: {
type: 'clip_skip',
id: CLIP_SKIP,
skipped_layers: clipSkip,
},
[RANGE_OF_SIZE]: {
type: 'range_of_size',
id: RANGE_OF_SIZE,
Expand All @@ -124,27 +131,27 @@ export const buildCanvasInpaintGraph = (
edges: [
{
source: {
node_id: NEGATIVE_CONDITIONING,
field: 'conditioning',
node_id: MAIN_MODEL_LOADER,
field: 'unet',
},
destination: {
node_id: INPAINT,
field: 'negative_conditioning',
field: 'unet',
},
},
{
source: {
node_id: POSITIVE_CONDITIONING,
field: 'conditioning',
node_id: MAIN_MODEL_LOADER,
field: 'clip',
},
destination: {
node_id: INPAINT,
field: 'positive_conditioning',
node_id: CLIP_SKIP,
field: 'clip',
},
},
{
source: {
node_id: MAIN_MODEL_LOADER,
node_id: CLIP_SKIP,
field: 'clip',
},
destination: {
Expand All @@ -154,7 +161,7 @@ export const buildCanvasInpaintGraph = (
},
{
source: {
node_id: MAIN_MODEL_LOADER,
node_id: CLIP_SKIP,
field: 'clip',
},
destination: {
Expand All @@ -164,12 +171,22 @@ export const buildCanvasInpaintGraph = (
},
{
source: {
node_id: MAIN_MODEL_LOADER,
field: 'unet',
node_id: NEGATIVE_CONDITIONING,
field: 'conditioning',
},
destination: {
node_id: INPAINT,
field: 'unet',
field: 'negative_conditioning',
},
},
{
source: {
node_id: POSITIVE_CONDITIONING,
field: 'conditioning',
},
destination: {
node_id: INPAINT,
field: 'positive_conditioning',
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { addDynamicPromptsToGraph } from './addDynamicPromptsToGraph';
import { addLoRAsToGraph } from './addLoRAsToGraph';
import { addVAEToGraph } from './addVAEToGraph';
import {
CLIP_SKIP,
LATENTS_TO_IMAGE,
MAIN_MODEL_LOADER,
NEGATIVE_CONDITIONING,
Expand All @@ -28,6 +29,7 @@ export const buildCanvasTextToImageGraph = (
cfgScale: cfg_scale,
scheduler,
steps,
clipSkip,
iterations,
seed,
shouldRandomizeSeed,
Expand Down Expand Up @@ -79,6 +81,11 @@ export const buildCanvasTextToImageGraph = (
id: MAIN_MODEL_LOADER,
model,
},
[CLIP_SKIP]: {
type: 'clip_skip',
id: CLIP_SKIP,
skipped_layers: clipSkip,
},
[LATENTS_TO_IMAGE]: {
type: 'l2i',
id: LATENTS_TO_IMAGE,
Expand Down Expand Up @@ -110,14 +117,24 @@ export const buildCanvasTextToImageGraph = (
node_id: MAIN_MODEL_LOADER,
field: 'clip',
},
destination: {
node_id: CLIP_SKIP,
field: 'clip',
},
},
{
source: {
node_id: CLIP_SKIP,
field: 'clip',
},
destination: {
node_id: POSITIVE_CONDITIONING,
field: 'clip',
},
},
{
source: {
node_id: MAIN_MODEL_LOADER,
node_id: CLIP_SKIP,
field: 'clip',
},
destination: {
Expand Down
Loading