Download docs/final_deck/build/sparse.py from Anupam-Rawat-IITB/code-switch-mtp: direct link, hf CLI and curl.
- Browser
- Download file 14.4 kB
-
https://huggingface.co/Anupam-Rawat-IITB/code-switch-mtp/resolve/main/docs/final_deck/build/sparse.py
- Command line
-
hf download hf://Anupam-Rawat-IITB/code-switch-mtp/docs/final_deck/build/sparse.py
-
curl -L -o sparse.py https://huggingface.co/Anupam-Rawat-IITB/code-switch-mtp/resolve/main/docs/final_deck/build/sparse.py
14.4 kB
| """Rebuilds every slide in the sparse format: one table, at most three takeaways, one caveat line; everything else goes to the notes. | |
| Usage: python sparse.py <deck root>. Reads the dense slides from project_v26/ so that every number that was on a slide ends up in its notes.""" | |
| import json, re, sys, html | |
| from html.parser import HTMLParser | |
| root = sys.argv[1] | |
| OLD = f"{root}/project_v26/slides/" | |
| class Dump(HTMLParser): | |
| """Plain-text dump of a dense slide: table rows as 'cell, cell, cell.', other blocks as sentences; the aside separately.""" | |
| def __init__(self): | |
| super().__init__(); self.out, self.aside, self.buf, self.row, self.in_aside, self.in_cell = [], [], [], None, False, False | |
| def flush(self): | |
| t = " ".join("".join(self.buf).split()); self.buf = [] | |
| if t: | |
| (self.aside if self.in_aside else self.out).append(t if t[-1] in ".:;?!" else t + ".") | |
| def handle_starttag(self, tag, attrs): | |
| if tag == "aside": self.flush(); self.in_aside = True | |
| elif tag == "tr": self.flush(); self.row = [] | |
| elif tag in ("td", "th"): self.in_cell = True; self.buf = [] | |
| elif tag in ("p", "li", "h1", "h2", "h3"): self.flush() | |
| def handle_endtag(self, tag): | |
| if tag in ("td", "th") and self.row is not None: | |
| self.row.append(" ".join("".join(self.buf).split())); self.buf = []; self.in_cell = False | |
| elif tag == "tr" and self.row is not None: | |
| self.out.append("Row: " + " | ".join(self.row) + "."); self.row = None | |
| elif tag in ("p", "li", "h1", "h2", "h3", "aside"): | |
| self.flush() | |
| if tag == "aside": self.in_aside = False | |
| def handle_data(self, data): self.buf.append(data) | |
| def notes(slide_id, lead): | |
| d = Dump(); d.feed(open(OLD + slide_id + ".html").read()); d.flush() | |
| body = [x.replace("Backup: ", "Earlier title: ") for x in d.out if not x.startswith("CS 769 project")] | |
| return html.escape(lead + " Detail moved from the slide: " + " ".join(body) + " Further notes: " + " ".join(d.aside), quote=False) | |
| def table(head, rows, widths=None, ours=()): | |
| ths = "".join(f'<th{" class=" + chr(34) + w + chr(34) if w else ""}>{h}</th>' for h, w in zip(head, widths or [""] * len(head))) | |
| trs = "\n".join(f'<tr{" class=" + chr(34) + "v-ours" + chr(34) if i in ours else ""}>' + "".join(f"<td>{c}</td>" for c in r) + "</tr>" for i, r in enumerate(rows)) | |
| return f'<table class="t v-t">\n<tr class="t-head">{ths}</tr>\n{trs}\n</table>' | |
| def slide(sid, title, lead, body, takes=(), cav="", by=False, sub=""): | |
| parts = [f'<section id="{sid}" class="v z{LEVELS.get(sid, 0)}{" m2" if sid == "method" else ""}">', f'<h2 class="v-title">{title}</h2>'] | |
| if by: parts.append('<p class="v-by">CS 769 project, group G31. Anupam Rawat, Sujeet Kumar Singh, Bilal Baig. Mentor: Karthika N J.</p>') | |
| if sub: parts.append(f'<p class="v-sub">{sub}</p>') | |
| parts.append(body) | |
| if takes: parts.append('<ul class="v-takes">\n' + "\n".join(f"<li>{t}</li>" for t in takes) + "\n</ul>") | |
| if cav: parts.append(f'<p class="v-cav">{cav}</p>') | |
| parts.append(f"<aside>{notes(sid, lead)}</aside>\n</section>\n") | |
| open(f"{root}/project/slides/{sid}.html", "w").write("\n".join(parts)) | |
| import os | |
| LEVELS = json.load(open(f"{root}/build/levels.json")) if os.path.exists(f"{root}/build/levels.json") else {} | |
| S = json.load(open(f"{root}/build/results_summary.json")) | |
| x = lambda v: f"{v:.1f}×" | |
| names = [("Hindi (Devanagari)", "Devanagari"), ("Hindi (ITRANS)", "ITRANS"), ("Hindi (IAST)", "IAST"), ("Hindi (ISO)", "ISO"), ("English", "English")] | |
| slide("results", "Lossless decoding at 2.9× greedy speed on Hindi (Devanagari) and 4.9× on Hindi (ITRANS)", | |
| "Say: the output is the same as greedy decoding, token for token, and it comes out 2.9 times faster in Devanagari. The low-looping column is the same system on documents where the model does not repeat itself.", | |
| table(["Speed-up over greedy decoding", "Chain of 3 drafts", "Draft tree", "Tree + 12 copied tokens", "Same, low-looping documents"], | |
| [[lab, x(S[n]["speedups"][0]), x(S[n]["speedups"][1]), f"<b>{x(S[n]['speedups'][4])}</b>", x(S[n]["low_looping"])] for lab, n in names], ["", "v-c", "v-c", "v-c", "v-c"]), | |
| ["Output is identical to greedy decoding on every document.", "The copied tokens give the speed; the draft tree alone is no faster than the chain.", | |
| "Romanized Hindi is fastest because its continuation largely restates the prompt."], | |
| "Measured end to end on one GTX 1080 Ti, fp32, batch size 1, 16 verified tokens per round.", by=True) | |
| steps = [("1. Backbone", "Frozen Llama-3.2-1B gives its own next token.", "Given", "", 0), | |
| ("2. Draft heads", "Three heads draft the next three tokens.", "Prior work", "Hydra, Medusa-2", 0), | |
| ("3. Shortlist", "Heads score 4,352 likely tokens.", "Prior work", "FR-Spec", 0), | |
| ("4. Confidence tree", "The most confident draft nodes are kept.", "Prior work", "EAGLE-2", 0), | |
| ("5. Copied branch", "Up to 12 tokens copied from the context.", "Prior work", "Prompt lookup decoding", 0), | |
| ("6. Allocator", "Splits the budget using word-group features.", "Ours", "About 1% faster", 1), | |
| ("7. Verify", "One backbone pass keeps only matching drafts.", "Prior work", "Lossless verification", 0)] | |
| flow = '<div class="v-flow">\n' + "\n".join(f'<div class="v-step{" v-ours" if o else ""}">\n<p class="v-n">{n}</p>\n<p class="v-d">{d}</p>\n<p class="v-tag{" hot" if o else ""}">{t}</p>\n' + (f'<p class="v-src">{src}</p>\n' if src else "") + '</div>' for n, d, t, src, o in steps) + "\n</div>" | |
| slide("method", "One lossless decoder; the allocator and the diagnosis are ours", | |
| "Walk the boxes left to right: one round of decoding. Boxes 2 to 5 and 7 come from prior work (Hydra, Medusa-2 and Hydra++, FR-Spec, EAGLE-2, prompt lookup decoding, lossless verification); box 6 and the word-group diagnosis are ours.", | |
| flow, ["A draft is kept only if it equals the backbone’s own choice, so the output cannot change.", "Ours: the word-group diagnosis, the allocator, and the measurement across five conditions."], | |
| "The measured speeds use boxes 1 to 5 and 7; the allocator of box 6 adds about 1% (1 seed).") | |
| slide("gains", "The speed comes from prior work; our linguistic part adds about 1%", | |
| "Read the table top to bottom: each part and what it measurably does, in Devanagari. The last three rows are ours.", | |
| table(["Part", "Source", "Measured effect, Hindi (Devanagari)"], | |
| [["Three dependent heads, chain of 3 drafts", "Prior work", "1.8× greedy"], | |
| ["Shortlist of 4,352 tokens", "Prior work", "Tree from 1.4× to 1.8×"], | |
| ["Confidence tree", "Prior work", "29% more tokens per pass, no faster than the chain"], | |
| ["Copied branch of 12 tokens", "Prior work", "<b>2.9×</b>; 2.6× on low-looping documents"], | |
| ["Five look-ahead heads instead of three", "Variation", "10% more tokens per pass, no speed gain"], | |
| ["Learned allocator with linguistic features", "Ours", "About 1% faster (1 seed)"], | |
| ["Code-switch-enriched training text", "Ours", "First draft inside English words 30% to 34%"], | |
| ["Word-group diagnosis", "Ours", "Locates the failures: group starts, 36% against 73% to 76%"]], ["v-p", "v-s", ""], ours=(5, 6, 7)), | |
| ["Much of the copied branch’s gain is the 1B model restating its prompt."]) | |
| slide("main", "Drafts fail at the start of a Hindi word group", | |
| "Define the word group first, then read the Devanagari row: inside a word and at function words the first draft is right about three times in four; at a group start, one time in three.", | |
| table(["First-draft accuracy", "Inside a word", "Function-word start", "Group start"], | |
| [["Hindi (Devanagari)", "73%", "76%", "<b>36%</b>"], ["Hindi (ITRANS)", "59%", "46%", "<b>21%</b>"], ["Hindi (IAST)", "67%", "40%", "<b>19%</b>"], ["Hindi (ISO)", "66%", "53%", "<b>19%</b>"]], ["", "v-c", "v-c", "v-c"]), | |
| ["Group starts are a quarter of the tokens and are where drafts fail.", "Postpositions and auxiliaries are draftable (77%, 83%); the second noun of a compound is not (33%).", | |
| "The pattern holds in all four spellings; English has no word-start penalty."], | |
| sub="A word group is a content word with the postpositions, auxiliaries and particles that follow it: <span class=\"hi\">हॉल में, जा रहा है, देखते ही</span>.") | |
| slide("backup", "Dependent heads accept 15% to 25% more tokens per pass than vanilla MTP", | |
| "This is the drafter comparison behind everything else: three kinds of heads, five conditions, tokens per backbone pass with a chain of three drafts.", | |
| table(["Tokens per backbone pass", "English", "Hindi (Devanagari)", "Hindi (ITRANS)", "Hindi (IAST)", "Hindi (ISO)"], | |
| [["Independent heads (vanilla MTP)", "1.33", "1.69", "1.43", "1.59", "1.65"], ["Dependent heads", "1.66", "2.09", "1.68", "1.85", "1.90"], | |
| ["Dependent heads with backbone targets", "1.75", "<b>2.20</b>", "1.68", "1.87", "1.89"]], ["", "v-c", "v-c", "v-c", "v-c", "v-c"]), | |
| ["Dependent heads gain 15% to 25% over vanilla MTP in every condition.", "Word-group rules for the draft tree never beat the head’s own confidence.", | |
| "Devanagari and ITRANS have the same share of draftable positions and different results."]) | |
| slide("tokenizer", "A better tokenizer does not replace drafting", | |
| "Two models on the same Devanagari text: Llama-3.2-1B with a general tokenizer and Sarvam-1 with an Indic tokenizer. Tokens per pass ranks them one way, words per pass the other.", | |
| table(["Hindi (Devanagari)", "Llama-3.2-1B", "Sarvam-1"], | |
| [["Tokens per word", "2.63", "1.45"], ["Tokens per backbone pass", "2.19", "1.64"], ["Words per backbone pass", "0.83", "<b>1.13</b>"], | |
| ["Words per pass with greedy decoding", "0.38", "0.69"], ["First-draft accuracy at group starts", "36%", "35%"]], ["", "v-w", "v-w"]), | |
| ["In words per pass Sarvam-1 is 1.35× ahead, although it accepts fewer tokens per pass.", "The tokenizer gain and the drafting gain overlap: 3.0× together, not 4.0×.", | |
| "Group starts stay at 35% to 36% with either tokenizer."], | |
| "Sarvam-1 has 2.4B parameters against 1B. Words per pass is not wall-clock speed.") | |
| slide("tax", "Drafting shrinks the cost of a poor tokenization", | |
| "Milliseconds per word on one A40: greedy decoding against the tree with 12 copied tokens. Romanized text costs Sarvam-1 many more tokens per word; drafting takes most of that penalty away.", | |
| table(["Milliseconds per word", "Tokens per word", "Greedy decoding", "Tree + 12 copied tokens", "Speed-up"], | |
| [["Llama-3.2-1B, Devanagari", "2.55", "28.4", "<b>11.1</b>", "2.6×"], ["Llama-3.2-1B, ITRANS", "2.61", "28.3", "<b>6.3</b>", "4.5×"], | |
| ["Sarvam-1, Devanagari", "1.47", "30.8", "15.8", "1.9×"], ["Sarvam-1, ITRANS", "3.36", "70.4", "22.2", "3.2×"]], ["", "v-c", "v-c", "v-c", "v-c"]), | |
| ["With Sarvam-1, ITRANS is 2.3× slower per word than Devanagari under greedy decoding and 1.4× slower with drafting.", | |
| "Sarvam-1 needs 1.7× fewer tokens per word but takes 1.9× longer per token, so Llama stays ahead."], | |
| "One A40; the models differ in size (2.4B against 1B parameters). Part of the ITRANS gain is the prompt being restated.") | |
| slide("copying", "Copying is where the speed comes from", | |
| "The more the model's continuation reuses its prompt, the more the copied branch adds. Tokens per pass here use 8 verified tokens and 5 copied tokens.", | |
| table(["Tokens per backbone pass", "Continuation tokens reused from the prompt", "Draft tree", "Tree + copied branch"], | |
| [["Hindi (Devanagari)", "55%", "2.67", "<b>3.58</b>"], ["Hindi (ITRANS)", "87%", "2.08", "<b>4.78</b>"], ["Hindi (IAST)", "88%", "2.29", "<b>5.01</b>"], | |
| ["Hindi (ISO)", "89%", "2.35", "<b>5.00</b>"], ["English", "13%", "2.18", "<b>2.52</b>"]], ["", "v-w", "v-c", "v-c"]), | |
| ["The heads cannot follow copied words: at such group starts the first draft is right 34% of the time, the copied token 60%.", | |
| "Stopping the copy at the word-group boundary loses 7% to 22%; a longer copy of 12 tokens wins.", | |
| "The copied branch is prompt lookup decoding from prior work; our part is the measurement."], | |
| "Greedy decoding with a 1B model: part of the reuse is the model restating its prompt.") | |
| slide("depth", "Deeper heads help only where three drafts are already accepted", | |
| "Five look-ahead heads against three: the gain in tokens per pass by condition, next to how often a chain of three drafts is fully accepted.", | |
| table(["Gain of five heads over three, tokens per pass", "8 verified tokens", "16 verified tokens", "24 verified tokens", "Rounds that accept all three drafts"], | |
| [["Hindi (Devanagari), 3 seeds", "+5.7%", "+9.5%", "<b>+11.5%</b>", "22%"], ["English, 1 seed", "+0.7%", "+1.9%", "+2.5%", "7%"], ["Hindi (ITRANS), 1 seed", "+0.5%", "+1.0%", "+2.1%", "6%"]], | |
| ["", "v-c", "v-c", "v-c", "v-w"]), | |
| ["Two more heads pay in proportion to how often three drafts are already accepted.", "They are not faster: 2.46× against 2.50× greedy, because drafting takes longer.", | |
| "Trainable parameters double, from 46.1M to 92.3M."]) | |
| slide("switch", "Switch points are not special; drafting is weak inside English words", | |
| "Real Hindi text with English words in it. The first column is agreement with the frozen model, which is what lossless verification accepts.", | |
| table(["Position in the text", "First draft equals the frozen model’s choice", "Accepted drafts, tree", "Accepted drafts, tree + copied branch"], | |
| [["Entering English", "40%", "1.12", "1.28"], ["Returning to Hindi, function word", "60%", "1.62", "1.80"], ["Returning to Hindi, content word", "40%", "1.08", "1.18"], | |
| ["Inside English words", "<b>30%</b>", "0.80", "1.04"], ["Control: Hindi group start", "41%", "1.27", "1.43"]], ["", "v-w", "v-c", "v-w"]), | |
| ["Entering English (40%) is as hard as an ordinary Hindi group start (41%).", "Inside English words the first draft is right 30% of the time, though the frozen model is right 64%.", | |
| "Training on code-switch-enriched Hindi lifts that to 34%; English prose alone does not."]) | |