# -*- coding: utf-8 -*- """sephirot_gen.py v2 — 十六质点世界修复合成数据引擎(全量重建版) 枚举空间 N = 16×64×32×4×27×27×5×5 = 2,388,787,200(与论文一致) 核心:Feistel 双射 + 同置换游走(cycle-walking)→ 索引空间均匀洗牌 每行由索引确定性重建(断点安全、可验证) 用法: python sephirot_gen.py --smoke # 100万行快速验证 python sephirot_gen.py --full # 全量23亿,16 workers python sephirot_gen.py --verify 50000 # 抽样重生成一致性校验 """ import os, sys, json, gzip, time, math, hashlib, random, multiprocessing as mp BASE = os.path.dirname(os.path.abspath(__file__)) SEEDS = os.path.join(BASE, "seeds.json") DATA = os.path.join(BASE, "data") WORKERS = 16 SHARD_ROWS = 2_000_000 # 每分片行数 DIMS = [16, 64, 32, 4, 27, 27, 5, 5] N = math.prod(DIMS) # 2,388,787,200 with open(SEEDS, encoding="utf-8") as f: S = json.load(f) SECTOR = S["sector"]; REGION = S["region"] INSTR = S["instrument"]; FRAME = S["frame"] LEXK = S["sephirot_keys"]; LEX = S["sephirot_lex"] TOP = S["top_terms"] MASTER_KEY = int.from_bytes(hashlib.sha256( b"worldfix-v2|corpus=61562415|sephirot=16").digest()[:8], "big") STANCES = ["预防", "缓解", "修复", "再生"] INTENS = ["微调", "常规", "强化", "集中", "极限"] HORIZON = ["即时", "季度", "年度", "五年", "世代"] def feistel(x: int) -> int: """4轮 Feistel on 2^32(L=R=16bit),固定网络→双射""" l, r = (x >> 16) & 0xFFFF, x & 0xFFFF for rnd in range(4): f = ((r * 2654435761 + MASTER_KEY + rnd * 0x9E3779B9) ^ (r << 7)) & 0xFFFF l, r = r, (l ^ f) & 0xFFFF return (l << 16) | r def shuffle_index(i: int) -> int: """同置换游走:i= 20000: gz.write(("\n".join(buf) + "\n").encode()) buf.clear() if buf: gz.write(("\n".join(buf) + "\n").encode()) os.replace(tmp, fin) pos = end json.dump({"pos": pos, "hi": hi}, open(mani, "w")) cnt_since += SHARD_ROWS el = time.time() - t0 # 日志写文件(避免stdout管道背压死锁) with open(os.path.join(wdir, "gen.log"), "a", encoding="utf-8") as lf: lf.write(f"[w{wid:02d}] {pos-lo}/{hi-lo} ({(pos-lo)/(hi-lo)*100:.2f}%) " f"{cnt_since/max(el,1e-9):,.0f} r/s t+{el:.0f}s\n") json.dump({"pos": pos, "hi": hi, "done": True}, open(mani, "w")) # 收尾覆写,清掉陈旧/越界清单 def full(): os.makedirs(DATA, exist_ok=True) chunk = N // WORKERS jobs = [(w, w * chunk, (w + 1) * chunk if w < WORKERS - 1 else N) for w in range(WORKERS)] t0 = time.time() with mp.Pool(WORKERS) as pool: pool.starmap(worker, jobs) print(f"FULL DONE {N:,} rows in {(time.time()-t0)/60:.1f} min") def smoke(total=1_000_000): os.makedirs(DATA, exist_ok=True) chunk = total // WORKERS jobs = [(w, w * chunk, (w + 1) * chunk if w < WORKERS - 1 else total) for w in range(WORKERS)] t0 = time.time() with mp.Pool(WORKERS) as pool: pool.starmap(worker, jobs) print(f"SMOKE DONE {total:,} rows in {time.time()-t0:.1f}s") def verify(n=50000): import itertools bad = 0 rng = random.Random(7) samples = [rng.randrange(N) for _ in range(min(n, 5000))] + list(range(10)) for o in samples: r1 = make_row(o) # 重算两次一致性 if r1 != make_row(o): bad += 1 # 双射性抽查:前100万索引无碰撞 seen = set() dup = 0 for o in range(1_000_000): h = shuffle_index(o) if h in seen: dup += 1 seen.add(h) print(f"verify: 一致性坏行={bad}/{len(samples)} 百万抽样碰撞={dup}") print("VERIFY PASS" if bad == 0 and dup == 0 else "VERIFY FAIL") # 字段范围校验 o = 123456789 s, sec, reg, st, ins, frm, it, hz = decode(shuffle_index(o)) assert 0 <= s < 16 and 0 <= sec < 64 and 0 <= reg < 32 and 0 <= st < 4 \ and 0 <= ins < 27 and 0 <= frm < 27 and 0 <= it < 5 and 0 <= hz < 5 print("decode ranges OK; N =", f"{N:,}") if __name__ == "__main__": a = sys.argv[1] if len(sys.argv) > 1 else "--verify" if a == "--full": full() elif a == "--smoke": smoke(int(sys.argv[2]) if len(sys.argv) > 2 else 1_000_000) elif a == "--verify": verify(int(sys.argv[2]) if len(sys.argv) > 2 else 50000) else: print(__doc__)