Datasets:
Download src/eval_bpb.py from SlayerLab/polish-dynaword: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/datasets/SlayerLab/polish-dynaword/resolve/4fbe51379b807370e4268e0a7b2809ffc5ac43da/src/eval_bpb.py
- Command line
-
hf download hf://datasets/SlayerLab/polish-dynaword@4fbe51379b807370e4268e0a7b2809ffc5ac43da/src/eval_bpb.py
-
curl -L -o eval_bpb.py https://huggingface.co/datasets/SlayerLab/polish-dynaword/resolve/4fbe51379b807370e4268e0a7b2809ffc5ac43da/src/eval_bpb.py
2.19 kB
| #!/usr/bin/env python3 | |
| """Convert modded-nanogpt's validation loss into paper-ready, tokenizer-agnostic | |
| metrics: perplexity and bits-per-byte (bpb). | |
| Perplexity depends on the tokenizer, so it is NOT comparable across models with | |
| different vocabularies. Bits-per-byte normalises by raw UTF-8 bytes and IS | |
| comparable (this is what to report against Bielik / Llama-based models). | |
| bpb = val_loss[nats/token] / ln(2) * (tokens / bytes) | |
| Usage: | |
| python3 src/eval_bpb.py --val-loss 3.21 # val loss from the training log | |
| """ | |
| import argparse, struct | |
| import numpy as np | |
| from tokenizers import Tokenizer | |
| VAL = "/home/ubuntu/dynaword/shards/polish_val_000000.bin" | |
| TOK = "/home/ubuntu/dynaword/polish_bpe_32k.json" | |
| def load_shard(path): | |
| with open(path, "rb") as f: | |
| header = np.frombuffer(f.read(256 * 4), dtype=np.int32) | |
| assert header[0] == 20240520 and header[1] == 1, "bad shard header" | |
| ntok = int(header[2]) | |
| toks = np.frombuffer(f.read(ntok * 2), dtype=np.uint16) | |
| return toks | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--val-loss", type=float, required=True, help="nats/token from training log") | |
| ap.add_argument("--vocab", type=int, default=32768) | |
| args = ap.parse_args() | |
| toks = load_shard(VAL) | |
| tok = Tokenizer.from_file(TOK) | |
| # decode in chunks -> UTF-8 bytes (held-out reconstructs to the original text) | |
| nbytes = 0 | |
| step = 1_000_000 | |
| for i in range(0, len(toks), step): | |
| nbytes += len(tok.decode(toks[i:i+step].tolist()).encode("utf-8")) | |
| ntok = len(toks) | |
| ln2 = np.log(2) | |
| ppl = float(np.exp(args.val_loss)) | |
| bpb = args.val_loss / ln2 * (ntok / nbytes) | |
| bpt = nbytes / ntok | |
| rand_bpb = np.log(args.vocab) / ln2 * (ntok / nbytes) # uniform baseline | |
| print(f"held-out val: {ntok:,} tokens | {nbytes:,} bytes | {bpt:.3f} bytes/token") | |
| print(f"val loss : {args.val_loss:.4f} nats/token") | |
| print(f"perplexity : {ppl:.2f} (tokenizer-specific; NOT cross-model comparable)") | |
| print(f"bits-per-byte: {bpb:.4f} (tokenizer-AGNOSTIC; report this)") | |
| print(f" (uniform-{args.vocab} baseline bpb = {rand_bpb:.3f})") | |
| if __name__ == "__main__": | |
| main() | |