"""train point-1 on tinyshakespeare. fits logits[b] = w * h[b] to the byte histogram. the loss is convex in one variable so newton's method gets the exact optimum. writes model.safetensors, config.json, report.txt. """ import json, math, os, struct, urllib.request CORPUS_URL = "https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt" CORPUS_LOCAL = "corpus.txt" BYTES_VOCAB = [10] + list(range(32, 127)) H = {b: max(-1.0, min(1.0, (b - 79) / 47.0)) for b in BYTES_VOCAB} def get_corpus(): if not os.path.exists(CORPUS_LOCAL): urllib.request.urlretrieve(CORPUS_URL, CORPUS_LOCAL) return CORPUS_LOCAL def histogram(path): counts = dict.fromkeys(BYTES_VOCAB, 0) total = 0 with open(path, "rb") as f: while chunk := f.read(65536): for b in chunk: if b in counts: counts[b] += 1 total += len(chunk) return counts, total def write_safetensors(w): payload = struct.pack("3} w = {wi:+.6f} loss = {li:.4f}" for s, wi, li in log] lines += [ "", f"uniform {uniform_loss:.4f} nats/byte", f"unigram floor {floor:.4f}", f"point-1 {loss:.4f}", "", "top tokens:", ] lines += [f" '{t}' {p:.4f}" for p, t in ranked[:8]] report = "\n".join(lines) print(report) with open("report.txt", "w") as f: f.write(report + "\n") if __name__ == "__main__": main()