File size: 1,863 Bytes
c388140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
from __future__ import annotations

from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from blockdiffusion.models.unet16 import UNet16, UNet16Config
from blockdiffusion.text.simple_tokenizer import SimpleVocab
from blockdiffusion.text.text_encoder import TextEncoder, TextEncoderConfig


@dataclass(frozen=True)
class TextCondConfig:
    """

    文本条件配置(用于 checkpoint/meta 保存)。

    """

    max_text_len: int = 32
    emb_dim: int = 128
    dropout: float = 0.0


class TextCondUNet16(nn.Module):
    """

    文本条件 UNet:

    - TextEncoder 将 tokens/mask 编码为 cond 向量

    - cond 向量注入到 UNet 的 time embedding(加和)

    - tokens/mask 为空或 mask 全 0 时等价于无条件

    """

    def __init__(

        self,

        unet_cfg: UNet16Config,

        vocab: SimpleVocab,

        text_cfg: TextCondConfig,

    ):
        super().__init__()
        self.unet_cfg = unet_cfg
        self.text_cfg = text_cfg

        self.vocab = vocab  # 仅用于保存 meta;不注册为 buffer/module

        te_cfg = TextEncoderConfig(
            vocab_size=vocab.size,
            max_len=text_cfg.max_text_len,
            emb_dim=text_cfg.emb_dim,
            out_dim=unet_cfg.time_emb_dim,
            dropout=text_cfg.dropout,
        )
        self.text_encoder = TextEncoder(te_cfg, pad_id=vocab.pad_id)
        self.unet = UNet16(unet_cfg)

    def forward(

        self,

        x: torch.Tensor,

        t: torch.Tensor,

        tokens: Optional[torch.Tensor] = None,

        mask: Optional[torch.Tensor] = None,

    ) -> torch.Tensor:
        cond = None
        if tokens is not None:
            cond = self.text_encoder(tokens, mask=mask)
        return self.unet(x, t, cond=cond)