#!/usr/bin/env python3 """Micro-batch shares of likelihood scoring: a CPU re-implementation of the scorer's batch construction (llama-perplexity --multiple-choice) and of the hybrid memory's split_equal on the bench settings (-c 1024 -np 16 -ub 512; MMLU and HellaSwag -c 4096 -np 4), counting micro-batches of at most eight tokens (every layer runs matrix-vector products) and scored rows whose final layer and head run in products of at most eight columns. Not a GPU trace. Tokenizer: the teacher's Hugging Face tokenizer.json, whose token ids were checked against the engine's on Winogrande sentences by winogrande_ramo.py (Winogrande itself is not simulated here). Inputs from the environment: THOR_BENCH_INPUT (the bench dataset's bench/ directory, with OpenBookQA and PIQA rebuilt by ricostruisci_banchi.py) and THOR_TOKENIZER (hf/tokenizer/tokenizer.json). Usage: python3 microbatch_shares.py OUT.json [task,task,...]""" import json import os import struct import sys from pathlib import Path from tokenizers import Tokenizer _TK = Tokenizer.from_file(os.environ["THOR_TOKENIZER"]) def tok(s): return _TK.encode(s, add_special_tokens=True).ids def read_mc(p): if p.endswith(".txt"): LN=open(p,encoding="utf-8").read().split("\n") while LN and LN[-1]=="": LN.pop() return [(LN[6*i],LN[6*i+2:6*i+6]) for i in range(len(LN)//6)] b=open(p,"rb").read(); n=struct.unpack_from("n_ub: break if not cur: return None idx=[[i for i,t in enumerate(toks) if t[0]==c and not used[i]] for c in cur] ns=len(cur); got=[[] for _ in cur]; k=0 while all(kn_ub: break return [i for g in got for i in g] def run(name,path,np_,c,n_ub=512,n_batch_req=2048,limit=None): T=read_mc(path) if limit: T=T[:limit] n_ctx=np_*c; n_batch=min(n_ctx,n_batch_req); max_seq=min(128,np_) P=[prep(q,A) for q,A in T] st=dict(ub=0,ub_small=0,rel=0,rel_small=0,out=0,out_mv=0,items=len(T),items_body=0,items_head=0) i0=0 while i0max_seq: break pre=frozenset(range(s0,s0+na)) for j in range(cp): toks.append([pre,j==cp-1,True,i1]) for s,sq in enumerate(seqs): for j in range(cp,len(sq)): nl=j); OpenBookQA and PIQA are rebuilt by ricostruisci_banchi.py B = os.environ.get("THOR_BENCH_INPUT", "") j = lambda n: os.path.join(B, n) jobs = {"arc_easy": (j("arc_easy_test.bin"), 16, 1024), "arc_challenge": (j("arc_challenge_test.bin"), 16, 1024), "openbookqa": (j("openbookqa_test.bin"), 16, 1024), "boolq": (j("boolq_test.bin"), 16, 1024), "truthfulqa_mc1": (j("truthfulqa_mc1_test.bin"), 16, 1024), "piqa_full": (j("piqa_full.bin"), 16, 1024), "mmlu_pro_mc": (j("mmlu_pro_mc.bin"), 16, 1024), "mmlu_full": (j("mmlu_test.bin"), 4, 4096), "hellaswag": (j("hellaswag_val_full.txt"), 4, 4096)} out = Path(sys.argv[1]) names = sys.argv[2].split(",") if len(sys.argv) > 2 else list(jobs) res = {k: run(k, *jobs[k]) for k in names} out.write_text(json.dumps(res, indent=1))