Spaces:
Sleeping
Sleeping
Download blockdiffusion/models/text_unet16.py from wkea/blockdiffusion-api: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/main/blockdiffusion/models/text_unet16.py
- Command line
-
hf download hf://spaces/wkea/blockdiffusion-api/blockdiffusion/models/text_unet16.py
-
curl -L -o text_unet16.py https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/main/blockdiffusion/models/text_unet16.py
1.86 kB
| 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 | |
| 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) | |