UDream commited on
Commit
7fdd78a
·
verified ·
1 Parent(s): 70af4e8

Add model card, config, modeling_udream_bi.py, tokenizer, and safetensors index

Browse files
README.md ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - uk
5
+ - en
6
+ - ru
7
+ - pl
8
+ - cs
9
+ - be
10
+ library_name: transformers
11
+ pipeline_tag: feature-extraction
12
+ base_model: UDream/UDream-1.0-Base
13
+ tags:
14
+ - udream
15
+ - text-encoder
16
+ - bidirectional
17
+ - moe
18
+ - mixture-of-experts
19
+ - ukrainian
20
+ - text-to-image
21
+ - diffusion
22
+ - multilingual
23
+ - custom_code
24
+ ---
25
+
26
+ # UDream 1.0 Bi-Encode (`UDream/UDream-1.0-Bi-Encode`)
27
+
28
+ **UDream 1.0 Bi-Encode** is an Apache 2.0, Ukrainian-first **bidirectional sparse Mixture-of-Experts (MoE) text encoder** (**4.14B total / 1.44B active parameters**, `d_model=2048`, **~8.6 GB VRAM** in `bf16`), adapted from [`UDream/UDream-1.0-Base`](https://huggingface.co/UDream/UDream-1.0-Base) specifically for **text-to-image (DiT / Flow Matching) conditioning** and **bilingual Ukrainian ↔ English semantic representations**.
29
+
30
+ In a controlled 7-encoder downstream text-to-image benchmark (`Photon-Small 433M`, identical 100k MONET EN + 1k Ukrainian signage dataset, 8,000 steps), `UDream-1.0-Bi-Encode`:
31
+ - **Outperformed all tested Apache 2.0 / causal 4B encoders** (`Qwen3.5-4B-Base`, `Gemma-4-E4B` Base, `Gemma-4-E4B-it`, causal `UDream-1.0-Base`, `UDream-1.0-SFT`) even on **held-out pure English validation** (`val_cond`, `cond_gain`, `cond_gain85`),
32
+ - Achieved **#1 overall zero-shot Ukrainian → English T2I transfer (`ua_en_img_cos = 0.8245`)** across all 7 tested models (including `T5Gemma-2-4B`),
33
+ - Eliminated channel outliers to **`top1_energy = 2.60%`** (`penult`) / **`3.99%`** (`last`) with **`100.0%` raw (un-normalized) EN ↔ UA Top-1 retrieval** and **25% fewer Ukrainian tokens** than Gemma 4 / Qwen 3.5.
34
+
35
+ ---
36
+
37
+ ## Architecture & Adaptation Recipe
38
+
39
+ | Property | Specification |
40
+ |---|---|
41
+ | Base Checkpoint | [`UDream/UDream-1.0-Base`](https://huggingface.co/UDream/UDream-1.0-Base) (pretrained on 63.73B tokens) |
42
+ | Architecture | Encoder-only Bidirectional Sparse MoE (`UDreamBiEncoderModel`, `is_causal=False`) |
43
+ | Parameters (Total / Active) | **4.14B / 1.44B** backbone (`d_model=2048`, 26 layers: 4 dense + 22 MoE, 32 routed `top_k=6` + 1 shared) |
44
+ | Hybrid Bidirectional Attention | 22 local sliding-window layers (`window=512`, RoPE `10k`) + 4 global **NoPE** layers (`5, 11, 17, 23`) |
45
+ | Inference Footprint | **~8.6 GB VRAM** (`bf16`), `~1.8 ms/prompt` (RTX 5090), exact padding invariance (`fp32 cos=1.000000`) |
46
+ | Tokenizer | `udream-64k` ByteLevel BPE (`vocab_size=64,128`, ~20.9 tok/prompt UA vs 26.1 Gemma 4 / 27.4 Qwen 3.5) |
47
+ | Bidirectional Adaptation | 3,000 steps on 1× RTX 5090 (~1.7h): **744M** full-rank FP32 params (all 26 Attention, 4 Dense FFN, 22 Shared experts, 22 MoE routers, RMSNorm/FiLM); **3.46B** routed experts + token embeddings frozen in `bf16` to preserve world & Ukrainian knowledge |
48
+ | Adaptation Objectives | Joint **Span-MLM (65%) + MNTP (35%)** (25% geometric spans 1..4; 49.2M tokens across EN prose, UA prose, and visual captions) + **Symmetric EN ↔ UA InfoNCE** (144k parallel pairs on both `(-4,-3,-2)` and `-1`) |
49
+
50
+ ---
51
+
52
+ ## Quickstart (`transformers`)
53
+
54
+ Pass `trust_remote_code=True`. During training, every prompt starts with `<doc>` (id `4`) at position 0, which is stripped on output so 100% of prompt words are kept. The built-in `.encode()` helper handles this automatically.
55
+
56
+ ### 1. Token-level states for Text-to-Image (DiT / Flow Matching)
57
+
58
+ ```python
59
+ import torch
60
+ from transformers import AutoModel, AutoTokenizer
61
+
62
+ repo_id = "UDream/UDream-1.0-Bi-Encode"
63
+ tok = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
64
+ enc = AutoModel.from_pretrained(
65
+ repo_id,
66
+ trust_remote_code=True,
67
+ dtype=torch.bfloat16,
68
+ device_map="cuda",
69
+ ).eval()
70
+
71
+ prompts = [
72
+ "a cozy wooden cabin in a snowy pine forest at golden hour, film grain",
73
+ "затишна дерев'яна хатина в засніженому сосновому лісі на заході сонця, зерно плівки",
74
+ ]
75
+
76
+ # Recommended for T2I DiT conditioning: average of final-normed layers (-4, -3, -2)
77
+ hidden, mask = enc.encode(prompts, tok, max_length=128, layers=(-4, -3, -2))
78
+ print(hidden.shape, mask.shape) # torch.Size([2, L, 2048]) torch.Size([2, L])
79
+ ```
80
+
81
+ ### 2. Bilingual EN ↔ UA Semantic Embeddings
82
+
83
+ ```python
84
+ emb = enc.encode(prompts, tok, max_length=128, layers=(-1,), pool="mean")
85
+ print("EN <-> UA cosine similarity:", float((emb[0] * emb[1]).sum()))
86
+ ```
87
+
88
+ ### 3. Standard Hugging Face `forward()` API
89
+
90
+ ```python
91
+ ids = tok("<doc>" + prompts[0], return_tensors="pt", add_special_tokens=False).to(enc.device)
92
+ with torch.inference_mode():
93
+ out = enc(**ids, output_hidden_states=True)
94
+ # out.last_hidden_state[:, 1:, :] -> [1, L, 2048] (stripping <doc>)
95
+ ```
96
+
97
+ ---
98
+
99
+ ## Benchmarks
100
+
101
+ ### 1. Text Encoder X-Ray (16 parallel EN ↔ UA visual scene pairs, `bf16`)
102
+
103
+ | Encoder | License | `dim` | VRAM (GB) | UA tok/prompt | `top1_energy` ↓ | `raw_ua_en_top1` ↑ | `z_ua_en_pair_cos` ↑ | `z_ua_en_margin` ↑ | `z_ua_en_top1` ↑ |
104
+ |---|---|---:|---:|---:|---:|---:|---:|---:|---:|
105
+ | **`UDream-1.0-Bi-Encode` (`-4,-3,-2`)** | **Apache 2.0** | 2048 | **8.60** | **20.9** | **2.60%** | **100.0%** | **+0.7095** | **+0.7767** | **100.0%** |
106
+ | **`UDream-1.0-Bi-Encode` (`-1`)** | **Apache 2.0** | 2048 | **8.60** | **20.9** | **3.99%** | **100.0%** | **+0.7242** | **+0.7880** | **100.0%** |
107
+ | `UDream-1.0-Base` (causal) | Apache 2.0 | 2048 | 8.60 | 20.9 | 7.61% | 93.8% | +0.4783 | +0.5919 | 100.0% |
108
+ | `T5Gemma-2-4B` (`-1`, bidirectional) | Gemma ToU | 2560 | 8.17 | 26.1 | 34.46% | 96.9% | +0.3075 | +0.5230 | 100.0% |
109
+ | `Qwen3.5-4B-Base` | Apache 2.0 | 2560 | 9.89 | 27.4 | 9.31% | 56.3% | +0.0010 | +0.3150 | 100.0% |
110
+ | `Gemma-4-E2B` (Base) | Apache 2.0 | 1536 | 9.62 | 26.1 | 55.55% | 28.1% | -0.0772 | +0.2710 | 100.0% |
111
+ | `Gemma-4-E4B` (Base) | Apache 2.0 | 2560 | 15.44 | 26.1 | 13.70% | 21.9% | -0.2542 | +0.2234 | 100.0% |
112
+ | `Qwen3-4B` | Apache 2.0 | 2560 | 8.65 | 36.8 | 87.20% | 6.3% | — | +0.2180 | 75.0% |
113
+ | `Gemma-4-E4B-it` | Apache 2.0 | 2560 | 15.43 | 26.1 | 89.95% | 21.9% | -0.2130 | +0.1041 | 53.1% |
114
+
115
+ ### 2. Downstream Text-to-Image DiT Shootout (8,000 steps, `Photon-Small 433M`)
116
+
117
+ All 7 frozen encoders trained identical `Photon-Small (433M)` DiT models (`bs=64`, `lr=2e-4`, `SPRINT keep=0.25`, `seed=0`) on the exact same 100k EN MONET + 1k UA signage dataset, evaluated on a strictly held-out EN MONET validation chunk (`val_cond`, `cond_gain = val_uncond - val_cond`, high-noise `t=0.85` `cond_gain85`) and zero-shot Ukrainian ↔ English image latent cosine similarity (`ua_en_img_cos`):
118
+
119
+ | Rank | Frozen Text Encoder | Attention / License | `val_cond` ↓ (EN) | `cond_gain` ↑ (EN) | `val_cond85` ↓ | `cond_gain85` ↑ | Zero-shot UA `early` / `best` ↑ |
120
+ |---:|---|---|---:|---:|---:|---:|---:|
121
+ | 1 | `T5Gemma-2-4B` (`-1`) | Bidirectional / Gemma ToU | **0.83375** | **+0.00720** | **0.77627** | **+0.00685** | 0.699 / 0.741 |
122
+ | **2 (#1 Apache 2.0)** | **`UDream-1.0-Bi-Encode` (`-4,-3,-2`)** | **Bidirectional / Apache 2.0** | **0.83502** | **+0.00546** | **0.77795** | **+0.00516** | **0.814 / 0.825** |
123
+ | 3 | `Qwen3.5-4B-Base` | Causal hybrid / Apache 2.0 | 0.83522 | +0.00532 | 0.77800 | +0.00465 | 0.660 / 0.705 |
124
+ | 4 | `Gemma-4-E4B` (Base) | Causal / Apache 2.0 | 0.83578 | +0.00490 | 0.77862 | +0.00479 | 0.359 / 0.422 |
125
+ | 5 | `UDream-1.0-SFT` | Causal / Apache 2.0 | 0.83591 | +0.00487 | 0.77839 | +0.00452 | 0.720 / 0.753 |
126
+ | 6 | `UDream-1.0-Base` | Causal / Apache 2.0 | 0.83614 | +0.00490 | 0.77845 | +0.00469 | 0.804 / 0.817 |
127
+ | 7 | `Gemma-4-E4B-it` | Causal / Apache 2.0 | 0.83629 | +0.00381 | 0.77881 | +0.00346 | 0.578 / 0.626 |
128
+
129
+ ---
130
+
131
+ ## Limitations
132
+
133
+ - **Encoder-only:** `UDream-1.0-Bi-Encode` removes the LM head and uses bidirectional attention (`is_causal=False`); it is not meant for autoregressive text generation (use [`UDream/UDream-1.0-Base`](https://huggingface.co/UDream/UDream-1.0-Base) for generation).
134
+ - **Optimal sequence length:** Adaptation was performed at `seq_len=256` (within the 512-token local sliding window) targeting T2I prompts and sentence/paragraph embeddings; long-document retrieval beyond 512 tokens has not been benchmarked.
135
+ - **MoE `bf16` rounding:** Because 22 layers use discrete `top_k=6/32` MoE routing, `bf16` batch-size GEMM rounding can occasionally flip a borderline expert (`cos ≈ 0.997` between single and padded batch; `fp32` is exact at `cos = 1.000000`, `max_diff = 5e-5`).
136
+
137
+ ---
138
+
139
+ ## Citation
140
+
141
+ ```bibtex
142
+ @misc{dormidontov2026udream10bi,
143
+ title = {UDream 1.0 Bi-Encode: A Bidirectional Ukrainian-First Sparse MoE Text Encoder for Diffusion and Cross-Lingual Representations},
144
+ author = {Dormidontov, Oleksandr},
145
+ year = {2026},
146
+ url = {https://udream.com.ua}
147
+ }
148
+ ```
config.json ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "UDreamBiEncoderModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_udream_bi.UDreamBiEncoderConfig",
7
+ "AutoModel": "modeling_udream_bi.UDreamBiEncoderModel"
8
+ },
9
+ "bidirectional": true,
10
+ "bos_token_id": 1,
11
+ "cond_dim": 256,
12
+ "d_model": 2048,
13
+ "default_t2i_layers": [
14
+ -4,
15
+ -3,
16
+ -2
17
+ ],
18
+ "dense_d_ff": 5632,
19
+ "doc_token_id": 4,
20
+ "dropout": 0.0,
21
+ "dtype": "bfloat16",
22
+ "eos_token_id": 2,
23
+ "expert_d_ff": 768,
24
+ "global_every": 6,
25
+ "head_dim": 128,
26
+ "hidden_size": 2048,
27
+ "hormone_dropout": 0.1,
28
+ "init_std": 0.02,
29
+ "is_decoder": false,
30
+ "max_position_embeddings": 4096,
31
+ "model_type": "udream_bi_encoder",
32
+ "moe_top_k": 6,
33
+ "n_dense_first": 2,
34
+ "n_dense_last": 2,
35
+ "n_experts": 32,
36
+ "n_heads": 16,
37
+ "n_hormones": 8,
38
+ "n_kv_heads": 4,
39
+ "n_layers": 26,
40
+ "num_attention_heads": 16,
41
+ "num_hidden_layers": 26,
42
+ "num_key_value_heads": 4,
43
+ "pad_token_id": 0,
44
+ "rms_eps": 1e-05,
45
+ "rope_theta": 10000.0,
46
+ "shared_d_ff": 2048,
47
+ "transformers_version": "5.24.1",
48
+ "unk_token_id": 3,
49
+ "vocab_size": 64128,
50
+ "window": 512
51
+ }
configuration_udream_bi.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ """UDream 1.0 Bidirectional Text Encoder configuration for Hugging Face Transformers."""
3
+ from transformers import PretrainedConfig
4
+
5
+
6
+ class UDreamBiEncoderConfig(PretrainedConfig):
7
+ """Configuration class for UDream-1.0-Bi-Encode (Bidirectional Sparse MoE Text Encoder)."""
8
+
9
+ model_type = "udream_bi_encoder"
10
+
11
+ def __init__(
12
+ self,
13
+ vocab_size: int = 64128,
14
+ d_model: int = 2048,
15
+ n_layers: int = 26,
16
+ n_heads: int = 16,
17
+ n_kv_heads: int = 4,
18
+ head_dim: int = 128,
19
+ global_every: int = 6,
20
+ window: int = 512,
21
+ rope_theta: float = 10000.0,
22
+ dense_d_ff: int = 5632,
23
+ n_dense_first: int = 2,
24
+ n_dense_last: int = 2,
25
+ n_experts: int = 32,
26
+ expert_d_ff: int = 768,
27
+ moe_top_k: int = 6,
28
+ shared_d_ff: int = 2048,
29
+ n_hormones: int = 8,
30
+ cond_dim: int = 256,
31
+ hormone_dropout: float = 0.1,
32
+ dropout: float = 0.0,
33
+ rms_eps: float = 1e-5,
34
+ init_std: float = 0.02,
35
+ max_position_embeddings: int = 4096,
36
+ bidirectional: bool = True,
37
+ doc_token_id: int = 4,
38
+ bos_token_id: int = 1,
39
+ eos_token_id: int = 2,
40
+ pad_token_id: int = 0,
41
+ unk_token_id: int = 3,
42
+ default_t2i_layers: tuple = (-4, -3, -2),
43
+ **kwargs,
44
+ ):
45
+ if "top_k" in kwargs:
46
+ moe_top_k = kwargs.pop("top_k")
47
+ self.vocab_size = vocab_size
48
+ self.d_model = d_model
49
+ self.n_layers = n_layers
50
+ self.n_heads = n_heads
51
+ self.n_kv_heads = n_kv_heads
52
+ self.head_dim = head_dim
53
+ self.global_every = global_every
54
+ self.window = window
55
+ self.rope_theta = rope_theta
56
+ self.dense_d_ff = dense_d_ff
57
+ self.n_dense_first = n_dense_first
58
+ self.n_dense_last = n_dense_last
59
+ self.n_experts = n_experts
60
+ self.expert_d_ff = expert_d_ff
61
+ self.moe_top_k = moe_top_k
62
+ self.shared_d_ff = shared_d_ff
63
+ self.n_hormones = n_hormones
64
+ self.cond_dim = cond_dim
65
+ self.hormone_dropout = hormone_dropout
66
+ self.dropout = dropout
67
+ self.rms_eps = rms_eps
68
+ self.init_std = init_std
69
+ self.max_position_embeddings = max_position_embeddings
70
+ self.bidirectional = bidirectional
71
+ self.doc_token_id = doc_token_id
72
+ self.default_t2i_layers = list(default_t2i_layers)
73
+
74
+ # Standard HF aliases
75
+ self.hidden_size = d_model
76
+ self.num_hidden_layers = n_layers
77
+ self.num_attention_heads = n_heads
78
+ self.num_key_value_heads = n_kv_heads
79
+ self.is_decoder = False
80
+
81
+ super().__init__(
82
+ bos_token_id=bos_token_id,
83
+ eos_token_id=eos_token_id,
84
+ pad_token_id=pad_token_id,
85
+ unk_token_id=unk_token_id,
86
+ **kwargs,
87
+ )
88
+
89
+ def is_global(self, layer: int) -> bool:
90
+ """Every `global_every`-th layer (1-indexed: 6, 12, 18, 24 -> 0-idx 5, 11, 17, 23) is NoPE global."""
91
+ return (layer + 1) % self.global_every == 0
92
+
93
+ def is_moe(self, layer: int) -> bool:
94
+ return self.n_dense_first <= layer < self.n_layers - self.n_dense_last
model.safetensors.index.json ADDED
@@ -0,0 +1,463 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_parameters": 4197420040,
4
+ "total_size": 8394840080
5
+ },
6
+ "weight_map": {
7
+ "blocks.0.attn.k.weight": "model-00001-of-00002.safetensors",
8
+ "blocks.0.attn.kn.w": "model-00001-of-00002.safetensors",
9
+ "blocks.0.attn.o.weight": "model-00001-of-00002.safetensors",
10
+ "blocks.0.attn.q.weight": "model-00001-of-00002.safetensors",
11
+ "blocks.0.attn.qn.w": "model-00001-of-00002.safetensors",
12
+ "blocks.0.attn.v.weight": "model-00001-of-00002.safetensors",
13
+ "blocks.0.ffn.down.weight": "model-00001-of-00002.safetensors",
14
+ "blocks.0.ffn.gate.weight": "model-00001-of-00002.safetensors",
15
+ "blocks.0.ffn.up.weight": "model-00001-of-00002.safetensors",
16
+ "blocks.0.film1.proj.weight": "model-00001-of-00002.safetensors",
17
+ "blocks.0.film2.proj.weight": "model-00001-of-00002.safetensors",
18
+ "blocks.0.n1.w": "model-00001-of-00002.safetensors",
19
+ "blocks.0.n2.w": "model-00001-of-00002.safetensors",
20
+ "blocks.1.attn.k.weight": "model-00001-of-00002.safetensors",
21
+ "blocks.1.attn.kn.w": "model-00001-of-00002.safetensors",
22
+ "blocks.1.attn.o.weight": "model-00001-of-00002.safetensors",
23
+ "blocks.1.attn.q.weight": "model-00001-of-00002.safetensors",
24
+ "blocks.1.attn.qn.w": "model-00001-of-00002.safetensors",
25
+ "blocks.1.attn.v.weight": "model-00001-of-00002.safetensors",
26
+ "blocks.1.ffn.down.weight": "model-00001-of-00002.safetensors",
27
+ "blocks.1.ffn.gate.weight": "model-00001-of-00002.safetensors",
28
+ "blocks.1.ffn.up.weight": "model-00001-of-00002.safetensors",
29
+ "blocks.1.film1.proj.weight": "model-00001-of-00002.safetensors",
30
+ "blocks.1.film2.proj.weight": "model-00001-of-00002.safetensors",
31
+ "blocks.1.n1.w": "model-00001-of-00002.safetensors",
32
+ "blocks.1.n2.w": "model-00001-of-00002.safetensors",
33
+ "blocks.10.attn.k.weight": "model-00001-of-00002.safetensors",
34
+ "blocks.10.attn.kn.w": "model-00001-of-00002.safetensors",
35
+ "blocks.10.attn.o.weight": "model-00001-of-00002.safetensors",
36
+ "blocks.10.attn.q.weight": "model-00001-of-00002.safetensors",
37
+ "blocks.10.attn.qn.w": "model-00001-of-00002.safetensors",
38
+ "blocks.10.attn.v.weight": "model-00001-of-00002.safetensors",
39
+ "blocks.10.ffn.h_router.weight": "model-00001-of-00002.safetensors",
40
+ "blocks.10.ffn.router.weight": "model-00001-of-00002.safetensors",
41
+ "blocks.10.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
42
+ "blocks.10.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
43
+ "blocks.10.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
44
+ "blocks.10.ffn.w_down": "model-00001-of-00002.safetensors",
45
+ "blocks.10.ffn.w_gate": "model-00001-of-00002.safetensors",
46
+ "blocks.10.ffn.w_up": "model-00001-of-00002.safetensors",
47
+ "blocks.10.film1.proj.weight": "model-00001-of-00002.safetensors",
48
+ "blocks.10.film2.proj.weight": "model-00001-of-00002.safetensors",
49
+ "blocks.10.n1.w": "model-00001-of-00002.safetensors",
50
+ "blocks.10.n2.w": "model-00001-of-00002.safetensors",
51
+ "blocks.11.attn.k.weight": "model-00001-of-00002.safetensors",
52
+ "blocks.11.attn.kn.w": "model-00001-of-00002.safetensors",
53
+ "blocks.11.attn.o.weight": "model-00001-of-00002.safetensors",
54
+ "blocks.11.attn.q.weight": "model-00001-of-00002.safetensors",
55
+ "blocks.11.attn.qn.w": "model-00001-of-00002.safetensors",
56
+ "blocks.11.attn.v.weight": "model-00001-of-00002.safetensors",
57
+ "blocks.11.ffn.h_router.weight": "model-00001-of-00002.safetensors",
58
+ "blocks.11.ffn.router.weight": "model-00001-of-00002.safetensors",
59
+ "blocks.11.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
60
+ "blocks.11.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
61
+ "blocks.11.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
62
+ "blocks.11.ffn.w_down": "model-00001-of-00002.safetensors",
63
+ "blocks.11.ffn.w_gate": "model-00001-of-00002.safetensors",
64
+ "blocks.11.ffn.w_up": "model-00001-of-00002.safetensors",
65
+ "blocks.11.film1.proj.weight": "model-00001-of-00002.safetensors",
66
+ "blocks.11.film2.proj.weight": "model-00001-of-00002.safetensors",
67
+ "blocks.11.n1.w": "model-00001-of-00002.safetensors",
68
+ "blocks.11.n2.w": "model-00001-of-00002.safetensors",
69
+ "blocks.12.attn.k.weight": "model-00001-of-00002.safetensors",
70
+ "blocks.12.attn.kn.w": "model-00001-of-00002.safetensors",
71
+ "blocks.12.attn.o.weight": "model-00001-of-00002.safetensors",
72
+ "blocks.12.attn.q.weight": "model-00001-of-00002.safetensors",
73
+ "blocks.12.attn.qn.w": "model-00001-of-00002.safetensors",
74
+ "blocks.12.attn.v.weight": "model-00001-of-00002.safetensors",
75
+ "blocks.12.ffn.h_router.weight": "model-00001-of-00002.safetensors",
76
+ "blocks.12.ffn.router.weight": "model-00001-of-00002.safetensors",
77
+ "blocks.12.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
78
+ "blocks.12.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
79
+ "blocks.12.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
80
+ "blocks.12.ffn.w_down": "model-00001-of-00002.safetensors",
81
+ "blocks.12.ffn.w_gate": "model-00001-of-00002.safetensors",
82
+ "blocks.12.ffn.w_up": "model-00001-of-00002.safetensors",
83
+ "blocks.12.film1.proj.weight": "model-00001-of-00002.safetensors",
84
+ "blocks.12.film2.proj.weight": "model-00001-of-00002.safetensors",
85
+ "blocks.12.n1.w": "model-00001-of-00002.safetensors",
86
+ "blocks.12.n2.w": "model-00001-of-00002.safetensors",
87
+ "blocks.13.attn.k.weight": "model-00001-of-00002.safetensors",
88
+ "blocks.13.attn.kn.w": "model-00001-of-00002.safetensors",
89
+ "blocks.13.attn.o.weight": "model-00001-of-00002.safetensors",
90
+ "blocks.13.attn.q.weight": "model-00001-of-00002.safetensors",
91
+ "blocks.13.attn.qn.w": "model-00001-of-00002.safetensors",
92
+ "blocks.13.attn.v.weight": "model-00001-of-00002.safetensors",
93
+ "blocks.13.ffn.h_router.weight": "model-00002-of-00002.safetensors",
94
+ "blocks.13.ffn.router.weight": "model-00002-of-00002.safetensors",
95
+ "blocks.13.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
96
+ "blocks.13.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
97
+ "blocks.13.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
98
+ "blocks.13.ffn.w_down": "model-00002-of-00002.safetensors",
99
+ "blocks.13.ffn.w_gate": "model-00002-of-00002.safetensors",
100
+ "blocks.13.ffn.w_up": "model-00001-of-00002.safetensors",
101
+ "blocks.13.film1.proj.weight": "model-00002-of-00002.safetensors",
102
+ "blocks.13.film2.proj.weight": "model-00002-of-00002.safetensors",
103
+ "blocks.13.n1.w": "model-00001-of-00002.safetensors",
104
+ "blocks.13.n2.w": "model-00001-of-00002.safetensors",
105
+ "blocks.14.attn.k.weight": "model-00002-of-00002.safetensors",
106
+ "blocks.14.attn.kn.w": "model-00002-of-00002.safetensors",
107
+ "blocks.14.attn.o.weight": "model-00002-of-00002.safetensors",
108
+ "blocks.14.attn.q.weight": "model-00002-of-00002.safetensors",
109
+ "blocks.14.attn.qn.w": "model-00002-of-00002.safetensors",
110
+ "blocks.14.attn.v.weight": "model-00002-of-00002.safetensors",
111
+ "blocks.14.ffn.h_router.weight": "model-00002-of-00002.safetensors",
112
+ "blocks.14.ffn.router.weight": "model-00002-of-00002.safetensors",
113
+ "blocks.14.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
114
+ "blocks.14.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
115
+ "blocks.14.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
116
+ "blocks.14.ffn.w_down": "model-00002-of-00002.safetensors",
117
+ "blocks.14.ffn.w_gate": "model-00002-of-00002.safetensors",
118
+ "blocks.14.ffn.w_up": "model-00002-of-00002.safetensors",
119
+ "blocks.14.film1.proj.weight": "model-00002-of-00002.safetensors",
120
+ "blocks.14.film2.proj.weight": "model-00002-of-00002.safetensors",
121
+ "blocks.14.n1.w": "model-00002-of-00002.safetensors",
122
+ "blocks.14.n2.w": "model-00002-of-00002.safetensors",
123
+ "blocks.15.attn.k.weight": "model-00002-of-00002.safetensors",
124
+ "blocks.15.attn.kn.w": "model-00002-of-00002.safetensors",
125
+ "blocks.15.attn.o.weight": "model-00002-of-00002.safetensors",
126
+ "blocks.15.attn.q.weight": "model-00002-of-00002.safetensors",
127
+ "blocks.15.attn.qn.w": "model-00002-of-00002.safetensors",
128
+ "blocks.15.attn.v.weight": "model-00002-of-00002.safetensors",
129
+ "blocks.15.ffn.h_router.weight": "model-00002-of-00002.safetensors",
130
+ "blocks.15.ffn.router.weight": "model-00002-of-00002.safetensors",
131
+ "blocks.15.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
132
+ "blocks.15.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
133
+ "blocks.15.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
134
+ "blocks.15.ffn.w_down": "model-00002-of-00002.safetensors",
135
+ "blocks.15.ffn.w_gate": "model-00002-of-00002.safetensors",
136
+ "blocks.15.ffn.w_up": "model-00002-of-00002.safetensors",
137
+ "blocks.15.film1.proj.weight": "model-00002-of-00002.safetensors",
138
+ "blocks.15.film2.proj.weight": "model-00002-of-00002.safetensors",
139
+ "blocks.15.n1.w": "model-00002-of-00002.safetensors",
140
+ "blocks.15.n2.w": "model-00002-of-00002.safetensors",
141
+ "blocks.16.attn.k.weight": "model-00002-of-00002.safetensors",
142
+ "blocks.16.attn.kn.w": "model-00002-of-00002.safetensors",
143
+ "blocks.16.attn.o.weight": "model-00002-of-00002.safetensors",
144
+ "blocks.16.attn.q.weight": "model-00002-of-00002.safetensors",
145
+ "blocks.16.attn.qn.w": "model-00002-of-00002.safetensors",
146
+ "blocks.16.attn.v.weight": "model-00002-of-00002.safetensors",
147
+ "blocks.16.ffn.h_router.weight": "model-00002-of-00002.safetensors",
148
+ "blocks.16.ffn.router.weight": "model-00002-of-00002.safetensors",
149
+ "blocks.16.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
150
+ "blocks.16.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
151
+ "blocks.16.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
152
+ "blocks.16.ffn.w_down": "model-00002-of-00002.safetensors",
153
+ "blocks.16.ffn.w_gate": "model-00002-of-00002.safetensors",
154
+ "blocks.16.ffn.w_up": "model-00002-of-00002.safetensors",
155
+ "blocks.16.film1.proj.weight": "model-00002-of-00002.safetensors",
156
+ "blocks.16.film2.proj.weight": "model-00002-of-00002.safetensors",
157
+ "blocks.16.n1.w": "model-00002-of-00002.safetensors",
158
+ "blocks.16.n2.w": "model-00002-of-00002.safetensors",
159
+ "blocks.17.attn.k.weight": "model-00002-of-00002.safetensors",
160
+ "blocks.17.attn.kn.w": "model-00002-of-00002.safetensors",
161
+ "blocks.17.attn.o.weight": "model-00002-of-00002.safetensors",
162
+ "blocks.17.attn.q.weight": "model-00002-of-00002.safetensors",
163
+ "blocks.17.attn.qn.w": "model-00002-of-00002.safetensors",
164
+ "blocks.17.attn.v.weight": "model-00002-of-00002.safetensors",
165
+ "blocks.17.ffn.h_router.weight": "model-00002-of-00002.safetensors",
166
+ "blocks.17.ffn.router.weight": "model-00002-of-00002.safetensors",
167
+ "blocks.17.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
168
+ "blocks.17.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
169
+ "blocks.17.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
170
+ "blocks.17.ffn.w_down": "model-00002-of-00002.safetensors",
171
+ "blocks.17.ffn.w_gate": "model-00002-of-00002.safetensors",
172
+ "blocks.17.ffn.w_up": "model-00002-of-00002.safetensors",
173
+ "blocks.17.film1.proj.weight": "model-00002-of-00002.safetensors",
174
+ "blocks.17.film2.proj.weight": "model-00002-of-00002.safetensors",
175
+ "blocks.17.n1.w": "model-00002-of-00002.safetensors",
176
+ "blocks.17.n2.w": "model-00002-of-00002.safetensors",
177
+ "blocks.18.attn.k.weight": "model-00002-of-00002.safetensors",
178
+ "blocks.18.attn.kn.w": "model-00002-of-00002.safetensors",
179
+ "blocks.18.attn.o.weight": "model-00002-of-00002.safetensors",
180
+ "blocks.18.attn.q.weight": "model-00002-of-00002.safetensors",
181
+ "blocks.18.attn.qn.w": "model-00002-of-00002.safetensors",
182
+ "blocks.18.attn.v.weight": "model-00002-of-00002.safetensors",
183
+ "blocks.18.ffn.h_router.weight": "model-00002-of-00002.safetensors",
184
+ "blocks.18.ffn.router.weight": "model-00002-of-00002.safetensors",
185
+ "blocks.18.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
186
+ "blocks.18.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
187
+ "blocks.18.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
188
+ "blocks.18.ffn.w_down": "model-00002-of-00002.safetensors",
189
+ "blocks.18.ffn.w_gate": "model-00002-of-00002.safetensors",
190
+ "blocks.18.ffn.w_up": "model-00002-of-00002.safetensors",
191
+ "blocks.18.film1.proj.weight": "model-00002-of-00002.safetensors",
192
+ "blocks.18.film2.proj.weight": "model-00002-of-00002.safetensors",
193
+ "blocks.18.n1.w": "model-00002-of-00002.safetensors",
194
+ "blocks.18.n2.w": "model-00002-of-00002.safetensors",
195
+ "blocks.19.attn.k.weight": "model-00002-of-00002.safetensors",
196
+ "blocks.19.attn.kn.w": "model-00002-of-00002.safetensors",
197
+ "blocks.19.attn.o.weight": "model-00002-of-00002.safetensors",
198
+ "blocks.19.attn.q.weight": "model-00002-of-00002.safetensors",
199
+ "blocks.19.attn.qn.w": "model-00002-of-00002.safetensors",
200
+ "blocks.19.attn.v.weight": "model-00002-of-00002.safetensors",
201
+ "blocks.19.ffn.h_router.weight": "model-00002-of-00002.safetensors",
202
+ "blocks.19.ffn.router.weight": "model-00002-of-00002.safetensors",
203
+ "blocks.19.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
204
+ "blocks.19.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
205
+ "blocks.19.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
206
+ "blocks.19.ffn.w_down": "model-00002-of-00002.safetensors",
207
+ "blocks.19.ffn.w_gate": "model-00002-of-00002.safetensors",
208
+ "blocks.19.ffn.w_up": "model-00002-of-00002.safetensors",
209
+ "blocks.19.film1.proj.weight": "model-00002-of-00002.safetensors",
210
+ "blocks.19.film2.proj.weight": "model-00002-of-00002.safetensors",
211
+ "blocks.19.n1.w": "model-00002-of-00002.safetensors",
212
+ "blocks.19.n2.w": "model-00002-of-00002.safetensors",
213
+ "blocks.2.attn.k.weight": "model-00001-of-00002.safetensors",
214
+ "blocks.2.attn.kn.w": "model-00001-of-00002.safetensors",
215
+ "blocks.2.attn.o.weight": "model-00001-of-00002.safetensors",
216
+ "blocks.2.attn.q.weight": "model-00001-of-00002.safetensors",
217
+ "blocks.2.attn.qn.w": "model-00001-of-00002.safetensors",
218
+ "blocks.2.attn.v.weight": "model-00001-of-00002.safetensors",
219
+ "blocks.2.ffn.h_router.weight": "model-00001-of-00002.safetensors",
220
+ "blocks.2.ffn.router.weight": "model-00001-of-00002.safetensors",
221
+ "blocks.2.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
222
+ "blocks.2.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
223
+ "blocks.2.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
224
+ "blocks.2.ffn.w_down": "model-00001-of-00002.safetensors",
225
+ "blocks.2.ffn.w_gate": "model-00001-of-00002.safetensors",
226
+ "blocks.2.ffn.w_up": "model-00001-of-00002.safetensors",
227
+ "blocks.2.film1.proj.weight": "model-00001-of-00002.safetensors",
228
+ "blocks.2.film2.proj.weight": "model-00001-of-00002.safetensors",
229
+ "blocks.2.n1.w": "model-00001-of-00002.safetensors",
230
+ "blocks.2.n2.w": "model-00001-of-00002.safetensors",
231
+ "blocks.20.attn.k.weight": "model-00002-of-00002.safetensors",
232
+ "blocks.20.attn.kn.w": "model-00002-of-00002.safetensors",
233
+ "blocks.20.attn.o.weight": "model-00002-of-00002.safetensors",
234
+ "blocks.20.attn.q.weight": "model-00002-of-00002.safetensors",
235
+ "blocks.20.attn.qn.w": "model-00002-of-00002.safetensors",
236
+ "blocks.20.attn.v.weight": "model-00002-of-00002.safetensors",
237
+ "blocks.20.ffn.h_router.weight": "model-00002-of-00002.safetensors",
238
+ "blocks.20.ffn.router.weight": "model-00002-of-00002.safetensors",
239
+ "blocks.20.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
240
+ "blocks.20.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
241
+ "blocks.20.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
242
+ "blocks.20.ffn.w_down": "model-00002-of-00002.safetensors",
243
+ "blocks.20.ffn.w_gate": "model-00002-of-00002.safetensors",
244
+ "blocks.20.ffn.w_up": "model-00002-of-00002.safetensors",
245
+ "blocks.20.film1.proj.weight": "model-00002-of-00002.safetensors",
246
+ "blocks.20.film2.proj.weight": "model-00002-of-00002.safetensors",
247
+ "blocks.20.n1.w": "model-00002-of-00002.safetensors",
248
+ "blocks.20.n2.w": "model-00002-of-00002.safetensors",
249
+ "blocks.21.attn.k.weight": "model-00002-of-00002.safetensors",
250
+ "blocks.21.attn.kn.w": "model-00002-of-00002.safetensors",
251
+ "blocks.21.attn.o.weight": "model-00002-of-00002.safetensors",
252
+ "blocks.21.attn.q.weight": "model-00002-of-00002.safetensors",
253
+ "blocks.21.attn.qn.w": "model-00002-of-00002.safetensors",
254
+ "blocks.21.attn.v.weight": "model-00002-of-00002.safetensors",
255
+ "blocks.21.ffn.h_router.weight": "model-00002-of-00002.safetensors",
256
+ "blocks.21.ffn.router.weight": "model-00002-of-00002.safetensors",
257
+ "blocks.21.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
258
+ "blocks.21.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
259
+ "blocks.21.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
260
+ "blocks.21.ffn.w_down": "model-00002-of-00002.safetensors",
261
+ "blocks.21.ffn.w_gate": "model-00002-of-00002.safetensors",
262
+ "blocks.21.ffn.w_up": "model-00002-of-00002.safetensors",
263
+ "blocks.21.film1.proj.weight": "model-00002-of-00002.safetensors",
264
+ "blocks.21.film2.proj.weight": "model-00002-of-00002.safetensors",
265
+ "blocks.21.n1.w": "model-00002-of-00002.safetensors",
266
+ "blocks.21.n2.w": "model-00002-of-00002.safetensors",
267
+ "blocks.22.attn.k.weight": "model-00002-of-00002.safetensors",
268
+ "blocks.22.attn.kn.w": "model-00002-of-00002.safetensors",
269
+ "blocks.22.attn.o.weight": "model-00002-of-00002.safetensors",
270
+ "blocks.22.attn.q.weight": "model-00002-of-00002.safetensors",
271
+ "blocks.22.attn.qn.w": "model-00002-of-00002.safetensors",
272
+ "blocks.22.attn.v.weight": "model-00002-of-00002.safetensors",
273
+ "blocks.22.ffn.h_router.weight": "model-00002-of-00002.safetensors",
274
+ "blocks.22.ffn.router.weight": "model-00002-of-00002.safetensors",
275
+ "blocks.22.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
276
+ "blocks.22.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
277
+ "blocks.22.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
278
+ "blocks.22.ffn.w_down": "model-00002-of-00002.safetensors",
279
+ "blocks.22.ffn.w_gate": "model-00002-of-00002.safetensors",
280
+ "blocks.22.ffn.w_up": "model-00002-of-00002.safetensors",
281
+ "blocks.22.film1.proj.weight": "model-00002-of-00002.safetensors",
282
+ "blocks.22.film2.proj.weight": "model-00002-of-00002.safetensors",
283
+ "blocks.22.n1.w": "model-00002-of-00002.safetensors",
284
+ "blocks.22.n2.w": "model-00002-of-00002.safetensors",
285
+ "blocks.23.attn.k.weight": "model-00002-of-00002.safetensors",
286
+ "blocks.23.attn.kn.w": "model-00002-of-00002.safetensors",
287
+ "blocks.23.attn.o.weight": "model-00002-of-00002.safetensors",
288
+ "blocks.23.attn.q.weight": "model-00002-of-00002.safetensors",
289
+ "blocks.23.attn.qn.w": "model-00002-of-00002.safetensors",
290
+ "blocks.23.attn.v.weight": "model-00002-of-00002.safetensors",
291
+ "blocks.23.ffn.h_router.weight": "model-00002-of-00002.safetensors",
292
+ "blocks.23.ffn.router.weight": "model-00002-of-00002.safetensors",
293
+ "blocks.23.ffn.shared.down.weight": "model-00002-of-00002.safetensors",
294
+ "blocks.23.ffn.shared.gate.weight": "model-00002-of-00002.safetensors",
295
+ "blocks.23.ffn.shared.up.weight": "model-00002-of-00002.safetensors",
296
+ "blocks.23.ffn.w_down": "model-00002-of-00002.safetensors",
297
+ "blocks.23.ffn.w_gate": "model-00002-of-00002.safetensors",
298
+ "blocks.23.ffn.w_up": "model-00002-of-00002.safetensors",
299
+ "blocks.23.film1.proj.weight": "model-00002-of-00002.safetensors",
300
+ "blocks.23.film2.proj.weight": "model-00002-of-00002.safetensors",
301
+ "blocks.23.n1.w": "model-00002-of-00002.safetensors",
302
+ "blocks.23.n2.w": "model-00002-of-00002.safetensors",
303
+ "blocks.24.attn.k.weight": "model-00002-of-00002.safetensors",
304
+ "blocks.24.attn.kn.w": "model-00002-of-00002.safetensors",
305
+ "blocks.24.attn.o.weight": "model-00002-of-00002.safetensors",
306
+ "blocks.24.attn.q.weight": "model-00002-of-00002.safetensors",
307
+ "blocks.24.attn.qn.w": "model-00002-of-00002.safetensors",
308
+ "blocks.24.attn.v.weight": "model-00002-of-00002.safetensors",
309
+ "blocks.24.ffn.down.weight": "model-00002-of-00002.safetensors",
310
+ "blocks.24.ffn.gate.weight": "model-00002-of-00002.safetensors",
311
+ "blocks.24.ffn.up.weight": "model-00002-of-00002.safetensors",
312
+ "blocks.24.film1.proj.weight": "model-00002-of-00002.safetensors",
313
+ "blocks.24.film2.proj.weight": "model-00002-of-00002.safetensors",
314
+ "blocks.24.n1.w": "model-00002-of-00002.safetensors",
315
+ "blocks.24.n2.w": "model-00002-of-00002.safetensors",
316
+ "blocks.25.attn.k.weight": "model-00002-of-00002.safetensors",
317
+ "blocks.25.attn.kn.w": "model-00002-of-00002.safetensors",
318
+ "blocks.25.attn.o.weight": "model-00002-of-00002.safetensors",
319
+ "blocks.25.attn.q.weight": "model-00002-of-00002.safetensors",
320
+ "blocks.25.attn.qn.w": "model-00002-of-00002.safetensors",
321
+ "blocks.25.attn.v.weight": "model-00002-of-00002.safetensors",
322
+ "blocks.25.ffn.down.weight": "model-00002-of-00002.safetensors",
323
+ "blocks.25.ffn.gate.weight": "model-00002-of-00002.safetensors",
324
+ "blocks.25.ffn.up.weight": "model-00002-of-00002.safetensors",
325
+ "blocks.25.film1.proj.weight": "model-00002-of-00002.safetensors",
326
+ "blocks.25.film2.proj.weight": "model-00002-of-00002.safetensors",
327
+ "blocks.25.n1.w": "model-00002-of-00002.safetensors",
328
+ "blocks.25.n2.w": "model-00002-of-00002.safetensors",
329
+ "blocks.3.attn.k.weight": "model-00001-of-00002.safetensors",
330
+ "blocks.3.attn.kn.w": "model-00001-of-00002.safetensors",
331
+ "blocks.3.attn.o.weight": "model-00001-of-00002.safetensors",
332
+ "blocks.3.attn.q.weight": "model-00001-of-00002.safetensors",
333
+ "blocks.3.attn.qn.w": "model-00001-of-00002.safetensors",
334
+ "blocks.3.attn.v.weight": "model-00001-of-00002.safetensors",
335
+ "blocks.3.ffn.h_router.weight": "model-00001-of-00002.safetensors",
336
+ "blocks.3.ffn.router.weight": "model-00001-of-00002.safetensors",
337
+ "blocks.3.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
338
+ "blocks.3.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
339
+ "blocks.3.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
340
+ "blocks.3.ffn.w_down": "model-00001-of-00002.safetensors",
341
+ "blocks.3.ffn.w_gate": "model-00001-of-00002.safetensors",
342
+ "blocks.3.ffn.w_up": "model-00001-of-00002.safetensors",
343
+ "blocks.3.film1.proj.weight": "model-00001-of-00002.safetensors",
344
+ "blocks.3.film2.proj.weight": "model-00001-of-00002.safetensors",
345
+ "blocks.3.n1.w": "model-00001-of-00002.safetensors",
346
+ "blocks.3.n2.w": "model-00001-of-00002.safetensors",
347
+ "blocks.4.attn.k.weight": "model-00001-of-00002.safetensors",
348
+ "blocks.4.attn.kn.w": "model-00001-of-00002.safetensors",
349
+ "blocks.4.attn.o.weight": "model-00001-of-00002.safetensors",
350
+ "blocks.4.attn.q.weight": "model-00001-of-00002.safetensors",
351
+ "blocks.4.attn.qn.w": "model-00001-of-00002.safetensors",
352
+ "blocks.4.attn.v.weight": "model-00001-of-00002.safetensors",
353
+ "blocks.4.ffn.h_router.weight": "model-00001-of-00002.safetensors",
354
+ "blocks.4.ffn.router.weight": "model-00001-of-00002.safetensors",
355
+ "blocks.4.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
356
+ "blocks.4.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
357
+ "blocks.4.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
358
+ "blocks.4.ffn.w_down": "model-00001-of-00002.safetensors",
359
+ "blocks.4.ffn.w_gate": "model-00001-of-00002.safetensors",
360
+ "blocks.4.ffn.w_up": "model-00001-of-00002.safetensors",
361
+ "blocks.4.film1.proj.weight": "model-00001-of-00002.safetensors",
362
+ "blocks.4.film2.proj.weight": "model-00001-of-00002.safetensors",
363
+ "blocks.4.n1.w": "model-00001-of-00002.safetensors",
364
+ "blocks.4.n2.w": "model-00001-of-00002.safetensors",
365
+ "blocks.5.attn.k.weight": "model-00001-of-00002.safetensors",
366
+ "blocks.5.attn.kn.w": "model-00001-of-00002.safetensors",
367
+ "blocks.5.attn.o.weight": "model-00001-of-00002.safetensors",
368
+ "blocks.5.attn.q.weight": "model-00001-of-00002.safetensors",
369
+ "blocks.5.attn.qn.w": "model-00001-of-00002.safetensors",
370
+ "blocks.5.attn.v.weight": "model-00001-of-00002.safetensors",
371
+ "blocks.5.ffn.h_router.weight": "model-00001-of-00002.safetensors",
372
+ "blocks.5.ffn.router.weight": "model-00001-of-00002.safetensors",
373
+ "blocks.5.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
374
+ "blocks.5.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
375
+ "blocks.5.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
376
+ "blocks.5.ffn.w_down": "model-00001-of-00002.safetensors",
377
+ "blocks.5.ffn.w_gate": "model-00001-of-00002.safetensors",
378
+ "blocks.5.ffn.w_up": "model-00001-of-00002.safetensors",
379
+ "blocks.5.film1.proj.weight": "model-00001-of-00002.safetensors",
380
+ "blocks.5.film2.proj.weight": "model-00001-of-00002.safetensors",
381
+ "blocks.5.n1.w": "model-00001-of-00002.safetensors",
382
+ "blocks.5.n2.w": "model-00001-of-00002.safetensors",
383
+ "blocks.6.attn.k.weight": "model-00001-of-00002.safetensors",
384
+ "blocks.6.attn.kn.w": "model-00001-of-00002.safetensors",
385
+ "blocks.6.attn.o.weight": "model-00001-of-00002.safetensors",
386
+ "blocks.6.attn.q.weight": "model-00001-of-00002.safetensors",
387
+ "blocks.6.attn.qn.w": "model-00001-of-00002.safetensors",
388
+ "blocks.6.attn.v.weight": "model-00001-of-00002.safetensors",
389
+ "blocks.6.ffn.h_router.weight": "model-00001-of-00002.safetensors",
390
+ "blocks.6.ffn.router.weight": "model-00001-of-00002.safetensors",
391
+ "blocks.6.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
392
+ "blocks.6.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
393
+ "blocks.6.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
394
+ "blocks.6.ffn.w_down": "model-00001-of-00002.safetensors",
395
+ "blocks.6.ffn.w_gate": "model-00001-of-00002.safetensors",
396
+ "blocks.6.ffn.w_up": "model-00001-of-00002.safetensors",
397
+ "blocks.6.film1.proj.weight": "model-00001-of-00002.safetensors",
398
+ "blocks.6.film2.proj.weight": "model-00001-of-00002.safetensors",
399
+ "blocks.6.n1.w": "model-00001-of-00002.safetensors",
400
+ "blocks.6.n2.w": "model-00001-of-00002.safetensors",
401
+ "blocks.7.attn.k.weight": "model-00001-of-00002.safetensors",
402
+ "blocks.7.attn.kn.w": "model-00001-of-00002.safetensors",
403
+ "blocks.7.attn.o.weight": "model-00001-of-00002.safetensors",
404
+ "blocks.7.attn.q.weight": "model-00001-of-00002.safetensors",
405
+ "blocks.7.attn.qn.w": "model-00001-of-00002.safetensors",
406
+ "blocks.7.attn.v.weight": "model-00001-of-00002.safetensors",
407
+ "blocks.7.ffn.h_router.weight": "model-00001-of-00002.safetensors",
408
+ "blocks.7.ffn.router.weight": "model-00001-of-00002.safetensors",
409
+ "blocks.7.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
410
+ "blocks.7.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
411
+ "blocks.7.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
412
+ "blocks.7.ffn.w_down": "model-00001-of-00002.safetensors",
413
+ "blocks.7.ffn.w_gate": "model-00001-of-00002.safetensors",
414
+ "blocks.7.ffn.w_up": "model-00001-of-00002.safetensors",
415
+ "blocks.7.film1.proj.weight": "model-00001-of-00002.safetensors",
416
+ "blocks.7.film2.proj.weight": "model-00001-of-00002.safetensors",
417
+ "blocks.7.n1.w": "model-00001-of-00002.safetensors",
418
+ "blocks.7.n2.w": "model-00001-of-00002.safetensors",
419
+ "blocks.8.attn.k.weight": "model-00001-of-00002.safetensors",
420
+ "blocks.8.attn.kn.w": "model-00001-of-00002.safetensors",
421
+ "blocks.8.attn.o.weight": "model-00001-of-00002.safetensors",
422
+ "blocks.8.attn.q.weight": "model-00001-of-00002.safetensors",
423
+ "blocks.8.attn.qn.w": "model-00001-of-00002.safetensors",
424
+ "blocks.8.attn.v.weight": "model-00001-of-00002.safetensors",
425
+ "blocks.8.ffn.h_router.weight": "model-00001-of-00002.safetensors",
426
+ "blocks.8.ffn.router.weight": "model-00001-of-00002.safetensors",
427
+ "blocks.8.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
428
+ "blocks.8.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
429
+ "blocks.8.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
430
+ "blocks.8.ffn.w_down": "model-00001-of-00002.safetensors",
431
+ "blocks.8.ffn.w_gate": "model-00001-of-00002.safetensors",
432
+ "blocks.8.ffn.w_up": "model-00001-of-00002.safetensors",
433
+ "blocks.8.film1.proj.weight": "model-00001-of-00002.safetensors",
434
+ "blocks.8.film2.proj.weight": "model-00001-of-00002.safetensors",
435
+ "blocks.8.n1.w": "model-00001-of-00002.safetensors",
436
+ "blocks.8.n2.w": "model-00001-of-00002.safetensors",
437
+ "blocks.9.attn.k.weight": "model-00001-of-00002.safetensors",
438
+ "blocks.9.attn.kn.w": "model-00001-of-00002.safetensors",
439
+ "blocks.9.attn.o.weight": "model-00001-of-00002.safetensors",
440
+ "blocks.9.attn.q.weight": "model-00001-of-00002.safetensors",
441
+ "blocks.9.attn.qn.w": "model-00001-of-00002.safetensors",
442
+ "blocks.9.attn.v.weight": "model-00001-of-00002.safetensors",
443
+ "blocks.9.ffn.h_router.weight": "model-00001-of-00002.safetensors",
444
+ "blocks.9.ffn.router.weight": "model-00001-of-00002.safetensors",
445
+ "blocks.9.ffn.shared.down.weight": "model-00001-of-00002.safetensors",
446
+ "blocks.9.ffn.shared.gate.weight": "model-00001-of-00002.safetensors",
447
+ "blocks.9.ffn.shared.up.weight": "model-00001-of-00002.safetensors",
448
+ "blocks.9.ffn.w_down": "model-00001-of-00002.safetensors",
449
+ "blocks.9.ffn.w_gate": "model-00001-of-00002.safetensors",
450
+ "blocks.9.ffn.w_up": "model-00001-of-00002.safetensors",
451
+ "blocks.9.film1.proj.weight": "model-00001-of-00002.safetensors",
452
+ "blocks.9.film2.proj.weight": "model-00001-of-00002.safetensors",
453
+ "blocks.9.n1.w": "model-00001-of-00002.safetensors",
454
+ "blocks.9.n2.w": "model-00001-of-00002.safetensors",
455
+ "emb.weight": "model-00001-of-00002.safetensors",
456
+ "hormones.net.0.bias": "model-00001-of-00002.safetensors",
457
+ "hormones.net.0.weight": "model-00001-of-00002.safetensors",
458
+ "hormones.net.2.bias": "model-00001-of-00002.safetensors",
459
+ "hormones.net.2.weight": "model-00001-of-00002.safetensors",
460
+ "hormones.null": "model-00001-of-00002.safetensors",
461
+ "norm.w": "model-00002-of-00002.safetensors"
462
+ }
463
+ }
modeling_udream_bi.py ADDED
@@ -0,0 +1,423 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ """Standalone Hugging Face Transformers modeling file for UDream-1.0-Bi-Encode."""
3
+ import os
4
+ from typing import List, Optional, Sequence, Tuple, Union
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ from transformers import PreTrainedModel
10
+ from transformers.modeling_outputs import BaseModelOutput
11
+
12
+ try:
13
+ from .configuration_udream_bi import UDreamBiEncoderConfig
14
+ except ImportError:
15
+ from configuration_udream_bi import UDreamBiEncoderConfig
16
+
17
+
18
+ class RMSNorm(nn.Module):
19
+ def __init__(self, d: int, eps: float = 1e-5):
20
+ super().__init__()
21
+ self.w = nn.Parameter(torch.ones(d))
22
+ self.eps = eps
23
+
24
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
25
+ dt = x.dtype
26
+ xf = x.float()
27
+ xf = xf * torch.rsqrt(xf.pow(2).mean(-1, keepdim=True) + self.eps)
28
+ return (xf * self.w.float()).to(dt)
29
+
30
+
31
+ def build_rope(head_dim: int, seq: int, theta: float, device: torch.device, dtype: torch.dtype):
32
+ inv = 1.0 / (theta ** (torch.arange(0, head_dim, 2, device=device).float() / head_dim))
33
+ t = torch.arange(seq, device=device).float()
34
+ f = torch.outer(t, inv)
35
+ return torch.cos(f).to(dtype), torch.sin(f).to(dtype)
36
+
37
+
38
+ def apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
39
+ x1, x2 = x[..., 0::2], x[..., 1::2]
40
+ o1 = x1 * cos - x2 * sin
41
+ o2 = x1 * sin + x2 * cos
42
+ return torch.stack((o1, o2), dim=-1).flatten(-2)
43
+
44
+
45
+ def _can_use_grouped_mm(x: torch.Tensor) -> bool:
46
+ if os.environ.get("UDREAM_GROUPED_MM", "1") != "1":
47
+ return False
48
+ if not (x.is_cuda and hasattr(torch, "_grouped_mm") and x.dtype == torch.bfloat16):
49
+ return False
50
+ major, _ = torch.cuda.get_device_capability(x.device)
51
+ return major >= 9
52
+
53
+
54
+ class BidirectionalAttention(nn.Module):
55
+ def __init__(self, cfg: UDreamBiEncoderConfig, layer: int):
56
+ super().__init__()
57
+ self.cfg = cfg
58
+ self.layer = layer
59
+ self.is_global = cfg.is_global(layer)
60
+ self.nh = cfg.n_heads
61
+ self.nkv = cfg.n_kv_heads
62
+ self.hd = cfg.head_dim
63
+ d = cfg.d_model
64
+ self.q = nn.Linear(d, self.nh * self.hd, bias=False)
65
+ self.k = nn.Linear(d, self.nkv * self.hd, bias=False)
66
+ self.v = nn.Linear(d, self.nkv * self.hd, bias=False)
67
+ self.o = nn.Linear(self.nh * self.hd, d, bias=False)
68
+ self.qn = RMSNorm(self.hd, cfg.rms_eps)
69
+ self.kn = RMSNorm(self.hd, cfg.rms_eps)
70
+
71
+ def forward(
72
+ self,
73
+ x: torch.Tensor,
74
+ rope: Tuple[torch.Tensor, torch.Tensor],
75
+ attention_mask: Optional[torch.Tensor] = None,
76
+ ) -> torch.Tensor:
77
+ B, T, _ = x.shape
78
+ q = self.qn(self.q(x).view(B, T, self.nh, self.hd).transpose(1, 2))
79
+ k = self.kn(self.k(x).view(B, T, self.nkv, self.hd).transpose(1, 2))
80
+ v = self.v(x).view(B, T, self.nkv, self.hd).transpose(1, 2)
81
+ if not self.is_global:
82
+ cos, sin = rope
83
+ q = apply_rope(q, cos, sin)
84
+ k = apply_rope(k, cos, sin)
85
+
86
+ rep = self.nh // self.nkv
87
+ k_rep = k.repeat_interleave(rep, dim=1)
88
+ v_rep = v.repeat_interleave(rep, dim=1)
89
+ w = self.cfg.window
90
+
91
+ valid_kv = attention_mask[:, :T].bool() if attention_mask is not None else None
92
+
93
+ if self.is_global or T <= w:
94
+ if valid_kv is None:
95
+ o = F.scaled_dot_product_attention(q, k_rep, v_rep, is_causal=False)
96
+ else:
97
+ amask = valid_kv[:, None, None, :]
98
+ empty = ~valid_kv.any(dim=-1)
99
+ if empty.any():
100
+ amask = amask.clone()
101
+ amask[empty, :, :, 0] = True
102
+ o = F.scaled_dot_product_attention(q, k_rep, v_rep, attn_mask=amask, is_causal=False)
103
+ else:
104
+ # Bidirectional sliding window |q - kv| < w for long sequences (T > 512)
105
+ outs = []
106
+ for cs in range(0, T, 512):
107
+ ce = min(cs + 512, T)
108
+ kv_lo = max(0, cs - (w - 1))
109
+ kv_hi = min(T, ce + (w - 1))
110
+ q_pos = torch.arange(cs, ce, device=q.device)[:, None]
111
+ kv_pos = torch.arange(kv_lo, kv_hi, device=q.device)[None, :]
112
+ amask = ((q_pos - kv_pos).abs() < w)[None, None, :, :]
113
+ if valid_kv is not None:
114
+ amask = amask & valid_kv[:, kv_lo:kv_hi][:, None, None, :]
115
+ diag = (q_pos == kv_pos)[None, None, :, :]
116
+ amask = amask | diag
117
+ outs.append(
118
+ F.scaled_dot_product_attention(
119
+ q[:, :, cs:ce, :], k_rep[:, :, kv_lo:kv_hi, :], v_rep[:, :, kv_lo:kv_hi, :],
120
+ attn_mask=amask, is_causal=False,
121
+ )
122
+ )
123
+ o = torch.cat(outs, dim=2)
124
+
125
+ return self.o(o.transpose(1, 2).reshape(B, T, -1))
126
+
127
+
128
+ class SwiGLU(nn.Module):
129
+ def __init__(self, d: int, dff: int):
130
+ super().__init__()
131
+ self.up = nn.Linear(d, dff, bias=False)
132
+ self.gate = nn.Linear(d, dff, bias=False)
133
+ self.down = nn.Linear(dff, d, bias=False)
134
+
135
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
136
+ return self.down(F.silu(self.gate(x)) * self.up(x))
137
+
138
+
139
+ class MoE(nn.Module):
140
+ def __init__(self, cfg: UDreamBiEncoderConfig):
141
+ super().__init__()
142
+ self.cfg = cfg
143
+ d, E, dff = cfg.d_model, cfg.n_experts, cfg.expert_d_ff
144
+ self.E = E
145
+ self.k = getattr(cfg, "moe_top_k", getattr(cfg, "top_k", 6))
146
+ self.router = nn.Linear(d, E, bias=False)
147
+ self.w_up = nn.Parameter(torch.empty(E, d, dff))
148
+ self.w_gate = nn.Parameter(torch.empty(E, d, dff))
149
+ self.w_down = nn.Parameter(torch.empty(E, dff, d))
150
+ self.shared = SwiGLU(d, cfg.shared_d_ff)
151
+ self.h_router = nn.Linear(cfg.cond_dim, E, bias=False)
152
+
153
+ def forward(self, x: torch.Tensor, cond: Optional[torch.Tensor]) -> torch.Tensor:
154
+ B, T, d = x.shape
155
+ xf = x.reshape(-1, d)
156
+ logits = self.router(xf).float()
157
+ if cond is not None:
158
+ bias = self.h_router(cond.to(self.h_router.weight.dtype)).float()
159
+ if bias.shape[1] == 1 and T > 1:
160
+ bias = bias.expand(B, T, self.E)
161
+ logits = logits + bias.reshape(-1, self.E)
162
+ probs = logits.softmax(-1)
163
+ topv, topi = probs.topk(self.k, dim=-1)
164
+ topv = topv / topv.sum(-1, keepdim=True)
165
+
166
+ N = xf.shape[0]
167
+ flat_e = topi.reshape(-1)
168
+ flat_w = topv.reshape(-1).to(xf.dtype)
169
+ flat_t = torch.arange(N, device=xf.device).repeat_interleave(self.k)
170
+ order = torch.argsort(flat_e)
171
+ se, st, sw = flat_e[order], flat_t[order], flat_w[order]
172
+ bounds = torch.searchsorted(se, torch.arange(self.E + 1, device=xf.device))
173
+
174
+ out = torch.zeros_like(xf)
175
+ if _can_use_grouped_mm(xf):
176
+ xs = xf[st]
177
+ offs = bounds[1:].to(torch.int32)
178
+ hg = torch._grouped_mm(xs, self.w_gate, offs=offs)
179
+ hu = torch._grouped_mm(xs, self.w_up, offs=offs)
180
+ y = torch._grouped_mm(F.silu(hg) * hu, self.w_down, offs=offs)
181
+ out.index_add_(0, st, y * sw.unsqueeze(-1))
182
+ else:
183
+ bl = bounds.tolist()
184
+ for e in range(self.E):
185
+ lo, hi = bl[e], bl[e + 1]
186
+ if hi <= lo:
187
+ continue
188
+ tk = st[lo:hi]
189
+ h = xf[tk]
190
+ y = (F.silu(h @ self.w_gate[e]) * (h @ self.w_up[e])) @ self.w_down[e]
191
+ out.index_add_(0, tk, y * sw[lo:hi].unsqueeze(-1))
192
+
193
+ return out.view(B, T, d) + self.shared(x)
194
+
195
+
196
+ class HormoneEncoder(nn.Module):
197
+ def __init__(self, cfg: UDreamBiEncoderConfig):
198
+ super().__init__()
199
+ self.null = nn.Parameter(torch.zeros(cfg.n_hormones))
200
+ self.net = nn.Sequential(
201
+ nn.Linear(cfg.n_hormones, cfg.cond_dim),
202
+ nn.SiLU(),
203
+ nn.Linear(cfg.cond_dim, cfg.cond_dim),
204
+ )
205
+
206
+ def forward(self, h: Optional[torch.Tensor], B: int, device: torch.device, dtype: torch.dtype) -> torch.Tensor:
207
+ if h is None:
208
+ h = self.null.to(device=device, dtype=dtype).expand(B, -1)
209
+ else:
210
+ h = h.to(device=device, dtype=dtype)
211
+ return self.net(h)[:, None, :]
212
+
213
+
214
+ class FiLM(nn.Module):
215
+ def __init__(self, cfg: UDreamBiEncoderConfig):
216
+ super().__init__()
217
+ self.proj = nn.Linear(cfg.cond_dim, 2 * cfg.d_model, bias=False)
218
+
219
+ def forward(self, x: torch.Tensor, cond: Optional[torch.Tensor]) -> torch.Tensor:
220
+ if cond is None:
221
+ return x
222
+ g, b = self.proj(cond.to(self.proj.weight.dtype)).chunk(2, dim=-1)
223
+ return (x * (1 + g) + b).to(x.dtype)
224
+
225
+
226
+ class Block(nn.Module):
227
+ def __init__(self, cfg: UDreamBiEncoderConfig, layer: int):
228
+ super().__init__()
229
+ self.is_moe = cfg.is_moe(layer)
230
+ self.n1 = RMSNorm(cfg.d_model, cfg.rms_eps)
231
+ self.attn = BidirectionalAttention(cfg, layer)
232
+ self.n2 = RMSNorm(cfg.d_model, cfg.rms_eps)
233
+ self.ffn = MoE(cfg) if self.is_moe else SwiGLU(cfg.d_model, cfg.dense_d_ff)
234
+ self.film1 = FiLM(cfg)
235
+ self.film2 = FiLM(cfg)
236
+
237
+ def forward(
238
+ self,
239
+ x: torch.Tensor,
240
+ cond: Optional[torch.Tensor],
241
+ rope: Tuple[torch.Tensor, torch.Tensor],
242
+ attention_mask: Optional[torch.Tensor] = None,
243
+ ) -> torch.Tensor:
244
+ x = x + self.attn(self.film1(self.n1(x), cond), rope, attention_mask=attention_mask)
245
+ h = self.film2(self.n2(x), cond)
246
+ y = self.ffn(h, cond) if self.is_moe else self.ffn(h)
247
+ return x + y
248
+
249
+
250
+ class UDreamBiEncoderModel(PreTrainedModel):
251
+ """Bidirectional Sparse MoE Text Encoder (4.14B total / 1.44B active, d_model=2048).
252
+
253
+ Designed for text-to-image diffusion conditioning (e.g. Photon v2 / DiT) and
254
+ bilingual Ukrainian <-> English semantic embeddings.
255
+ """
256
+
257
+ config_class = UDreamBiEncoderConfig
258
+ base_model_prefix = "udream_bi_encoder"
259
+ supports_gradient_checkpointing = False
260
+ _no_split_modules = ["Block"]
261
+
262
+ def __init__(self, config: UDreamBiEncoderConfig):
263
+ super().__init__(config)
264
+ self.cfg = config
265
+ self.emb = nn.Embedding(config.vocab_size, config.d_model)
266
+ self.hormones = HormoneEncoder(config)
267
+ self.blocks = nn.ModuleList(Block(config, i) for i in range(config.n_layers))
268
+ self.norm = RMSNorm(config.d_model, config.rms_eps)
269
+ self._rope_cache = {}
270
+ self.post_init()
271
+
272
+ def get_input_embeddings(self):
273
+ return self.emb
274
+
275
+ def set_input_embeddings(self, value):
276
+ self.emb = value
277
+
278
+ def _get_rope(self, max_pos: int, device: torch.device, dtype: torch.dtype):
279
+ cap = max(512, max_pos)
280
+ key = (device, dtype)
281
+ cached = self._rope_cache.get(key)
282
+ if cached is None or cached[0].shape[0] < cap:
283
+ self._rope_cache[key] = build_rope(self.cfg.head_dim, cap, self.cfg.rope_theta, device, dtype)
284
+ return self._rope_cache[key]
285
+
286
+ def forward(
287
+ self,
288
+ input_ids: Optional[torch.LongTensor] = None,
289
+ attention_mask: Optional[torch.Tensor] = None,
290
+ position_ids: Optional[torch.LongTensor] = None,
291
+ inputs_embeds: Optional[torch.FloatTensor] = None,
292
+ output_hidden_states: Optional[bool] = None,
293
+ return_dict: Optional[bool] = None,
294
+ hormones: Optional[torch.Tensor] = None,
295
+ use_hormones: bool = True,
296
+ **kwargs,
297
+ ) -> Union[Tuple, BaseModelOutput]:
298
+ output_hidden_states = (
299
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
300
+ )
301
+ return_dict = True if return_dict is None else return_dict
302
+
303
+ if inputs_embeds is not None:
304
+ x = inputs_embeds
305
+ B, T, _ = x.shape
306
+ else:
307
+ B, T = input_ids.shape
308
+ x = self.emb(input_ids)
309
+
310
+ if position_ids is None:
311
+ cos_all, sin_all = self._get_rope(T, x.device, x.dtype)
312
+ cos = cos_all[:T][None, None, :, :]
313
+ sin = sin_all[:T][None, None, :, :]
314
+ else:
315
+ pos_safe = position_ids.clamp(min=0)
316
+ max_pos = int(pos_safe.max().item()) + 1
317
+ cos_all, sin_all = self._get_rope(max_pos, x.device, x.dtype)
318
+ cos = cos_all[pos_safe][:, None, :, :]
319
+ sin = sin_all[pos_safe][:, None, :, :]
320
+
321
+ cond = self.hormones(hormones, B, x.device, x.dtype) if use_hormones else None
322
+
323
+ all_hidden = (x,) if output_hidden_states else None
324
+ for i, blk in enumerate(self.blocks):
325
+ x = blk(x, cond, (cos, sin), attention_mask=attention_mask)
326
+ if output_hidden_states and i < len(self.blocks) - 1:
327
+ all_hidden = all_hidden + (x,)
328
+
329
+ x = self.norm(x)
330
+ if output_hidden_states:
331
+ all_hidden = all_hidden + (x,)
332
+
333
+ if not return_dict:
334
+ return tuple(v for v in (x, all_hidden) if v is not None)
335
+ return BaseModelOutput(last_hidden_state=x, hidden_states=all_hidden)
336
+
337
+ @torch.inference_mode()
338
+ def encode(
339
+ self,
340
+ prompts: Union[str, Sequence[str]],
341
+ tokenizer,
342
+ max_length: int = 256,
343
+ layers: Sequence[int] = (-4, -3, -2),
344
+ normalize_taps: bool = True,
345
+ prepend_doc: bool = True,
346
+ pool: Optional[str] = None,
347
+ ):
348
+ """Convenience encoder matching UDream-1.0-Bi-Encode training/T2I convention.
349
+
350
+ Prepends `<doc>` (id=4) at position 0, runs bidirectional attention with exact
351
+ padding mask, averages final-normed `layers` (default `(-4, -3, -2)`), and strips
352
+ the `<doc>` prefix so 100% of prompt tokens are preserved.
353
+
354
+ Args:
355
+ prompts: String or list of strings.
356
+ tokenizer: Matching `UDream/UDream-1.0-Bi-Encode` tokenizer.
357
+ max_length: Max prompt tokens (excluding `<doc>`).
358
+ layers: Block indices to average (negative indices follow Python convention,
359
+ e.g. `(-4, -3, -2)` -> blocks 22, 23, 24; `(-1,)` -> final block 25).
360
+ normalize_taps: Apply `self.norm` to each tapped layer before averaging.
361
+ prepend_doc: Prepend `<doc>` token as trained, then strip it on output.
362
+ pool: `None` -> `(token_states [B, L, 2048], mask [B, L])`;
363
+ `"mean"` -> L2-normalized pooled embeddings `[B, 2048]`.
364
+ """
365
+ single = isinstance(prompts, str)
366
+ plist: List[str] = [prompts] if single else list(prompts)
367
+ is_empty = [not p for p in plist]
368
+ doc_id = self.cfg.doc_token_id
369
+ pad_id = self.cfg.pad_token_id if self.cfg.pad_token_id is not None else 0
370
+ pre = [doc_id] if (prepend_doc and doc_id is not None) else []
371
+ n_pre = len(pre)
372
+
373
+ seqs = []
374
+ for p in plist:
375
+ if not p:
376
+ seqs.append(list(pre))
377
+ else:
378
+ tids = tokenizer.encode(p, add_special_tokens=False)[:max_length]
379
+ seqs.append(list(pre) + tids)
380
+ T = max(n_pre + 1, max((len(s) for s in seqs), default=n_pre + 1))
381
+ B = len(plist)
382
+ device = next(self.parameters()).device
383
+ ids = torch.full((B, T), pad_id, dtype=torch.long, device=device)
384
+ mask = torch.zeros(B, T, dtype=torch.bool, device=device)
385
+ for i, s in enumerate(seqs):
386
+ if s:
387
+ ids[i, :len(s)] = torch.tensor(s, dtype=torch.long, device=device)
388
+ mask[i, :len(s)] = True
389
+
390
+ x = self.emb(ids)
391
+ cos_all, sin_all = self._get_rope(T, x.device, x.dtype)
392
+ rope = (cos_all[:T][None, None, :, :], sin_all[:T][None, None, :, :])
393
+ cond = self.hormones(None, B, x.device, x.dtype)
394
+
395
+ n_layers = len(self.blocks)
396
+ tap_indices = sorted({i % n_layers for i in layers})
397
+ max_li = max(tap_indices)
398
+ taps = []
399
+ for li in range(max_li + 1):
400
+ x = self.blocks[li](x, cond, rope, attention_mask=mask)
401
+ if li in tap_indices:
402
+ taps.append((self.norm(x) if normalize_taps else x).float())
403
+ h = torch.stack(taps).mean(0)
404
+
405
+ if n_pre > 0:
406
+ h, mask = h[:, n_pre:], mask[:, n_pre:]
407
+ if h.shape[1] == 0:
408
+ h = h.new_zeros(B, 1, self.cfg.d_model)
409
+ mask = torch.zeros(B, 1, dtype=torch.bool, device=device)
410
+ for i, empty in enumerate(is_empty):
411
+ if empty:
412
+ h[i].zero_()
413
+ mask[i] = False
414
+
415
+ if pool == "mean":
416
+ mf = mask.float().unsqueeze(-1)
417
+ pooled = (h * mf).sum(dim=1) / mf.sum(dim=1).clamp(min=1.0)
418
+ pooled = F.normalize(pooled, dim=-1)
419
+ return pooled[0] if single else pooled
420
+
421
+ if single:
422
+ return h[0], mask[0]
423
+ return h, mask
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "bos_token": "<bos>",
4
+ "eos_token": "<eos>",
5
+ "model_max_length": 4096,
6
+ "pad_token": "<pad>",
7
+ "tokenizer_class": "TokenizersBackend",
8
+ "unk_token": "<unk>"
9
+ }