stable-diffusion-webui
70 строк · 2.1 Кб
1from __future__ import annotations
2import torch
3
4
5class Emphasis:
6"""Emphasis class decides how to death with (emphasized:1.1) text in prompts"""
7
8name: str = "Base"
9description: str = ""
10
11tokens: list[list[int]]
12"""tokens from the chunk of the prompt"""
13
14multipliers: torch.Tensor
15"""tensor with multipliers, once for each token"""
16
17z: torch.Tensor
18"""output of cond transformers network (CLIP)"""
19
20def 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
23pass
24
25
26class EmphasisNone(Emphasis):
27name = "None"
28description = "disable the mechanism entirely and treat (:.1.1) as literal characters"
29
30
31class EmphasisIgnore(Emphasis):
32name = "Ignore"
33description = "treat all empasised words as if they have no emphasis"
34
35
36class EmphasisOriginal(Emphasis):
37name = "Original"
38description = "the orginal emphasis implementation"
39
40def after_transformers(self):
41original_mean = self.z.mean()
42self.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
45new_mean = self.z.mean()
46self.z = self.z * (original_mean / new_mean)
47
48
49class EmphasisOriginalNoNorm(EmphasisOriginal):
50name = "No norm"
51description = "same as orginal, but without normalization (seems to work better for SDXL)"
52
53def after_transformers(self):
54self.z = self.z * self.multipliers.reshape(self.multipliers.shape + (1,)).expand(self.z.shape)
55
56
57def get_current_option(emphasis_option_name):
58return next(iter([x for x in options if x.name == emphasis_option_name]), EmphasisOriginal)
59
60
61def get_options_descriptions():
62return ", ".join(f"{x.name}: {x.description}" for x in options)
63
64
65options = [
66EmphasisNone,
67EmphasisIgnore,
68EmphasisOriginal,
69EmphasisOriginalNoNorm,
70]
71