File size: 8,642 Bytes
7fdd78a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
license: apache-2.0
language:
  - uk
  - en
  - ru
  - pl
  - cs
  - be
library_name: transformers
pipeline_tag: feature-extraction
base_model: UDream/UDream-1.0-Base
tags:
  - udream
  - text-encoder
  - bidirectional
  - moe
  - mixture-of-experts
  - ukrainian
  - text-to-image
  - diffusion
  - multilingual
  - custom_code
---

# UDream 1.0 Bi-Encode (`UDream/UDream-1.0-Bi-Encode`)

**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**.

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`:
- **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`),
- Achieved **#1 overall zero-shot Ukrainian → English T2I transfer (`ua_en_img_cos = 0.8245`)** across all 7 tested models (including `T5Gemma-2-4B`),
- 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.

---

## Architecture & Adaptation Recipe

| Property | Specification |
|---|---|
| Base Checkpoint | [`UDream/UDream-1.0-Base`](https://huggingface.co/UDream/UDream-1.0-Base) (pretrained on 63.73B tokens) |
| Architecture | Encoder-only Bidirectional Sparse MoE (`UDreamBiEncoderModel`, `is_causal=False`) |
| Parameters (Total / Active) | **4.14B / 1.44B** backbone (`d_model=2048`, 26 layers: 4 dense + 22 MoE, 32 routed `top_k=6` + 1 shared) |
| Hybrid Bidirectional Attention | 22 local sliding-window layers (`window=512`, RoPE `10k`) + 4 global **NoPE** layers (`5, 11, 17, 23`) |
| Inference Footprint | **~8.6 GB VRAM** (`bf16`), `~1.8 ms/prompt` (RTX 5090), exact padding invariance (`fp32 cos=1.000000`) |
| Tokenizer | `udream-64k` ByteLevel BPE (`vocab_size=64,128`, ~20.9 tok/prompt UA vs 26.1 Gemma 4 / 27.4 Qwen 3.5) |
| 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 |
| 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`) |

---

## Quickstart (`transformers`)

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.

### 1. Token-level states for Text-to-Image (DiT / Flow Matching)

```python
import torch
from transformers import AutoModel, AutoTokenizer

repo_id = "UDream/UDream-1.0-Bi-Encode"
tok = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
enc = AutoModel.from_pretrained(
    repo_id,
    trust_remote_code=True,
    dtype=torch.bfloat16,
    device_map="cuda",
).eval()

prompts = [
    "a cozy wooden cabin in a snowy pine forest at golden hour, film grain",
    "затишна дерев'яна хатина в засніженому сосновому лісі на заході сонця, зерно плівки",
]

# Recommended for T2I DiT conditioning: average of final-normed layers (-4, -3, -2)
hidden, mask = enc.encode(prompts, tok, max_length=128, layers=(-4, -3, -2))
print(hidden.shape, mask.shape)  # torch.Size([2, L, 2048]) torch.Size([2, L])
```

### 2. Bilingual EN ↔ UA Semantic Embeddings

```python
emb = enc.encode(prompts, tok, max_length=128, layers=(-1,), pool="mean")
print("EN <-> UA cosine similarity:", float((emb[0] * emb[1]).sum()))
```

### 3. Standard Hugging Face `forward()` API

```python
ids = tok("<doc>" + prompts[0], return_tensors="pt", add_special_tokens=False).to(enc.device)
with torch.inference_mode():
    out = enc(**ids, output_hidden_states=True)
# out.last_hidden_state[:, 1:, :] -> [1, L, 2048] (stripping <doc>)
```

---

## Benchmarks

### 1. Text Encoder X-Ray (16 parallel EN ↔ UA visual scene pairs, `bf16`)

| 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` ↑ |
|---|---|---:|---:|---:|---:|---:|---:|---:|---:|
| **`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%** |
| **`UDream-1.0-Bi-Encode` (`-1`)** | **Apache 2.0** | 2048 | **8.60** | **20.9** | **3.99%** | **100.0%** | **+0.7242** | **+0.7880** | **100.0%** |
| `UDream-1.0-Base` (causal) | Apache 2.0 | 2048 | 8.60 | 20.9 | 7.61% | 93.8% | +0.4783 | +0.5919 | 100.0% |
| `T5Gemma-2-4B` (`-1`, bidirectional) | Gemma ToU | 2560 | 8.17 | 26.1 | 34.46% | 96.9% | +0.3075 | +0.5230 | 100.0% |
| `Qwen3.5-4B-Base` | Apache 2.0 | 2560 | 9.89 | 27.4 | 9.31% | 56.3% | +0.0010 | +0.3150 | 100.0% |
| `Gemma-4-E2B` (Base) | Apache 2.0 | 1536 | 9.62 | 26.1 | 55.55% | 28.1% | -0.0772 | +0.2710 | 100.0% |
| `Gemma-4-E4B` (Base) | Apache 2.0 | 2560 | 15.44 | 26.1 | 13.70% | 21.9% | -0.2542 | +0.2234 | 100.0% |
| `Qwen3-4B` | Apache 2.0 | 2560 | 8.65 | 36.8 | 87.20% | 6.3% | — | +0.2180 | 75.0% |
| `Gemma-4-E4B-it` | Apache 2.0 | 2560 | 15.43 | 26.1 | 89.95% | 21.9% | -0.2130 | +0.1041 | 53.1% |

### 2. Downstream Text-to-Image DiT Shootout (8,000 steps, `Photon-Small 433M`)

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`):

| Rank | Frozen Text Encoder | Attention / License | `val_cond` ↓ (EN) | `cond_gain` ↑ (EN) | `val_cond85` ↓ | `cond_gain85` ↑ | Zero-shot UA `early` / `best` ↑ |
|---:|---|---|---:|---:|---:|---:|---:|
| 1 | `T5Gemma-2-4B` (`-1`) | Bidirectional / Gemma ToU | **0.83375** | **+0.00720** | **0.77627** | **+0.00685** | 0.699 / 0.741 |
| **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** |
| 3 | `Qwen3.5-4B-Base` | Causal hybrid / Apache 2.0 | 0.83522 | +0.00532 | 0.77800 | +0.00465 | 0.660 / 0.705 |
| 4 | `Gemma-4-E4B` (Base) | Causal / Apache 2.0 | 0.83578 | +0.00490 | 0.77862 | +0.00479 | 0.359 / 0.422 |
| 5 | `UDream-1.0-SFT` | Causal / Apache 2.0 | 0.83591 | +0.00487 | 0.77839 | +0.00452 | 0.720 / 0.753 |
| 6 | `UDream-1.0-Base` | Causal / Apache 2.0 | 0.83614 | +0.00490 | 0.77845 | +0.00469 | 0.804 / 0.817 |
| 7 | `Gemma-4-E4B-it` | Causal / Apache 2.0 | 0.83629 | +0.00381 | 0.77881 | +0.00346 | 0.578 / 0.626 |

---

## Limitations

- **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).
- **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.
- **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`).

---

## Citation

```bibtex
@misc{dormidontov2026udream10bi,
  title  = {UDream 1.0 Bi-Encode: A Bidirectional Ukrainian-First Sparse MoE Text Encoder for Diffusion and Cross-Lingual Representations},
  author = {Dormidontov, Oleksandr},
  year   = {2026},
  url    = {https://udream.com.ua}
}
```