File size: 1,337 Bytes
9ed958c
903e660
9ed958c
903e660
 
9ed958c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
903e660
 
 
 
 
 
 
 
 
 
 
 
9ed958c
 
903e660
 
 
 
 
9ed958c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Helpers shared by the Kimodo blocks."""

from dataclasses import replace

import torch
from diffusers.modular_pipelines import ModularPipelineBlocks


class ResolvableBlock(ModularPipelineBlocks):
    """Leaf block whose generic component specs can carry the model repository's resolved classes and location.

    `KimodoBlocks(components_repo=...)` fills `component_overrides`, so a sub-block popped out of the pipeline still
    loads the right custom classes from the right repository.
    """

    component_overrides = {}

    def resolve(self, specs):
        return [
            replace(spec, **self.component_overrides[spec.name]) if spec.name in self.component_overrides else spec
            for spec in specs
        ]


class Cancelled(Exception):
    pass


def checkpoint(cancel):
    if cancel is not None and cancel.is_set():
        raise Cancelled("Generation cancelled")


@torch.no_grad()
def encode_prompt(text_encoder, tokenizer, prompt, device):
    """Encode one prompt into LLM2Vec features shaped (1, 1, hidden)."""
    from .prompt_utils import tokenize_prompt
    from .sanitize import sanitize_texts

    inputs = tokenize_prompt(tokenizer, sanitize_texts([prompt])[0])
    features = text_encoder(**{name: value.to(text_encoder.device) for name, value in inputs.items()})
    return features.to(device)