ks46 commited on
Commit
8306a33
·
verified ·
1 Parent(s): fe25546

Add tokenizer and dataset card

Browse files
.gitattributes CHANGED
@@ -1,60 +1 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.avro filter=lfs diff=lfs merge=lfs -text
4
  *.bin filter=lfs diff=lfs merge=lfs -text
5
- *.bz2 filter=lfs diff=lfs merge=lfs -text
6
- *.ckpt filter=lfs diff=lfs merge=lfs -text
7
- *.ftz filter=lfs diff=lfs merge=lfs -text
8
- *.gz filter=lfs diff=lfs merge=lfs -text
9
- *.h5 filter=lfs diff=lfs merge=lfs -text
10
- *.joblib filter=lfs diff=lfs merge=lfs -text
11
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
12
- *.lz4 filter=lfs diff=lfs merge=lfs -text
13
- *.mds filter=lfs diff=lfs merge=lfs -text
14
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
15
- *.model filter=lfs diff=lfs merge=lfs -text
16
- *.msgpack filter=lfs diff=lfs merge=lfs -text
17
- *.npy filter=lfs diff=lfs merge=lfs -text
18
- *.npz filter=lfs diff=lfs merge=lfs -text
19
- *.onnx filter=lfs diff=lfs merge=lfs -text
20
- *.ot filter=lfs diff=lfs merge=lfs -text
21
- *.parquet filter=lfs diff=lfs merge=lfs -text
22
- *.pb filter=lfs diff=lfs merge=lfs -text
23
- *.pickle filter=lfs diff=lfs merge=lfs -text
24
- *.pkl filter=lfs diff=lfs merge=lfs -text
25
- *.pt filter=lfs diff=lfs merge=lfs -text
26
- *.pth filter=lfs diff=lfs merge=lfs -text
27
- *.rar filter=lfs diff=lfs merge=lfs -text
28
- *.safetensors filter=lfs diff=lfs merge=lfs -text
29
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
30
- *.tar.* filter=lfs diff=lfs merge=lfs -text
31
- *.tar filter=lfs diff=lfs merge=lfs -text
32
- *.tflite filter=lfs diff=lfs merge=lfs -text
33
- *.tgz filter=lfs diff=lfs merge=lfs -text
34
- *.wasm filter=lfs diff=lfs merge=lfs -text
35
- *.xz filter=lfs diff=lfs merge=lfs -text
36
- *.zip filter=lfs diff=lfs merge=lfs -text
37
- *.zst filter=lfs diff=lfs merge=lfs -text
38
- *tfevents* filter=lfs diff=lfs merge=lfs -text
39
- # Audio files - uncompressed
40
- *.pcm filter=lfs diff=lfs merge=lfs -text
41
- *.sam filter=lfs diff=lfs merge=lfs -text
42
- *.raw filter=lfs diff=lfs merge=lfs -text
43
- # Audio files - compressed
44
- *.aac filter=lfs diff=lfs merge=lfs -text
45
- *.flac filter=lfs diff=lfs merge=lfs -text
46
- *.mp3 filter=lfs diff=lfs merge=lfs -text
47
- *.ogg filter=lfs diff=lfs merge=lfs -text
48
- *.wav filter=lfs diff=lfs merge=lfs -text
49
- # Image files - uncompressed
50
- *.bmp filter=lfs diff=lfs merge=lfs -text
51
- *.gif filter=lfs diff=lfs merge=lfs -text
52
- *.png filter=lfs diff=lfs merge=lfs -text
53
- *.tiff filter=lfs diff=lfs merge=lfs -text
54
- # Image files - compressed
55
- *.jpg filter=lfs diff=lfs merge=lfs -text
56
- *.jpeg filter=lfs diff=lfs merge=lfs -text
57
- *.webp filter=lfs diff=lfs merge=lfs -text
58
- # Video files - compressed
59
- *.mp4 filter=lfs diff=lfs merge=lfs -text
60
- *.webm filter=lfs diff=lfs merge=lfs -text
 
 
 
 
1
  *.bin filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pretty_name: URLs (tokenized)
