stable-diffusion-webui

Форк
0
70 строк · 2.1 Кб
1
from __future__ import annotations
2
import torch
3

4

5
class Emphasis:
6
    """Emphasis class decides how to death with (emphasized:1.1) text in prompts"""
7

8
    name: str = "Base"
9
    description: str = ""
10

11
    tokens: list[list[int]]
12
    """tokens from the chunk of the prompt"""
13

14
    multipliers: torch.Tensor
15
    """tensor with multipliers, once for each token"""
16

17
    z: torch.Tensor
18
    """output of cond transformers network (CLIP)"""
19

20
    def after_transformers(self):
21
        """Called after cond transformers network has processed the chunk of the prompt; this function should modify self.z to apply the emphasis"""
22

23
        pass
24

25

26
class EmphasisNone(Emphasis):
27
    name = "None"
28
    description = "disable the mechanism entirely and treat (:.1.1) as literal characters"
29

30

31
class EmphasisIgnore(Emphasis):
32
    name = "Ignore"
33
    description = "treat all empasised words as if they have no emphasis"
34

35

36
class EmphasisOriginal(Emphasis):
37
    name = "Original"
38
    description = "the orginal emphasis implementation"
39

40
    def after_transformers(self):
41
        original_mean = self.z.mean()
42
        self.z = self.z * self.multipliers.reshape(self.multipliers.shape + (1,)).expand(self.z.shape)
43

44
        # restoring original mean is likely not correct, but it seems to work well to prevent artifacts that happen otherwise
45
        new_mean = self.z.mean()
46
        self.z = self.z * (original_mean / new_mean)
47

48

49
class EmphasisOriginalNoNorm(EmphasisOriginal):
50
    name = "No norm"
51
    description = "same as orginal, but without normalization (seems to work better for SDXL)"
52

53
    def after_transformers(self):
54
        self.z = self.z * self.multipliers.reshape(self.multipliers.shape + (1,)).expand(self.z.shape)
55

56

57
def get_current_option(emphasis_option_name):
58
    return next(iter([x for x in options if x.name == emphasis_option_name]), EmphasisOriginal)
59

60

61
def get_options_descriptions():
62
    return ", ".join(f"{x.name}: {x.description}" for x in options)
63

64

65
options = [
66
    EmphasisNone,
67
    EmphasisIgnore,
68
    EmphasisOriginal,
69
    EmphasisOriginalNoNorm,
70
]
71

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.