Datasets:
Download scripts/data.py from Lottolabs/LFM3-1.7B-SelfDistill-63M: direct link, hf CLI and curl.
- Browser
- Download file 2.38 kB
-
https://huggingface.co/datasets/Lottolabs/LFM3-1.7B-SelfDistill-63M/resolve/main/scripts/data.py
- Command line
-
hf download hf://datasets/Lottolabs/LFM3-1.7B-SelfDistill-63M/scripts/data.py
-
curl -L -o data.py https://huggingface.co/datasets/Lottolabs/LFM3-1.7B-SelfDistill-63M/resolve/main/scripts/data.py
2.38 kB
| """Generated records -> token arrays, and the length-bucketed batch sampler. | |
| pack(jsonl files) -> data/<name>.npz: tokens int32 (all records concatenated: prompt + output), start int64, | |
| plen int32, olen int32, domain/src/thinking/sampling/finish metadata (+ ids).""" | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| DOMAINS = ["chat", "code", "math", "tool", "multi", "bench"] | |
| def pack(files, out, max_len=8192): | |
| toks, start, plen, olen, meta = [], [], [], [], [] | |
| pos = 0 | |
| for f in files: | |
| for line in open(f): | |
| r = json.loads(line) | |
| p, o = r["prompt_ids"], r["out_ids"] | |
| if not o: | |
| continue | |
| seq = (p + o)[:max_len] | |
| toks.append(np.asarray(seq, dtype=np.int32)) | |
| start.append(pos) | |
| plen.append(len(p)) | |
| olen.append(len(seq) - len(p)) | |
| pos += len(seq) | |
| meta.append({k: r[k] for k in ("id", "domain", "src", "thinking", "sampling", "finish")}) | |
| np.savez(out, tokens=np.concatenate(toks), start=np.asarray(start, np.int64), plen=np.asarray(plen, np.int32), | |
| olen=np.asarray(olen, np.int32), meta=np.asarray([json.dumps(m) for m in meta])) | |
| return len(start), pos | |
| class Data: | |
| def __init__(self, path): | |
| z = np.load(path) | |
| self.tokens, self.start, self.plen, self.olen = z["tokens"], z["start"], z["plen"], z["olen"] | |
| self.meta = [json.loads(m) for m in z["meta"]] | |
| self.n = len(self.start) | |
| def seq(self, i): | |
| s = self.start[i] | |
| return self.tokens[s: s + self.plen[i] + self.olen[i]] | |
| def batches(self, budget, rng, buckets=(512, 1024, 1536, 2048, 3072, 4096, 6144, 8192), epochs=1): | |
| """Yield lists of record indices; each batch pads to one bucket length and holds <= budget tokens.""" | |
| lens = self.plen + self.olen | |
| for _ in range(epochs): | |
| order = rng.permutation(self.n) | |
| pend = {b: [] for b in buckets} | |
| for i in order: | |
| if self.olen[i] < 2: | |
| continue | |
| b = next(b for b in buckets if lens[i] <= b) | |
| pend[b].append(i) | |
| if (len(pend[b]) + 1) * b > budget: | |
| yield b, pend[b] | |
| pend[b] = [] | |
| for b, v in pend.items(): | |
| if v: | |
| yield b, v | |