3
+ license: other
4
+ license_name: per-source-see-card
5
+ license_link: https://huggingface.co/datasets/ks46/urls-sampled#licensing-and-attribution
6
+ language:
7
+ - multilingual
8
+ task_categories:
9
+ - text-generation
10
+ tags:
11
+ - urls
12
+ - web
13
+ - common-crawl
14
+ - tokenized
15
+ - bpe
16
+ - memmap
17
+ size_categories:
18
+ - 100B<n<1T
19
+ ---
20
+
21
+ # URLs (tokenized)
22
+
23
+ [`ks46/urls-sampled`](https://huggingface.co/datasets/ks46/urls-sampled) run through a byte-level
24
+ BPE built for URLs, stored as flat `uint16` token streams that memory-map
25
+ directly into a training loop.
26
+
27
+ | | |
28
+ |---|---|
29
+ | Shards | _(building)_ |
30
+ | URLs | _(building)_ |
31
+ | Tokens | _(building)_ |
32
+ | Vocabulary | 8,192 |
33
+ | Token dtype | `uint16`, little-endian |
34
+
35
+ **There is no parquet here and the dataset viewer will not render it.** These
36
+ are raw token bins; see *Reading the data* below.
37
+
38
+ ## Layout
39
+
40
+ ```
41
+ tokenizer/ the exact vocabulary that produced every bin
42
+ tokenizer.json
43
+ tokenizer_config.json
44
+ training_meta.json
45
+ data/
46
+ tokens-s00000.bin flat little-endian uint16
47
+ tokens-s00000.json sidecar: counts, dtype, eos id, tokenizer sha256
48
+ ...
49
+ ```
50
+
51
+ One `.bin` per source shard, in the same order and with the same membership as
52
+ `ks46/urls-sampled`. Shard *n* here holds exactly the URLs of `data/part-n.parquet`
53
+ there, so the source dataset's guarantees carry over: each shard is an unbiased
54
+ 1-in-2,048 sample of the whole corpus, and distinct shards are disjoint because
55
+ membership is a pure function of the URL.
56
+
57
+ Every sidecar records the sha256 of `tokenizer/tokenizer.json`. Check it before
58
+ mixing bins from different builds — token ids from a different vocabulary are
59
+ silently meaningless, not an error.
60
+
61
+ ## Format
62
+
63
+ Each URL is stored as `[<eos>] ++ ids`: a leading end-of-sequence token, then
64
+ the URL's tokens. `<eos>` is id **0** and is the only special token.
65
+
66
+ ```
67
+ [0] t t t t [0] t t t [0] t t t t t ...
68
+ ^ URL 1 ^ URL 2 ^ URL 3
69
+ ```
70
+
71
+ The leading `<eos>` is the conditioning prefix: at inference the model is shown
72
+ `<eos>` and the log-probabilities of what follows give that URL's code length.
73
+ It also delimits URLs, so boundaries are recoverable by scanning for zeros
74
+ without needing an index. Training that slices fixed-size blocks can ignore
75
+ boundaries entirely.
76
+
77
+ ## Reading the data
78
+
79
+ ```python
80
+ import json
81
+ import numpy as np
82
+ from huggingface_hub import hf_hub_download
83
+
84
+ bin_path = hf_hub_download("ks46/urls-tokenized", "data/tokens-s00000.bin",
85
+ repo_type="dataset")
86
+ meta = json.load(open(hf_hub_download("ks46/urls-tokenized",
87
+ "data/tokens-s00000.json",
88
+ repo_type="dataset")))
89
+
90
+ tokens = np.memmap(bin_path, dtype=np.uint16, mode="r") # zero-copy
91
+ assert len(tokens) == meta["tokens"]
92
+
93
+ # A fixed-size training block, nanoGPT style
94
+ block = tokens[1_000_000 : 1_000_512].astype(np.int64)
95
+
96
+ # Or split back into individual URLs
97
+ starts = np.flatnonzero(tokens == meta["eos_token_id"])
98
+ first_url_ids = tokens[starts[0] + 1 : starts[1]]
99
+ ```
100
+
101
+ ## Recovering the original URL — read this before decoding
102
+
103
+ Decoding gives **canonical** text, not the original URL. Host labels were
104
+ reordered TLD-first before tokenization, so `https://www.example.com/a` is
105
+ stored as `https://com.example.www/a`. To get the original back, apply the same
106
+ function again — it is an involution, so one function serves both directions:
107
+
108
+ ```python
109
+ from tokenizers import Tokenizer
110
+ tok = Tokenizer.from_file(hf_hub_download("ks46/urls-tokenized",
111
+ "tokenizer/tokenizer.json",
112
+ repo_type="dataset"))
113
+ url = canonical(tok.decode(first_url_ids.tolist())) # canonical() below
114
+ ```
115
+
116
+ The full function, which is all you need:
117
+
118
+ ```python
119
+ _SCHEMES = ("https://", "http://")
120
+ _AUTH_END = ("/", "?", "#")
121
+
122
+
123
+ def _is_ipv4(host: str) -> bool:
124
+ parts = host.split(".")
125
+ if len(parts) != 4:
126
+ return False
127
+ return all(p.isascii() and p.isdigit() and len(p) <= 3 and int(p) < 256
128
+ for p in parts)
129
+
130
+
131
+ def canonical(url: str) -> str:
132
+ """Reverse the host's label order. Self-inverse: canonical(canonical(u)) == u."""
133
+ for scheme in _SCHEMES:
134
+ if url.startswith(scheme):
135
+ break
136
+ else:
137
+ return url # unrecognised scheme: pass through untouched
138
+
139
+ rest = url[len(scheme):]
140
+ cut = len(rest)
141
+ for ch in _AUTH_END:
142
+ i = rest.find(ch)
143
+ if i != -1 and i < cut:
144
+ cut = i
145
+ authority, tail = rest[:cut], rest[cut:]
146
+
147
+ userinfo, at, hostport = authority.rpartition("@")
148
+ host, colon, port = hostport.rpartition(":")
149
+ if not colon or not (port.isascii() and port.isdigit()):
150
+ host, colon, port = hostport, "", ""
151
+
152
+ # Bracketed IPv6 holds ':' and no meaningful labels; dotted quads reverse
153
+ # into other dotted quads, so both are left exactly as they are.
154
+ if not (host.startswith("[") or _is_ipv4(host)):
155
+ host = ".".join(reversed(host.split(".")))
156
+
157
+ return f"{scheme}{userinfo}{at}{host}{colon}{port}{tail}"
158
+
159
+
160
+ if __name__ == "__main__":
161
+ cases = [
162
+ "https://www.example.com/blog/2024/01/post.html?utm_source=x&id=42#top",
163
+ "http://example.com",
164
+ "https://a.b.c.d.e.f/",
165
+ "https://user:pass@www.example.com:8443/p?q=1",
166
+ "https://1.2.3.4/path", # IPv4: untouched
167
+ "https://[::1]:8080/x", # IPv6: untouched
168
+ "https://example.com./trailing", # trailing dot
169
+ "https://example..com/empty", # empty label
170
+ "https://localhost/", # single label
171
+ "https:///no-host", # empty authority
172
+ "ftp://weird.example.com/x", # unknown scheme: untouched
173
+ "http://129.222.104.0/25,US,US-CA,San",
174
+ "not a url at all",
175
+ "https://xn--80ak6aa92e.com/é中", # punycode + raw UTF-8
176
+ "https://WWW.Example.COM/Case",
177
+ ]
178
+ print(f"{'input':<58} {'canonical':<58} involution")
179
+ ok = True
180
+ for u in cases:
181
+ c = canonical(u)
182
+ inv = canonical(c) == u
183
+ ok &= inv
184
+ print(f"{u:<58.57} {c:<58.57} {'OK' if inv else 'FAIL'}")
185
+ raise SystemExit(0 if ok else 1)
186
+ ```
187
+
188
+ The pipeline is lossless: `canonical(decode(encode(canonical(u)))) == u`. Both
189
+ implementations (this one and the Rust one that wrote the bins) were checked
190
+ against each other on 5,000,000 real URLs, byte-for-byte, and every shard has
191
+ 1,000 URLs each from its head, middle and tail decoded back out of the finished
192
+ file and compared to the source before it is published.
193
+
194
+ ## How the tokenizer works
195
+
196
+ Byte-level BPE, vocabulary 8,192, `max_token_length` 24. The initial
197
+ alphabet is all 256 bytes, so **every** URL is encodable — percent-escapes,
198
+ punycode, raw UTF-8 in paths — and there is no `<unk>` and no fallback path.
199
+ There is no normalizer, because one would break byte-exact round-tripping.
200
+
201
+ Unlike a general-purpose BPE, URLs are **structurally pre-split** before any
202
+ merge is applied. Each delimiter below becomes its own piece, and BPE can never
203
+ merge across one:
204
+
205
+ | set | source | characters |
206
+ |---|---|---|
207
+ | gen-delims | RFC 3986 §2.2 | `:/?#[]@` |
208
+ | sub-delims | RFC 3986 §2.2 | `!$&'()*+,;=` |
209
+ | unwise | RFC 2396 §2.4.3 + backtick | `` {}|\^`~ `` |
210
+ | unreserved punctuation | RFC 3986 §2.3, split anyway | `.-_` |
211
+ | pct-encoding marker | RFC 3986 §2.1 | `%` |
212
+
213
+ Two things are kept whole: `http://` and `https://`, which open nearly every
214
+ URL, and well-formed percent-escapes (`%[0-9A-Fa-f]{2}`), so `%C3` is one piece. A
215
+ bare `%` not followed by two hex digits is not an escape, so `100%` splits into
216
+ `100` + `%`. What survives as content is exactly a run of alphanumerics.
217
+
218
+ The full pattern:
219
+
220
+ ```
221
+ https?://|%[0-9A-Fa-f]{2}|[!#$%&'()*+,\-./:;=?@\[\\\]\^_`{|}~]
222
+ ```
223
+
224
+ Host labels are then reordered TLD-first so the vocabulary's host hierarchy
225
+ matches the naming tree — `com` is learned once and shared by every `.com`.
226
+ `news.bbc.co.uk` becomes `uk` · `.` · `co` · `.` · `bbc` · `.` · `news`, each a
227
+ single token.
228
+
229
+ ## Intended use
230
+
231
+ Language modelling over URLs, and lossless URL compression in particular: the
232
+ byte-exact round-trip means a model's log-probabilities over this stream are a
233
+ valid code length for the original URL.
234
+
235
+ ## Limitations and biases
236
+
237
+ - Inherits every bias of the source corpus, which is Common Crawl derived. It
238
+ is a sample of what crawlers reached, not of the web as it exists.
239
+ - Malformed URLs are **kept, not filtered** — stray quotes, literal spaces,
240
+ commas, bare IP literals with junk paths. Any consumer must handle them.
241
+ - Tokenization is tuned for compression, not for semantics. Aggressive
242
+ structural splitting costs roughly 35 tokens per URL, which is more than an
243
+ unsplit BPE would use.
244
+ - A shard is an unbiased sample of the corpus, but a **prefix of a shard is
245
+ not**: shards are stored SURT-sorted, so the first rows are
246
+ alphabetically-first hosts — IP literals and malformed junk. Sample by stride
247
+ or shuffle; never take the head.
248
+
249
+ ## Reproduction
250
+
251
+ Built by [linklet](https://huggingface.co/datasets/ks46/urls-sampled) —
252
+ `rust/urltok` tokenizes a shard in ~94 s on 32 cores:
253
+
254
+ ```
255
+ urltok tokenize --parquet part-00000.parquet \
256
+ --tokenizer tokenizer/ --out tokens-s00000.bin
257
+ ```
258
+
259
+ ## Licensing and attribution
260
+
261
+ Same terms as the source corpus. See
262
+ [ks46/urls-sampled](https://huggingface.co/datasets/ks46/urls-sampled#licensing-and-attribution).
263
+ This dataset adds no new content — it is a reversible re-encoding of URLs that
264
+ are already published there.
tokenizer/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer/tokenizer_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "eos_token": "<eos>",
4
+ "model_max_length": 1000000000000000019884624838656,
5
+ "tokenizer_class": "TokenizersBackend"
6
+ }
tokenizer/training_meta.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "train_shard": 0,
3
+ "train_urls": 36578304,
4
+ "eval_shard": 1,
5
+ "vocab_size": 8192,
6
+ "max_token_length": 24,
7
+ "split": true,
8
+ "split_re": "https?://|%[0-9A-Fa-f]{2}|[!#$%&'()*+,\\-./:;=?@\\[\\\\\\]\\^_`{|}~]",
9
+ "canonical": true,
10
+ "train_seconds": 350.44420623779297,
11
+ "vocab": {
12
+ "vocab_size": 8192,
13
+ "max_token_len": 19,
14
+ "tokens_ge_25": 0,
15
+ "mean_token_len": 3.8985347985347985
16
+ },
17
+ "eval": {
18
+ "urls": 200000,
19
+ "tokens": 6918922,
20
+ "chars": 16919878,
21
+ "chars_per_token": 2.4454500281980343,
22
+ "tokens_per_url": 34.59461,
23
+ "chars_per_url": 84.59939
24
+ }
25
+ }