File size: 1,960 Bytes
00ff9d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Types shared between the text path, the denoiser and the sampler.

These are the contracts between modules: the text path produces
``TextConditioning``; the denoiser consumes it and runs one of the
``DFlowPath`` routings per forward.
"""

from __future__ import annotations

from dataclasses import dataclass
from enum import StrEnum
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from torch import Tensor


# Learned suffix tokens appended to every prompt's text tokens.
SUFFIX_TOKEN_COUNT = 3


class DFlowPath(StrEnum):
    """Which trained routing of the 18 middle layers a forward runs.

    FULL: every token through all 30 image layers (the main prediction).
    PATH_DROP: the middle is skipped for every token (the weak pass of
        path-drop guidance).
    THREE_QUARTER: one image token per 2x2 group (and every text token) goes
        through the middle; the rest skip it.
    """

    FULL = "full"
    PATH_DROP = "path_drop"
    THREE_QUARTER = "three_quarter"


@dataclass(frozen=True)
class TextConditioning:
    """Text tokens for a prompt batch, ready for the denoiser's text refiner.

    All prompts in a batch have the same token count ``L`` (the dense
    attention path has no padding); batches of different lengths run as
    separate forwards.

    Attributes:
        tokens: ``[B, L, 2304]`` float32: the summed projected encoder taps
            followed by the learned suffix tokens (or the learned
            unconditional token and the suffix for an empty prompt).
        key_bias: ``[B, L]`` float32 additive attention-logit bias on each
            text token's key (prompt emphasis with weight > 0), or None when no
            token is weighted.
        value_scale: ``[B, L]`` float32 multiplier on each text token's value
            (prompt emphasis with weight <= 0), or None when no token is
            weighted.
    """

    tokens: Tensor
    key_bias: Tensor | None
    value_scale: Tensor | None