File size: 7,103 Bytes
5ccbf36
 
 
 
 
a66ce43
 
5ccbf36
a66ce43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c9427
a66ce43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccbf36
a66ce43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Embed FineWiki shards via a running TEI server (one dense vector per article).

The companion to `embed_sections.py`, which produces one vector per section
via late-chunking ColBERT. This module is the simpler dense path: each
article goes to TEI as a truncated document, gets back a single pooled vector.

Usage:
  python embed_articles.py --cache-dir /path/to/hf-cache --output /path/to/embeddings \\
      --wiki enwiki --model-subdir qwen3-embedding-0.6b --dimensions 1024

For title embeddings, add: --text-column title --output-suffix title --char-cap 256
"""

from __future__ import annotations

import argparse
import asyncio
import time
from pathlib import Path

import httpx
import numpy as np

from usearchwiki import Shard, load_lang, load_shard_texts, write_bin


def select_shards(all_shards: list[Shard], gpu_id: int, world_size: int) -> list[Shard]:
    return [
        shard for index, shard in enumerate(all_shards) if index % world_size == gpu_id
    ]


async def embed_texts(
    client: httpx.AsyncClient,
    url: str,
    texts: list[str],
    batch_size: int,
    concurrency: int,
    character_cap: int,
    dimensions: int,
) -> tuple[np.ndarray, dict[str, float]]:
    """Embed `texts` via TEI in input order; empty inputs map to zero rows."""
    semaphore = asyncio.Semaphore(concurrency)
    output = np.zeros((len(texts), dimensions), dtype=np.float16)

    nonempty_indices = [index for index, text in enumerate(texts) if text]
    truncated = [texts[index][:character_cap] for index in nonempty_indices]
    received: list[list[float] | None] = [None] * len(truncated)

    async def submit(start: int) -> None:
        end = min(start + batch_size, len(truncated))
        chunk = truncated[start:end]
        async with semaphore:
            response = await client.post(
                url, json={"inputs": chunk, "truncate": True}, timeout=600.0
            )
            response.raise_for_status()
            vectors = response.json()
        for offset, vector in enumerate(vectors):
            received[start + offset] = vector

    started = time.monotonic()
    if truncated:
        await asyncio.gather(
            *(submit(index) for index in range(0, len(truncated), batch_size))
        )
    elapsed = time.monotonic() - started

    for received_index, vector in enumerate(received):
        output[nonempty_indices[received_index]] = np.asarray(vector, dtype=np.float16)

    return output, {
        "embed_seconds": elapsed,
        "n_docs": len(texts),
        "n_nonempty": len(truncated),
    }


async def run(args: argparse.Namespace) -> None:
    output_root = Path(args.output) / args.model_subdir
    output_root.mkdir(parents=True, exist_ok=True)

    suffix = f".{args.output_suffix}" if args.output_suffix else ""

    shards = load_lang(args.cache_dir, args.wiki)
    owned = select_shards(shards, args.gpu_id, args.world_size)
    pending = [
        shard
        for shard in owned
        if not (output_root / shard.wikiname / f"{shard.stem}{suffix}.f16bin").exists()
    ]
    print(
        f"[gpu{args.gpu_id} TEI col={args.text_column} suffix='{args.output_suffix}'] "
        f"{len(owned)} owned, {len(pending)} pending @ {args.url}",
        flush=True,
    )
    if not pending:
        return

    started = time.monotonic()
    total_docs = 0

    async with httpx.AsyncClient() as probe:
        try:
            response = await probe.get(
                args.url.rsplit("/", 1)[0] + "/health", timeout=10.0
            )
            response.raise_for_status()
        except Exception as error:
            raise SystemExit(f"TEI not reachable at {args.url}: {error}") from error

    async with httpx.AsyncClient() as client:
        for shard in pending:
            wiki_dir = output_root / shard.wikiname
            wiki_dir.mkdir(parents=True, exist_ok=True)
            vector_path = wiki_dir / f"{shard.stem}{suffix}.f16bin"

            _identifiers, texts = load_shard_texts(shard, text_column=args.text_column)
            if not texts:
                np.zeros((0, args.dimensions), dtype=np.float16).tofile(vector_path)
                continue

            embeddings, stats = await embed_texts(
                client,
                args.url,
                texts,
                batch_size=args.batch_size,
                concurrency=args.concurrency,
                character_cap=args.character_cap,
                dimensions=args.dimensions,
            )
            if embeddings.shape != (len(texts), args.dimensions):
                raise RuntimeError(
                    f"shape {embeddings.shape} != ({len(texts)}, {args.dimensions})"
                )

            temp_path = vector_path.with_suffix(vector_path.suffix + ".tmp")
            write_bin(temp_path, embeddings, dtype="f16")
            temp_path.rename(vector_path)

            total_docs += stats["n_docs"]
            docs_per_second = stats["n_docs"] / max(stats["embed_seconds"], 1e-3)
            print(
                f"[gpu{args.gpu_id} TEI] {shard.wikiname}/{shard.stem}{suffix}: "
                f"{stats['n_docs']} docs ({stats['n_nonempty']} non-empty) "
                f"in {stats['embed_seconds']:.1f}s -> {docs_per_second:.0f} doc/s",
                flush=True,
            )

    wall_seconds = time.monotonic() - started
    print(
        f"[gpu{args.gpu_id} TEI] DONE: {total_docs} docs in {wall_seconds:.0f}s "
        f"-> {total_docs/max(wall_seconds,1):.0f} doc/s",
        flush=True,
    )


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--cache-dir", default="/home/ubuntu/wikiverse-data/hf-cache")
    parser.add_argument("--output", default="/home/ubuntu/wikiverse-data/embeddings")
    parser.add_argument("--gpu-id", type=int, default=0)
    parser.add_argument("--world-size", type=int, default=8)
    parser.add_argument(
        "--wiki", required=True, help="single language code (enwiki, dewiki, etc)"
    )
    parser.add_argument("--url", default="http://localhost:8080/embed")
    parser.add_argument(
        "--batch-size", type=int, default=32, help="docs per HTTP request"
    )
    parser.add_argument("--concurrency", type=int, default=8)
    parser.add_argument("--text-column", default="text", choices=["text", "title"])
    parser.add_argument(
        "--character-cap",
        type=int,
        default=16384,
        help="truncate each input at this many characters (~max_length × 4 for English)",
    )
    parser.add_argument(
        "--output-suffix",
        default="body",
        help="filename suffix, e.g. 'body' or 'title'",
    )
    parser.add_argument(
        "--model-subdir",
        default="qwen3-embedding-0.6b",
        help="output goes to {output}/{model-subdir}/, e.g. snowflake-arctic-embed-l-v2.0",
    )
    parser.add_argument(
        "--dimensions",
        type=int,
        default=1024,
        help="embedding dimensionality (1024 Qwen3/arctic, 768 nomic, 4096 e5-mistral)",
    )
    args = parser.parse_args()
    asyncio.run(run(args))


if __name__ == "__main__":
    main()