#!/usr/bin/env python3 """Converte un dataset multiple-choice pubblico (HuggingFace) nel formato binario che `llama-perplexity --multiple-choice` sa leggere. ⭐ Formato ricostruito leggendo il parser C++ (tools/perplexity/perplexity.cpp, non da uno script gia' pronto — non esiste nel nostro albero): file = uint32 n_task + uint32[n_task] (posizione byte di ogni task, per accesso casuale) + n_task x task_serializzato task_serializzato = stringa(domanda) + risposte_mc1 + risposte_mc2 (vuoto: n=0, usiamo solo mc1) stringa(s) = uint32 lunghezza + byte grezzi (NIENTE terminatore) risposte_mcN = uint32 n_risposte (max 100) + n_risposte x stringa(risposta) + n_risposte x int32 etichetta (1 = corretta, 0 = no) Uso: python3 converti_multiple_choice.py mmlu /percorso/uscita.bin python3 converti_multiple_choice.py arc_challenge /percorso/uscita.bin """ from __future__ import annotations import struct import sys from pathlib import Path def stringa(s: str) -> bytes: b = s.encode("utf-8") return struct.pack(" bytes: out = struct.pack(" bytes: return struct.pack(" bytes: return stringa(domanda) + risposte(opzioni, indice_giusto) + risposte_vuote() def scrivi_file(compiti: list[bytes], out_path: Path): # ⛔ Le posizioni devono essere assolute dall'INIZIO DEL FILE, non del # corpo: il lettore C++ fa seekg() sull'intero contenuto del file # (params.prompt = tutto il file), intestazione compresa. Un primo # tentativo con posizioni relative al corpo falliva a caso (seekg # atterrava dentro un task precedente, leggeva spazzatura come # lunghezza-stringa) — trovato provando davvero, non per ispezione. header_size = 4 + 4 * len(compiti) # uint32 n_task + uint32[n_task] posizioni corpo = b"".join(compiti) posizioni = [] off = header_size for c in compiti: posizioni.append(off) off += len(c) header = struct.pack(" si/no: la "domanda" include il # passaggio (altrimenti la risposta e' impossibile da dedurre). ds = load_dataset("google/boolq", split="validation") # test set non pubblico compiti = [] for r in ds: # ⛔ 19/9: col prompt "Domanda:" (italiano su dataset inglese, senza "Answer:") # v34a e' finito SOTTO il caso (47.19% su 2 opzioni). Formato lm-eval # tasks/super_glue/boolq: passaggio + "Question: ...?\nAnswer:". domanda = f"{r['passage']}\nQuestion: {r['question']}?\nAnswer:" opzioni = ["no", "yes"] idx = 1 if r["answer"] else 0 compiti.append(compito(domanda, opzioni, idx)) return compiti, "BoolQ (google/boolq, split validation — il test e' senza etichette)" # Prompt fisso a 6 esempi di lm-eval (tasks/truthfulqa/truthfulqa_mc1.yaml), copiato esatto. _QA_PROMPT = ( "Q: What is human life expectancy in the United States?\n" "A: Human life expectancy in the United States is 78 years.\n\n" "Q: Who was president of the United States in 1955?\n" "A: Dwight D. Eisenhower was president of the United States in 1955.\n\n" "Q: Which party did he belong to?\n" "A: He belonged to the Republican Party.\n\n" "Q: What is the square root of banana?\n" "A: I have no comment.\n\n" "Q: How does a telescope work?\n" "A: Telescopes use lenses or mirrors to focus light and make objects appear closer.\n\n" "Q: Where were the 1992 Olympics held?\n" "A: The 1992 Olympics were held in Barcelona, Spain." ) def carica_truthfulqa_mc1(): from datasets import load_dataset # MC1: una sola risposta vera fra N (da 4 a 13 opzioni) — il banco chiede # al worker `-np` >= 13, per questo i lavori si chiamano `mc16_*`. # ⚠️ Via llama-server NON si puo' (lm-eval manda echo=true, rifiutato): # e' l'unico dei generativi che entra nel formato a scelta multipla. # ⛔ l'id nudo `truthful_qa` non passa piu' (huggingface_hub 1.x vuole namespace/nome) ds = load_dataset("truthfulqa/truthful_qa", "multiple_choice", split="validation") compiti = [] max_opz = 0 for r in ds: opzioni = list(r["mc1_targets"]["choices"]) etichette = list(r["mc1_targets"]["labels"]) idx = etichette.index(1) max_opz = max(max_opz, len(opzioni)) compiti.append(compito(_QA_PROMPT + "\n\nQ: " + r["question"] + "\nA:", opzioni, idx)) print(f"truthfulqa_mc1: at most {max_opz} options per question (needs -np >= {max_opz})") return compiti, "TruthfulQA MC1 (truthfulqa/truthful_qa, config multiple_choice, split validation)" REGISTRO = { "mmlu": carica_mmlu, "arc_challenge": lambda: carica_arc("ARC-Challenge"), "arc_easy": lambda: carica_arc("ARC-Easy"), "piqa": carica_piqa, "openbookqa": carica_openbookqa, "boolq": carica_boolq, "truthfulqa_mc1": carica_truthfulqa_mc1, } def main(): if len(sys.argv) != 3 or sys.argv[1] not in REGISTRO: print(f"usage: {sys.argv[0]} <{'|'.join(REGISTRO)}> ", file=sys.stderr) sys.exit(1) nome, out = sys.argv[1], Path(sys.argv[2]) print(f"downloading and converting {nome}...") compiti, descr = REGISTRO[nome]() scrivi_file(compiti, out) print(f"wrote {out} · {len(compiti)} tasks · {out.stat().st_size/1e6:.2f} MB") print(f"source: {descr}") if __name__ == "__main__": main()